Skip to content

Commit

Permalink
refactor: DisputeParameters -> ITournamentParametersProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Feb 8, 2025
1 parent 1d9e8ec commit 4162b64
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 113 deletions.
22 changes: 6 additions & 16 deletions cartesi-rollups/contracts/script/DaveConsensus.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,21 @@ import {Script} from "forge-std/Script.sol";
import {Machine} from "prt-contracts/Machine.sol";

import "prt-contracts/tournament/factories/MultiLevelTournamentFactory.sol";
import "prt-contracts/CanonicalConstants.sol";
import "prt-contracts/CanonicalTournamentParametersProvider.sol";
import "rollups-contracts/inputs/IInputBox.sol";
import "src/DaveConsensus.sol";

contract DaveConcensusScript is Script {
function run(Machine.Hash initialHash, IInputBox inputBox) external {
DisputeParameters memory disputeParameters = DisputeParameters({
timeConstants: TimeConstants({
matchEffort: ArbitrationConstants.MATCH_EFFORT,
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
}),
commitmentStructures: new CommitmentStructure[](ArbitrationConstants.LEVELS)
});

for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
disputeParameters.commitmentStructures[i] = CommitmentStructure({
log2step: ArbitrationConstants.log2step(i),
height: ArbitrationConstants.height(i)
});
}
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
new TopTournamentFactory(), new MiddleTournamentFactory(), new BottomTournamentFactory(), disputeParameters
new TopTournamentFactory(),
new MiddleTournamentFactory(),
new BottomTournamentFactory(),
new CanonicalTournamentParametersProvider()
);

new DaveConsensus(inputBox, address(0x0), factory, initialHash);

vm.stopBroadcast();
Expand Down
2 changes: 1 addition & 1 deletion cartesi-rollups/node/blockchain-reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ mod blockchain_reader_tests {
anvil,
provider,
Address::from_hex("0x5fbdb2315678afecb367f032d93f642f64180aa3").unwrap(),
Address::from_hex("0x0165878a594ca255338adfa4d48449f69242eb8f").unwrap(),
Address::from_hex("0xa513e6e4b8f2a923d98304ec87f64353c4d5c853").unwrap(),
)
}

Expand Down
21 changes: 2 additions & 19 deletions prt/contracts/script/TopTournament.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,17 @@ import {Machine} from "src/Machine.sol";

import "src/tournament/factories/MultiLevelTournamentFactory.sol";
import "src/IDataProvider.sol";
import "src/CanonicalConstants.sol";
import "src/CanonicalTournamentParametersProvider.sol";

contract TopTournamentScript is Script {
function run(Machine.Hash initialHash) external {
DisputeParameters memory disputeParameters = DisputeParameters({
timeConstants: TimeConstants({
matchEffort: ArbitrationConstants.MATCH_EFFORT,
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
}),
commitmentStructures: new CommitmentStructure[](
ArbitrationConstants.LEVELS
)
});

for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
disputeParameters.commitmentStructures[i] = CommitmentStructure({
log2step: ArbitrationConstants.log2step(i),
height: ArbitrationConstants.height(i)
});
}

vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
new TopTournamentFactory(),
new MiddleTournamentFactory(),
new BottomTournamentFactory(),
disputeParameters
new CanonicalTournamentParametersProvider()
);

factory.instantiate(initialHash, IDataProvider(address(0x0)));
Expand Down
27 changes: 27 additions & 0 deletions prt/contracts/src/CanonicalTournamentParametersProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.17;

import "./ITournamentParametersProvider.sol";
import "./CanonicalConstants.sol";

contract CanonicalTournamentParametersProvider is
ITournamentParametersProvider
{
/// @inheritdoc ITournamentParametersProvider
function tournamentParameters(uint64 level)
external
pure
override
returns (TournamentParameters memory)
{
return TournamentParameters({
levels: ArbitrationConstants.LEVELS,
log2step: ArbitrationConstants.log2step(level),
height: ArbitrationConstants.height(level),
matchEffort: ArbitrationConstants.MATCH_EFFORT,
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
});
}
}
15 changes: 15 additions & 0 deletions prt/contracts/src/ITournamentParametersProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.17;

import {TournamentParameters} from "./TournamentParameters.sol";

interface ITournamentParametersProvider {
/// @notice Get tournament parameters for a given level.
/// @param level The tournament level (0 = top)
function tournamentParameters(uint64 level)
external
view
returns (TournamentParameters memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,43 @@ pragma solidity ^0.8.17;

import "../../IMultiLevelTournamentFactory.sol";
import "../../TournamentParameters.sol";
import "../../ITournamentParametersProvider.sol";

import "./multilevel/TopTournamentFactory.sol";
import "./multilevel/MiddleTournamentFactory.sol";
import "./multilevel/BottomTournamentFactory.sol";

struct CommitmentStructure {
uint64 log2step;
uint64 height;
}

struct TimeConstants {
Time.Duration matchEffort;
Time.Duration maxAllowance;
}

struct DisputeParameters {
TimeConstants timeConstants;
CommitmentStructure[] commitmentStructures;
}

contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
TopTournamentFactory immutable topFactory;
MiddleTournamentFactory immutable middleFactory;
BottomTournamentFactory immutable bottomFactory;
uint64 immutable levels;
Time.Duration immutable matchEffort;
Time.Duration immutable maxAllowance;
uint64 immutable log2step0;
uint64 immutable height0;
CommitmentStructure[] commitmentStructures;

error CommitmentStructuresArrayLengthTooSmall();
error CommitmentStructuresArrayLengthTooLarge();
ITournamentParametersProvider immutable tournamentParametersProvider;

constructor(
TopTournamentFactory _topFactory,
MiddleTournamentFactory _middleFactory,
BottomTournamentFactory _bottomFactory,
DisputeParameters memory _disputeParameters
ITournamentParametersProvider _tournamentParametersProvider
) {
topFactory = _topFactory;
middleFactory = _middleFactory;
bottomFactory = _bottomFactory;

require(
_disputeParameters.commitmentStructures.length >= 1,
CommitmentStructuresArrayLengthTooSmall()
);
require(
_disputeParameters.commitmentStructures.length <= type(uint64).max,
CommitmentStructuresArrayLengthTooLarge()
);

levels = uint64(_disputeParameters.commitmentStructures.length);
matchEffort = _disputeParameters.timeConstants.matchEffort;
maxAllowance = _disputeParameters.timeConstants.maxAllowance;
log2step0 = _disputeParameters.commitmentStructures[0].log2step;
height0 = _disputeParameters.commitmentStructures[0].height;
commitmentStructures = _disputeParameters.commitmentStructures;
tournamentParametersProvider = _tournamentParametersProvider;
}

function instantiate(Machine.Hash _initialHash, IDataProvider _provider)
external
override
returns (ITournament)
{
TopTournament _tournament = this.instantiateTop(_initialHash, _provider);
TopTournament _tournament = instantiateTop(_initialHash, _provider);
emit tournamentCreated(_tournament);
return _tournament;
}

function instantiateTop(Machine.Hash _initialHash, IDataProvider _provider)
external
public
override
returns (TopTournament)
{
TopTournament _tournament = topFactory.instantiate(
Expand All @@ -96,7 +60,7 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
uint256 _startCycle,
uint64 _level,
IDataProvider _provider
) external returns (MiddleTournament) {
) external override returns (MiddleTournament) {
MiddleTournament _tournament = middleFactory.instantiate(
_initialHash,
_contestedCommitmentOne,
Expand Down Expand Up @@ -124,7 +88,7 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
uint256 _startCycle,
uint64 _level,
IDataProvider _provider
) external returns (BottomTournament) {
) external override returns (BottomTournament) {
BottomTournament _tournament = bottomFactory.instantiate(
_initialHash,
_contestedCommitmentOne,
Expand All @@ -146,26 +110,14 @@ contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
view
returns (TournamentParameters memory)
{
return TournamentParameters({
levels: levels,
log2step: log2step0,
height: height0,
matchEffort: matchEffort,
maxAllowance: maxAllowance
});
return _getTournamentParameters(0);
}

function _getTournamentParameters(uint64 _level)
internal
view
returns (TournamentParameters memory)
{
return TournamentParameters({
levels: levels,
log2step: commitmentStructures[_level].log2step,
height: commitmentStructures[_level].height,
matchEffort: matchEffort,
maxAllowance: maxAllowance
});
return tournamentParametersProvider.tournamentParameters(_level);
}
}
20 changes: 2 additions & 18 deletions prt/contracts/test/Util.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import "src/tournament/libs/Match.sol";
import "src/CanonicalConstants.sol";
import "src/CanonicalTournamentParametersProvider.sol";
import "src/tournament/concretes/TopTournament.sol";
import "src/tournament/concretes/MiddleTournament.sol";

Expand Down Expand Up @@ -289,28 +290,11 @@ contract Util {
internal
returns (MultiLevelTournamentFactory)
{
DisputeParameters memory disputeParameters = DisputeParameters({
timeConstants: TimeConstants({
matchEffort: ArbitrationConstants.MATCH_EFFORT,
maxAllowance: ArbitrationConstants.MAX_ALLOWANCE
}),
commitmentStructures: new CommitmentStructure[](
ArbitrationConstants.LEVELS
)
});

for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
disputeParameters.commitmentStructures[i] = CommitmentStructure({
log2step: ArbitrationConstants.log2step(i),
height: ArbitrationConstants.height(i)
});
}

return new MultiLevelTournamentFactory(
new TopTournamentFactory(),
new MiddleTournamentFactory(),
new BottomTournamentFactory(),
disputeParameters
new CanonicalTournamentParametersProvider()
);
}
}

0 comments on commit 4162b64

Please sign in to comment.