|
| 1 | +package parsebatchl2data |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/ethereum/go-ethereum/core/types" |
| 9 | + "github.com/rs/zerolog/log" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "io" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + //go:embed usage.md |
| 18 | + usage string |
| 19 | + inputFileName *string |
| 20 | +) |
| 21 | + |
| 22 | +var ParseBatchL2Data = &cobra.Command{ |
| 23 | + Use: "parse-batch-l2-data [flags]", |
| 24 | + Aliases: []string{"parsebatchl2data"}, |
| 25 | + Short: "Convert batch l2 data into an ndjson stream", |
| 26 | + Long: usage, |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + |
| 29 | + rawData, err := getInputData(cmd, args) |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + batchL2Data := strings.TrimSpace(strings.TrimPrefix(string(rawData), "0x")) |
| 34 | + rawBatchL2Data, err := hex.DecodeString(batchL2Data) |
| 35 | + if err != nil { |
| 36 | + log.Err(err).Msg("Unable to hex decode batch l2 data") |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + rawBatch, err := DecodeBatchV2(rawBatchL2Data) |
| 41 | + if err != nil { |
| 42 | + log.Error().Err(err).Msg("unable to decode l2 batch data") |
| 43 | + tryRawBatch(rawBatchL2Data) |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + blocks := rawBatch.Blocks |
| 48 | + |
| 49 | + for _, l2RawBlock := range blocks { |
| 50 | + blockData := struct { |
| 51 | + IndexL1InfoTree uint32 |
| 52 | + DeltaTimestamp uint32 |
| 53 | + }{l2RawBlock.IndexL1InfoTree, l2RawBlock.DeltaTimestamp} |
| 54 | + blockDataBytes, err := json.Marshal(blockData) |
| 55 | + if err != nil { |
| 56 | + log.Err(err).Msg("unable to marshal block data") |
| 57 | + return err |
| 58 | + } |
| 59 | + fmt.Println(string(blockDataBytes)) |
| 60 | + |
| 61 | + for i := range l2RawBlock.Transactions { |
| 62 | + if err := printTxData(&l2RawBlock.Transactions[i]); err != nil { |
| 63 | + log.Error().Err(err).Int("tx_index", i).Msg("Failed to print transaction data") |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + return nil |
| 68 | + }, |
| 69 | + Args: func(cmd *cobra.Command, args []string) error { |
| 70 | + return nil |
| 71 | + }, |
| 72 | +} |
| 73 | + |
| 74 | +func init() { |
| 75 | + flagSet := ParseBatchL2Data.PersistentFlags() |
| 76 | + inputFileName = flagSet.String("file", "", "Provide a file with the key information ") |
| 77 | +} |
| 78 | + |
| 79 | +func getInputData(cmd *cobra.Command, args []string) ([]byte, error) { |
| 80 | + if inputFileName != nil && *inputFileName != "" { |
| 81 | + return os.ReadFile(*inputFileName) |
| 82 | + } |
| 83 | + |
| 84 | + if len(args) > 1 { |
| 85 | + concat := strings.Join(args[1:], " ") |
| 86 | + return []byte(concat), nil |
| 87 | + } |
| 88 | + |
| 89 | + return io.ReadAll(os.Stdin) |
| 90 | +} |
| 91 | + |
| 92 | +func printTxData(rawL2Tx *L2TxRaw) error { |
| 93 | + signer := types.NewEIP155Signer(rawL2Tx.Tx.ChainId()) |
| 94 | + sender, err := signer.Sender(rawL2Tx.Tx) |
| 95 | + if err != nil { |
| 96 | + log.Error().Err(err).Msg("unable to reccover sender") |
| 97 | + return err |
| 98 | + } |
| 99 | + jsonTx, err := rawL2Tx.Tx.MarshalJSON() |
| 100 | + if err != nil { |
| 101 | + log.Error().Err(err).Msg("unable to json marshal tx") |
| 102 | + return err |
| 103 | + } |
| 104 | + txMap := make(map[string]string, 0) |
| 105 | + err = json.Unmarshal(jsonTx, &txMap) |
| 106 | + if err != nil { |
| 107 | + log.Error().Err(err).Msg("unable to remarshal json tx") |
| 108 | + return err |
| 109 | + } |
| 110 | + txMap["from"] = sender.String() |
| 111 | + jsonTx, err = json.Marshal(txMap) |
| 112 | + if err != nil { |
| 113 | + log.Error().Err(err).Msg("unable to marhshal tx with from") |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Println(string(jsonTx)) |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func tryRawBatch(rawBatchL2Data []byte) { |
| 122 | + rawBatch, err := DecodeForcedBatchV2(rawBatchL2Data) |
| 123 | + if err != nil { |
| 124 | + log.Error().Err(err).Msg("unable to decode raw l2 batch data") |
| 125 | + return |
| 126 | + } |
| 127 | + for i, t := range rawBatch.Transactions { |
| 128 | + if err := printTxData(&t); err != nil { |
| 129 | + log.Error().Err(err).Int("tx_index", i).Msg("Failed to print transaction data in forced batch") |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments