This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispute-manager.sol
177 lines (147 loc) · 5.72 KB
/
dispute-manager.sol
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
pragma solidity 0.8.9;
// SPDX-License-Identifier: UNLICENSED
import './merkle-utils.sol';
enum ChallengeStatus {
InProgress,
FraudDetected,
Forfeited
}
enum Mover {
Challenger,
Proposer
}
contract DisputeManager {
bytes32 root;
uint256 immutable splitFactor;
uint256 disputedIndex;
uint256 consensusIndex;
Mover lastMover;
ChallengeStatus public currentStatus;
uint256 public fraudIndex;
constructor(
bytes32[] memory _values,
uint256 _numSteps,
Mover _lastMover,
uint256 _splitFactor
) {
root = MerkleUtils.generateRoot(_values);
require(_splitFactor > 1, 'The splitting factor must be above 1');
require(_numSteps >= 1, 'There must be at least one element');
require(_values.length == _splitFactor + 1, 'There must be k+1 values');
disputedIndex = _numSteps;
lastMover = _lastMover;
splitFactor = _splitFactor;
consensusIndex = 0;
currentStatus = ChallengeStatus.InProgress;
}
function forfeit(Mover _mover) external {
if (_mover == lastMover) {
revert('The mover cannot be the same as the last mover');
}
currentStatus = ChallengeStatus.Forfeited;
}
function claimFraud(uint256 index, Mover _mover) external {
if (_mover == lastMover) {
revert('The mover cannot be the same as the last mover');
}
uint256 nextLeafIndex = getLeafIndex(index + 1);
uint256 currentLeafIndex = getLeafIndex(index);
require(currentLeafIndex + 1 == nextLeafIndex, 'Must be sibling nodes');
currentStatus = ChallengeStatus.FraudDetected;
fraudIndex = index;
}
function split(
WitnessProof calldata _consensusProof,
bytes32[] calldata _hashes,
WitnessProof calldata _disputedProof,
Mover _mover
) external {
if (_mover == lastMover) {
revert('The mover cannot be the same as the last mover');
}
if (!canSplitFurther()) {
revert('States cannot be split further');
}
if (currentStatus != ChallengeStatus.InProgress) {
revert('The challenge is already complete');
}
checkConsensusAndDisputeWitnesses(_consensusProof, _disputedProof);
if (_hashes[_hashes.length - 1] == _disputedProof.witness) {
revert('The last state supplied must differ from the disputed witness');
}
uint256 newConsensusLeafIndex = getLeafIndex(_consensusProof.index);
// The else case is when the consensus state is the second to last state in the state list.
// In that case, the disputed step not need to be updated.
uint256 newDisputedLeafIndex = getLeafIndex(_disputedProof.index);
if (_consensusProof.index != splitFactor - 1) {
newDisputedLeafIndex = getLeafIndex(_consensusProof.index + 1);
}
// The leaves are formed by concatenating consensusWitness + leaves supplied by the caller
uint256 intermediateLeaves = MerkleUtils.expectedNumOfLeaves(
newConsensusLeafIndex,
newDisputedLeafIndex,
splitFactor
) - 1;
if (_hashes.length != intermediateLeaves) {
revert('Incorrect amount of state hashes');
}
// Effects
consensusIndex = newConsensusLeafIndex;
disputedIndex = newDisputedLeafIndex;
bytes32[] memory newStateHashes = new bytes32[](_hashes.length + 1);
newStateHashes[0] = _consensusProof.witness;
for (uint256 i = 1; i < _hashes.length + 1; i++) {
newStateHashes[i] = _hashes[i - 1];
}
root = MerkleUtils.generateRoot(newStateHashes);
lastMover = _mover;
}
function checkConsensusAndDisputeWitnesses(
WitnessProof memory consensusProof,
WitnessProof memory disputedProof
) internal view {
if (consensusProof.index >= expectedNumOfLeaves() - 1) {
revert('Consensus witness cannot be the last stored state');
}
bool validConsensusWitness = validateLeafWitness(consensusProof, root, treeDepth());
if (!validConsensusWitness) {
revert('Invalid consensus witness proof');
}
bool validDisputeWitness = validateLeafWitness(disputedProof, root, treeDepth());
if (!validDisputeWitness) {
revert('Invalid dispute witness proof');
}
if (consensusProof.index + 1 != disputedProof.index) {
revert('Disputed state hash must be the next leaf after consensus state hash');
}
}
function validateLeafWitness(
WitnessProof memory _witnessProof,
bytes32 _root,
uint256 _depth
) internal pure returns (bool) {
require(
_witnessProof.nodes.length == _depth,
'The witness provided is not for a leaf node.'
);
return MerkleUtils.validateWitness(_witnessProof, _root);
}
function expectedNumOfLeaves() internal view returns (uint256) {
return MerkleUtils.expectedNumOfLeaves(consensusIndex, disputedIndex, splitFactor);
}
function treeDepth() internal view returns (uint256) {
uint256 numberOfElements = expectedNumOfLeaves();
uint256 result = log2(numberOfElements);
if (2**result == numberOfElements) {
return result;
} else {
return result + 1;
}
}
function getLeafIndex(uint256 index) internal view returns (uint256) {
return MerkleUtils.getLeafIndex(index, consensusIndex, disputedIndex, splitFactor);
}
function canSplitFurther() internal view returns (bool) {
return MerkleUtils.canSplitFurther(consensusIndex, disputedIndex, splitFactor);
}
}