Commit 7d2fa734 authored by Mark Tyneway's avatar Mark Tyneway Committed by Matthew Slipper

op-chain-ops: refactor

Cleanup code, refactor some interfaces, make logIndex canonical
parent 37d51876
...@@ -138,13 +138,15 @@ func (w *LegacyWithdrawal) Value() (*big.Int, error) { ...@@ -138,13 +138,15 @@ func (w *LegacyWithdrawal) Value() (*big.Int, error) {
return nil, err return nil, err
} }
value := new(big.Int)
// Parse the 4byte selector
method, err := abi.MethodById(w.Data) method, err := abi.MethodById(w.Data)
// If it is an unknown selector, there is no value
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse withdrawal value: %w", err) return value, nil
} }
value := new(big.Int)
if w.Sender == nil { if w.Sender == nil {
return nil, errors.New("sender is nil") return nil, errors.New("sender is nil")
} }
......
package crossdomain package crossdomain
import ( import (
"errors"
"fmt" "fmt"
"math/big" "math/big"
...@@ -19,7 +18,7 @@ var ( ...@@ -19,7 +18,7 @@ var (
) )
// MigrateWithdrawals will migrate a list of pending withdrawals given a StateDB. // MigrateWithdrawals will migrate a list of pending withdrawals given a StateDB.
func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossDomainMessenger, l1StandardBridge *common.Address) error { func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossDomainMessenger *common.Address) error {
for i, legacy := range withdrawals { for i, legacy := range withdrawals {
legacySlot, err := legacy.StorageSlot() legacySlot, err := legacy.StorageSlot()
if err != nil { if err != nil {
...@@ -36,7 +35,7 @@ func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossD ...@@ -36,7 +35,7 @@ func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossD
continue continue
} }
withdrawal, err := MigrateWithdrawal(legacy, l1CrossDomainMessenger, l1StandardBridge) withdrawal, err := MigrateWithdrawal(legacy, l1CrossDomainMessenger)
if err != nil { if err != nil {
return err return err
} }
...@@ -54,42 +53,11 @@ func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossD ...@@ -54,42 +53,11 @@ func MigrateWithdrawals(withdrawals []*LegacyWithdrawal, db vm.StateDB, l1CrossD
// MigrateWithdrawal will turn a LegacyWithdrawal into a bedrock // MigrateWithdrawal will turn a LegacyWithdrawal into a bedrock
// style Withdrawal. // style Withdrawal.
func MigrateWithdrawal(withdrawal *LegacyWithdrawal, l1CrossDomainMessenger, l1StandardBridge *common.Address) (*Withdrawal, error) { func MigrateWithdrawal(withdrawal *LegacyWithdrawal, l1CrossDomainMessenger *common.Address) (*Withdrawal, error) {
value := new(big.Int) // Attempt to parse the value
value, err := withdrawal.Value()
isFromL2StandardBridge := *withdrawal.Sender == predeploys.L2StandardBridgeAddr if err != nil {
return nil, fmt.Errorf("cannot migrate withdrawal: %w", err)
if withdrawal.Target == nil {
return nil, errors.New("withdrawal target cannot be nil")
}
isToL1StandardBridge := *withdrawal.Target == *l1StandardBridge
if isFromL2StandardBridge && isToL1StandardBridge {
abi, err := bindings.L1StandardBridgeMetaData.GetAbi()
if err != nil {
return nil, err
}
method, err := abi.MethodById(withdrawal.Data)
if err != nil {
return nil, err
}
if method.Name == "finalizeETHWithdrawal" {
data, err := method.Inputs.Unpack(withdrawal.Data[4:])
if err != nil {
return nil, err
}
// bounds check
if len(data) < 3 {
return nil, errors.New("not enough data")
}
var ok bool
value, ok = data[2].(*big.Int)
if !ok {
return nil, errors.New("not big.Int")
}
}
} }
abi, err := bindings.L1CrossDomainMessengerMetaData.GetAbi() abi, err := bindings.L1CrossDomainMessengerMetaData.GetAbi()
......
...@@ -25,11 +25,9 @@ func TestMigrateWithdrawal(t *testing.T) { ...@@ -25,11 +25,9 @@ func TestMigrateWithdrawal(t *testing.T) {
} }
l1CrossDomainMessenger := common.HexToAddress("0x25ace71c97B33Cc4729CF772ae268934F7ab5fA1") l1CrossDomainMessenger := common.HexToAddress("0x25ace71c97B33Cc4729CF772ae268934F7ab5fA1")
l1StandardBridge := common.HexToAddress("0x99C9fc46f92E8a1c0deC1b1747d010903E884bE1")
for i, legacy := range withdrawals { for i, legacy := range withdrawals {
t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) { t.Run(fmt.Sprintf("test%d", i), func(t *testing.T) {
withdrawal, err := crossdomain.MigrateWithdrawal(legacy, &l1CrossDomainMessenger, &l1StandardBridge) withdrawal, err := crossdomain.MigrateWithdrawal(legacy, &l1CrossDomainMessenger)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, withdrawal) require.NotNil(t, withdrawal)
......
...@@ -147,7 +147,7 @@ func (w *Withdrawal) StorageSlot() (common.Hash, error) { ...@@ -147,7 +147,7 @@ func (w *Withdrawal) StorageSlot() (common.Hash, error) {
// SentMessage, SentMessageExtension1 and MessagePassed. // SentMessage, SentMessageExtension1 and MessagePassed.
// These logs are enough for the standard withdrawal flow to happen // These logs are enough for the standard withdrawal flow to happen
// which is driven by events being emitted. // which is driven by events being emitted.
func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) { func (w *Withdrawal) Receipt(hdr *types.Header, txIndex uint) (*types.Receipt, error) {
// Create a new receipt with the state root, successful execution and no gas // Create a new receipt with the state root, successful execution and no gas
// used // used
receipt := types.NewReceipt(hdr.Root.Bytes(), false, 0) receipt := types.NewReceipt(hdr.Root.Bytes(), false, 0)
...@@ -155,6 +155,11 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) { ...@@ -155,6 +155,11 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) {
receipt.Logs = make([]*types.Log, 0) receipt.Logs = make([]*types.Log, 0)
} }
// Use a counter to track the log index. Each receipt has 3 events and there
// is 1 receipt per transaction. Increment the logIndex after appending the
// log to the receipt.
logIndex := txIndex * 3
// Create the SentMessage log. // Create the SentMessage log.
args := abi.Arguments{ args := abi.Arguments{
{Name: "target", Type: AddressType}, {Name: "target", Type: AddressType},
...@@ -177,12 +182,13 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) { ...@@ -177,12 +182,13 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) {
Data: data, Data: data,
BlockNumber: hdr.Number.Uint64(), BlockNumber: hdr.Number.Uint64(),
TxHash: common.Hash{}, TxHash: common.Hash{},
TxIndex: 0, TxIndex: txIndex,
BlockHash: hdr.Hash(), BlockHash: hdr.Hash(),
Index: 0, Index: logIndex,
Removed: false, Removed: false,
} }
receipt.Logs = append(receipt.Logs, sm) receipt.Logs = append(receipt.Logs, sm)
logIndex++
// Create the SentMessageExtension1 log. The L2CrossDomainMessenger // Create the SentMessageExtension1 log. The L2CrossDomainMessenger
// emits this event. The sender is indexed. // emits this event. The sender is indexed.
...@@ -195,12 +201,13 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) { ...@@ -195,12 +201,13 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) {
Data: common.LeftPadBytes(w.Value.Bytes(), 32), Data: common.LeftPadBytes(w.Value.Bytes(), 32),
BlockNumber: hdr.Number.Uint64(), BlockNumber: hdr.Number.Uint64(),
TxHash: common.Hash{}, TxHash: common.Hash{},
TxIndex: 0, TxIndex: txIndex,
BlockHash: hdr.Hash(), BlockHash: hdr.Hash(),
Index: 0, Index: logIndex,
Removed: false, Removed: false,
} }
receipt.Logs = append(receipt.Logs, sm1) receipt.Logs = append(receipt.Logs, sm1)
logIndex++
// Create the MessagePassed log. // Create the MessagePassed log.
mpargs := abi.Arguments{ mpargs := abi.Arguments{
...@@ -229,9 +236,9 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) { ...@@ -229,9 +236,9 @@ func (w *Withdrawal) Receipt(hdr *types.Header) (*types.Receipt, error) {
Data: mpdata, Data: mpdata,
BlockNumber: hdr.Number.Uint64(), BlockNumber: hdr.Number.Uint64(),
TxHash: common.Hash{}, TxHash: common.Hash{},
TxIndex: 0, TxIndex: txIndex,
BlockHash: hdr.Hash(), BlockHash: hdr.Hash(),
Index: 0, Index: logIndex,
Removed: false, Removed: false,
} }
receipt.Logs = append(receipt.Logs, mp) receipt.Logs = append(receipt.Logs, mp)
......
...@@ -80,7 +80,7 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m ...@@ -80,7 +80,7 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m
} }
log.Info("Starting to migrate withdrawals") log.Info("Starting to migrate withdrawals")
err = crossdomain.MigrateWithdrawals(withdrawals, db, &config.L1CrossDomainMessengerProxy, &config.L1StandardBridgeProxy) err = crossdomain.MigrateWithdrawals(withdrawals, db, &config.L1CrossDomainMessengerProxy)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot migrate withdrawals: %w", err) return nil, fmt.Errorf("cannot migrate withdrawals: %w", err)
} }
...@@ -115,7 +115,7 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m ...@@ -115,7 +115,7 @@ func MigrateDB(ldb ethdb.Database, config *DeployConfig, l1Block *types.Block, m
BaseFee: (*big.Int)(config.L2GenesisBlockBaseFeePerGas), BaseFee: (*big.Int)(config.L2GenesisBlockBaseFeePerGas),
} }
receipts, err := CreateReceipts(bedrockHeader, withdrawals, &config.L1CrossDomainMessengerProxy, &config.L1StandardBridgeProxy) receipts, err := CreateReceipts(bedrockHeader, withdrawals, &config.L1CrossDomainMessengerProxy)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -8,20 +8,16 @@ import ( ...@@ -8,20 +8,16 @@ import (
// CreateReceipts will create the set of bedrock genesis receipts given // CreateReceipts will create the set of bedrock genesis receipts given
// a list of legacy withdrawals. // a list of legacy withdrawals.
func CreateReceipts( func CreateReceipts(hdr *types.Header, withdrawals []*crossdomain.LegacyWithdrawal, l1CrossDomainMessenger *common.Address) ([]*types.Receipt, error) {
hdr *types.Header,
withdrawals []*crossdomain.LegacyWithdrawal,
l1CrossDomainMessenger, l1StandardBridge *common.Address,
) ([]*types.Receipt, error) {
receipts := make([]*types.Receipt, 0) receipts := make([]*types.Receipt, 0)
for _, withdrawal := range withdrawals { for i, withdrawal := range withdrawals {
wd, err := crossdomain.MigrateWithdrawal(withdrawal, l1CrossDomainMessenger, l1StandardBridge) wd, err := crossdomain.MigrateWithdrawal(withdrawal, l1CrossDomainMessenger)
if err != nil { if err != nil {
return nil, err return nil, err
} }
receipt, err := wd.Receipt(hdr) receipt, err := wd.Receipt(hdr, uint(i))
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment