Skip to content

Commit f339e2b

Browse files
committed
feat(DisputeKitPlurality): split the responsibility of drawing jurors between Core and Dispute Kit
1 parent f30d6bf commit f339e2b

File tree

6 files changed

+179
-38
lines changed

6 files changed

+179
-38
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"solidity.compileUsingRemoteVersion": "v0.8.9+commit.e5eed63a",
2+
"solidity.compileUsingRemoteVersion": "v0.8.10+commit.fc410830",
33
"mochaExplorer.files": "contracts/test/**/*.{j,t}s"
44
}

CHANGELOG.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
## 0.1.0 (2021-12-17)
22

3+
- refactor: add arbitrator data index getter ([47a1623](https://github.com/kleros/kleros-v2/commit/47a1623))
4+
- refactor: add evidence contracts ([09a34f3](https://github.com/kleros/kleros-v2/commit/09a34f3))
5+
- refactor: add interfaces + capped math ([e25b21f](https://github.com/kleros/kleros-v2/commit/e25b21f))
6+
- refactor: add simple evidence home contract ([1b82f62](https://github.com/kleros/kleros-v2/commit/1b82f62))
7+
- refactor: fix contract name ([6eb744a](https://github.com/kleros/kleros-v2/commit/6eb744a))
8+
- refactor: remove foreign evidence interface ([ff8c50c](https://github.com/kleros/kleros-v2/commit/ff8c50c))
9+
- refactor(bridge): use ArbRetryableTx#getSubmissionPrice ([61bc2f3](https://github.com/kleros/kleros-v2/commit/61bc2f3))
10+
- refactor(sdk): rename ([3241d10](https://github.com/kleros/kleros-v2/commit/3241d10))
11+
- feat: add arbitrum L1 bridge and dependencies ([b412772](https://github.com/kleros/kleros-v2/commit/b412772))
12+
- feat: add arbitrum L2 bridge ([457b060](https://github.com/kleros/kleros-v2/commit/457b060))
313
- feat: dispute kit in progress ([95b8ada](https://github.com/kleros/kleros-v2/commit/95b8ada))
414
- feat: modern toolchain setup and simple RNG smart contracts ([17f6a76](https://github.com/kleros/kleros-v2/commit/17f6a76))
515
- feat(Arbitration): standard update ([ed930de](https://github.com/kleros/kleros-v2/commit/ed930de))
16+
- chore: .gitignore ([0ed4d74](https://github.com/kleros/kleros-v2/commit/0ed4d74))
17+
- chore: .gitignore and removal of unnecessary yarn cache as we are using "zero installs" ([a6cfdd0](https://github.com/kleros/kleros-v2/commit/a6cfdd0))
18+
- chore: added GitHub code scanning ([4a70475](https://github.com/kleros/kleros-v2/commit/4a70475))
19+
- chore: added the hardhat config for layer 2 networks, added hardhat-deploy and mocha ([a12ea0e](https://github.com/kleros/kleros-v2/commit/a12ea0e))
20+
- chore: gitignore typechain ([b50f777](https://github.com/kleros/kleros-v2/commit/b50f777))
21+
- chore(typechain): clean generated files ([775ddd0](https://github.com/kleros/kleros-v2/commit/775ddd0))
22+
- test: add evidence contract tests ([590d800](https://github.com/kleros/kleros-v2/commit/590d800))
23+
- test: added a test for IncrementalNG ([65a996b](https://github.com/kleros/kleros-v2/commit/65a996b))
24+
- test(EvidenceModule): add test file ([9f00f98](https://github.com/kleros/kleros-v2/commit/9f00f98))
25+
- fix: according to evidence standard + comments ([5c95828](https://github.com/kleros/kleros-v2/commit/5c95828))
26+
- fix: unused code ([26b5dc3](https://github.com/kleros/kleros-v2/commit/26b5dc3))
627
- fix(Arbitrator): memory to calldata ([4770b1f](https://github.com/kleros/kleros-v2/commit/4770b1f))
28+
- fix(EvidenceModule): typos + castings + imports ([789c022](https://github.com/kleros/kleros-v2/commit/789c022))
729
- fix(IArbitrator): appeals removed from the standard ([02c20ce](https://github.com/kleros/kleros-v2/commit/02c20ce))
830
- fix(IArbitrator): change name to arbitration cost ([0ba4f29](https://github.com/kleros/kleros-v2/commit/0ba4f29))
931
- fix(IArbitrator): interface simplification ([e81fb8b](https://github.com/kleros/kleros-v2/commit/e81fb8b))
1032
- fix(IArbitrator): replaced appealCost with fundingStatus ([f189dd9](https://github.com/kleros/kleros-v2/commit/f189dd9))
11-
- chore: added GitHub code scanning ([4a70475](https://github.com/kleros/kleros-v2/commit/4a70475))
12-
- chore: added the hardhat config for layer 2 networks, added hardhat-deploy and mocha ([a12ea0e](https://github.com/kleros/kleros-v2/commit/a12ea0e))
13-
- test: added a test for IncrementalNG ([65a996b](https://github.com/kleros/kleros-v2/commit/65a996b))
1433
- docs: initial commit ([23356e7](https://github.com/kleros/kleros-v2/commit/23356e7))
1534
- docs: license file added ([cb62d2c](https://github.com/kleros/kleros-v2/commit/cb62d2c))
1635
- docs: readme and spdx headers ([8a5b397](https://github.com/kleros/kleros-v2/commit/8a5b397))

contracts/src/arbitration/CentralizedArbitrator.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ contract CentralizedArbitrator is IArbitrator {
125125
uint256 _arbitrationFee,
126126
uint256 _appealDuration,
127127
uint256 _appealFee
128-
) public {
128+
) {
129129
arbitrationFee = _arbitrationFee;
130130
appealDuration = _appealDuration;
131131
appealFee = _appealFee;

contracts/src/arbitration/DisputeKitPlurality.sol

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity ^0.8;
44

55
import "./IArbitrator.sol";
66
import "../rng/RNG.sol";
7-
import "../data-structures/SortitionSumTreeFactory.sol";
7+
import "./mock/MockKlerosCore.sol";
88

99
contract DisputeKitPlurality {
1010
// Core --> IN
@@ -28,14 +28,12 @@ contract DisputeKitPlurality {
2828
// appeal crowdfunding
2929
// redistribution when ruling is final
3030

31-
IArbitrator public immutable core;
32-
RNG public immutable rng;
33-
34-
using SortitionSumTreeFactory for SortitionSumTreeFactory.SortitionSumTrees; // Use library functions for sortition sum trees.
31+
// TODO: extract the necessary interfaces
32+
MockKlerosCore public immutable core;
3533

36-
// SortitionSumTreeFactory.SortitionSumTrees internal sortitionSumTrees; // The sortition sum trees.
34+
RNG public immutable rng;
3735

38-
constructor(IArbitrator _core, RNG _rng) {
36+
constructor(MockKlerosCore _core, RNG _rng) {
3937
core = _core;
4038
rng = _rng;
4139
}
@@ -70,5 +68,89 @@ contract DisputeKitPlurality {
7068
// ALPHA_DIVISOR = 10000
7169
}
7270

73-
function drawJurors(SortitionSumTreeFactory.SortitionSumTrees calldata tree) public {}
71+
/**
72+
* @dev Draws jurors for a dispute. Can be called in parts.
73+
* @param _disputeID The ID of the dispute.
74+
* @param _iterations The number of iterations to run.
75+
*/
76+
function drawJurors(uint256 _disputeID, uint256 _iterations) public {
77+
uint96 subcourtID = core.getDispute(_disputeID).subcourtID;
78+
bytes32 key = bytes32(bytes12(subcourtID)); // due to new conversion restrictions in v0.8
79+
(
80+
uint256 k, /* stack */
81+
,
82+
uint256[] memory nodes
83+
) = core.getSortitionSumTree(key);
84+
85+
// TODO: run this only when starting the drawing period
86+
uint256 randomNumber = rng.getUncorrelatedRN(block.number);
87+
88+
// TODO: batching with boundary checks
89+
for (uint256 i = 0; i < _iterations; i++) {
90+
uint256 treeIndex = draw(uint256(keccak256(abi.encodePacked(randomNumber, _disputeID, i))), k, nodes);
91+
bytes32 id = core.getSortitionSumTreeID(key, treeIndex);
92+
(
93+
address drawnAddress, /* subcourtID */
94+
95+
) = stakePathIDToAccountAndSubcourtID(id);
96+
97+
// TODO: Save the vote.
98+
// dispute.votes[dispute.votes.length - 1][i].account = drawnAddress;
99+
// jurors[drawnAddress].lockedTokens += dispute.tokensAtStakePerJuror[dispute.tokensAtStakePerJuror.length - 1];
100+
// emit Draw(drawnAddress, _disputeID, dispute.votes.length - 1, i);
101+
102+
// TODO: Stop if dispute is fully drawn.
103+
// if (i == dispute.votes[dispute.votes.length - 1].length - 1) break;
104+
}
105+
}
106+
107+
function draw(
108+
uint256 _drawnNumber,
109+
uint256 _k,
110+
uint256[] memory _nodes
111+
) private pure returns (uint256 treeIndex) {
112+
uint256 currentDrawnNumber = _drawnNumber % _nodes[0];
113+
while ((_k * treeIndex) + 1 < _nodes.length) {
114+
// While it still has children.
115+
for (uint256 i = 1; i <= _k; i++) {
116+
// Loop over children.
117+
uint256 nodeIndex = (_k * treeIndex) + i;
118+
uint256 nodeValue = _nodes[nodeIndex];
119+
120+
if (currentDrawnNumber >= nodeValue)
121+
currentDrawnNumber -= nodeValue; // Go to the next child.
122+
else {
123+
// Pick this child.
124+
treeIndex = nodeIndex;
125+
break;
126+
}
127+
}
128+
}
129+
}
130+
131+
/**
132+
* @dev Unpacks a stake path ID into an account and a subcourt ID.
133+
* @param _stakePathID The stake path ID to unpack.
134+
* @return account The account.
135+
* @return subcourtID The subcourt ID.
136+
*/
137+
function stakePathIDToAccountAndSubcourtID(bytes32 _stakePathID)
138+
internal
139+
pure
140+
returns (address account, uint96 subcourtID)
141+
{
142+
assembly {
143+
// solium-disable-line security/no-inline-assembly
144+
let ptr := mload(0x40)
145+
for {
146+
let i := 0x00
147+
} lt(i, 0x14) {
148+
i := add(i, 0x01)
149+
} {
150+
mstore8(add(add(ptr, 0x0c), i), byte(i, _stakePathID))
151+
}
152+
account := mload(ptr)
153+
subcourtID := _stakePathID
154+
}
155+
}
74156
}

contracts/src/arbitration/Mock/MockKlerosCore.sol

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8;
4+
5+
import {SortitionSumTreeFactory} from "../../data-structures/SortitionSumTreeFactory.sol";
6+
// import "../DisputeKitPlurality.sol";
7+
import "../IArbitrable.sol";
8+
9+
contract MockKlerosCore {
10+
using SortitionSumTreeFactory for SortitionSumTreeFactory.SortitionSumTrees; // Use library functions for sortition sum trees.
11+
SortitionSumTreeFactory.SortitionSumTrees internal sortitionSumTrees; // The sortition sum trees.
12+
13+
struct Dispute {
14+
// Note that appeal `0` is equivalent to the first round of the dispute.
15+
uint96 subcourtID; // The ID of the subcourt the dispute is in.
16+
IArbitrable arbitrated; // The arbitrated arbitrable contract.
17+
// The number of choices jurors have when voting. This does not include choice `0` which is reserved for "refuse to arbitrate"/"no ruling".
18+
uint256 numberOfChoices;
19+
//Period period; // The current period of the dispute.
20+
uint256 lastPeriodChange; // The last time the period was changed.
21+
// The votes in the form `votes[appeal][voteID]`. On each round, a new list is pushed and packed with as many empty votes as there are draws. We use `dispute.votes.length` to get the number of appeals plus 1 for the first round.
22+
// Vote[][] votes;
23+
//VoteCounter[] voteCounters; // The vote counters in the form `voteCounters[appeal]`.
24+
uint256[] tokensAtStakePerJuror; // The amount of tokens at stake for each juror in the form `tokensAtStakePerJuror[appeal]`.
25+
uint256[] totalFeesForJurors; // The total juror fees paid in the form `totalFeesForJurors[appeal]`.
26+
uint256 drawsInRound; // A counter of draws made in the current round.
27+
uint256 commitsInRound; // A counter of commits made in the current round.
28+
uint256[] votesInEachRound; // A counter of votes made in each round in the form `votesInEachRound[appeal]`.
29+
// A counter of vote reward repartitions made in each round in the form `repartitionsInEachRound[appeal]`.
30+
uint256[] repartitionsInEachRound;
31+
uint256[] penaltiesInEachRound; // The amount of tokens collected from penalties in each round in the form `penaltiesInEachRound[appeal]`.
32+
bool ruled; // True if the ruling has been executed, false otherwise.
33+
}
34+
35+
Dispute[] public disputes;
36+
37+
constructor() {
38+
sortitionSumTrees.createTree(bytes32(0), 3);
39+
}
40+
41+
// DisputeKitPlurality disputeKit;
42+
43+
function getSortitionSumTree(bytes32 _key)
44+
public
45+
view
46+
returns (
47+
uint256 K,
48+
uint256[] memory stack,
49+
uint256[] memory nodes
50+
)
51+
{
52+
SortitionSumTreeFactory.SortitionSumTree storage tree = sortitionSumTrees.sortitionSumTrees[_key];
53+
K = tree.K;
54+
stack = tree.stack;
55+
nodes = tree.nodes;
56+
}
57+
58+
function getSortitionSumTreeID(bytes32 _key, uint256 _nodeIndex) public view returns (bytes32 ID) {
59+
ID = sortitionSumTrees.sortitionSumTrees[_key].nodeIndexesToIDs[_nodeIndex];
60+
}
61+
62+
function getDispute(uint256 _id) public view returns (Dispute memory) {
63+
return disputes[_id];
64+
}
65+
}

0 commit comments

Comments
 (0)