From b59611020733f0454dfa11b5be99fb8951ac49aa Mon Sep 17 00:00:00 2001 From: Stephen Chen <20940639+stephenctw@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:54:39 +0800 Subject: [PATCH] fix(prt-contracts): avoid circular dependencies --- .../src/IMultiLevelTournamentFactory.sol | 38 +++++++++++++++++++ .../abstracts/NonLeafTournament.sol | 7 ++-- .../abstracts/NonRootTournament.sol | 7 +--- .../tournament/concretes/BottomTournament.sol | 6 +-- .../tournament/concretes/MiddleTournament.sol | 8 ++-- .../tournament/concretes/TopTournament.sol | 10 ++--- .../factories/MultiLevelTournamentFactory.sol | 14 ++----- .../multilevel/BottomTournamentFactory.sol | 6 +-- .../multilevel/MiddleTournamentFactory.sol | 8 ++-- .../multilevel/TopTournamentFactory.sol | 2 +- prt/contracts/test/Util.sol | 1 + 11 files changed, 63 insertions(+), 44 deletions(-) create mode 100644 prt/contracts/src/IMultiLevelTournamentFactory.sol diff --git a/prt/contracts/src/IMultiLevelTournamentFactory.sol b/prt/contracts/src/IMultiLevelTournamentFactory.sol new file mode 100644 index 00000000..3f9ec4aa --- /dev/null +++ b/prt/contracts/src/IMultiLevelTournamentFactory.sol @@ -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); +} diff --git a/prt/contracts/src/tournament/abstracts/NonLeafTournament.sol b/prt/contracts/src/tournament/abstracts/NonLeafTournament.sol index 1388a277..80daad29 100644 --- a/prt/contracts/src/tournament/abstracts/NonLeafTournament.sol +++ b/prt/contracts/src/tournament/abstracts/NonLeafTournament.sol @@ -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"; @@ -22,7 +21,7 @@ abstract contract NonLeafTournament is Tournament { // // Constants // - MultiLevelTournamentFactory immutable tournamentFactory; + IMultiLevelTournamentFactory immutable tournamentFactory; // // Storage @@ -47,7 +46,7 @@ abstract contract NonLeafTournament is Tournament { // // Constructor // - constructor(MultiLevelTournamentFactory _tournamentFactory) { + constructor(IMultiLevelTournamentFactory _tournamentFactory) { tournamentFactory = _tournamentFactory; } diff --git a/prt/contracts/src/tournament/abstracts/NonRootTournament.sol b/prt/contracts/src/tournament/abstracts/NonRootTournament.sol index bdc6dcfe..03e7cdff 100644 --- a/prt/contracts/src/tournament/abstracts/NonRootTournament.sol +++ b/prt/contracts/src/tournament/abstracts/NonRootTournament.sol @@ -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; @@ -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; diff --git a/prt/contracts/src/tournament/concretes/BottomTournament.sol b/prt/contracts/src/tournament/concretes/BottomTournament.sol index 098e23c9..01eea93f 100644 --- a/prt/contracts/src/tournament/concretes/BottomTournament.sol +++ b/prt/contracts/src/tournament/concretes/BottomTournament.sol @@ -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( @@ -28,8 +27,7 @@ contract BottomTournament is LeafTournament, NonRootTournament { _contestedFinalStateTwo, _allowance, _startCycle, - _level, - _parent + _level ) {} } diff --git a/prt/contracts/src/tournament/concretes/MiddleTournament.sol b/prt/contracts/src/tournament/concretes/MiddleTournament.sol index 3108856a..87aac502 100644 --- a/prt/contracts/src/tournament/concretes/MiddleTournament.sol +++ b/prt/contracts/src/tournament/concretes/MiddleTournament.sol @@ -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 { @@ -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( @@ -31,8 +30,7 @@ contract MiddleTournament is NonLeafTournament, NonRootTournament { _contestedFinalStateTwo, _allowance, _startCycle, - _level, - _parent + _level ) {} } diff --git a/prt/contracts/src/tournament/concretes/TopTournament.sol b/prt/contracts/src/tournament/concretes/TopTournament.sol index 14ab0c85..5851cd96 100644 --- a/prt/contracts/src/tournament/concretes/TopTournament.sol +++ b/prt/contracts/src/tournament/concretes/TopTournament.sol @@ -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) {} } diff --git a/prt/contracts/src/tournament/factories/MultiLevelTournamentFactory.sol b/prt/contracts/src/tournament/factories/MultiLevelTournamentFactory.sol index f6ff5722..8a357d16 100644 --- a/prt/contracts/src/tournament/factories/MultiLevelTournamentFactory.sol +++ b/prt/contracts/src/tournament/factories/MultiLevelTournamentFactory.sol @@ -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; @@ -64,8 +60,7 @@ contract MultiLevelTournamentFactory is ITournamentFactory { _contestedFinalStateTwo, _allowance, _startCycle, - _level, - NonLeafTournament(msg.sender) + _level ); return _tournament; @@ -89,8 +84,7 @@ contract MultiLevelTournamentFactory is ITournamentFactory { _contestedFinalStateTwo, _allowance, _startCycle, - _level, - NonLeafTournament(msg.sender) + _level ); return _tournament; diff --git a/prt/contracts/src/tournament/factories/multilevel/BottomTournamentFactory.sol b/prt/contracts/src/tournament/factories/multilevel/BottomTournamentFactory.sol index 2aaad4b3..868ebf1a 100644 --- a/prt/contracts/src/tournament/factories/multilevel/BottomTournamentFactory.sol +++ b/prt/contracts/src/tournament/factories/multilevel/BottomTournamentFactory.sol @@ -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, @@ -27,8 +26,7 @@ contract BottomTournamentFactory { _contestedFinalStateTwo, _allowance, _startCycle, - _level, - _parent + _level ); return _tournament; diff --git a/prt/contracts/src/tournament/factories/multilevel/MiddleTournamentFactory.sol b/prt/contracts/src/tournament/factories/multilevel/MiddleTournamentFactory.sol index f15f4971..3e0425ae 100644 --- a/prt/contracts/src/tournament/factories/multilevel/MiddleTournamentFactory.sol +++ b/prt/contracts/src/tournament/factories/multilevel/MiddleTournamentFactory.sol @@ -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"; @@ -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, @@ -35,8 +34,7 @@ contract MiddleTournamentFactory { _allowance, _startCycle, _level, - _parent, - MultiLevelTournamentFactory(msg.sender) + IMultiLevelTournamentFactory(msg.sender) ); return _tournament; diff --git a/prt/contracts/src/tournament/factories/multilevel/TopTournamentFactory.sol b/prt/contracts/src/tournament/factories/multilevel/TopTournamentFactory.sol index 1fc01372..7da89000 100644 --- a/prt/contracts/src/tournament/factories/multilevel/TopTournamentFactory.sol +++ b/prt/contracts/src/tournament/factories/multilevel/TopTournamentFactory.sol @@ -13,7 +13,7 @@ contract TopTournamentFactory { returns (TopTournament) { TopTournament _tournament = new TopTournament( - _initialHash, MultiLevelTournamentFactory(msg.sender) + _initialHash, IMultiLevelTournamentFactory(msg.sender) ); return _tournament; diff --git a/prt/contracts/test/Util.sol b/prt/contracts/test/Util.sol index a91bfd31..baea76a0 100644 --- a/prt/contracts/test/Util.sol +++ b/prt/contracts/test/Util.sol @@ -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";