Skip to content

Commit

Permalink
Deposit queue
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoog committed Dec 29, 2024
1 parent f3b9f0c commit b299bc9
Show file tree
Hide file tree
Showing 26 changed files with 235 additions and 87 deletions.
37 changes: 31 additions & 6 deletions op-node/rollup/derive/deposit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
var (
DepositEventABI = "TransactionDeposited(address,address,uint256,bytes)"
DepositEventABIHash = crypto.Keccak256Hash([]byte(DepositEventABI))
DepositEventVersion0 = common.Hash{}
DepositEventVersion0 = uint64(0)
DepositEventVersion1 = uint64(1)
)

// UnmarshalDepositLogEvent decodes an EVM log entry emitted by the deposit contract into typed deposit data.
Expand All @@ -27,7 +28,7 @@ var (
// event TransactionDeposited(
// address indexed from,
// address indexed to,
// uint256 indexed version,
// uint256 indexed nonceAndVersion,
// bytes opaqueData
// );
//
Expand All @@ -51,7 +52,7 @@ func UnmarshalDepositLogEvent(ev *types.Log) (*types.DepositTx, error) {
// indexed 1
to := common.BytesToAddress(ev.Topics[2][12:])
// indexed 2
version := ev.Topics[3]
nonceAndVersion := ev.Topics[3]
// unindexed data
// Solidity serializes the event's Data field as follows:
// abi.encode(abi.encodPacked(uint256 mint, uint256 value, uint64 gasLimit, uint8 isCreation, bytes data))
Expand Down Expand Up @@ -83,19 +84,38 @@ func UnmarshalDepositLogEvent(ev *types.Log) (*types.DepositTx, error) {
dep.From = from
dep.IsSystemTransaction = false

_, version := unpackNonceAndVersion(nonceAndVersion)

var err error
switch version {
case DepositEventVersion0:
err = unmarshalDepositVersion0(&dep, to, opaqueData)
case DepositEventVersion1:
err = unmarshalDepositVersion1(&dep, to, opaqueData)
default:
return nil, fmt.Errorf("invalid deposit version, got %s", version)
return nil, fmt.Errorf("invalid deposit version, got %d", version)
}
if err != nil {
return nil, fmt.Errorf("failed to decode deposit (version %s): %w", version, err)
return nil, fmt.Errorf("failed to decode deposit (version %d): %w", version, err)
}
return &dep, nil
}

func unpackNonceAndVersion(nonceAndVersion common.Hash) (nonce uint64, version uint64) {
i := new(big.Int).SetBytes(nonceAndVersion[:])
mask64 := new(big.Int).SetBytes(common.Hex2Bytes("ffffffffffffffff"))
version = mask64.And(mask64, i).Uint64()
nonce = i.Lsh(i, 128).Uint64()
return
}

func packNonceAndVersion(nonce uint64, version uint64) common.Hash {
i := new(big.Int).SetUint64(nonce)
i.Rsh(i, 128)
i.Or(i, new(big.Int).SetUint64(version))
return common.BytesToHash(i.Bytes())
}

func unmarshalDepositVersion0(dep *types.DepositTx, to common.Address, opaqueData []byte) error {
if len(opaqueData) < 32+32+8+1 {
return fmt.Errorf("unexpected opaqueData length: %d", len(opaqueData))
Expand Down Expand Up @@ -140,6 +160,11 @@ func unmarshalDepositVersion0(dep *types.DepositTx, to common.Address, opaqueDat
return nil
}

func unmarshalDepositVersion1(dep *types.DepositTx, to common.Address, opaqueData []byte) error {
// version 1 simply adds a nonce; the rest is the same
return unmarshalDepositVersion0(dep, to, opaqueData)
}

// MarshalDepositLogEvent returns an EVM log entry that encodes a TransactionDeposited event from the deposit contract.
// This is the reverse of the deposit transaction derivation.
func MarshalDepositLogEvent(depositContractAddr common.Address, deposit *types.DepositTx) (*types.Log, error) {
Expand All @@ -151,7 +176,7 @@ func MarshalDepositLogEvent(depositContractAddr common.Address, deposit *types.D
DepositEventABIHash,
eth.AddressAsLeftPaddedHash(deposit.From),
toBytes,
DepositEventVersion0,
packNonceAndVersion(0, DepositEventVersion0),
}

data := make([]byte, 64, 64+3*32)
Expand Down
8 changes: 4 additions & 4 deletions op-node/rollup/derive/deposit_log_tob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ func FuzzDeriveDepositsBadVersion(f *testing.F) {
// Generate any topic but the deposit event versions we support.
// TODO: As opposed to keeping this hardcoded, a method such as IsValidVersion(v) should be
// used here.
badTopic := DepositEventVersion0
for badTopic == DepositEventVersion0 {
typeProvider.Fuzz(&badTopic)
badVersion := DepositEventVersion0
for badVersion == DepositEventVersion0 {
typeProvider.Fuzz(&badVersion)
}

// Set our bad topic and update our state
log.Topics[3] = badTopic
log.Topics[3] = packNonceAndVersion(0, badVersion)
hasBadDepositVersion = true
}
}
Expand Down
15 changes: 10 additions & 5 deletions op-node/rollup/derive/system_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var (
var (
ConfigUpdateEventABI = "ConfigUpdate(uint256,uint8,bytes)"
ConfigUpdateEventABIHash = crypto.Keccak256Hash([]byte(ConfigUpdateEventABI))
ConfigUpdateEventVersion0 = common.Hash{}
ConfigUpdateEventVersion0 = uint64(0)
ConfigUpdateEventVersion1 = uint64(1)
)

// UpdateSystemConfigWithL1Receipts filters all L1 receipts to find config updates and applies the config updates to the given sysCfg
Expand All @@ -53,7 +54,7 @@ func UpdateSystemConfigWithL1Receipts(sysCfg *eth.SystemConfig, receipts []*type
// parse log data for:
//
// event ConfigUpdate(
// uint256 indexed version,
// uint256 indexed nonceAndVersion,
// UpdateType indexed updateType,
// bytes data
// );
Expand All @@ -66,10 +67,14 @@ func ProcessSystemConfigUpdateLogEvent(destSysCfg *eth.SystemConfig, ev *types.L
}

// indexed 0
version := ev.Topics[1]
if version != ConfigUpdateEventVersion0 {
return fmt.Errorf("unrecognized SystemConfig update event version: %s", version)
_, version := unpackNonceAndVersion(ev.Topics[1])
switch version {
case ConfigUpdateEventVersion0:
case ConfigUpdateEventVersion1:
default:
return fmt.Errorf("unrecognized SystemConfig update event version: %d", version)
}

// indexed 1
updateType := ev.Topics[2]

Expand Down
12 changes: 6 additions & 6 deletions op-node/rollup/derive/system_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateUnsafeBlockSigner,
},
},
Expand All @@ -77,7 +77,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateBatcher,
},
},
Expand All @@ -101,7 +101,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateFeeScalars,
},
},
Expand All @@ -127,7 +127,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateGasLimit,
},
},
Expand All @@ -151,7 +151,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateFeeScalars,
},
},
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestProcessSystemConfigUpdateLogEvent(t *testing.T) {
log: &types.Log{
Topics: []common.Hash{
ConfigUpdateEventABIHash,
ConfigUpdateEventVersion0,
packNonceAndVersion(0, ConfigUpdateEventVersion0),
SystemConfigUpdateEIP1559Params,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface IOptimismPortal2 {
event DisputeGameBlacklisted(IDisputeGame indexed disputeGame);
event Initialized(uint8 version);
event RespectedGameTypeSet(GameType indexed newGameType, Timestamp indexed updatedAt);
event TransactionDeposited(address indexed from, address indexed to, uint256 indexed version, bytes opaqueData);
event TransactionDeposited(address indexed from, address indexed to, uint256 indexed nonceAndVersion, bytes opaqueData);
event WithdrawalFinalized(bytes32 indexed withdrawalHash, bool success);
event WithdrawalProven(bytes32 indexed withdrawalHash, address indexed from, address indexed to);
event WithdrawalProvenExtension1(bytes32 indexed withdrawalHash, address indexed proofSubmitter);
Expand Down Expand Up @@ -92,6 +92,7 @@ interface IOptimismPortal2 {
function paused() external view returns (bool);
function proofMaturityDelaySeconds() external view returns (uint256);
function proofSubmitters(bytes32, uint256) external view returns (address);
function transactionDepositedNonce() external view returns (uint256);
function proveWithdrawalTransaction(
Types.WithdrawalTransaction memory _tx,
uint256 _disputeGameIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface IOptimismPortalInterop {
event DisputeGameBlacklisted(IDisputeGame indexed disputeGame);
event Initialized(uint8 version);
event RespectedGameTypeSet(GameType indexed newGameType, Timestamp indexed updatedAt);
event TransactionDeposited(address indexed from, address indexed to, uint256 indexed version, bytes opaqueData);
event TransactionDeposited(address indexed from, address indexed to, uint256 indexed nonceAndVersion, bytes opaqueData);
event WithdrawalFinalized(bytes32 indexed withdrawalHash, bool success);
event WithdrawalProven(bytes32 indexed withdrawalHash, address indexed from, address indexed to);
event WithdrawalProvenExtension1(bytes32 indexed withdrawalHash, address indexed proofSubmitter);
Expand Down Expand Up @@ -93,6 +93,7 @@ interface IOptimismPortalInterop {
function paused() external view returns (bool);
function proofMaturityDelaySeconds() external view returns (uint256);
function proofSubmitters(bytes32, uint256) external view returns (address);
function transactionDepositedNonce() external view returns (uint256);
function proveWithdrawalTransaction(
Types.WithdrawalTransaction memory _tx,
uint256 _disputeGameIndex,
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts-bedrock/interfaces/L1/ISystemConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface ISystemConfig {
address gasPayingToken;
}

event ConfigUpdate(uint256 indexed version, UpdateType indexed updateType, bytes data);
event ConfigUpdate(uint256 indexed nonceAndVersion, UpdateType indexed updateType, bytes data);
event Initialized(uint8 version);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

Expand All @@ -45,6 +45,7 @@ interface ISystemConfig {
function gasLimit() external view returns (uint64);
function eip1559Denominator() external view returns (uint32);
function eip1559Elasticity() external view returns (uint32);
function configUpdateNonce() external view returns (uint256);
function gasPayingToken() external view returns (address addr_, uint8 decimals_);
function gasPayingTokenName() external view returns (string memory name_);
function gasPayingTokenSymbol() external view returns (string memory symbol_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ISystemConfig } from "interfaces/L1/ISystemConfig.sol";
import { IResourceMetering } from "interfaces/L1/IResourceMetering.sol";

interface ISystemConfigInterop {
event ConfigUpdate(uint256 indexed version, ISystemConfig.UpdateType indexed updateType, bytes data);
event ConfigUpdate(uint256 indexed nonceAndVersion, ISystemConfig.UpdateType indexed updateType, bytes data);
event Initialized(uint8 version);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

Expand All @@ -27,6 +27,7 @@ interface ISystemConfigInterop {
function gasLimit() external view returns (uint64);
function eip1559Denominator() external view returns (uint32);
function eip1559Elasticity() external view returns (uint32);
function configUpdateNonce() external view returns (uint256);
function gasPayingToken() external view returns (address addr_, uint8 decimals_);
function gasPayingTokenName() external view returns (string memory name_);
function gasPayingTokenSymbol() external view returns (string memory symbol_);
Expand Down
14 changes: 7 additions & 7 deletions packages/contracts-bedrock/snapshots/.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ GasBenchMark_L1BlockInterop_SetValuesInterop:test_setL1BlockValuesInterop_benchm
GasBenchMark_L1BlockInterop_SetValuesInterop_Warm:test_setL1BlockValuesInterop_benchmark() (gas: 5144)
GasBenchMark_L1Block_SetValuesEcotone:test_setL1BlockValuesEcotone_benchmark() (gas: 158531)
GasBenchMark_L1Block_SetValuesEcotone_Warm:test_setL1BlockValuesEcotone_benchmark() (gas: 7597)
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_0() (gas: 369235)
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_1() (gas: 2967442)
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_0() (gas: 564429)
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_1() (gas: 4076577)
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_0() (gas: 467041)
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_1() (gas: 3512790)
GasBenchMark_L1StandardBridge_Finalize:test_finalizeETHWithdrawal_benchmark() (gas: 72667)
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_0() (gas: 369292)
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_1() (gas: 2967499)
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_0() (gas: 564486)
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_1() (gas: 4076634)
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_0() (gas: 466926)
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_1() (gas: 3512847)
GasBenchMark_L1StandardBridge_Finalize:test_finalizeETHWithdrawal_benchmark() (gas: 72667)
15 changes: 14 additions & 1 deletion packages/contracts-bedrock/snapshots/abi/OptimismPortal2.json
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "transactionDepositedNonce",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "version",
Expand Down Expand Up @@ -745,7 +758,7 @@
{
"indexed": true,
"internalType": "uint256",
"name": "version",
"name": "nonceAndVersion",
"type": "uint256"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "transactionDepositedNonce",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "version",
Expand Down Expand Up @@ -763,7 +776,7 @@
{
"indexed": true,
"internalType": "uint256",
"name": "version",
"name": "nonceAndVersion",
"type": "uint256"
},
{
Expand Down
15 changes: 14 additions & 1 deletion packages/contracts-bedrock/snapshots/abi/SystemConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "configUpdateNonce",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "disputeGameFactory",
Expand Down Expand Up @@ -750,7 +763,7 @@
{
"indexed": true,
"internalType": "uint256",
"name": "version",
"name": "nonceAndVersion",
"type": "uint256"
},
{
Expand Down
Loading

0 comments on commit b299bc9

Please sign in to comment.