Skip to content

Commit

Permalink
Merge pull request #171 from NethermindEth/ringbuffer
Browse files Browse the repository at this point in the history
Alt: Reuse storage slots with ring-buffers
  • Loading branch information
AnshuJalan authored Oct 10, 2024
2 parents e5e198f + 2aa0d38 commit 2cd00dd
Show file tree
Hide file tree
Showing 46 changed files with 235 additions and 139 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright 2024 Nethermind

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion SmartContracts/scripts/BaseScript.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Script} from "forge-std/Script.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/scripts/deployment/DeployAVS.s.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseScript} from "../BaseScript.sol";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseScript} from "../BaseScript.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/scripts/deployment/mock/DeployMockAVS.s.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseScript} from "../../BaseScript.sol";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseScript} from "../../BaseScript.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/scripts/misc/EmptyContract.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

/// @dev This is solely used as a placeholder during empty proxy deployment
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/avs/PreconfConstants.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

library PreconfConstants {
Expand Down
4 changes: 2 additions & 2 deletions SmartContracts/src/avs/PreconfRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BLS12381} from "../libraries/BLS12381.sol";
Expand Down Expand Up @@ -28,7 +28,7 @@ contract PreconfRegistry is IPreconfRegistry, BLSSignatureChecker, Initializable
// Maps a validator's BLS pub key hash to the validator's details
mapping(bytes32 publicKeyHash => Validator) internal validators;

uint256[196] private __gap; // = 200 - 4
uint256[46] private __gap; // = 50 - 4

constructor(IPreconfServiceManager _preconfServiceManager) {
preconfServiceManager = _preconfServiceManager;
Expand Down
4 changes: 2 additions & 2 deletions SmartContracts/src/avs/PreconfServiceManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IPreconfServiceManager} from "../interfaces/IPreconfServiceManager.sol";
Expand All @@ -20,7 +20,7 @@ contract PreconfServiceManager is IPreconfServiceManager, ReentrancyGuard {
/// @dev This is currently just a flag and not actually being used to lock the stake.
mapping(address operator => uint256 timestamp) public stakeLockedUntil;

uint256[199] private __gap; // 200 - 1
uint256[49] private __gap; // 50 - 1

constructor(address _preconfRegistry, address _preconfTaskManager, IAVSDirectory _avsDirectory, ISlasher _slasher) {
preconfRegistry = _preconfRegistry;
Expand Down
194 changes: 122 additions & 72 deletions SmartContracts/src/avs/PreconfTaskManager.sol

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SmartContracts/src/avs/utils/BLSSignatureChecker.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BLS12381} from "../../libraries/BLS12381.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/eigenlayer-mvp/AVSDirectory.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IAVSDirectory} from "../interfaces/eigenlayer-mvp/IAVSDirectory.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/eigenlayer-mvp/DelegationManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IDelegationManager} from "../interfaces/eigenlayer-mvp/IDelegationManager.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/eigenlayer-mvp/Slasher.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {ISlasher} from "../interfaces/eigenlayer-mvp/ISlasher.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/eigenlayer-mvp/StrategyManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IStrategyManager} from "../interfaces/eigenlayer-mvp/IStrategyManager.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/interfaces/IPreconfRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BLS12381} from "../libraries/BLS12381.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/interfaces/IPreconfServiceManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IAVSDirectory} from "./eigenlayer-mvp/IAVSDirectory.sol";
Expand Down
22 changes: 20 additions & 2 deletions SmartContracts/src/interfaces/IPreconfTaskManager.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {EIP4788} from "../libraries/EIP4788.sol";
Expand Down Expand Up @@ -43,6 +43,20 @@ interface IPreconfTaskManager {
address fallbackPreconfer;
}

struct PosterInfo {
// Address of lookahead poster
address poster;
// Start timestamp of the epoch for which the lookahead was posted
uint64 epochTimestamp;
}

struct ProposerInfo {
// Address of the Taiko block proposer
address proposer;
// Height of the L2 block
uint64 blockId;
}

