calldata_source.go 2.82 KB
Newer Older
protolambda's avatar
protolambda committed
1 2 3 4
package derive

import (
	"context"
5
	"errors"
protolambda's avatar
protolambda committed
6 7 8
	"fmt"
	"io"

9
	"github.com/ethereum/go-ethereum"
protolambda's avatar
protolambda committed
10 11 12
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
13

14
	"github.com/ethereum-optimism/optimism/op-service/eth"
protolambda's avatar
protolambda committed
15 16
)

17
// CalldataSource is a fault tolerant approach to fetching data.
18 19
// The constructor will never fail & it will instead re-attempt the fetcher
// at a later point.
20
type CalldataSource struct {
21 22 23 24
	// Internal state + data
	open bool
	data []eth.Data
	// Required to re-attempt fetching
25
	ref     eth.L1BlockRef
26
	dsCfg   DataSourceConfig
27 28
	fetcher L1TransactionFetcher
	log     log.Logger
29 30

	batcherAddr common.Address
31 32
}

33
// NewCalldataSource creates a new calldata source. It suppresses errors in fetching the L1 block if they occur.
34
// If there is an error, it will attempt to fetch the result on the next call to `Next`.
35 36
func NewCalldataSource(ctx context.Context, log log.Logger, dsCfg DataSourceConfig, fetcher L1TransactionFetcher, ref eth.L1BlockRef, batcherAddr common.Address) DataIter {
	_, txs, err := fetcher.InfoAndTxsByHash(ctx, ref.Hash)
protolambda's avatar
protolambda committed
37
	if err != nil {
38
		return &CalldataSource{
39
			open:        false,
40
			ref:         ref,
41
			dsCfg:       dsCfg,
42 43 44
			fetcher:     fetcher,
			log:         log,
			batcherAddr: batcherAddr,
45
		}
46 47 48 49
	}
	return &CalldataSource{
		open: true,
		data: DataFromEVMTransactions(dsCfg, batcherAddr, txs, log.New("origin", ref)),
50 51 52 53 54 55
	}
}

// Next returns the next piece of data if it has it. If the constructor failed, this
// will attempt to reinitialize itself. If it cannot find the block it returns a ResetError
// otherwise it returns a temporary error if fetching the block returns an error.
56
func (ds *CalldataSource) Next(ctx context.Context) (eth.Data, error) {
57
	if !ds.open {
58
		if _, txs, err := ds.fetcher.InfoAndTxsByHash(ctx, ds.ref.Hash); err == nil {
59
			ds.open = true
60
			ds.data = DataFromEVMTransactions(ds.dsCfg, ds.batcherAddr, txs, ds.log)
61 62 63 64 65 66 67 68 69 70 71 72
		} else if errors.Is(err, ethereum.NotFound) {
			return nil, NewResetError(fmt.Errorf("failed to open calldata source: %w", err))
		} else {
			return nil, NewTemporaryError(fmt.Errorf("failed to open calldata source: %w", err))
		}
	}
	if len(ds.data) == 0 {
		return nil, io.EOF
	} else {
		data := ds.data[0]
		ds.data = ds.data[1:]
		return data, nil
protolambda's avatar
protolambda committed
73 74 75
	}
}

76 77 78
// 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.
// This will return an empty array if no valid transactions are found.
79
func DataFromEVMTransactions(dsCfg DataSourceConfig, batcherAddr common.Address, txs types.Transactions, log log.Logger) []eth.Data {
80 81 82
	out := []eth.Data{}
	for _, tx := range txs {
		if isValidBatchTx(tx, dsCfg.l1Signer, dsCfg.batchInboxAddress, batcherAddr) {
protolambda's avatar
protolambda committed
83 84 85 86 87
			out = append(out, tx.Data())
		}
	}
	return out
}