|
| 1 | +package bundles |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/0xsoniclabs/sonic/ethapi" |
| 9 | + "github.com/0xsoniclabs/sonic/tests" |
| 10 | + "github.com/ethereum/go-ethereum" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 13 | + "github.com/ethereum/go-ethereum/core/types" |
| 14 | + "github.com/ethereum/go-ethereum/rpc" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// Prepare bundle is a wrapper around the rpc method sonic_prepareBundle, which |
| 19 | +// prepares a bundle for execution by filling in all necessary fields and |
| 20 | +// encoding them properly. |
| 21 | +// |
| 22 | +// It accepts transactions in the form of CallMsg to keep compatibility with |
| 23 | +// standard go-ethereum client methods like EstimateGas. |
| 24 | +// CallMsg is a more convenient type to prepare transactions, |
| 25 | +// it does not encode fields into hex and is compatible with standard |
| 26 | +// go-ethereum client methods like EstimateGas. |
| 27 | +// Unfortunately, it does not include nonce, therefore this function needs |
| 28 | +// to assign a fitting value. |
| 29 | +// |
| 30 | +// This function should be part of the go-ethereum client object, being the entry |
| 31 | +// point to the api from go programs. |
| 32 | +func PrepareBundle( |
| 33 | + t *testing.T, client *tests.PooledEhtClient, |
| 34 | + earliest, latest uint64, |
| 35 | + txs []ethereum.CallMsg, |
| 36 | +) (ethapi.RPCPreparedBundle, error) { |
| 37 | + |
| 38 | + nonces := make(map[common.Address]uint64) |
| 39 | + for _, tx := range txs { |
| 40 | + if _, ok := nonces[tx.From]; !ok { |
| 41 | + nonce, err := client.PendingNonceAt(t.Context(), tx.From) |
| 42 | + require.NoError(t, err, "failed to get pending nonce") |
| 43 | + nonces[tx.From] = nonce |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Convert CallMsg without nonce into TransactionArgs with nonce and hex encoding of fields |
| 48 | + txsArgs := make([]ethapi.TransactionArgs, len(txs)) |
| 49 | + for i, tx := range txs { |
| 50 | + nonce := nonces[tx.From] |
| 51 | + nonces[tx.From] = nonce + 1 |
| 52 | + txArgs := ethapi.TransactionArgs{ |
| 53 | + From: &tx.From, |
| 54 | + To: tx.To, |
| 55 | + Nonce: (*hexutil.Uint64)(&nonce), |
| 56 | + GasPrice: (*hexutil.Big)(tx.GasPrice), |
| 57 | + Value: (*hexutil.Big)(tx.Value), |
| 58 | + Data: (*hexutil.Bytes)(&tx.Data), |
| 59 | + } |
| 60 | + txsArgs[i] = txArgs |
| 61 | + } |
| 62 | + |
| 63 | + var gasLimits ethapi.BundleGasLimits |
| 64 | + err := client.Client().Call(&gasLimits, "sonic_estimateGasForTransactions", txsArgs, "latest", nil, nil) |
| 65 | + require.NoError(t, err, "failed to estimate gas for bundle") |
| 66 | + |
| 67 | + for i := range txsArgs { |
| 68 | + txsArgs[i].Gas = (*hexutil.Uint64)(&gasLimits.GasLimits[i]) |
| 69 | + } |
| 70 | + |
| 71 | + // Call sonic_prepareBundle to get a bundle with all fields properly filled in and encoded |
| 72 | + var preparedBundle ethapi.RPCPreparedBundle |
| 73 | + err = client.Client().Call(&preparedBundle, "sonic_prepareBundle", |
| 74 | + ethapi.PrepareBundleArgs{ |
| 75 | + Transactions: txsArgs, |
| 76 | + EarliestBlock: rpc.BlockNumber(earliest), |
| 77 | + LatestBlock: rpc.BlockNumber(latest), |
| 78 | + }) |
| 79 | + require.NoError(t, err, "failed to call sonic_prepareBundle") |
| 80 | + return preparedBundle, nil |
| 81 | +} |
| 82 | + |
| 83 | +// SubmitBundle is a wrapper around the rpc method sonic_submitBundle, which |
| 84 | +// submits a prepared bundle for execution. |
| 85 | +// It uses types.Transaction just like the method SendTransaction. |
| 86 | +// This function should be part of the go-ethereum client object, being the entry |
| 87 | +// point to the api from go programs. |
| 88 | +func SubmitBundle(client *tests.PooledEhtClient, |
| 89 | + txs []*types.Transaction, |
| 90 | + plan ethapi.RPCExecutionPlan, |
| 91 | +) (common.Hash, error) { |
| 92 | + encodedTransactions := make([]hexutil.Bytes, len(txs)) |
| 93 | + for i, tx := range txs { |
| 94 | + data, err := tx.MarshalBinary() |
| 95 | + if err != nil { |
| 96 | + return common.Hash{}, fmt.Errorf("failed to marshal transaction: %w", err) |
| 97 | + } |
| 98 | + encodedTransactions[i] = hexutil.Bytes(data) |
| 99 | + } |
| 100 | + |
| 101 | + var bundleHash common.Hash |
| 102 | + err := client.Client().Call(&bundleHash, "sonic_submitBundle", |
| 103 | + ethapi.SubmitBundleArgs{ |
| 104 | + SignedTransactions: encodedTransactions, |
| 105 | + ExecutionPlan: plan, |
| 106 | + }) |
| 107 | + return bundleHash, err |
| 108 | +} |
| 109 | + |
| 110 | +func WaitForBundleExecution( |
| 111 | + ctxt context.Context, |
| 112 | + client *rpc.Client, |
| 113 | + executionPlanHash common.Hash, |
| 114 | +) (ethapi.RPCBundleInfo, error) { |
| 115 | + infos, err := waitForBundlesExecution( |
| 116 | + ctxt, client, |
| 117 | + []common.Hash{executionPlanHash}, |
| 118 | + ) |
| 119 | + if err != nil { |
| 120 | + return ethapi.RPCBundleInfo{}, err |
| 121 | + } |
| 122 | + if len(infos) != 1 { |
| 123 | + return ethapi.RPCBundleInfo{}, fmt.Errorf("failed to obtain bundle info") |
| 124 | + } |
| 125 | + return infos[0], nil |
| 126 | +} |
| 127 | + |
| 128 | +func getBundleInfo( |
| 129 | + ctxt context.Context, |
| 130 | + client *rpc.Client, |
| 131 | + executionPlanHash common.Hash, |
| 132 | +) (ethapi.RPCBundleInfo, error) { |
| 133 | + var info ethapi.RPCBundleInfo |
| 134 | + err := client.CallContext( |
| 135 | + ctxt, |
| 136 | + &info, |
| 137 | + "sonic_getBundleInfo", |
| 138 | + executionPlanHash, |
| 139 | + ) |
| 140 | + return info, err |
| 141 | +} |
| 142 | + |
| 143 | +func waitForBundlesExecution( |
| 144 | + ctxt context.Context, |
| 145 | + client *rpc.Client, |
| 146 | + executionPlanHashes []common.Hash, |
| 147 | +) ([]ethapi.RPCBundleInfo, error) { |
| 148 | + |
| 149 | + infos := make([]ethapi.RPCBundleInfo, len(executionPlanHashes)) |
| 150 | + done := make([]bool, len(executionPlanHashes)) |
| 151 | + |
| 152 | + err := tests.WaitFor(ctxt, func(innerCtx context.Context) (bool, error) { |
| 153 | + |
| 154 | + allFinished := true |
| 155 | + for i, plan := range executionPlanHashes { |
| 156 | + if done[i] { |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + info, err := getBundleInfo(innerCtx, client, plan) |
| 161 | + if err != nil { |
| 162 | + return false, err |
| 163 | + } |
| 164 | + |
| 165 | + if info.Status != ethapi.BundleStatusPending { |
| 166 | + infos[i] = info |
| 167 | + done[i] = true |
| 168 | + } else { |
| 169 | + allFinished = false |
| 170 | + } |
| 171 | + } |
| 172 | + return allFinished, nil |
| 173 | + }) |
| 174 | + return infos, err |
| 175 | +} |
0 commit comments