From 7f25f6614c7d72d9ed4a397fbb3a6006640ef78a Mon Sep 17 00:00:00 2001 From: Jamie Holdstock Date: Wed, 15 Sep 2021 20:24:25 +0100 Subject: [PATCH] Remove string compare in dcrd error handling. (#295) dcrd now returns the same error for duplicate transactions regardless of whether they are mined or only exist in the mempool. --- rpc/dcrd.go | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/rpc/dcrd.go b/rpc/dcrd.go index 7e5b13f..7292d34 100644 --- a/rpc/dcrd.go +++ b/rpc/dcrd.go @@ -11,7 +11,6 @@ import ( "encoding/json" "errors" "fmt" - "strings" "github.com/decred/dcrd/chaincfg/v3" dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v3" @@ -126,34 +125,16 @@ func (c *DcrdRPC) SendRawTransaction(txHex string) error { allowHighFees := false err := c.Call(c.ctx, "sendrawtransaction", nil, txHex, allowHighFees) if err != nil { - - // sendrawtransaction can return two different errors when a tx already - // exists: - // - // If the tx is in mempool... - // Error code: -40 - // message: rejected transaction : already have transaction - // - // If the tx is in a mined block... - // Error code: -1 - // message: rejected transaction : transaction already exists - // + // sendrawtransaction returns error code -40 (ErrRPCDuplicateTx) if the + // provided transaction already exists in the mempool or in a mined + // block. // It's not a problem if the transaction has already been broadcast, so - // we will capture these errors and return nil. - - // Exists in mempool. + // we will capture this error and return nil. var e *wsrpc.Error if errors.As(err, &e) && e.Code == ErrRPCDuplicateTx { return nil } - // Exists in mined block. - // We cannot use error code -1 here because it is a generic code for - // many errors, so we instead need to string match on the message. - if strings.Contains(err.Error(), "transaction already exists") { - return nil - } - return err } return nil