diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-11-23 12:12:10 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-03-12 12:19:09 +0800 |
commit | 4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8 (patch) | |
tree | a11ac5336cff17e8dccf3802ab964feda846d6aa /internal/ethapi | |
parent | b45aaef1780d9ad225792497752ba4b74cb4f7e9 (diff) | |
download | dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar.gz dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar.bz2 dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar.lz dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar.xz dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.tar.zst dexon-4a02390bfefc00e3c1b3e70a59f8e2c19f5f1dd8.zip |
api: allow sending batch of raw transactions
Diffstat (limited to 'internal/ethapi')
-rw-r--r-- | internal/ethapi/api.go | 39 | ||||
-rw-r--r-- | internal/ethapi/backend.go | 1 |
2 files changed, 40 insertions, 0 deletions
diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index df5f4385d..7ea7cf2aa 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1279,6 +1279,31 @@ func submitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c return tx.Hash(), nil } +// submitTransactions is a helper function that submits batch of tx to txPool and logs a message. +func submitTransactions(ctx context.Context, b Backend, txs []*types.Transaction) ([]common.Hash, error) { + errs := b.SendTxs(ctx, txs) + var hashes []common.Hash + for i, err := range errs { + if err != nil { + return nil, err + } + tx := txs[i] + if tx.To() == nil { + signer := types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Number()) + from, err := types.Sender(signer, tx) + if err != nil { + return nil, err + } + addr := crypto.CreateAddress(from, tx.Nonce()) + log.Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", addr.Hex()) + } else { + log.Info("Submitted transaction", "fullhash", tx.Hash().Hex(), "recipient", tx.To()) + } + hashes = append(hashes, tx.Hash()) + } + return hashes, nil +} + // SendTransaction creates a transaction for the given argument, sign it and submit it to the // transaction pool. func (s *PublicTransactionPoolAPI) SendTransaction(ctx context.Context, args SendTxArgs) (common.Hash, error) { @@ -1326,6 +1351,20 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod return submitTransaction(ctx, s.b, tx) } +// SendRawTransactions will add the signed transaction to the transaction pool. +// The sender is responsible for signing the transaction and using the correct nonce. +func (s *PublicTransactionPoolAPI) SendRawTransactions(ctx context.Context, encodedTxs []hexutil.Bytes) ([]common.Hash, error) { + var txs []*types.Transaction + for _, encodedTx := range encodedTxs { + tx := new(types.Transaction) + if err := rlp.DecodeBytes(encodedTx, tx); err != nil { + return nil, err + } + txs = append(txs, tx) + } + return submitTransactions(ctx, s.b, txs) +} + // Sign calculates an ECDSA signature for: // keccack256("\x19Ethereum Signed Message:\n" + len(message) + message). // diff --git a/internal/ethapi/backend.go b/internal/ethapi/backend.go index 85383a71f..51f236890 100644 --- a/internal/ethapi/backend.go +++ b/internal/ethapi/backend.go @@ -64,6 +64,7 @@ type Backend interface { // TxPool API SendTx(ctx context.Context, signedTx *types.Transaction) error + SendTxs(ctx context.Context, signedTxs []*types.Transaction) []error GetPoolTransactions() (types.Transactions, error) GetPoolTransaction(txHash common.Hash) *types.Transaction GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error) |