File size: 42,374 Bytes
dec6d5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 |
pragma solidity ^0.4.24;
contract PEpsilon {
Pinakion public pinakion;
Kleros public court;
uint public balance;
uint public disputeID;
uint public desiredOutcome;
uint public epsilon;
bool public settled;
uint public maxAppeals;
mapping (address => uint) public withdraw;
address public attacker;
uint public remainingWithdraw;
modifier onlyBy(address _account) {require(msg.sender == _account); _;}
event AmountShift(uint val, uint epsilon ,address juror);
event Log(uint val, address addr, string message);
constructor(Pinakion _pinakion, Kleros _kleros, uint _disputeID, uint _desiredOutcome, uint _epsilon, uint _maxAppeals) public {
pinakion = _pinakion;
court = _kleros;
disputeID = _disputeID;
desiredOutcome = _desiredOutcome;
epsilon = _epsilon;
attacker = msg.sender;
maxAppeals = _maxAppeals;
}
function receiveApproval(address _from, uint _amount, address, bytes) public onlyBy(pinakion) {
require(pinakion.transferFrom(_from, this, _amount));
balance += _amount;
}
function withdrawJuror() {
withdrawSelect(msg.sender);
}
function withdrawSelect(address _juror) {
uint amount = withdraw[_juror];
withdraw[_juror] = 0;
balance = sub(balance, amount);
remainingWithdraw = sub(remainingWithdraw, amount);
require(pinakion.transfer(_juror, amount));
}
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
assert(_b <= _a);
return _a - _b;
}
function withdrawAttacker(){
require(settled);
if (balance > remainingWithdraw) {
uint amount = balance - remainingWithdraw;
balance = remainingWithdraw;
require(pinakion.transfer(attacker, amount));
}
}
function settle() public {
require(court.disputeStatus(disputeID) == Arbitrator.DisputeStatus.Solved);
require(!settled);
settled = true;
var (, , appeals, choices, , , ,) = court.disputes(disputeID);
if (court.currentRuling(disputeID) != desiredOutcome){
uint amountShift = court.getStakePerDraw();
uint winningChoice = court.getWinningChoice(disputeID, appeals);
for (uint i=0; i <= (appeals > maxAppeals ? maxAppeals : appeals); i++){
if (winningChoice != 0){
uint votesLen = 0;
for (uint c = 0; c <= choices; c++) {
votesLen += court.getVoteCount(disputeID, i, c);
}
emit Log(amountShift, 0x0 ,"stakePerDraw");
emit Log(votesLen, 0x0, "votesLen");
uint totalToRedistribute = 0;
uint nbCoherent = 0;
for (uint j=0; j < votesLen; j++){
uint voteRuling = court.getVoteRuling(disputeID, i, j);
address voteAccount = court.getVoteAccount(disputeID, i, j);
emit Log(voteRuling, voteAccount, "voted");
if (voteRuling != winningChoice){
totalToRedistribute += amountShift;
if (voteRuling == desiredOutcome){
withdraw[voteAccount] += amountShift + epsilon;
remainingWithdraw += amountShift + epsilon;
emit AmountShift(amountShift, epsilon, voteAccount);
}
} else {
nbCoherent++;
}
}
uint toRedistribute = (totalToRedistribute - amountShift) / (nbCoherent + 1);
for (j = 0; j < votesLen; j++){
voteRuling = court.getVoteRuling(disputeID, i, j);
voteAccount = court.getVoteAccount(disputeID, i, j);
if (voteRuling == desiredOutcome){
withdraw[voteAccount] += toRedistribute;
remainingWithdraw += toRedistribute;
emit AmountShift(toRedistribute, 0, voteAccount);
}
}
}
}
}
}
}
pragma solidity ^0.4.24;
contract ApproveAndCallFallBack {
function receiveApproval(address from, uint256 _amount, address _token, bytes _data) public;
}
contract TokenController {
function proxyPayment(address _owner) public payable returns(bool);
function onTransfer(address _from, address _to, uint _amount) public returns(bool);
function onApprove(address _owner, address _spender, uint _amount) public
returns(bool);
}
contract Controlled {
modifier onlyController { require(msg.sender == controller); _; }
address public controller;
function Controlled() public { controller = msg.sender;}
function changeController(address _newController) public onlyController {
controller = _newController;
}
}
contract Pinakion is Controlled {
string public name;
uint8 public decimals;
string public symbol;
string public version = 'MMT_0.2';
struct Checkpoint {
uint128 fromBlock;
uint128 value;
}
Pinakion public parentToken;
uint public parentSnapShotBlock;
uint public creationBlock;
mapping (address => Checkpoint[]) balances;
mapping (address => mapping (address => uint256)) allowed;
Checkpoint[] totalSupplyHistory;
bool public transfersEnabled;
MiniMeTokenFactory public tokenFactory;
function Pinakion(
address _tokenFactory,
address _parentToken,
uint _parentSnapShotBlock,
string _tokenName,
uint8 _decimalUnits,
string _tokenSymbol,
bool _transfersEnabled
) public {
tokenFactory = MiniMeTokenFactory(_tokenFactory);
name = _tokenName;
decimals = _decimalUnits;
symbol = _tokenSymbol;
parentToken = Pinakion(_parentToken);
parentSnapShotBlock = _parentSnapShotBlock;
transfersEnabled = _transfersEnabled;
creationBlock = block.number;
}
function transfer(address _to, uint256 _amount) public returns (bool success) {
require(transfersEnabled);
doTransfer(msg.sender, _to, _amount);
return true;
}
function transferFrom(address _from, address _to, uint256 _amount
) public returns (bool success) {
if (msg.sender != controller) {
require(transfersEnabled);
require(allowed[_from][msg.sender] >= _amount);
allowed[_from][msg.sender] -= _amount;
}
doTransfer(_from, _to, _amount);
return true;
}
function doTransfer(address _from, address _to, uint _amount
) internal {
if (_amount == 0) {
Transfer(_from, _to, _amount);
return;
}
require(parentSnapShotBlock < block.number);
require((_to != 0) && (_to != address(this)));
var previousBalanceFrom = balanceOfAt(_from, block.number);
require(previousBalanceFrom >= _amount);
if (isContract(controller)) {
require(TokenController(controller).onTransfer(_from, _to, _amount));
}
updateValueAtNow(balances[_from], previousBalanceFrom - _amount);
var previousBalanceTo = balanceOfAt(_to, block.number);
require(previousBalanceTo + _amount >= previousBalanceTo);
updateValueAtNow(balances[_to], previousBalanceTo + _amount);
Transfer(_from, _to, _amount);
}
function balanceOf(address _owner) public constant returns (uint256 balance) {
return balanceOfAt(_owner, block.number);
}
function approve(address _spender, uint256 _amount) public returns (bool success) {
require(transfersEnabled);
if (isContract(controller)) {
require(TokenController(controller).onApprove(msg.sender, _spender, _amount));
}
allowed[msg.sender][_spender] = _amount;
Approval(msg.sender, _spender, _amount);
return true;
}
function allowance(address _owner, address _spender
) public constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}
function approveAndCall(address _spender, uint256 _amount, bytes _extraData
) public returns (bool success) {
require(approve(_spender, _amount));
ApproveAndCallFallBack(_spender).receiveApproval(
msg.sender,
_amount,
this,
_extraData
);
return true;
}
function totalSupply() public constant returns (uint) {
return totalSupplyAt(block.number);
}
function balanceOfAt(address _owner, uint _blockNumber) public constant
returns (uint) {
if ((balances[_owner].length == 0)
|| (balances[_owner][0].fromBlock > _blockNumber)) {
if (address(parentToken) != 0) {
return parentToken.balanceOfAt(_owner, min(_blockNumber, parentSnapShotBlock));
} else {
return 0;
}
} else {
return getValueAt(balances[_owner], _blockNumber);
}
}
function totalSupplyAt(uint _blockNumber) public constant returns(uint) {
if ((totalSupplyHistory.length == 0)
|| (totalSupplyHistory[0].fromBlock > _blockNumber)) {
if (address(parentToken) != 0) {
return parentToken.totalSupplyAt(min(_blockNumber, parentSnapShotBlock));
} else {
return 0;
}
} else {
return getValueAt(totalSupplyHistory, _blockNumber);
}
}
function createCloneToken(
string _cloneTokenName,
uint8 _cloneDecimalUnits,
string _cloneTokenSymbol,
uint _snapshotBlock,
bool _transfersEnabled
) public returns(address) {
if (_snapshotBlock == 0) _snapshotBlock = block.number;
Pinakion cloneToken = tokenFactory.createCloneToken(
this,
_snapshotBlock,
_cloneTokenName,
_cloneDecimalUnits,
_cloneTokenSymbol,
_transfersEnabled
);
cloneToken.changeController(msg.sender);
NewCloneToken(address(cloneToken), _snapshotBlock);
return address(cloneToken);
}
function generateTokens(address _owner, uint _amount
) public onlyController returns (bool) {
uint curTotalSupply = totalSupply();
require(curTotalSupply + _amount >= curTotalSupply);
uint previousBalanceTo = balanceOf(_owner);
require(previousBalanceTo + _amount >= previousBalanceTo);
updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount);
updateValueAtNow(balances[_owner], previousBalanceTo + _amount);
Transfer(0, _owner, _amount);
return true;
}
function destroyTokens(address _owner, uint _amount
) onlyController public returns (bool) {
uint curTotalSupply = totalSupply();
require(curTotalSupply >= _amount);
uint previousBalanceFrom = balanceOf(_owner);
require(previousBalanceFrom >= _amount);
updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount);
updateValueAtNow(balances[_owner], previousBalanceFrom - _amount);
Transfer(_owner, 0, _amount);
return true;
}
function enableTransfers(bool _transfersEnabled) public onlyController {
transfersEnabled = _transfersEnabled;
}
function getValueAt(Checkpoint[] storage checkpoints, uint _block
) constant internal returns (uint) {
if (checkpoints.length == 0) return 0;
if (_block >= checkpoints[checkpoints.length-1].fromBlock)
return checkpoints[checkpoints.length-1].value;
if (_block < checkpoints[0].fromBlock) return 0;
uint min = 0;
uint max = checkpoints.length-1;
while (max > min) {
uint mid = (max + min + 1)/ 2;
if (checkpoints[mid].fromBlock<=_block) {
min = mid;
} else {
max = mid-1;
}
}
return checkpoints[min].value;
}
function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value
) internal {
if ((checkpoints.length == 0)
|| (checkpoints[checkpoints.length -1].fromBlock < block.number)) {
Checkpoint storage newCheckPoint = checkpoints[ checkpoints.length++ ];
newCheckPoint.fromBlock = uint128(block.number);
newCheckPoint.value = uint128(_value);
} else {
Checkpoint storage oldCheckPoint = checkpoints[checkpoints.length-1];
oldCheckPoint.value = uint128(_value);
}
}
function isContract(address _addr) constant internal returns(bool) {
uint size;
if (_addr == 0) return false;
assembly {
size := extcodesize(_addr)
}
return size>0;
}
function min(uint a, uint b) pure internal returns (uint) {
return a < b ? a : b;
}
function () public payable {
require(isContract(controller));
require(TokenController(controller).proxyPayment.value(msg.value)(msg.sender));
}
function claimTokens(address _token) public onlyController {
if (_token == 0x0) {
controller.transfer(this.balance);
return;
}
Pinakion token = Pinakion(_token);
uint balance = token.balanceOf(this);
token.transfer(controller, balance);
ClaimedTokens(_token, controller, balance);
}
event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount);
event Transfer(address indexed _from, address indexed _to, uint256 _amount);
event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock);
event Approval(
address indexed _owner,
address indexed _spender,
uint256 _amount
);
}
contract MiniMeTokenFactory {
function createCloneToken(
address _parentToken,
uint _snapshotBlock,
string _tokenName,
uint8 _decimalUnits,
string _tokenSymbol,
bool _transfersEnabled
) public returns (Pinakion) {
Pinakion newToken = new Pinakion(
this,
_parentToken,
_snapshotBlock,
_tokenName,
_decimalUnits,
_tokenSymbol,
_transfersEnabled
);
newToken.changeController(msg.sender);
return newToken;
}
}
contract RNG{
function contribute(uint _block) public payable;
function requestRN(uint _block) public payable {
contribute(_block);
}
function getRN(uint _block) public returns (uint RN);
function getUncorrelatedRN(uint _block) public returns (uint RN) {
uint baseRN=getRN(_block);
if (baseRN==0)
return 0;
else
return uint(keccak256(msg.sender,baseRN));
}
}
contract BlockHashRNG is RNG {
mapping (uint => uint) public randomNumber;
mapping (uint => uint) public reward;
function contribute(uint _block) public payable { reward[_block]+=msg.value; }
function getRN(uint _block) public returns (uint RN) {
RN=randomNumber[_block];
if (RN==0){
saveRN(_block);
return randomNumber[_block];
}
else
return RN;
}
function saveRN(uint _block) public {
if (blockhash(_block) != 0x0)
randomNumber[_block] = uint(blockhash(_block));
if (randomNumber[_block] != 0) {
uint rewardToSend = reward[_block];
reward[_block] = 0;
msg.sender.send(rewardToSend);
}
}
}
contract BlockHashRNGFallback is BlockHashRNG {
function saveRN(uint _block) public {
if (_block<block.number && randomNumber[_block]==0) {
if (blockhash(_block)!=0x0)
randomNumber[_block]=uint(blockhash(_block));
else
randomNumber[_block]=uint(blockhash(block.number-1));
}
if (randomNumber[_block] != 0) {
uint rewardToSend=reward[_block];
reward[_block]=0;
msg.sender.send(rewardToSend);
}
}
}
contract Arbitrable{
Arbitrator public arbitrator;
bytes public arbitratorExtraData;
modifier onlyArbitrator {require(msg.sender==address(arbitrator)); _;}
event Ruling(Arbitrator indexed _arbitrator, uint indexed _disputeID, uint _ruling);
event MetaEvidence(uint indexed _metaEvidenceID, string _evidence);
event Dispute(Arbitrator indexed _arbitrator, uint indexed _disputeID, uint _metaEvidenceID);
event Evidence(Arbitrator indexed _arbitrator, uint indexed _disputeID, address _party, string _evidence);
constructor(Arbitrator _arbitrator, bytes _arbitratorExtraData) public {
arbitrator = _arbitrator;
arbitratorExtraData = _arbitratorExtraData;
}
function rule(uint _disputeID, uint _ruling) public onlyArbitrator {
emit Ruling(Arbitrator(msg.sender),_disputeID,_ruling);
executeRuling(_disputeID,_ruling);
}
function executeRuling(uint _disputeID, uint _ruling) internal;
}
contract Arbitrator{
enum DisputeStatus {Waiting, Appealable, Solved}
modifier requireArbitrationFee(bytes _extraData) {require(msg.value>=arbitrationCost(_extraData)); _;}
modifier requireAppealFee(uint _disputeID, bytes _extraData) {require(msg.value>=appealCost(_disputeID, _extraData)); _;}
event AppealPossible(uint _disputeID);
event DisputeCreation(uint indexed _disputeID, Arbitrable _arbitrable);
event AppealDecision(uint indexed _disputeID, Arbitrable _arbitrable);
function createDispute(uint _choices, bytes _extraData) public requireArbitrationFee(_extraData) payable returns(uint disputeID) {}
function arbitrationCost(bytes _extraData) public constant returns(uint fee);
function appeal(uint _disputeID, bytes _extraData) public requireAppealFee(_disputeID,_extraData) payable {
emit AppealDecision(_disputeID, Arbitrable(msg.sender));
}
function appealCost(uint _disputeID, bytes _extraData) public constant returns(uint fee);
function disputeStatus(uint _disputeID) public constant returns(DisputeStatus status);
function currentRuling(uint _disputeID) public constant returns(uint ruling);
}
contract Kleros is Arbitrator, ApproveAndCallFallBack {
Pinakion public pinakion;
uint public constant NON_PAYABLE_AMOUNT = (2**256 - 2) / 2;
RNG public rng;
uint public arbitrationFeePerJuror = 0.05 ether;
uint16 public defaultNumberJuror = 3;
uint public minActivatedToken = 0.1 * 1e18;
uint[5] public timePerPeriod;
uint public alpha = 2000;
uint constant ALPHA_DIVISOR = 1e4;
uint public maxAppeals = 5;
address public governor;
uint public session = 1;
uint public lastPeriodChange;
uint public segmentSize;
uint public rnBlock;
uint public randomNumber;
enum Period {
Activation,
Draw,
Vote,
Appeal,
Execution
}
Period public period;
struct Juror {
uint balance;
uint atStake;
uint lastSession;
uint segmentStart;
uint segmentEnd;
}
mapping (address => Juror) public jurors;
struct Vote {
address account;
uint ruling;
}
struct VoteCounter {
uint winningChoice;
uint winningCount;
mapping (uint => uint) voteCount;
}
enum DisputeState {
Open,
Resolving,
Executable,
Executed
}
struct Dispute {
Arbitrable arbitrated;
uint session;
uint appeals;
uint choices;
uint16 initialNumberJurors;
uint arbitrationFeePerJuror;
DisputeState state;
Vote[][] votes;
VoteCounter[] voteCounter;
mapping (address => uint) lastSessionVote;
uint currentAppealToRepartition;
AppealsRepartitioned[] appealsRepartitioned;
}
enum RepartitionStage {
Incoherent,
Coherent,
AtStake,
Complete
}
struct AppealsRepartitioned {
uint totalToRedistribute;
uint nbCoherent;
uint currentIncoherentVote;
uint currentCoherentVote;
uint currentAtStakeVote;
RepartitionStage stage;
}
Dispute[] public disputes;
event NewPeriod(Period _period, uint indexed _session);
event TokenShift(address indexed _account, uint _disputeID, int _amount);
event ArbitrationReward(address indexed _account, uint _disputeID, uint _amount);
modifier onlyBy(address _account) {require(msg.sender == _account); _;}
modifier onlyDuring(Period _period) {require(period == _period); _;}
modifier onlyGovernor() {require(msg.sender == governor); _;}
constructor(Pinakion _pinakion, RNG _rng, uint[5] _timePerPeriod, address _governor) public {
pinakion = _pinakion;
rng = _rng;
lastPeriodChange = now;
timePerPeriod = _timePerPeriod;
governor = _governor;
}
function receiveApproval(address _from, uint _amount, address, bytes) public onlyBy(pinakion) {
require(pinakion.transferFrom(_from, this, _amount));
jurors[_from].balance += _amount;
}
function withdraw(uint _value) public {
Juror storage juror = jurors[msg.sender];
require(juror.atStake <= juror.balance);
require(_value <= juror.balance-juror.atStake);
require(juror.lastSession != session);
juror.balance -= _value;
require(pinakion.transfer(msg.sender,_value));
}
function passPeriod() public {
require(now-lastPeriodChange >= timePerPeriod[uint8(period)]);
if (period == Period.Activation) {
rnBlock = block.number + 1;
rng.requestRN(rnBlock);
period = Period.Draw;
} else if (period == Period.Draw) {
randomNumber = rng.getUncorrelatedRN(rnBlock);
require(randomNumber != 0);
period = Period.Vote;
} else if (period == Period.Vote) {
period = Period.Appeal;
} else if (period == Period.Appeal) {
period = Period.Execution;
} else if (period == Period.Execution) {
period = Period.Activation;
++session;
segmentSize = 0;
rnBlock = 0;
randomNumber = 0;
}
lastPeriodChange = now;
NewPeriod(period, session);
}
function activateTokens(uint _value) public onlyDuring(Period.Activation) {
Juror storage juror = jurors[msg.sender];
require(_value <= juror.balance);
require(_value >= minActivatedToken);
require(juror.lastSession != session);
juror.lastSession = session;
juror.segmentStart = segmentSize;
segmentSize += _value;
juror.segmentEnd = segmentSize;
}
function voteRuling(uint _disputeID, uint _ruling, uint[] _draws) public onlyDuring(Period.Vote) {
Dispute storage dispute = disputes[_disputeID];
Juror storage juror = jurors[msg.sender];
VoteCounter storage voteCounter = dispute.voteCounter[dispute.appeals];
require(dispute.lastSessionVote[msg.sender] != session);
require(_ruling <= dispute.choices);
require(validDraws(msg.sender, _disputeID, _draws));
dispute.lastSessionVote[msg.sender] = session;
voteCounter.voteCount[_ruling] += _draws.length;
if (voteCounter.winningCount < voteCounter.voteCount[_ruling]) {
voteCounter.winningCount = voteCounter.voteCount[_ruling];
voteCounter.winningChoice = _ruling;
} else if (voteCounter.winningCount==voteCounter.voteCount[_ruling] && _draws.length!=0) {
voteCounter.winningChoice = 0;
}
for (uint i = 0; i < _draws.length; ++i) {
dispute.votes[dispute.appeals].push(Vote({
account: msg.sender,
ruling: _ruling
}));
}
juror.atStake += _draws.length * getStakePerDraw();
uint feeToPay = _draws.length * dispute.arbitrationFeePerJuror;
msg.sender.transfer(feeToPay);
ArbitrationReward(msg.sender, _disputeID, feeToPay);
}
function penalizeInactiveJuror(address _jurorAddress, uint _disputeID, uint[] _draws) public {
Dispute storage dispute = disputes[_disputeID];
Juror storage inactiveJuror = jurors[_jurorAddress];
require(period > Period.Vote);
require(dispute.lastSessionVote[_jurorAddress] != session);
dispute.lastSessionVote[_jurorAddress] = session;
require(validDraws(_jurorAddress, _disputeID, _draws));
uint penality = _draws.length * minActivatedToken * 2 * alpha / ALPHA_DIVISOR;
penality = (penality < inactiveJuror.balance) ? penality : inactiveJuror.balance;
inactiveJuror.balance -= penality;
TokenShift(_jurorAddress, _disputeID, -int(penality));
jurors[msg.sender].balance += penality / 2;
TokenShift(msg.sender, _disputeID, int(penality / 2));
jurors[governor].balance += penality / 2;
TokenShift(governor, _disputeID, int(penality / 2));
msg.sender.transfer(_draws.length*dispute.arbitrationFeePerJuror);
}
function oneShotTokenRepartition(uint _disputeID) public onlyDuring(Period.Execution) {
Dispute storage dispute = disputes[_disputeID];
require(dispute.state == DisputeState.Open);
require(dispute.session+dispute.appeals <= session);
uint winningChoice = dispute.voteCounter[dispute.appeals].winningChoice;
uint amountShift = getStakePerDraw();
for (uint i = 0; i <= dispute.appeals; ++i) {
if (winningChoice!=0 || (dispute.voteCounter[dispute.appeals].voteCount[0] == dispute.voteCounter[dispute.appeals].winningCount)) {
uint totalToRedistribute = 0;
uint nbCoherent = 0;
for (uint j = 0; j < dispute.votes[i].length; ++j) {
Vote storage vote = dispute.votes[i][j];
if (vote.ruling != winningChoice) {
Juror storage juror = jurors[vote.account];
uint penalty = amountShift<juror.balance ? amountShift : juror.balance;
juror.balance -= penalty;
TokenShift(vote.account, _disputeID, int(-penalty));
totalToRedistribute += penalty;
} else {
++nbCoherent;
}
}
if (nbCoherent == 0) {
jurors[governor].balance += totalToRedistribute;
TokenShift(governor, _disputeID, int(totalToRedistribute));
} else {
uint toRedistribute = totalToRedistribute / nbCoherent;
for (j = 0; j < dispute.votes[i].length; ++j) {
vote = dispute.votes[i][j];
if (vote.ruling == winningChoice) {
juror = jurors[vote.account];
juror.balance += toRedistribute;
TokenShift(vote.account, _disputeID, int(toRedistribute));
}
}
}
}
for (j = 0; j < dispute.votes[i].length; ++j) {
vote = dispute.votes[i][j];
juror = jurors[vote.account];
juror.atStake -= amountShift;
}
}
dispute.state = DisputeState.Executable;
}
function multipleShotTokenRepartition(uint _disputeID, uint _maxIterations) public onlyDuring(Period.Execution) {
Dispute storage dispute = disputes[_disputeID];
require(dispute.state <= DisputeState.Resolving);
require(dispute.session+dispute.appeals <= session);
dispute.state = DisputeState.Resolving;
uint winningChoice = dispute.voteCounter[dispute.appeals].winningChoice;
uint amountShift = getStakePerDraw();
uint currentIterations = 0;
for (uint i = dispute.currentAppealToRepartition; i <= dispute.appeals; ++i) {
if (dispute.appealsRepartitioned.length < i+1) {
dispute.appealsRepartitioned.length++;
}
if (winningChoice==0 && (dispute.voteCounter[dispute.appeals].voteCount[0] != dispute.voteCounter[dispute.appeals].winningCount)) {
dispute.appealsRepartitioned[i].stage = RepartitionStage.AtStake;
}
if (dispute.appealsRepartitioned[i].stage == RepartitionStage.Incoherent) {
for (uint j = dispute.appealsRepartitioned[i].currentIncoherentVote; j < dispute.votes[i].length; ++j) {
if (currentIterations >= _maxIterations) {
return;
}
Vote storage vote = dispute.votes[i][j];
if (vote.ruling != winningChoice) {
Juror storage juror = jurors[vote.account];
uint penalty = amountShift<juror.balance ? amountShift : juror.balance;
juror.balance -= penalty;
TokenShift(vote.account, _disputeID, int(-penalty));
dispute.appealsRepartitioned[i].totalToRedistribute += penalty;
} else {
++dispute.appealsRepartitioned[i].nbCoherent;
}
++dispute.appealsRepartitioned[i].currentIncoherentVote;
++currentIterations;
}
dispute.appealsRepartitioned[i].stage = RepartitionStage.Coherent;
}
if (dispute.appealsRepartitioned[i].stage == RepartitionStage.Coherent) {
if (dispute.appealsRepartitioned[i].nbCoherent == 0) {
jurors[governor].balance += dispute.appealsRepartitioned[i].totalToRedistribute;
TokenShift(governor, _disputeID, int(dispute.appealsRepartitioned[i].totalToRedistribute));
dispute.appealsRepartitioned[i].stage = RepartitionStage.AtStake;
} else {
uint toRedistribute = dispute.appealsRepartitioned[i].totalToRedistribute / dispute.appealsRepartitioned[i].nbCoherent;
for (j = dispute.appealsRepartitioned[i].currentCoherentVote; j < dispute.votes[i].length; ++j) {
if (currentIterations >= _maxIterations) {
return;
}
vote = dispute.votes[i][j];
if (vote.ruling == winningChoice) {
juror = jurors[vote.account];
juror.balance += toRedistribute;
TokenShift(vote.account, _disputeID, int(toRedistribute));
}
++currentIterations;
++dispute.appealsRepartitioned[i].currentCoherentVote;
}
dispute.appealsRepartitioned[i].stage = RepartitionStage.AtStake;
}
}
if (dispute.appealsRepartitioned[i].stage == RepartitionStage.AtStake) {
for (j = dispute.appealsRepartitioned[i].currentAtStakeVote; j < dispute.votes[i].length; ++j) {
if (currentIterations >= _maxIterations) {
return;
}
vote = dispute.votes[i][j];
juror = jurors[vote.account];
juror.atStake -= amountShift;
++currentIterations;
++dispute.appealsRepartitioned[i].currentAtStakeVote;
}
dispute.appealsRepartitioned[i].stage = RepartitionStage.Complete;
}
if (dispute.appealsRepartitioned[i].stage == RepartitionStage.Complete) {
++dispute.currentAppealToRepartition;
}
}
dispute.state = DisputeState.Executable;
}
function amountJurors(uint _disputeID) public view returns (uint nbJurors) {
Dispute storage dispute = disputes[_disputeID];
return (dispute.initialNumberJurors + 1) * 2**dispute.appeals - 1;
}
function validDraws(address _jurorAddress, uint _disputeID, uint[] _draws) public view returns (bool valid) {
uint draw = 0;
Juror storage juror = jurors[_jurorAddress];
Dispute storage dispute = disputes[_disputeID];
uint nbJurors = amountJurors(_disputeID);
if (juror.lastSession != session) return false;
if (dispute.session+dispute.appeals != session) return false;
if (period <= Period.Draw) return false;
for (uint i = 0; i < _draws.length; ++i) {
if (_draws[i] <= draw) return false;
draw = _draws[i];
if (draw > nbJurors) return false;
uint position = uint(keccak256(randomNumber, _disputeID, draw)) % segmentSize;
require(position >= juror.segmentStart);
require(position < juror.segmentEnd);
}
return true;
}
function createDispute(uint _choices, bytes _extraData) public payable returns (uint disputeID) {
uint16 nbJurors = extraDataToNbJurors(_extraData);
require(msg.value >= arbitrationCost(_extraData));
disputeID = disputes.length++;
Dispute storage dispute = disputes[disputeID];
dispute.arbitrated = Arbitrable(msg.sender);
if (period < Period.Draw)
dispute.session = session;
else
dispute.session = session+1;
dispute.choices = _choices;
dispute.initialNumberJurors = nbJurors;
dispute.arbitrationFeePerJuror = arbitrationFeePerJuror;
dispute.votes.length++;
dispute.voteCounter.length++;
DisputeCreation(disputeID, Arbitrable(msg.sender));
return disputeID;
}
function appeal(uint _disputeID, bytes _extraData) public payable onlyDuring(Period.Appeal) {
super.appeal(_disputeID,_extraData);
Dispute storage dispute = disputes[_disputeID];
require(msg.value >= appealCost(_disputeID, _extraData));
require(dispute.session+dispute.appeals == session);
require(dispute.arbitrated == msg.sender);
dispute.appeals++;
dispute.votes.length++;
dispute.voteCounter.length++;
}
function executeRuling(uint disputeID) public {
Dispute storage dispute = disputes[disputeID];
require(dispute.state == DisputeState.Executable);
dispute.state = DisputeState.Executed;
dispute.arbitrated.rule(disputeID, dispute.voteCounter[dispute.appeals].winningChoice);
}
function arbitrationCost(bytes _extraData) public view returns (uint fee) {
return extraDataToNbJurors(_extraData) * arbitrationFeePerJuror;
}
function appealCost(uint _disputeID, bytes _extraData) public view returns (uint fee) {
Dispute storage dispute = disputes[_disputeID];
if(dispute.appeals >= maxAppeals) return NON_PAYABLE_AMOUNT;
return (2*amountJurors(_disputeID) + 1) * dispute.arbitrationFeePerJuror;
}
function extraDataToNbJurors(bytes _extraData) internal view returns (uint16 nbJurors) {
if (_extraData.length < 2)
return defaultNumberJuror;
else
return (uint16(_extraData[0]) << 8) + uint16(_extraData[1]);
}
function getStakePerDraw() public view returns (uint minActivatedTokenInAlpha) {
return (alpha * minActivatedToken) / ALPHA_DIVISOR;
}
function getVoteAccount(uint _disputeID, uint _appeals, uint _voteID) public view returns (address account) {
return disputes[_disputeID].votes[_appeals][_voteID].account;
}
function getVoteRuling(uint _disputeID, uint _appeals, uint _voteID) public view returns (uint ruling) {
return disputes[_disputeID].votes[_appeals][_voteID].ruling;
}
function getWinningChoice(uint _disputeID, uint _appeals) public view returns (uint winningChoice) {
return disputes[_disputeID].voteCounter[_appeals].winningChoice;
}
function getWinningCount(uint _disputeID, uint _appeals) public view returns (uint winningCount) {
return disputes[_disputeID].voteCounter[_appeals].winningCount;
}
function getVoteCount(uint _disputeID, uint _appeals, uint _choice) public view returns (uint voteCount) {
return disputes[_disputeID].voteCounter[_appeals].voteCount[_choice];
}
function getLastSessionVote(uint _disputeID, address _juror) public view returns (uint lastSessionVote) {
return disputes[_disputeID].lastSessionVote[_juror];
}
function isDrawn(uint _disputeID, address _juror, uint _draw) public view returns (bool drawn) {
Dispute storage dispute = disputes[_disputeID];
Juror storage juror = jurors[_juror];
if (juror.lastSession != session
|| (dispute.session+dispute.appeals != session)
|| period<=Period.Draw
|| _draw>amountJurors(_disputeID)
|| _draw==0
|| segmentSize==0
) {
return false;
} else {
uint position = uint(keccak256(randomNumber,_disputeID,_draw)) % segmentSize;
return (position >= juror.segmentStart) && (position < juror.segmentEnd);
}
}
function currentRuling(uint _disputeID) public view returns (uint ruling) {
Dispute storage dispute = disputes[_disputeID];
return dispute.voteCounter[dispute.appeals].winningChoice;
}
function disputeStatus(uint _disputeID) public view returns (DisputeStatus status) {
Dispute storage dispute = disputes[_disputeID];
if (dispute.session+dispute.appeals < session)
return DisputeStatus.Solved;
else if(dispute.session+dispute.appeals == session) {
if (dispute.state == DisputeState.Open) {
if (period < Period.Appeal)
return DisputeStatus.Waiting;
else if (period == Period.Appeal)
return DisputeStatus.Appealable;
else return DisputeStatus.Solved;
} else return DisputeStatus.Solved;
} else return DisputeStatus.Waiting;
}
function executeOrder(bytes32 _data, uint _value, address _target) public onlyGovernor {
_target.call.value(_value)(_data);
}
function setRng(RNG _rng) public onlyGovernor {
rng = _rng;
}
function setArbitrationFeePerJuror(uint _arbitrationFeePerJuror) public onlyGovernor {
arbitrationFeePerJuror = _arbitrationFeePerJuror;
}
function setDefaultNumberJuror(uint16 _defaultNumberJuror) public onlyGovernor {
defaultNumberJuror = _defaultNumberJuror;
}
function setMinActivatedToken(uint _minActivatedToken) public onlyGovernor {
minActivatedToken = _minActivatedToken;
}
function setTimePerPeriod(uint[5] _timePerPeriod) public onlyGovernor {
timePerPeriod = _timePerPeriod;
}
function setAlpha(uint _alpha) public onlyGovernor {
alpha = _alpha;
}
function setMaxAppeals(uint _maxAppeals) public onlyGovernor {
maxAppeals = _maxAppeals;
}
function setGovernor(address _governor) public onlyGovernor {
governor = _governor;
}
} |