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.
This commit is contained in:
Jamie Holdstock 2021-09-15 20:24:25 +01:00 committed by GitHub
parent 522a363bda
commit 7f25f6614c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 <hash>: already have transaction <hash>
//
// If the tx is in a mined block...
// Error code: -1
// message: rejected transaction <hash>: 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