From 6d5a59646a0fb6b142e77630738bbb52dc091bd9 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 22 Jun 2021 15:11:53 +0800 Subject: [PATCH] rpc: `SendTransaction` returns error message for invalid tx on broadcast sync mode (#159) * SendTransaction returns error message for invalid tx - use sync broadcast - treat error response to error return Closes: #158 * Apply suggestions from code review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/eth_api.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/ethereum/rpc/eth_api.go b/ethereum/rpc/eth_api.go index 2924ea9b..909fa05b 100644 --- a/ethereum/rpc/eth_api.go +++ b/ethereum/rpc/eth_api.go @@ -403,9 +403,13 @@ func (e *PublicEthAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, e // Broadcast transaction in sync mode (default) // NOTE: If error is encountered on the node, the broadcast will not return an error - asyncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastAsync) - if _, err := asyncCtx.BroadcastTx(txBytes); err != nil { - e.logger.WithError(err).Errorln("failed to broadcast Eth tx") + syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync) + rsp, err := syncCtx.BroadcastTx(txBytes) + if err != nil || rsp.Code != 0 { + if err == nil { + err = errors.New(rsp.RawLog) + } + e.logger.WithError(err).Errorln("failed to broadcast tx") return txHash, err } @@ -463,10 +467,13 @@ func (e *PublicEthAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, erro tmTx := tmtypes.Tx(txBytes) txHash := common.BytesToHash(tmTx.Hash()) - asyncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastAsync) - - if _, err := asyncCtx.BroadcastTx(txBytes); err != nil { - e.logger.WithError(err).Errorln("failed to broadcast eth tx") + syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync) + rsp, err := syncCtx.BroadcastTx(txBytes) + if err != nil || rsp.Code != 0 { + if err == nil { + err = errors.New(rsp.RawLog) + } + e.logger.WithError(err).Errorln("failed to broadcast tx") return txHash, err }