Skip to content

Commit d7892f7

Browse files
committed
refactor: DisputeParameters -> ITournamentParametersProvider
1 parent c24cce4 commit d7892f7

File tree

7 files changed

+64
-113
lines changed

7 files changed

+64
-113
lines changed

cartesi-rollups/contracts/script/DaveConsensus.s.sol

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,21 @@ import {Script} from "forge-std/Script.sol";
88
import {Machine} from "prt-contracts/Machine.sol";
99

1010
import "prt-contracts/tournament/factories/MultiLevelTournamentFactory.sol";
11-
import "prt-contracts/CanonicalConstants.sol";
11+
import "prt-contracts/CanonicalTournamentParametersProvider.sol";
1212
import "rollups-contracts/inputs/IInputBox.sol";
1313
import "src/DaveConsensus.sol";
1414

1515
contract DaveConcensusScript is Script {
1616
function run(Machine.Hash initialHash, IInputBox inputBox) external {
17-
DisputeParameters memory disputeParameters = DisputeParameters({
18-
timeConstants: TimeConstants({
19-
matchEffort: ArbitrationConstants.MATCH_EFFORT,
20-
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
21-
}),
22-
commitmentStructures: new CommitmentStructure[](ArbitrationConstants.LEVELS)
23-
});
24-
25-
for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
26-
disputeParameters.commitmentStructures[i] = CommitmentStructure({
27-
log2step: ArbitrationConstants.log2step(i),
28-
height: ArbitrationConstants.height(i)
29-
});
30-
}
3117
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
3218

3319
MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
34-
new TopTournamentFactory(), new MiddleTournamentFactory(), new BottomTournamentFactory(), disputeParameters
20+
new TopTournamentFactory(),
21+
new MiddleTournamentFactory(),
22+
new BottomTournamentFactory(),
23+
new CanonicalTournamentParametersProvider()
3524
);
25+
3626
new DaveConsensus(inputBox, address(0x0), factory, initialHash);
3727

3828
vm.stopBroadcast();

cartesi-rollups/node/blockchain-reader/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ mod blockchain_reader_tests {
503503
anvil,
504504
provider,
505505
Address::from_hex("0x5fbdb2315678afecb367f032d93f642f64180aa3").unwrap(),
506-
Address::from_hex("0x0165878a594ca255338adfa4d48449f69242eb8f").unwrap(),
506+
Address::from_hex("0xa513e6e4b8f2a923d98304ec87f64353c4d5c853").unwrap(),
507507
)
508508
}
509509

prt/contracts/script/TopTournament.s.sol

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,17 @@ import {Machine} from "src/Machine.sol";
99

1010
import "src/tournament/factories/MultiLevelTournamentFactory.sol";
1111
import "src/IDataProvider.sol";
12-
import "src/CanonicalConstants.sol";
12+
import "src/CanonicalTournamentParametersProvider.sol";
1313

1414
contract TopTournamentScript is Script {
1515
function run(Machine.Hash initialHash) external {
16-
DisputeParameters memory disputeParameters = DisputeParameters({
17-
timeConstants: TimeConstants({
18-
matchEffort: ArbitrationConstants.MATCH_EFFORT,
19-
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
20-
}),
21-
commitmentStructures: new CommitmentStructure[](
22-
ArbitrationConstants.LEVELS
23-
)
24-
});
25-
26-
for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
27-
disputeParameters.commitmentStructures[i] = CommitmentStructure({
28-
log2step: ArbitrationConstants.log2step(i),
29-
height: ArbitrationConstants.height(i)
30-
});
31-
}
32-
3316
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
3417

3518
MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
3619
new TopTournamentFactory(),
3720
new MiddleTournamentFactory(),
3821
new BottomTournamentFactory(),
39-
disputeParameters
22+
new CanonicalTournamentParametersProvider()
4023
);
4124

