From 1450cd1e0dc45f6f66eda984acaa1071cd5f4728 Mon Sep 17 00:00:00 2001 From: Mitch Date: Sun, 29 Sep 2024 16:46:47 -0400 Subject: [PATCH] compensate for balance inquiries beyond the delay window --- .../src/core/ProofCommitmentEscrow.sol | 11 +++++++++- l1-contracts/src/core/Rollup.sol | 2 +- l1-contracts/test/Rollup.t.sol | 3 +-- .../test/prover-coordination/EscrowERC20.sol | 3 +-- .../ProofCommitmentEscrow.t.sol | 20 ++++++++++++++++--- .../test/prover-coordination/Signatures.t.sol | 2 +- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/l1-contracts/src/core/ProofCommitmentEscrow.sol b/l1-contracts/src/core/ProofCommitmentEscrow.sol index 9bb9132ec45e..147cd2c69dba 100644 --- a/l1-contracts/src/core/ProofCommitmentEscrow.sol +++ b/l1-contracts/src/core/ProofCommitmentEscrow.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. +// Copyright 2024 Aztec Labs. pragma solidity >=0.8.27; import {SafeERC20} from "@oz/token/ERC20/utils/SafeERC20.sol"; @@ -124,6 +124,8 @@ contract ProofCommitmentEscrow is IProofCommitmentEscrow { /** * @notice Get the minimum balance of a prover at a given timestamp. * + * @dev Returns 0 if the timestamp is beyond the WITHDRAW_DELAY from the current block timestamp + * * @param _timestamp The timestamp at which to check the balance * @param _prover The address of the prover * @@ -135,6 +137,13 @@ contract ProofCommitmentEscrow is IProofCommitmentEscrow { override returns (uint256) { + // If the timestamp is beyond the WITHDRAW_DELAY, the minimum possible balance is 0; + // the prover could issue a withdraw request in this block for the full amount, + // and execute it exactly WITHDRAW_DELAY later. + if (_timestamp >= block.timestamp + WITHDRAW_DELAY) { + return 0; + } + uint256 balance = deposits[_prover]; if (withdrawRequests[_prover].executableAt <= _timestamp) { balance -= withdrawRequests[_prover].amount; diff --git a/l1-contracts/src/core/Rollup.sol b/l1-contracts/src/core/Rollup.sol index 930a022dcd80..b90983c872c1 100644 --- a/l1-contracts/src/core/Rollup.sol +++ b/l1-contracts/src/core/Rollup.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. +// Copyright 2024 Aztec Labs. pragma solidity >=0.8.27; import {IProofCommitmentEscrow} from "@aztec/core/interfaces/IProofCommitmentEscrow.sol"; diff --git a/l1-contracts/test/Rollup.t.sol b/l1-contracts/test/Rollup.t.sol index 9ecc2e07e452..a58403e5abce 100644 --- a/l1-contracts/test/Rollup.t.sol +++ b/l1-contracts/test/Rollup.t.sol @@ -1,9 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. +// Copyright 2024 Aztec Labs. pragma solidity >=0.8.27; import {DecoderBase} from "./decoders/Base.sol"; -import {console} from "forge-std/console.sol"; import {DataStructures} from "@aztec/core/libraries/DataStructures.sol"; import {Constants} from "@aztec/core/libraries/ConstantsGen.sol"; diff --git a/l1-contracts/test/prover-coordination/EscrowERC20.sol b/l1-contracts/test/prover-coordination/EscrowERC20.sol index 22085e612d89..48b743e825c0 100644 --- a/l1-contracts/test/prover-coordination/EscrowERC20.sol +++ b/l1-contracts/test/prover-coordination/EscrowERC20.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// docs:start:contract +// Copyright 2024 Aztec Labs. pragma solidity ^0.8.27; import {ERC20} from "@oz/token/ERC20/ERC20.sol"; @@ -13,4 +13,3 @@ contract EscrowERC20 is ERC20 { _mint(to, amount); } } -// docs:end:contract diff --git a/l1-contracts/test/prover-coordination/ProofCommitmentEscrow.t.sol b/l1-contracts/test/prover-coordination/ProofCommitmentEscrow.t.sol index 2fa835783929..aacc1f6ce4dc 100644 --- a/l1-contracts/test/prover-coordination/ProofCommitmentEscrow.t.sol +++ b/l1-contracts/test/prover-coordination/ProofCommitmentEscrow.t.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. +// Copyright 2024 Aztec Labs. pragma solidity >=0.8.27; import {Test} from "forge-std/Test.sol"; @@ -271,7 +271,7 @@ contract TestProofCommitmentEscrow is Test { ); assertEq( - _escrow.minBalanceAtTime(withdrawReadyAt, prover), + _escrow.minBalanceAtTime(withdrawReadyAt - 1, prover), depositAmount, "Min balance should match deposit amount before withdraw request matures" ); @@ -285,10 +285,24 @@ contract TestProofCommitmentEscrow is Test { "Min balance should be unaffected by pending withdraw request before maturity" ); + assertEq( + _escrow.minBalanceAtTime(block.timestamp + _escrow.WITHDRAW_DELAY(), prover), + 0, + "Min balance should be 0 at or beyond the delay window" + ); + + vm.warp(block.timestamp + 1); + assertEq( _escrow.minBalanceAtTime(withdrawReadyAt, prover), depositAmount - withdrawAmount, - "Min balance should reflect pending withdraw request after maturity" + "Min balance should be 75 after withdraw request matures" + ); + + assertEq( + _escrow.minBalanceAtTime(withdrawReadyAt + 1, prover), + 0, + "Min balance should be 0 at or beyond the delay window" ); } diff --git a/l1-contracts/test/prover-coordination/Signatures.t.sol b/l1-contracts/test/prover-coordination/Signatures.t.sol index 9c946e5b9f5f..1b3fd1fbbb68 100644 --- a/l1-contracts/test/prover-coordination/Signatures.t.sol +++ b/l1-contracts/test/prover-coordination/Signatures.t.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -// Copyright 2023 Aztec Labs. +// Copyright 2024 Aztec Labs. pragma solidity >=0.8.27; import {Test} from "forge-std/Test.sol";