Skip to content

Commit

Permalink
fix(prt-contracts): avoid circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Sep 5, 2024
1 parent 38726af commit b596110
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 44 deletions.
38 changes: 38 additions & 0 deletions prt/contracts/src/IMultiLevelTournamentFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.17;

import "./ITournamentFactory.sol";

import "./tournament/concretes/TopTournament.sol";
import "./tournament/concretes/MiddleTournament.sol";
import "./tournament/concretes/BottomTournament.sol";

interface IMultiLevelTournamentFactory is ITournamentFactory {
function instantiateTop(Machine.Hash _initialHash)
external
returns (TopTournament);

function instantiateMiddle(
Machine.Hash _initialHash,
Tree.Node _contestedCommitmentOne,
Machine.Hash _contestedFinalStateOne,
Tree.Node _contestedCommitmentTwo,
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level
) external returns (MiddleTournament);

function instantiateBottom(
Machine.Hash _initialHash,
Tree.Node _contestedCommitmentOne,
Machine.Hash _contestedFinalStateOne,
Tree.Node _contestedCommitmentTwo,
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level
) external returns (BottomTournament);
}
7 changes: 3 additions & 4 deletions prt/contracts/src/tournament/abstracts/NonLeafTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

pragma solidity ^0.8.17;

// import "../factories/multilevel/IMultiLevelTournamentFactory.sol";
import "../factories/MultiLevelTournamentFactory.sol";
import "../../IMultiLevelTournamentFactory.sol";
import "./Tournament.sol";
import "./NonRootTournament.sol";

