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

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"math/big"
	"os"
	"path"
	"time"

13
	"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
14 15 16 17 18
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
)

Joshua Gutow's avatar
Joshua Gutow committed
19
type TransactionWithMetadata struct {
20 21 22 23
	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
24
	BlockTime   uint64             `json:"block_time"`
25 26 27
	ChainId     uint64             `json:"chain_id"`
	Sender      common.Address     `json:"sender"`
	ValidSender bool               `json:"valid_sender"`
28 29 30
	Frames      []derive.Frame     `json:"frames"`
	FrameErr    string             `json:"frame_parse_error"`
	ValidFrames bool               `json:"valid_data"`
31 32 33 34 35 36 37 38 39 40 41
	Tx          *types.Transaction `json:"tx"`
}

type Config struct {
	Start, End   uint64
	ChainID      *big.Int
	BatchInbox   common.Address
	BatchSenders map[common.Address]struct{}
	OutDirectory string
}

42 43 44
// 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
func Batches(client *ethclient.Client, config Config) (totalValid, totalInvalid int) {
	if err := os.MkdirAll(config.OutDirectory, 0750); err != nil {
		log.Fatal(err)
	}
	number := new(big.Int).SetUint64(config.Start)
	signer := types.LatestSignerForChainID(config.ChainID)
	for i := config.Start; i < config.End; i++ {
		valid, invalid := fetchBatchesPerBlock(client, number, signer, config)
		totalValid += valid
		totalInvalid += invalid
		number = number.Add(number, common.Big1)
	}
	return
}

60
// fetchBatchesPerBlock gets a block & the parses all of the transactions in the block.
61
func fetchBatchesPerBlock(client *ethclient.Client, number *big.Int, signer types.Signer, config Config) (validBatchCount, invalidBatchCount int) {
62
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
63 64 65 66 67
	defer cancel()
	block, err := client.BlockByNumber(ctx, number)
	if err != nil {
		log.Fatal(err)
	}
68
	fmt.Println("Fetched block: ", number)
69 70 71 72 73 74
	for i, tx := range block.Transactions() {
		if tx.To() != nil && *tx.To() == config.BatchInbox {
			sender, err := signer.Sender(tx)
			if err != nil {
				log.Fatal(err)
			}
75
			validSender := true
76 77 78 79
			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
80 81 82 83 84 85 86 87 88 89 90 91
			}

			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 {
92
				validBatchCount += 1
93 94
			} else {
				invalidBatchCount += 1
95 96
			}

Joshua Gutow's avatar
Joshua Gutow committed
97
			txm := &TransactionWithMetadata{
98 99 100 101 102 103
				Tx:          tx,
				Sender:      sender,
				ValidSender: validSender,
				TxIndex:     uint64(i),
				BlockNumber: block.NumberU64(),
				BlockHash:   block.Hash(),
Joshua Gutow's avatar
Joshua Gutow committed
104
				BlockTime:   block.Time(),
105 106
				ChainId:     config.ChainID.Uint64(),
				InboxAddr:   config.BatchInbox,
107 108 109
				Frames:      frames,
				FrameErr:    frameError,
				ValidFrames: validFrames,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			}
			filename := path.Join(config.OutDirectory, fmt.Sprintf("%s.json", tx.Hash().String()))
			file, err := os.Create(filename)
			if err != nil {
				log.Fatal(err)
			}
			defer file.Close()
			enc := json.NewEncoder(file)
			if err := enc.Encode(txm); err != nil {
				log.Fatal(err)
			}
		}
	}
	return
}