4225
factory.instantiate(initialHash, IDataProvider(address(0x0)));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// (c) Cartesi and individual authors (see AUTHORS)
2+
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3+
4+
pragma solidity ^0.8.17;
5+
6+
import "./ITournamentParametersProvider.sol";
7+
import "./CanonicalConstants.sol";
8+
9+
contract CanonicalTournamentParametersProvider is
10+
ITournamentParametersProvider
11+
{
12+
/// @inheritdoc ITournamentParametersProvider
13+
function tournamentParameters(uint64 level)
14+
external
15+
pure
16+
override
17+
returns (TournamentParameters memory)
18+
{
19+
return TournamentParameters({
20+
levels: ArbitrationConstants.LEVELS,
21+
log2step: ArbitrationConstants.log2step(level),
22+
height: ArbitrationConstants.height(level),
23+
matchEffort: ArbitrationConstants.MATCH_EFFORT,
24+
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
25+
});
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// (c) Cartesi and individual authors (see AUTHORS)
2+
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3+
4+
pragma solidity ^0.8.17;
5+
6+
import {TournamentParameters} from "./TournamentParameters.sol";
7+
8+
interface ITournamentParametersProvider {
9+
/// @notice Get tournament parameters for a given level.
10+
/// @param level The tournament level (0 = top)
11+
function tournamentParameters(uint64 level)
12+
external
13+
view
14+
returns (TournamentParameters memory);
15+
}

prt/contracts/src/tournament/factories/MultiLevelTournamentFactory.sol

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,43 @@ pragma solidity ^0.8.17;
55

66
import "../../IMultiLevelTournamentFactory.sol";
77
import "../../TournamentParameters.sol";
8+
import "../../ITournamentParametersProvider.sol";
89

910
import "./multilevel/TopTournamentFactory.sol";
1011
import "./multilevel/MiddleTournamentFactory.sol";
1112
import "./multilevel/BottomTournamentFactory.sol";
1213

13-
struct CommitmentStructure {
14-
uint64 log2step;
15-
uint64 height;
16-
}
17-
18-
struct TimeConstants {
19-
Time.Duration matchEffort;
20-
Time.Duration maxAllowance;
21-
}
22-
23-
struct DisputeParameters {
24-
TimeConstants timeConstants;
25-
CommitmentStructure[] commitmentStructures;
26-
}
27-
2814
contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
2915
TopTournamentFactory immutable topFactory;
3016
MiddleTournamentFactory immutable middleFactory;
3117
BottomTournamentFactory immutable bottomFactory;
32-
uint64 immutable levels;
33-
Time.Duration immutable matchEffort;
34-
Time.Duration immutable maxAllowance;
35-
uint64 immutable log2step0;
36-
uint64 immutable height0;
37-
CommitmentStructure[] commitmentStructures;
38-
39-
error CommitmentStructuresArrayLengthTooSmall();
40-
error CommitmentStructuresArrayLengthTooLarge();
18+
ITournamentParametersProvider immutable tournamentParametersProvider;
4119

4220
constructor(
4321
TopTournamentFactory _topFactory,
4422
MiddleTournamentFactory _middleFactory,
4523
BottomTournamentFactory _bottomFactory,
46-
DisputeParameters memory _disputeParameters
24+
ITournamentParametersProvider _tournamentParametersProvider
4725
) {
4826
topFactory = _topFactory;
4927
middleFactory = _middleFactory;
5028
bottomFactory = _bottomFactory;
51-
52-
require(
53-
_disputeParameters.commitmentStructures.length >= 1,
54-
CommitmentStructuresArrayLengthTooSmall()
55-
);
56-
require(
57-
_disputeParameters.commitmentStructures.length <= type(uint64).max,
58-
CommitmentStructuresArrayLengthTooLarge()
59-
);
60-
61-
levels = uint64(_disputeParameters.commitmentStructures.length);
62-
matchEffort = _disputeParameters.timeConstants.matchEffort;
63-
maxAllowance = _disputeParameters.timeConstants.maxAllowance;
64-
log2step0 = _disputeParameters.commitmentStructures[0].log2step;
65-
height0 = _disputeParameters.commitmentStructures[0].height;
66-
commitmentStructures = _disputeParameters.commitmentStructures;
29+
tournamentParametersProvider = _tournamentParametersProvider;
6730
}
6831

6932
function instantiate(Machine.Hash _initialHash, IDataProvider _provider)
7033
external
7134
override
7235
returns (ITournament)
7336
{
74-
TopTournament _tournament = this.instantiateTop(_initialHash, _provider);
37+
TopTournament _tournament = instantiateTop(_initialHash, _provider);
7538
emit tournamentCreated(_tournament);
7639
return _tournament;
7740
}
7841

7942
function instantiateTop(Machine.Hash _initialHash, IDataProvider _provider)
80-
external
43+
public
44+
override
8145
returns (TopTournament)
8246
{
8347
TopTournament _tournament = topFactory.instantiate(
@@ -96,7 +60,7 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
9660
uint256 _startCycle,
9761
uint64 _level,
9862
IDataProvider _provider
99-
) external returns (MiddleTournament) {
63+
) external override returns (MiddleTournament) {
10064
MiddleTournament _tournament = middleFactory.instantiate(
10165
_initialHash,
10266
_contestedCommitmentOne,
@@ -124,7 +88,7 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
12488
uint256 _startCycle,
12589
uint64 _level,
12690
IDataProvider _provider
127-
) external returns (BottomTournament) {
91+
) external override returns (BottomTournament) {
12892
BottomTournament _tournament = bottomFactory.instantiate(
12993
_initialHash,
13094
_contestedCommitmentOne,
@@ -146,26 +110,14 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
146110
view
147111
returns (TournamentParameters memory)
148112
{
149-
return TournamentParameters({
150-
levels: levels,
151-
log2step: log2step0,
152-
height: height0,
153-
matchEffort: matchEffort,
154-
maxAllowance: maxAllowance
155-
});
113+
return _getTournamentParameters(0);
156114
}
157115

158116
function _getTournamentParameters(uint64 _level)
159117
internal
160118
view
161119
returns (TournamentParameters memory)
162120
{
163-
return TournamentParameters({
164-
levels: levels,
165-
log2step: commitmentStructures[_level].log2step,
166-
height: commitmentStructures[_level].height,
167-
matchEffort: matchEffort,
168-
maxAllowance: maxAllowance
169-
});
121+
return tournamentParametersProvider.tournamentParameters(_level);
170122
}
171123
}

prt/contracts/test/Util.sol

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import "src/tournament/libs/Match.sol";
1414
import "src/CanonicalConstants.sol";
15+
import "src/CanonicalTournamentParametersProvider.sol";
1516
import "src/tournament/concretes/TopTournament.sol";
1617
import "src/tournament/concretes/MiddleTournament.sol";
1718

@@ -289,28 +290,11 @@ contract Util {
289290
internal
290291
returns (MultiLevelTournamentFactory)
291292
{
292-
DisputeParameters memory disputeParameters = DisputeParameters({
293-
timeConstants: TimeConstants({
294-
matchEffort: ArbitrationConstants.MATCH_EFFORT,
295-
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
296-
}),
297-
commitmentStructures: new CommitmentStructure[](
298-
ArbitrationConstants.LEVELS
299-
)
300-
});
301-
302-
for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
303-
disputeParameters.commitmentStructures[i] = CommitmentStructure({
304-
log2step: ArbitrationConstants.log2step(i),
305-
height: ArbitrationConstants.height(i)
306-
});
307-
}
308-
309293
return new MultiLevelTournamentFactory(
310294
new TopTournamentFactory(),
311295
new MiddleTournamentFactory(),
312296
new BottomTournamentFactory(),
313-
disputeParameters
297+
new CanonicalTournamentParametersProvider()
314298
);
315299
}
316300
}

0 commit comments

Comments
 (0)