Commit 44c0ba5c authored by Joshua Gutow's avatar Joshua Gutow Committed by GitHub

Merge pull request #8342 from xenoliss/nit/op-node/calldata-source

nit(op-node): only store the needed rollup.Config fields in DataSource
parents 6950bc81 32f0cf4b
...@@ -28,17 +28,23 @@ type L1TransactionFetcher interface { ...@@ -28,17 +28,23 @@ type L1TransactionFetcher interface {
// This is not a stage in the pipeline, but a wrapper for another stage in the pipeline // This is not a stage in the pipeline, but a wrapper for another stage in the pipeline
type DataSourceFactory struct { type DataSourceFactory struct {
log log.Logger log log.Logger
cfg *rollup.Config dsCfg DataSourceConfig
fetcher L1TransactionFetcher fetcher L1TransactionFetcher
} }
func NewDataSourceFactory(log log.Logger, cfg *rollup.Config, fetcher L1TransactionFetcher) *DataSourceFactory { func NewDataSourceFactory(log log.Logger, cfg *rollup.Config, fetcher L1TransactionFetcher) *DataSourceFactory {
return &DataSourceFactory{log: log, cfg: cfg, fetcher: fetcher} return &DataSourceFactory{log: log, dsCfg: DataSourceConfig{l1Signer: cfg.L1Signer(), batchInboxAddress: cfg.BatchInboxAddress}, fetcher: fetcher}
} }
// OpenData returns a DataIter. This struct implements the `Next` function. // OpenData returns a DataIter. This struct implements the `Next` function.
func (ds *DataSourceFactory) OpenData(ctx context.Context, id eth.BlockID, batcherAddr common.Address) DataIter { func (ds *DataSourceFactory) OpenData(ctx context.Context, id eth.BlockID, batcherAddr common.Address) DataIter {
return NewDataSource(ctx, ds.log, ds.cfg, ds.fetcher, id, batcherAddr) return NewDataSource(ctx, ds.log, ds.dsCfg, ds.fetcher, id, batcherAddr)
}
// DataSourceConfig regroups the mandatory rollup.Config fields needed for DataFromEVMTransactions.
type DataSourceConfig struct {
l1Signer types.Signer
batchInboxAddress common.Address
} }
// DataSource is a fault tolerant approach to fetching data. // DataSource is a fault tolerant approach to fetching data.
...@@ -50,7 +56,7 @@ type DataSource struct { ...@@ -50,7 +56,7 @@ type DataSource struct {
data []eth.Data data []eth.Data
// Required to re-attempt fetching // Required to re-attempt fetching
id eth.BlockID id eth.BlockID
cfg *rollup.Config // TODO: `DataFromEVMTransactions` should probably not take the full config dsCfg DataSourceConfig
fetcher L1TransactionFetcher fetcher L1TransactionFetcher
log log.Logger log log.Logger
...@@ -59,13 +65,14 @@ type DataSource struct { ...@@ -59,13 +65,14 @@ type DataSource struct {
// NewDataSource creates a new calldata source. It suppresses errors in fetching the L1 block if they occur. // NewDataSource creates a new calldata source. It suppresses errors in fetching the L1 block if they occur.
// If there is an error, it will attempt to fetch the result on the next call to `Next`. // If there is an error, it will attempt to fetch the result on the next call to `Next`.
func NewDataSource(ctx context.Context, log log.Logger, cfg *rollup.Config, fetcher L1TransactionFetcher, block eth.BlockID, batcherAddr common.Address) DataIter { func NewDataSource(ctx context.Context, log log.Logger, dsCfg DataSourceConfig, fetcher L1TransactionFetcher, block eth.BlockID, batcherAddr common.Address) DataIter {
_, txs, err := fetcher.InfoAndTxsByHash(ctx, block.Hash) _, txs, err := fetcher.InfoAndTxsByHash(ctx, block.Hash)
if err != nil { if err != nil {
return &DataSource{ return &DataSource{
open: false, open: false,
id: block, id: block,
cfg: cfg, dsCfg: dsCfg,
fetcher: fetcher, fetcher: fetcher,
log: log, log: log,
batcherAddr: batcherAddr, batcherAddr: batcherAddr,
...@@ -73,7 +80,7 @@ func NewDataSource(ctx context.Context, log log.Logger, cfg *rollup.Config, fetc ...@@ -73,7 +80,7 @@ func NewDataSource(ctx context.Context, log log.Logger, cfg *rollup.Config, fetc
} else { } else {
return &DataSource{ return &DataSource{
open: true, open: true,
data: DataFromEVMTransactions(cfg, batcherAddr, txs, log.New("origin", block)), data: DataFromEVMTransactions(dsCfg, batcherAddr, txs, log.New("origin", block)),
} }
} }
} }
...@@ -85,7 +92,7 @@ func (ds *DataSource) Next(ctx context.Context) (eth.Data, error) { ...@@ -85,7 +92,7 @@ func (ds *DataSource) Next(ctx context.Context) (eth.Data, error) {
if !ds.open { if !ds.open {
if _, txs, err := ds.fetcher.InfoAndTxsByHash(ctx, ds.id.Hash); err == nil { if _, txs, err := ds.fetcher.InfoAndTxsByHash(ctx, ds.id.Hash); err == nil {
ds.open = true ds.open = true
ds.data = DataFromEVMTransactions(ds.cfg, ds.batcherAddr, txs, log.New("origin", ds.id)) ds.data = DataFromEVMTransactions(ds.dsCfg, ds.batcherAddr, txs, log.New("origin", ds.id))
} else if errors.Is(err, ethereum.NotFound) { } else if errors.Is(err, ethereum.NotFound) {
return nil, NewResetError(fmt.Errorf("failed to open calldata source: %w", err)) return nil, NewResetError(fmt.Errorf("failed to open calldata source: %w", err))
} else { } else {
...@@ -104,12 +111,11 @@ func (ds *DataSource) Next(ctx context.Context) (eth.Data, error) { ...@@ -104,12 +111,11 @@ func (ds *DataSource) Next(ctx context.Context) (eth.Data, error) {
// DataFromEVMTransactions filters all of the transactions and returns the calldata from transactions // DataFromEVMTransactions filters all of the transactions and returns the calldata from transactions
// that are sent to the batch inbox address from the batch sender address. // that are sent to the batch inbox address from the batch sender address.
// This will return an empty array if no valid transactions are found. // This will return an empty array if no valid transactions are found.
func DataFromEVMTransactions(config *rollup.Config, batcherAddr common.Address, txs types.Transactions, log log.Logger) []eth.Data { func DataFromEVMTransactions(dsCfg DataSourceConfig, batcherAddr common.Address, txs types.Transactions, log log.Logger) []eth.Data {
var out []eth.Data var out []eth.Data
l1Signer := config.L1Signer()
for j, tx := range txs { for j, tx := range txs {
if to := tx.To(); to != nil && *to == config.BatchInboxAddress { if to := tx.To(); to != nil && *to == dsCfg.batchInboxAddress {
seqDataSubmitter, err := l1Signer.Sender(tx) // optimization: only derive sender if To is correct seqDataSubmitter, err := dsCfg.l1Signer.Sender(tx) // optimization: only derive sender if To is correct
if err != nil { if err != nil {
log.Warn("tx in inbox with invalid signature", "index", j, "txHash", tx.Hash(), "err", err) log.Warn("tx in inbox with invalid signature", "index", j, "txHash", tx.Hash(), "err", err)
continue // bad signature, ignore continue // bad signature, ignore
......
...@@ -121,7 +121,7 @@ func TestDataFromEVMTransactions(t *testing.T) { ...@@ -121,7 +121,7 @@ func TestDataFromEVMTransactions(t *testing.T) {
} }
} }
out := DataFromEVMTransactions(cfg, batcherAddr, txs, testlog.Logger(t, log.LvlCrit)) out := DataFromEVMTransactions(DataSourceConfig{cfg.L1Signer(), cfg.BatchInboxAddress}, batcherAddr, txs, testlog.Logger(t, log.LvlCrit))
require.ElementsMatch(t, expectedData, out) require.ElementsMatch(t, expectedData, out)
} }
......
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