Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(protocol): fix getLastSyncedBlock by writing the block's verifiedTransitionId #17624

Merged
merged 16 commits into from
Jun 20, 2024
Merged
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/ITaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ interface ITaikoL1 {

/// @notice Gets the configuration of the TaikoL1 contract.
/// @return Config struct containing configuration parameters.
function getConfig() external view returns (TaikoData.Config memory);
function getConfig() external pure returns (TaikoData.Config memory);
}
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/TaikoL1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ contract TaikoL1 is EssentialContract, ITaikoL1, TaikoEvents, TaikoErrors {
}

/// @inheritdoc ITaikoL1
function getConfig() public view virtual override returns (TaikoData.Config memory) {
function getConfig() public pure virtual override returns (TaikoData.Config memory) {
// All hard-coded configurations:
// - treasury: the actual TaikoL2 address.
// - anchorGasLimit: 250_000 (based on internal devnet, its ~220_000
Expand Down
4 changes: 1 addition & 3 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ library LibProving {

local.blockId = blk.blockId;

if (LibUtils.shouldSyncStateRoot(_config.stateRootSyncInternal, local.blockId)) {
if (LibUtils.willSyncStateRoot(_config.stateRootSyncInternal, local.blockId)) {
local.stateRoot = _tran.stateRoot;
}

Expand Down Expand Up @@ -387,8 +387,6 @@ library LibProving {
uint256 reward; // reward to the new (current) prover

if (_ts.contester != address(0)) {
// assert(_blk.livenessBond == 0);

if (_local.sameTransition) {
// The contested transition is proven to be valid, contester loses the game
reward = _rewardAfterFriction(_ts.contestBond);
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/libs/LibUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ library LibUtils {
return _blockId % segmentSize == (_isBlockProposed ? 0 : segmentSize >> 1);
}

function shouldSyncStateRoot(
function willSyncStateRoot(
uint256 _stateRootSyncInternal,
uint256 _blockId
)
Expand Down
24 changes: 18 additions & 6 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ library LibVerifying {
uint32 lastVerifiedTransitionId;
uint16 tier;
bytes32 blockHash;
bytes32 stateRoot;
bytes32 syncStateRoot;
uint64 syncBlockId;
uint32 syncTransitionId;
address prover;
ITierRouter tierRouter;
}
Expand Down Expand Up @@ -139,9 +140,13 @@ library LibVerifying {
tier: local.tier
});

if (LibUtils.shouldSyncStateRoot(_config.stateRootSyncInternal, local.blockId)) {
local.stateRoot = ts.stateRoot;
local.syncBlockId = local.blockId;
if (LibUtils.willSyncStateRoot(_config.stateRootSyncInternal, local.blockId)) {
bytes32 stateRoot = ts.stateRoot;
if (stateRoot != 0) {
local.syncStateRoot = stateRoot;
local.syncBlockId = local.blockId;
local.syncTransitionId = local.tid;
}
}

++local.blockId;
Expand All @@ -163,13 +168,20 @@ library LibVerifying {
}
if (!_tko.batchTransfer(provers, bonds)) revert L1_BATCH_TRANSFER_FAILED();

if (local.stateRoot != 0) {
if (local.syncStateRoot != 0) {
_state.slotA.lastSyncedBlockId = local.syncBlockId;
_state.slotA.lastSynecdAt = uint64(block.timestamp);

// We write the synced block's verifiedTransitionId to storage
local.slot = local.syncBlockId % _config.blockRingBufferSize;
_state.blocks[local.slot].verifiedTransitionId = local.syncTransitionId;

ISignalService(_resolver.resolve(LibStrings.B_SIGNAL_SERVICE, false))
.syncChainData(
_config.chainId, LibStrings.H_STATE_ROOT, local.syncBlockId, local.stateRoot
_config.chainId,
LibStrings.H_STATE_ROOT,
local.syncBlockId,
local.syncStateRoot
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/protocol/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,9 @@ contract Bridge is EssentialContract, IBridge {
}

/// @dev Suggested by OpenZeppelin and copied from
/// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/83c7e45092dac350b070c421cd2bf7105616cf1a/contracts/metatx/ERC2771Forwarder.sol#L327C1-L370C6
/// https://github.com/OpenZeppelin/openzeppelin-contracts/
/// blob/83c7e45092dac350b070c421cd2bf7105616cf1a/contracts/
/// metatx/ERC2771Forwarder.sol#L327C1-L370C6
///
/// @dev Checks if the requested gas was correctly forwarded to the callee.
/// As a consequence of https://eips.ethereum.org/EIPS/eip-150[EIP-150]:
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L1/TaikoL1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.24;
import "./TaikoL1TestBase.sol";

contract TaikoL1_NoCooldown is TaikoL1 {
function getConfig() public view override returns (TaikoData.Config memory config) {
function getConfig() public pure override returns (TaikoData.Config memory config) {
config = TaikoL1.getConfig();
// over-write the following
config.maxBlocksToVerify = 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/test/L1/TaikoL1LibProvingWithTiers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.24;
import "./TaikoL1TestBase.sol";

contract TaikoL1Tiers is TaikoL1 {
function getConfig() public view override returns (TaikoData.Config memory config) {
function getConfig() public pure override returns (TaikoData.Config memory config) {
config = TaikoL1.getConfig();

config.maxBlocksToVerify = 0;
Expand Down Expand Up @@ -423,7 +423,7 @@ contract TaikoL1LibProvingWithTiers is TaikoL1TestBase {

bytes32 parentHash = GENESIS_BLOCK_HASH;
for (uint256 blockId = 1; blockId < conf.blockMaxProposals * 3; blockId++) {
bool storeStateRoot = LibUtils.shouldSyncStateRoot(syncInternal, blockId);
bool storeStateRoot = LibUtils.willSyncStateRoot(syncInternal, blockId);
console2.log("blockId:", blockId);
console2.log("storeStateRoot:", storeStateRoot);

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/test/L1/TaikoL1TestGroupBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.24;
import "./TaikoL1TestBase.sol";

contract TaikoL1New is TaikoL1 {
function getConfig() public view override returns (TaikoData.Config memory config) {
function getConfig() public pure override returns (TaikoData.Config memory config) {
config = TaikoL1.getConfig();
config.maxBlocksToVerify = 0;
config.blockMaxProposals = 10;
Expand Down