Expand All @@ -22,7 +21,7 @@ abstract contract NonLeafTournament is Tournament {
//
// Constants
//
MultiLevelTournamentFactory immutable tournamentFactory;
IMultiLevelTournamentFactory immutable tournamentFactory;

//
// Storage
Expand All @@ -47,7 +46,7 @@ abstract contract NonLeafTournament is Tournament {
//
// Constructor
//
constructor(MultiLevelTournamentFactory _tournamentFactory) {
constructor(IMultiLevelTournamentFactory _tournamentFactory) {
tournamentFactory = _tournamentFactory;
}

Expand Down
7 changes: 1 addition & 6 deletions prt/contracts/src/tournament/abstracts/NonRootTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ abstract contract NonRootTournament is Tournament {
//
// Constants
//
NonLeafTournament immutable parentTournament;

Tree.Node immutable contestedCommitmentOne;
Machine.Hash immutable contestedFinalStateOne;
Tree.Node immutable contestedCommitmentTwo;
Expand All @@ -32,11 +30,8 @@ abstract contract NonRootTournament is Tournament {
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
NonLeafTournament _parent
uint64 _level
) Tournament(_initialHash, _allowance, _startCycle, _level) {
parentTournament = _parent;

contestedCommitmentOne = _contestedCommitmentOne;
contestedFinalStateOne = _contestedFinalStateOne;
contestedCommitmentTwo = _contestedCommitmentTwo;
Expand Down
6 changes: 2 additions & 4 deletions prt/contracts/src/tournament/concretes/BottomTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ contract BottomTournament is LeafTournament, NonRootTournament {
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
NonLeafTournament _parent
uint64 _level
)
LeafTournament()
NonRootTournament(
Expand All @@ -28,8 +27,7 @@ contract BottomTournament is LeafTournament, NonRootTournament {
_contestedFinalStateTwo,
_allowance,
_startCycle,
_level,
_parent
_level
)
{}
}
8 changes: 3 additions & 5 deletions prt/contracts/src/tournament/concretes/MiddleTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity ^0.8.17;
import "../abstracts/NonLeafTournament.sol";
import "../abstracts/NonRootTournament.sol";

import "../factories/MultiLevelTournamentFactory.sol";
import "../../IMultiLevelTournamentFactory.sol";

/// @notice Middle tournament is non-top, non-bottom of a multi-level instance
contract MiddleTournament is NonLeafTournament, NonRootTournament {
Expand All @@ -19,8 +19,7 @@ contract MiddleTournament is NonLeafTournament, NonRootTournament {
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
NonLeafTournament _parent,
MultiLevelTournamentFactory _tournamentFactory
IMultiLevelTournamentFactory _tournamentFactory
)
NonLeafTournament(_tournamentFactory)
NonRootTournament(
Expand All @@ -31,8 +30,7 @@ contract MiddleTournament is NonLeafTournament, NonRootTournament {
_contestedFinalStateTwo,
_allowance,
_startCycle,
_level,
_parent
_level
)
{}
}
10 changes: 5 additions & 5 deletions prt/contracts/src/tournament/concretes/TopTournament.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ pragma solidity ^0.8.17;
import "../abstracts/RootTournament.sol";
import "../abstracts/NonLeafTournament.sol";

import "../factories/MultiLevelTournamentFactory.sol";
import "../../IMultiLevelTournamentFactory.sol";

import "../../Machine.sol";

/// @notice Top tournament of a multi-level instance
contract TopTournament is NonLeafTournament, RootTournament {
constructor(Machine.Hash _initialHash, MultiLevelTournamentFactory _factory)
NonLeafTournament(_factory)
RootTournament(_initialHash)
{}
constructor(
Machine.Hash _initialHash,
IMultiLevelTournamentFactory _factory
) NonLeafTournament(_factory) RootTournament(_initialHash) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

pragma solidity ^0.8.17;

import "../../ITournamentFactory.sol";

import "../concretes/TopTournament.sol";
import "../concretes/MiddleTournament.sol";
import "../concretes/BottomTournament.sol";
import "../../IMultiLevelTournamentFactory.sol";

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

contract MultiLevelTournamentFactory is ITournamentFactory {
contract MultiLevelTournamentFactory is IMultiLevelTournamentFactory {
TopTournamentFactory immutable topFactory;
MiddleTournamentFactory immutable middleFactory;
BottomTournamentFactory immutable bottomFactory;
Expand Down Expand Up @@ -64,8 +60,7 @@ contract MultiLevelTournamentFactory is ITournamentFactory {
_contestedFinalStateTwo,
_allowance,
_startCycle,
_level,
NonLeafTournament(msg.sender)
_level
);

return _tournament;
Expand All @@ -89,8 +84,7 @@ contract MultiLevelTournamentFactory is ITournamentFactory {
_contestedFinalStateTwo,
_allowance,
_startCycle,
_level,
NonLeafTournament(msg.sender)
_level
);

return _tournament;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ contract BottomTournamentFactory {
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
NonLeafTournament _parent
uint64 _level
) external returns (BottomTournament) {
BottomTournament _tournament = new BottomTournament(
_initialHash,
Expand All @@ -27,8 +26,7 @@ contract BottomTournamentFactory {
_contestedFinalStateTwo,
_allowance,
_startCycle,
_level,
_parent
_level
);

return _tournament;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity ^0.8.17;
import "../../abstracts/NonLeafTournament.sol";
import "../../concretes/MiddleTournament.sol";

import "../../factories/MultiLevelTournamentFactory.sol";
import "../../../IMultiLevelTournamentFactory.sol";

import "../../../Machine.sol";
import "../../../Tree.sol";
Expand All @@ -23,8 +23,7 @@ contract MiddleTournamentFactory {
Machine.Hash _contestedFinalStateTwo,
Time.Duration _allowance,
uint256 _startCycle,
uint64 _level,
NonLeafTournament _parent
uint64 _level
) external returns (MiddleTournament) {
MiddleTournament _tournament = new MiddleTournament(
_initialHash,
Expand All @@ -35,8 +34,7 @@ contract MiddleTournamentFactory {
_allowance,
_startCycle,
_level,
_parent,
MultiLevelTournamentFactory(msg.sender)
IMultiLevelTournamentFactory(msg.sender)
);

return _tournament;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract TopTournamentFactory {
returns (TopTournament)
{
TopTournament _tournament = new TopTournament(
_initialHash, MultiLevelTournamentFactory(msg.sender)
_initialHash, IMultiLevelTournamentFactory(msg.sender)
);

return _tournament;
Expand Down
1 change: 1 addition & 0 deletions prt/contracts/test/Util.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "src/tournament/concretes/TopTournament.sol";
import "src/tournament/concretes/MiddleTournament.sol";

import "src/tournament/factories/SingleLevelTournamentFactory.sol";
import "src/tournament/factories/MultiLevelTournamentFactory.sol";
import "src/tournament/factories/multilevel/TopTournamentFactory.sol";
import "src/tournament/factories/multilevel/MiddleTournamentFactory.sol";
import "src/tournament/factories/multilevel/BottomTournamentFactory.sol";
Expand Down

0 comments on commit b596110

Please sign in to comment.