event LookaheadUpdated(LookaheadSetParam[]);
event ProvedIncorrectPreconfirmation(address indexed preconfer, uint256 indexed blockId, address indexed disputer);
event ProvedIncorrectLookahead(address indexed poster, uint256 indexed timestamp, address indexed disputer);
Expand All @@ -53,6 +67,8 @@ interface IPreconfTaskManager {
error SenderIsNotThePreconfer();
/// @dev Preconfer is not present in the registry
error PreconferNotRegistered();
/// @dev Epoch timestamp is incorrect
error InvalidEpochTimestamp();
/// @dev The timestamp in the lookahead is not of a valid future slot in the present epoch
error InvalidSlotTimestamp();
/// @dev The chain id on which the preconfirmation was signed is different from the current chain's id
Expand All @@ -69,6 +85,8 @@ interface IPreconfTaskManager {
error LookaheadEntryIsCorrect();
/// @dev Cannot force push a lookahead since it is not lagging behind
error LookaheadIsNotRequired();
/// @dev The registry does not have a single registered preconfer
error NoRegisteredPreconfer();

/// @dev Accepts block proposal by an operator and forwards it to TaikoL1 contract
function newBlockProposal(
Expand Down Expand Up @@ -116,7 +134,7 @@ interface IPreconfTaskManager {
function getLookaheadTail() external view returns (uint256);

/// @dev Returns the entire lookahead buffer
function getLookaheadBuffer() external view returns (LookaheadBufferEntry[64] memory);
function getLookaheadBuffer() external view returns (LookaheadBufferEntry[128] memory);

/// @dev Returns the lookahead poster for an epoch
function getLookaheadPoster(uint256 epochTimestamp) external view returns (address);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface IAVSDirectory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface IDelegationManager {
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/interfaces/eigenlayer-mvp/ISlasher.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface ISlasher {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface IStrategyManager {
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/interfaces/taiko/ITaikoL1.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface ITaikoL1 {
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/libraries/BLS12381.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
// Functions in this library have been adapted from:
// https://github.com/ethyla/bls12-381-hash-to-curve/blob/main/src/HashToCurve.sol
pragma solidity 0.8.25;
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/libraries/EIP4788.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT

// Referenced from: https://ethresear.ch/t/slashing-proofoor-on-chain-slashed-validator-proofs/19421
pragma solidity 0.8.25;
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/libraries/MerkleUtils.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT

pragma solidity 0.8.25;

Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/src/mock/MockPreconfRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BLS12381} from "../libraries/BLS12381.sol";
Expand Down
5 changes: 2 additions & 3 deletions SmartContracts/src/mock/MockTaikoToken.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

contract MockTaikoToken {

address public lastAddr;
uint256 public lastAmount;

Expand All @@ -11,4 +10,4 @@ contract MockTaikoToken {
lastAmount = amount;
return true;
}
}
}
2 changes: 1 addition & 1 deletion SmartContracts/test/BaseTest.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Test} from "forge-std/Test.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/beacon/BeaconProofsVerification.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseTest} from "../BaseTest.sol";
Expand Down
4 changes: 2 additions & 2 deletions SmartContracts/test/blocks/BlockProposing.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BlocksFixtures} from "../fixtures/BlocksFixtures.sol";
Expand Down Expand Up @@ -166,7 +166,7 @@ contract BlockProposing is BlocksFixtures {
preconfTaskManager.newBlockProposal("Block Params", "Txn List", 1, lookaheadSetParams);

// Verify that the lookahead for the next epoch has been updated
IPreconfTaskManager.LookaheadBufferEntry[64] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();
IPreconfTaskManager.LookaheadBufferEntry[128] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();

// Check the first entry
vm.assertEq(lookaheadBuffer[3].preconfer, addr_1);
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/blocks/IncorrectPreconfirmations.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BlocksFixtures} from "../fixtures/BlocksFixtures.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/bls/BLSExpandMsgXmd.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
// Test has been referenced from https://github.com/ethyla/bls12-381-hash-to-curve/blob/main/test/expandMsgXmd.sol
pragma solidity 0.8.25;

Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/bls/BLSHashToFieldFp2.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
// Test has been referenced from https://github.com/ethyla/bls12-381-hash-to-curve/blob/main/test/hashToField.sol
pragma solidity 0.8.25;

Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/bls/script-test/BLSHashToCurveG2.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
// solhint-disable-next-line
pragma solidity 0.8.25;

Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/bls/script-test/BLSVerifySignature.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
// solhint-disable-next-line
pragma solidity 0.8.25;

Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/fixtures/BeaconProofs.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {EIP4788} from "src/libraries/EIP4788.sol";
Expand Down
2 changes: 1 addition & 1 deletion SmartContracts/test/fixtures/BlocksFixtures.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseTest} from "../BaseTest.sol";
Expand Down
6 changes: 5 additions & 1 deletion SmartContracts/test/fixtures/LookaheadFixtures.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BaseTest} from "../BaseTest.sol";
Expand Down Expand Up @@ -40,4 +40,8 @@ contract LookaheadFixtures is BaseTest {
preconfRegistry.registerPreconfer(vm.addr(i));
}
}

function computeFallbackPreconfer(bytes32 randomness, uint256 nextPreconferIndex) internal view returns (address) {
return vm.addr(uint256(randomness) % (nextPreconferIndex - 1) + 1);
}
}
22 changes: 15 additions & 7 deletions SmartContracts/test/lookahead/IncorrectLookahead.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {BeaconProofs} from "../fixtures/BeaconProofs.sol";
Expand Down Expand Up @@ -263,9 +263,11 @@ contract IncorrectLookahead is LookaheadFixtures {
// This beacon proposer is not added as a validator for our preconfer in lookahead
bytes memory beaconProposer = BeaconProofs.validator();

bytes32 randomness = bytes32(uint256(4));

// Set beacon block root such that addr_4 is randomly selected
beaconBlockRootContract.set(
PreconfConstants.MAINNET_BEACON_GENESIS + PreconfConstants.SECONDS_IN_SLOT, bytes32(uint256(4))
PreconfConstants.MAINNET_BEACON_GENESIS + PreconfConstants.SECONDS_IN_SLOT, randomness
);

// Prove the lookahead to be incorrect
Expand All @@ -277,8 +279,10 @@ contract IncorrectLookahead is LookaheadFixtures {
nextEpochStart + PreconfConstants.SECONDS_IN_EPOCH - PreconfConstants.SECONDS_IN_SLOT;

// Verify that the lookahead has the fallback preconfer
IPreconfTaskManager.LookaheadBufferEntry[64] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();
vm.assertEq(lookaheadBuffer[3].preconfer, addr_4);
IPreconfTaskManager.LookaheadBufferEntry[128] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();
vm.assertEq(
lookaheadBuffer[3].preconfer, computeFallbackPreconfer(randomness, preconfRegistry.getNextPreconferIndex())
);
vm.assertEq(lookaheadBuffer[3].timestamp, lastSlotTimestamp);
vm.assertEq(lookaheadBuffer[3].prevTimestamp, nextEpochStart - PreconfConstants.SECONDS_IN_SLOT);
vm.assertEq(lookaheadBuffer[3].isFallback, true);
Expand Down Expand Up @@ -329,9 +333,11 @@ contract IncorrectLookahead is LookaheadFixtures {
// This beacon proposer is not added as a validator for our preconfer in lookahead
bytes memory beaconProposer = BeaconProofs.validator();

bytes32 randomness = bytes32(uint256(4));

// Set beacon block root such that addr_4 is randomly selected
beaconBlockRootContract.set(
PreconfConstants.MAINNET_BEACON_GENESIS + PreconfConstants.SECONDS_IN_SLOT, bytes32(uint256(4))
PreconfConstants.MAINNET_BEACON_GENESIS + PreconfConstants.SECONDS_IN_SLOT, randomness
);

// Prove the lookahead to be incorrect
Expand All @@ -343,8 +349,10 @@ contract IncorrectLookahead is LookaheadFixtures {
nextEpochStart + PreconfConstants.SECONDS_IN_EPOCH - PreconfConstants.SECONDS_IN_SLOT;

// Verify that the lookahead has the fallback preconfer
IPreconfTaskManager.LookaheadBufferEntry[64] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();
vm.assertEq(lookaheadBuffer[3].preconfer, addr_4);
IPreconfTaskManager.LookaheadBufferEntry[128] memory lookaheadBuffer = preconfTaskManager.getLookaheadBuffer();
vm.assertEq(
lookaheadBuffer[3].preconfer, computeFallbackPreconfer(randomness, preconfRegistry.getNextPreconferIndex())
);
vm.assertEq(lookaheadBuffer[3].timestamp, lastSlotTimestamp);
vm.assertEq(lookaheadBuffer[3].prevTimestamp, nextEpochStart - PreconfConstants.SECONDS_IN_SLOT);
vm.assertEq(lookaheadBuffer[3].isFallback, true);
Expand Down
Loading

0 comments on commit 2cd00dd

Please sign in to comment.