Skip to content

Commit

Permalink
optimize SystemConfigByL2Hash function by avoid getBlockByHash with f…
Browse files Browse the repository at this point in the history
…ull block body (#2)
  • Loading branch information
Troublor authored and claymega committed Nov 1, 2024
1 parent 3f9340c commit 6ab801a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions op-service/sources/eth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ func (s *EthClient) ReadStorageAt(ctx context.Context, address common.Address, s
return common.BytesToHash(value.Bytes()), nil
}

// GetTransactionByBlockHashAndIndex returns the transaction at the given index in the block with the given hash.
func (s *EthClient) TxByBlockHashAndIndex(ctx context.Context, blockHash common.Hash, index uint64) (*types.Transaction, error) {
var tx *types.Transaction
err := s.client.CallContext(ctx, &tx, "eth_getTransactionByBlockHashAndIndex", blockHash.Hex(), hexutil.EncodeUint64(index))
if err != nil {
return nil, fmt.Errorf("failed to fetch transaction in block %s at index %d: %w", blockHash, index, err)
}
if tx == nil {
return nil, ethereum.NotFound
}
return tx, nil
}

func (s *EthClient) Close() {
s.client.Close()
}
28 changes: 25 additions & 3 deletions op-service/sources/l2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"

"github.com/ethereum-optimism/optimism/op-node/rollup"
Expand Down Expand Up @@ -158,12 +159,33 @@ func (s *L2Client) SystemConfigByL2Hash(ctx context.Context, hash common.Hash) (
return ref, nil
}

envelope, err := s.PayloadByHash(ctx, hash)
header, err := s.InfoByHash(ctx, hash)
if err != nil {
// w%: wrap to preserve ethereum.NotFound case
return eth.SystemConfig{}, fmt.Errorf("failed to determine block-hash of hash %v, could not get payload: %w", hash, err)
return eth.SystemConfig{}, fmt.Errorf("failed to determine block-hash of hash %v, could not get header: %w", hash, err)
}
txs := []hexutil.Bytes{}
first_tx, err := s.TxByBlockHashAndIndex(ctx, hash, 0)
if err == nil {
first_tx_binary, err := first_tx.MarshalBinary()
if err != nil {
return eth.SystemConfig{}, fmt.Errorf("failed to determine block-hash of hash %v, could not marshal first tx: %w", hash, err)
}
txs = append(txs, first_tx_binary)
} else if err != ethereum.NotFound {
// w%: wrap to preserve ethereum.NotFound case
return eth.SystemConfig{}, fmt.Errorf("failed to determine block-hash of hash %v, could not get first tx: %w", hash, err)
}
cfg, err := derive.PayloadToSystemConfig(s.rollupCfg, envelope.ExecutionPayload)

// PayloadToSystemConfig only needs this fields from the header
payload := eth.ExecutionPayload{}
payload.BlockNumber = hexutil.Uint64(header.NumberU64())
payload.BlockHash = header.Hash()
payload.Timestamp = hexutil.Uint64(header.Time())
payload.GasLimit = hexutil.Uint64(header.GasLimit())
payload.Transactions = txs

cfg, err := derive.PayloadToSystemConfig(s.rollupCfg, &payload)
if err != nil {
return eth.SystemConfig{}, err
}
Expand Down

0 comments on commit 6ab801a

Please sign in to comment.