fetch.go 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package fetch

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"math/big"
	"os"
	"path"
11
	"sync/atomic"
12 13
	"time"

14
	"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
15 16 17
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
18
	"golang.org/x/sync/errgroup"
19 20
)

Joshua Gutow's avatar
Joshua Gutow committed
21
type TransactionWithMetadata struct {
22 23 24 25
	TxIndex     uint64             `json:"tx_index"`
	InboxAddr   common.Address     `json:"inbox_address"`
	BlockNumber uint64             `json:"block_number"`
	BlockHash   common.Hash        `json:"block_hash"`
Joshua Gutow's avatar
Joshua Gutow committed
26
	BlockTime   uint64             `json:"block_time"`
27 28 29
	ChainId     uint64             `json:"chain_id"`
	Sender      common.Address     `json:"sender"`
	ValidSender bool               `json:"valid_sender"`
30 31 32
	Frames      []derive.Frame     `json:"frames"`
	FrameErr    string             `json:"frame_parse_error"`
	ValidFrames bool               `json:"valid_data"`
33 34 35 36
	Tx          *types.Transaction `json:"tx"`
}

type Config struct {
37 38 39 40 41 42
	Start, End         uint64
	ChainID            *big.Int
	BatchInbox         common.Address
	BatchSenders       map[common.Address]struct{}
	OutDirectory       string
	ConcurrentRequests uint64
43 44
}

45 46 47
// Batches fetches & stores all transactions sent to the batch inbox address in
// the given block range (inclusive to exclusive).
// The transactions & metadata are written to the out directory.
48
func Batches(client *ethclient.Client, config Config) (totalValid, totalInvalid uint64) {
49 50 51 52
	if err := os.MkdirAll(config.OutDirectory, 0750); err != nil {
		log.Fatal(err)
	}
	signer := types.LatestSignerForChainID(config.ChainID)
53 54 55 56 57
	concurrentRequests := int(config.ConcurrentRequests)

	g, ctx := errgroup.WithContext(context.Background())
	g.SetLimit(concurrentRequests)

58
	for i := config.Start; i < config.End; i++ {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		if err := ctx.Err(); err != nil {
			break
		}
		number := i
		g.Go(func() error {
			valid, invalid, err := fetchBatchesPerBlock(ctx, client, number, signer, config)
			if err != nil {
				return fmt.Errorf("error occurred while fetching block %d: %w", number, err)
			}
			atomic.AddUint64(&totalValid, valid)
			atomic.AddUint64(&totalInvalid, invalid)
			return nil
		})
	}
	if err := g.Wait(); err != nil {
		log.Fatal(err)
75 76 77 78
	}
	return
}

79
// fetchBatchesPerBlock gets a block & the parses all of the transactions in the block.
80 81 82 83
func fetchBatchesPerBlock(ctx context.Context, client *ethclient.Client, number uint64, signer types.Signer, config Config) (uint64, uint64, error) {
	validBatchCount := uint64(0)
	invalidBatchCount := uint64(0)
	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
84
	defer cancel()
85
	block, err := client.BlockByNumber(ctx, new(big.Int).SetUint64(number))
86
	if err != nil {
87
		return 0, 0, err
88
	}
89
	fmt.Println("Fetched block: ", number)
90 91 92 93
	for i, tx := range block.Transactions() {
		if tx.To() != nil && *tx.To() == config.BatchInbox {
			sender, err := signer.Sender(tx)
			if err != nil {
94
				return 0, 0, err
95
			}
96
			validSender := true
97 98 99 100
			if _, ok := config.BatchSenders[sender]; !ok {
				fmt.Printf("Found a transaction (%s) from an invalid sender (%s)\n", tx.Hash().String(), sender.String())
				invalidBatchCount += 1
				validSender = false
101 102 103 104 105 106 107 108 109 110 111 112
			}

			validFrames := true
			frameError := ""
			frames, err := derive.ParseFrames(tx.Data())
			if err != nil {
				fmt.Printf("Found a transaction (%s) with invalid data: %v\n", tx.Hash().String(), err)
				validFrames = false
				frameError = err.Error()
			}

			if validSender && validFrames {
113
				validBatchCount += 1
114 115
			} else {
				invalidBatchCount += 1
116 117
			}

Joshua Gutow's avatar
Joshua Gutow committed
118
			txm := &TransactionWithMetadata{
119 120 121 122 123 124
				Tx:          tx,
				Sender:      sender,
				ValidSender: validSender,
				TxIndex:     uint64(i),
				BlockNumber: block.NumberU64(),
				BlockHash:   block.Hash(),
Joshua Gutow's avatar
Joshua Gutow committed
125
				BlockTime:   block.Time(),
126 127
				ChainId:     config.ChainID.Uint64(),
				InboxAddr:   config.BatchInbox,
128 129 130
				Frames:      frames,
				FrameErr:    frameError,
				ValidFrames: validFrames,
131 132 133 134
			}
			filename := path.Join(config.OutDirectory, fmt.Sprintf("%s.json", tx.Hash().String()))
			file, err := os.Create(filename)
			if err != nil {
135
				return 0, 0, err
136 137 138 139
			}
			defer file.Close()
			enc := json.NewEncoder(file)
			if err := enc.Encode(txm); err != nil {
140
				return 0, 0, err
141 142 143
			}
		}
	}
144
	return validBatchCount, invalidBatchCount, nil
145
}