|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + evblock "github.com/evstack/ev-node/block" |
| 14 | + "github.com/evstack/ev-node/core/da" |
| 15 | + "github.com/evstack/ev-node/da/jsonrpc" |
| 16 | + rollcmd "github.com/evstack/ev-node/pkg/cmd" |
| 17 | + rollconf "github.com/evstack/ev-node/pkg/config" |
| 18 | + genesispkg "github.com/evstack/ev-node/pkg/genesis" |
| 19 | + seqcommon "github.com/evstack/ev-node/sequencers/common" |
| 20 | + "github.com/evstack/ev-node/types" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + flagNamespace = "namespace" |
| 25 | + flagGasPrice = "gas-price" |
| 26 | +) |
| 27 | + |
| 28 | +// PostTxCmd returns a command to post a signed Ethereum transaction to the DA layer |
| 29 | +func PostTxCmd() *cobra.Command { |
| 30 | + cobraCmd := &cobra.Command{ |
| 31 | + Use: "post-tx", |
| 32 | + Short: "Post a signed Ethereum transaction to the DA layer", |
| 33 | + Long: `Post a signed Ethereum transaction to the DA layer using the Evolve configuration. |
| 34 | +
|
| 35 | +This command submits a signed Ethereum transaction tzo the configured DA layer for forced inclusion. |
| 36 | +The transaction is provided as an argument, which accepts either: |
| 37 | + 1. A hex-encoded signed transaction (with or without 0x prefix) |
| 38 | + 2. A path to a file containing the hex-encoded transaction |
| 39 | + 3. A JSON object with a "raw" field containing the hex-encoded transaction |
| 40 | +
|
| 41 | +The command automatically detects the input format. |
| 42 | +
|
| 43 | +Examples: |
| 44 | + # From hex string |
| 45 | + evm post-tx 0x02f873... |
| 46 | +
|
| 47 | + # From file |
| 48 | + evm post-tx tx.txt |
| 49 | +
|
| 50 | + # From JSON |
| 51 | + evm post-tx '{"raw":"0x02f873..."}' |
| 52 | +`, |
| 53 | + Args: cobra.ExactArgs(1), |
| 54 | + RunE: postTxRunE, |
| 55 | + } |
| 56 | + |
| 57 | + // Add evolve config flags |
| 58 | + rollconf.AddFlags(cobraCmd) |
| 59 | + |
| 60 | + // Add command-specific flags |
| 61 | + cobraCmd.Flags().String(flagNamespace, "", "DA namespace ID (if not provided, uses config namespace)") |
| 62 | + cobraCmd.Flags().Float64(flagGasPrice, -1, "Gas price for DA submission (if not provided, uses config gas price)") |
| 63 | + |
| 64 | + return cobraCmd |
| 65 | +} |
| 66 | + |
| 67 | +// postTxRunE executes the post-tx command |
| 68 | +func postTxRunE(cmd *cobra.Command, args []string) error { |
| 69 | + nodeConfig, err := rollcmd.ParseConfig(cmd) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + logger := rollcmd.SetupLogger(nodeConfig.Log) |
| 75 | + |
| 76 | + txInput := args[0] |
| 77 | + if txInput == "" { |
| 78 | + return fmt.Errorf("transaction cannot be empty") |
| 79 | + } |
| 80 | + |
| 81 | + var txData []byte |
| 82 | + if _, err := os.Stat(txInput); err == nil { |
| 83 | + // Input is a file path |
| 84 | + txData, err = decodeTxFromFile(txInput) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to decode transaction from file: %w", err) |
| 87 | + } |
| 88 | + } else { |
| 89 | + // Input is a JSON string |
| 90 | + txData, err = decodeTxFromJSON(txInput) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("failed to decode transaction from JSON: %w", err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if len(txData) == 0 { |
| 97 | + return fmt.Errorf("transaction data cannot be empty") |
| 98 | + } |
| 99 | + |
| 100 | + // Get namespace (use flag if provided, otherwise use config) |
| 101 | + namespace, _ := cmd.Flags().GetString(flagNamespace) |
| 102 | + if namespace == "" { |
| 103 | + namespace = nodeConfig.DA.GetForcedInclusionNamespace() |
| 104 | + } |
| 105 | + |
| 106 | + if namespace == "" { |
| 107 | + return fmt.Errorf("forced inclusionnamespace cannot be empty") |
| 108 | + } |
| 109 | + |
| 110 | + namespaceBz := da.NamespaceFromString(namespace).Bytes() |
| 111 | + |
| 112 | + // Get gas price (use flag if provided, otherwise use config) |
| 113 | + gasPrice, err := cmd.Flags().GetFloat64(flagGasPrice) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("failed to get gas-price flag: %w", err) |
| 116 | + } |
| 117 | + |
| 118 | + logger.Info().Str("namespace", namespace).Float64("gas_price", gasPrice).Int("tx_size", len(txData)).Msg("posting transaction to DA layer") |
| 119 | + |
| 120 | + daClient, err := jsonrpc.NewClient( |
| 121 | + cmd.Context(), |
| 122 | + logger, |
| 123 | + nodeConfig.DA.Address, |
| 124 | + nodeConfig.DA.AuthToken, |
| 125 | + seqcommon.AbsoluteMaxBlobSize, |
| 126 | + ) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("failed to create DA client: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + // Submit transaction to DA layer |
| 132 | + logger.Info().Msg("submitting transaction to DA layer...") |
| 133 | + |
| 134 | + blobs := [][]byte{txData} |
| 135 | + options := []byte(nodeConfig.DA.SubmitOptions) |
| 136 | + |
| 137 | + dac := evblock.NewDAClient(&daClient.DA, nodeConfig, logger) |
| 138 | + result := dac.Submit(cmd.Context(), blobs, gasPrice, namespaceBz, options) |
| 139 | + |
| 140 | + // Check result |
| 141 | + switch result.Code { |
| 142 | + case da.StatusSuccess: |
| 143 | + logger.Info().Msg("transaction successfully submitted to DA layer") |
| 144 | + cmd.Printf("\n✓ Transaction posted successfully\n\n") |
| 145 | + cmd.Printf("Namespace: %s\n", namespace) |
| 146 | + cmd.Printf("DA Height: %d\n", result.Height) |
| 147 | + cmd.Printf("Data Size: %d bytes\n", len(txData)) |
| 148 | + |
| 149 | + genesisPath := filepath.Join(filepath.Dir(nodeConfig.ConfigPath()), "genesis.json") |
| 150 | + genesis, err := genesispkg.LoadGenesis(genesisPath) |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("failed to load genesis for calculating inclusion time estimate: %w", err) |
| 153 | + } |
| 154 | + |
| 155 | + _, epochEnd, _ := types.CalculateEpochBoundaries(result.Height, genesis.DAStartHeight, genesis.DAEpochForcedInclusion) |
| 156 | + cmd.Printf( |
| 157 | + "DA Blocks until inclusion: %d (at DA height %d)\n", |
| 158 | + epochEnd-(result.Height+1), |
| 159 | + epochEnd+1, |
| 160 | + ) |
| 161 | + |
| 162 | + cmd.Printf("\n") |
| 163 | + return nil |
| 164 | + |
| 165 | + case da.StatusTooBig: |
| 166 | + return fmt.Errorf("transaction too large for DA layer: %s", result.Message) |
| 167 | + |
| 168 | + case da.StatusNotIncludedInBlock: |
| 169 | + return fmt.Errorf("transaction not included in DA block: %s", result.Message) |
| 170 | + |
| 171 | + case da.StatusAlreadyInMempool: |
| 172 | + cmd.Printf("⚠ Transaction already in mempool\n") |
| 173 | + if result.Height > 0 { |
| 174 | + cmd.Printf(" DA Height: %d\n", result.Height) |
| 175 | + } |
| 176 | + return nil |
| 177 | + |
| 178 | + case da.StatusContextCanceled: |
| 179 | + return fmt.Errorf("submission canceled: %s", result.Message) |
| 180 | + |
| 181 | + default: |
| 182 | + return fmt.Errorf("DA submission failed (code: %d): %s", result.Code, result.Message) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// decodeTxFromFile reads an Ethereum transaction from a file and decodes it to bytes |
| 187 | +func decodeTxFromFile(filePath string) ([]byte, error) { |
| 188 | + data, err := os.ReadFile(filePath) |
| 189 | + if err != nil { |
| 190 | + return nil, fmt.Errorf("reading file: %w", err) |
| 191 | + } |
| 192 | + |
| 193 | + return decodeTxFromJSON(string(data)) |
| 194 | +} |
| 195 | + |
| 196 | +// decodeTxFromJSON decodes an Ethereum transaction from various formats to bytes |
| 197 | +func decodeTxFromJSON(input string) ([]byte, error) { |
| 198 | + input = strings.TrimSpace(input) |
| 199 | + |
| 200 | + // Try to decode as JSON with "raw" field |
| 201 | + var txJSON map[string]any |
| 202 | + if err := json.Unmarshal([]byte(input), &txJSON); err == nil { |
| 203 | + if rawTx, ok := txJSON["raw"].(string); ok { |
| 204 | + return decodeHexTx(rawTx) |
| 205 | + } |
| 206 | + return nil, fmt.Errorf("JSON must contain 'raw' field with hex-encoded transaction") |
| 207 | + } |
| 208 | + |
| 209 | + // Try to decode as hex string directly |
| 210 | + return decodeHexTx(input) |
| 211 | +} |
| 212 | + |
| 213 | +// decodeHexTx decodes a hex-encoded Ethereum transaction |
| 214 | +func decodeHexTx(hexStr string) ([]byte, error) { |
| 215 | + hexStr = strings.TrimSpace(hexStr) |
| 216 | + |
| 217 | + // Remove 0x prefix if present |
| 218 | + if strings.HasPrefix(hexStr, "0x") || strings.HasPrefix(hexStr, "0X") { |
| 219 | + hexStr = hexStr[2:] |
| 220 | + } |
| 221 | + |
| 222 | + // Decode hex string to bytes |
| 223 | + txBytes, err := hex.DecodeString(hexStr) |
| 224 | + if err != nil { |
| 225 | + return nil, fmt.Errorf("decoding hex transaction: %w", err) |
| 226 | + } |
| 227 | + |
| 228 | + if len(txBytes) == 0 { |
| 229 | + return nil, fmt.Errorf("decoded transaction is empty") |
| 230 | + } |
| 231 | + |
| 232 | + return txBytes, nil |
| 233 | +} |
0 commit comments