Skip to content

Commit

Permalink
Parameterize constants
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Dec 12, 2024
1 parent 6c58d83 commit 6fe9ce1
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 80 deletions.
15 changes: 14 additions & 1 deletion prt/contracts/script/TopTournament.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ import {Machine} from "src/Machine.sol";

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

contract TopTournamentScript is Script {
function run(Machine.Hash initialHash) external {
uint64[] memory log2step = new uint64[](ArbitrationConstants.LEVELS);
uint64[] memory height = new uint64[](ArbitrationConstants.LEVELS);

for (uint64 i; i < ArbitrationConstants.LEVELS; ++i) {
log2step[i] = ArbitrationConstants.log2step(i);
height[i] = ArbitrationConstants.height(i);
}

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

MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
new TopTournamentFactory(),
new MiddleTournamentFactory(),
new BottomTournamentFactory()
new BottomTournamentFactory(),
ArbitrationConstants.MATCH_EFFORT,
ArbitrationConstants.MAX_ALLOWANCE,
log2step,
height
);

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

pragma solidity ^0.8.17;

import "./tournament/libs/Time.sol";

struct TournamentParameters {
uint64 levels;
uint64 log2step;
uint64 height;
Time.Duration matchEffort;
Time.Duration maxAllowance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ abstract contract NonLeafTournament is Tournament {
// the inner tournament is bottom tournament at last level
// else instantiate middle tournament
Tournament _tournament;
if (_level == ArbitrationConstants.LEVELS - 1) {
if (_level == levels - 1) {
_tournament = tournamentFactory.instantiateBottom(
_initialHash,
_contestedCommitmentOne,
Expand Down
13 changes: 12 additions & 1 deletion prt/contracts/src/tournament/abstracts/NonRootTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.17;

import "./Tournament.sol";
import "./NonLeafTournament.sol";
import "../../TournamentParameters.sol";

/// @notice Non-root tournament needs to propagate side-effects to its parent
abstract contract NonRootTournament is Tournament {
Expand All @@ -31,8 +32,18 @@ abstract contract NonRootTournament is Tournament {
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider
) Tournament(_initialHash, _allowance, _startCycle, _level, _provider) {
)
Tournament(
_initialHash,
_allowance,
_startCycle,
_level,
_tournamentParameters,
_provider
)
{
contestedCommitmentOne = _contestedCommitmentOne;
contestedFinalStateOne = _contestedFinalStateOne;
contestedCommitmentTwo = _contestedCommitmentTwo;
Expand Down
8 changes: 7 additions & 1 deletion prt/contracts/src/tournament/abstracts/RootTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ pragma solidity ^0.8.17;

import "./Tournament.sol";
import "../../ITournament.sol";
import "../../TournamentParameters.sol";

/// @notice Root tournament has no parent
abstract contract RootTournament is Tournament, ITournament {
//
// Constructor
//
constructor(Machine.Hash _initialHash, IDataProvider _provider)
constructor(
Machine.Hash _initialHash,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider
)
Tournament(
_initialHash,
ArbitrationConstants.MAX_ALLOWANCE,
0,
0,
_tournamentParameters,
_provider
)
{}
Expand Down
37 changes: 25 additions & 12 deletions prt/contracts/src/tournament/abstracts/Tournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pragma solidity ^0.8.17;

import "../../CanonicalConstants.sol";
import "../../IDataProvider.sol";
import "../../TournamentParameters.sol";
import "../../Machine.sol";
import "../../Tree.sol";

Expand Down Expand Up @@ -42,9 +43,14 @@ abstract contract Tournament {

uint256 immutable startCycle;
uint64 immutable level;
uint64 immutable levels;
uint64 immutable log2step;
uint64 immutable height;

Time.Instant immutable startInstant;
Time.Duration immutable allowance;
Time.Duration immutable maxAllowance;
Time.Duration immutable matchEffort;

IDataProvider immutable provider;

Expand Down Expand Up @@ -93,18 +99,20 @@ abstract contract Tournament {
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider
) {
initialHash = _initialHash;
startCycle = _startCycle;
level = _level;
levels = _tournamentParameters.levels;
log2step = _tournamentParameters.log2step;
height = _tournamentParameters.height;
startInstant = Time.currentTime();
allowance = _allowance;
allowance = _allowance.min(_tournamentParameters.maxAllowance);
matchEffort = _tournamentParameters.matchEffort;
maxAllowance = _tournamentParameters.maxAllowance;
provider = _provider;

if (_allowance.gt(ArbitrationConstants.MAX_ALLOWANCE)) {
_allowance = ArbitrationConstants.MAX_ALLOWANCE;
}
}

//
Expand Down Expand Up @@ -132,7 +140,7 @@ abstract contract Tournament {
Tree.Node _commitmentRoot = _leftNode.join(_rightNode);

// Prove final state is in commitmentRoot
_commitmentRoot.requireFinalState(level, _finalState, _proof);
_commitmentRoot.requireFinalState(height, _finalState, _proof);

// Verify whether finalState is one of the two allowed of tournament if nested
requireValidContestedFinalState(_finalState);
Expand Down Expand Up @@ -294,10 +302,10 @@ abstract contract Tournament {
uint64 _height
)
{
_max_level = ArbitrationConstants.LEVELS;
_max_level = levels;
_level = level;
_log2step = ArbitrationConstants.log2step(level);
_height = ArbitrationConstants.height(level);
_log2step = log2step;
_height = height;
}

//
Expand Down Expand Up @@ -359,16 +367,21 @@ abstract contract Tournament {
if (_hasDanglingCommitment) {
(Match.IdHash _matchId, Match.State memory _matchState) = Match
.createMatch(
_danglingCommitment, _rootHash, _leftNode, _rightNode, level
_danglingCommitment,
_rootHash,
_leftNode,
_rightNode,
log2step,
height
);

matches[_matchId] = _matchState;

Clock.State storage _firstClock = clocks[_danglingCommitment];

// grant extra match effort for both clocks
_firstClock.addMatchEffort();
_newClock.addMatchEffort();
_firstClock.addMatchEffort(matchEffort, maxAllowance);
_newClock.addMatchEffort(matchEffort, maxAllowance);

_firstClock.advanceClock();

Expand Down
4 changes: 4 additions & 0 deletions prt/contracts/src/tournament/concretes/BottomTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pragma solidity ^0.8.17;
import "../abstracts/LeafTournament.sol";
import "../abstracts/NonRootTournament.sol";

import "../../TournamentParameters.sol";

/// @notice Bottom tournament of a multi-level instance
contract BottomTournament is LeafTournament, NonRootTournament {
constructor(
Expand All @@ -17,6 +19,7 @@ contract BottomTournament is LeafTournament, NonRootTournament {
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider
)
LeafTournament()
Expand All @@ -29,6 +32,7 @@ contract BottomTournament is LeafTournament, NonRootTournament {
_allowance,
_startCycle,
_level,
_tournamentParameters,
_provider
)
{}
Expand Down
3 changes: 3 additions & 0 deletions prt/contracts/src/tournament/concretes/MiddleTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma solidity ^0.8.17;
import "../abstracts/NonLeafTournament.sol";
import "../abstracts/NonRootTournament.sol";

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

/// @notice Middle tournament is non-top, non-bottom of a multi-level instance
Expand All @@ -19,6 +20,7 @@ contract MiddleTournament is NonLeafTournament, NonRootTournament {
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider,
IMultiLevelTournamentFactory _tournamentFactory
)
Expand All @@ -32,6 +34,7 @@ contract MiddleTournament is NonLeafTournament, NonRootTournament {
_allowance,
_startCycle,
_level,
_tournamentParameters,
_provider
)
{}
Expand Down
10 changes: 8 additions & 2 deletions prt/contracts/src/tournament/concretes/SingleLevelTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ pragma solidity ^0.8.17;
import "../abstracts/RootTournament.sol";
import "../abstracts/LeafTournament.sol";

import "../../TournamentParameters.sol";

contract SingleLevelTournament is LeafTournament, RootTournament {
constructor(Machine.Hash _initialHash, IDataProvider _provider)
constructor(
Machine.Hash _initialHash,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider
)
LeafTournament()
RootTournament(_initialHash, _provider)
RootTournament(_initialHash, _tournamentParameters, _provider)
{}
}
9 changes: 7 additions & 2 deletions prt/contracts/src/tournament/concretes/TopTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma solidity ^0.8.17;
import "../abstracts/RootTournament.sol";
import "../abstracts/NonLeafTournament.sol";

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

import "../../Machine.sol";
Expand All @@ -14,7 +15,11 @@ import "../../Machine.sol";
contract TopTournament is NonLeafTournament, RootTournament {
constructor(
Machine.Hash _initialHash,
TournamentParameters memory _tournamentParameters,
IDataProvider _provider,
IMultiLevelTournamentFactory _factory
) NonLeafTournament(_factory) RootTournament(_initialHash, _provider) {}
IMultiLevelTournamentFactory _tournamentFactory
)
NonLeafTournament(_tournamentFactory)
RootTournament(_initialHash, _tournamentParameters, _provider)
{}
}
Loading

0 comments on commit 6fe9ce1

Please sign in to comment.