From db124ddbd975e5c23557eaf482cbc59bccbc37a2 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 10 Jan 2024 15:12:06 +0100 Subject: [PATCH] Re-arrange marketplace constructor parameters first configuration, then contracts that we depend on --- contracts/FuzzMarketplace.sol | 5 +++-- contracts/Marketplace.sol | 7 ++++--- contracts/Proofs.sol | 9 +++------ contracts/TestMarketplace.sol | 6 +++--- contracts/TestProofs.sol | 6 ++++-- contracts/TestVerifier.sol | 15 +++++++++++++++ contracts/Verifier.sol | 11 +++++++++++ test/Marketplace.test.js | 23 +++++++++++++++++------ 8 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 contracts/TestVerifier.sol create mode 100644 contracts/Verifier.sol diff --git a/contracts/FuzzMarketplace.sol b/contracts/FuzzMarketplace.sol index 58cfa21..71673a2 100644 --- a/contracts/FuzzMarketplace.sol +++ b/contracts/FuzzMarketplace.sol @@ -3,13 +3,14 @@ pragma solidity ^0.8.0; import "./TestToken.sol"; import "./Marketplace.sol"; +import "./TestVerifier.sol"; contract FuzzMarketplace is Marketplace { constructor() Marketplace( - new TestToken(), MarketplaceConfig(CollateralConfig(10, 5, 3, 10), ProofConfig(10, 5, 64)), - address(0) + new TestToken(), + new TestVerifier() ) // solhint-disable-next-line no-empty-blocks { diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 4704c2d..48d5843 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -8,6 +8,7 @@ import "./Configuration.sol"; import "./Requests.sol"; import "./Proofs.sol"; import "./StateRetrieval.sol"; +import "./Verifier.sol"; contract Marketplace is Proofs, StateRetrieval { using EnumerableSet for EnumerableSet.Bytes32Set; @@ -55,10 +56,10 @@ contract Marketplace is Proofs, StateRetrieval { } constructor( - IERC20 token_, MarketplaceConfig memory configuration, - address verifierAddress - ) Proofs(configuration.proofs, verifierAddress) { + IERC20 token_, + IVerifier verifier + ) Proofs(configuration.proofs, verifier) { token = token_; require( diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index f4cc0b9..94287d3 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -4,19 +4,16 @@ pragma solidity ^0.8.8; import "./Configuration.sol"; import "./Requests.sol"; import "./Periods.sol"; - -interface IVerifier { - function verifyProof(uint[2] calldata _pA, uint[2][2] calldata _pB, uint[2] calldata _pC, uint[3] calldata _pubSignals) external view returns (bool); -} +import "./Verifier.sol"; abstract contract Proofs is Periods { ProofConfig private _config; IVerifier private _verifier; - constructor(ProofConfig memory config, address verifierAddress) Periods(config.period) { + constructor(ProofConfig memory config, IVerifier verifier) Periods(config.period) { require(block.number > 256, "Insufficient block height"); _config = config; - _verifier = IVerifier(verifierAddress); + _verifier = verifier; } mapping(SlotId => uint256) private _slotStarts; diff --git a/contracts/TestMarketplace.sol b/contracts/TestMarketplace.sol index 4924a77..13d79b4 100644 --- a/contracts/TestMarketplace.sol +++ b/contracts/TestMarketplace.sol @@ -6,11 +6,11 @@ import "./Marketplace.sol"; // exposes internal functions of Marketplace for testing contract TestMarketplace is Marketplace { constructor( - IERC20 token, MarketplaceConfig memory config, - address verifierAddress + IERC20 token, + IVerifier verifier ) - Marketplace(token, config, verifierAddress) // solhint-disable-next-line no-empty-blocks + Marketplace(config, token, verifier) // solhint-disable-next-line no-empty-blocks {} function forciblyFreeSlot(SlotId slotId) public { diff --git a/contracts/TestProofs.sol b/contracts/TestProofs.sol index 4045631..b0b4500 100644 --- a/contracts/TestProofs.sol +++ b/contracts/TestProofs.sol @@ -7,8 +7,10 @@ import "./Proofs.sol"; contract TestProofs is Proofs { mapping(SlotId => SlotState) private _states; - // solhint-disable-next-line no-empty-blocks - constructor(ProofConfig memory config, address verifierAddress) Proofs(config, verifierAddress) {} + constructor( + ProofConfig memory config, + IVerifier verifier + ) Proofs(config, verifier) {} // solhint-disable-line no-empty-blocks function slotState(SlotId slotId) public view override returns (SlotState) { return _states[slotId]; diff --git a/contracts/TestVerifier.sol b/contracts/TestVerifier.sol new file mode 100644 index 0000000..b3d3b5f --- /dev/null +++ b/contracts/TestVerifier.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.8; + +import "./Verifier.sol"; + +contract TestVerifier is IVerifier { + function verifyProof( + uint[2] calldata, + uint[2][2] calldata, + uint[2] calldata, + uint[3] calldata + ) external pure returns (bool) { + return false; + } +} diff --git a/contracts/Verifier.sol b/contracts/Verifier.sol new file mode 100644 index 0000000..4aba809 --- /dev/null +++ b/contracts/Verifier.sol @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.8; + +interface IVerifier { + function verifyProof( + uint[2] calldata _pA, + uint[2][2] calldata _pB, + uint[2] calldata _pC, + uint[3] calldata _pubSignals + ) external view returns (bool); +} diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index a544f83..7242ebb 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -32,7 +32,7 @@ const { const ACCOUNT_STARTING_BALANCE = 1_000_000_000 describe("Marketplace constructor", function () { - let Marketplace, token, config + let Marketplace, token, verifier, config beforeEach(async function () { await snapshot() @@ -41,6 +41,9 @@ describe("Marketplace constructor", function () { const TestToken = await ethers.getContractFactory("TestToken") token = await TestToken.deploy() + const TestVerifier = await ethers.getContractFactory("TestVerifier") + verifier = await TestVerifier.deploy() + Marketplace = await ethers.getContractFactory("TestMarketplace") config = exampleConfiguration() }) @@ -54,7 +57,7 @@ describe("Marketplace constructor", function () { config.collateral[property] = 101 await expect( - Marketplace.deploy(token.address, config) + Marketplace.deploy(config, token.address, verifier.address) ).to.be.revertedWith("Must be less than 100") }) } @@ -66,9 +69,9 @@ describe("Marketplace constructor", function () { config.collateral.slashPercentage = 1 config.collateral.maxNumberOfSlashes = 101 - await expect(Marketplace.deploy(token.address, config)).to.be.revertedWith( - "Maximum slashing exceeds 100%" - ) + await expect( + Marketplace.deploy(config, token.address, verifier.address) + ).to.be.revertedWith("Maximum slashing exceeds 100%") }) }) @@ -78,6 +81,7 @@ describe("Marketplace", function () { let marketplace let token + let verifier let client, host, host1, host2, host3 let request let slot @@ -96,8 +100,15 @@ describe("Marketplace", function () { await token.mint(account.address, ACCOUNT_STARTING_BALANCE) } + const TestVerifier = await ethers.getContractFactory("TestVerifier") + verifier = await TestVerifier.deploy() + const Marketplace = await ethers.getContractFactory("TestMarketplace") - marketplace = await Marketplace.deploy(token.address, config) + marketplace = await Marketplace.deploy( + config, + token.address, + verifier.address + ) request = await exampleRequest() request.client = client.address