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:
parent
522a363bda
commit
7f25f6614c
27
rpc/dcrd.go
27
rpc/dcrd.go
@ -11,7 +11,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/decred/dcrd/chaincfg/v3"
|
"github.com/decred/dcrd/chaincfg/v3"
|
||||||
dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v3"
|
dcrdtypes "github.com/decred/dcrd/rpc/jsonrpc/types/v3"
|
||||||
@ -126,34 +125,16 @@ func (c *DcrdRPC) SendRawTransaction(txHex string) error {
|
|||||||
allowHighFees := false
|
allowHighFees := false
|
||||||
err := c.Call(c.ctx, "sendrawtransaction", nil, txHex, allowHighFees)
|
err := c.Call(c.ctx, "sendrawtransaction", nil, txHex, allowHighFees)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// sendrawtransaction returns error code -40 (ErrRPCDuplicateTx) if the
|
||||||
// sendrawtransaction can return two different errors when a tx already
|
// provided transaction already exists in the mempool or in a mined
|
||||||
// exists:
|
// block.
|
||||||
//
|
|
||||||
// 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
|
|
||||||
//
|
|
||||||
// It's not a problem if the transaction has already been broadcast, so
|
// It's not a problem if the transaction has already been broadcast, so
|
||||||
// we will capture these errors and return nil.
|
// we will capture this error and return nil.
|
||||||
|
|
||||||
// Exists in mempool.
|
|
||||||
var e *wsrpc.Error
|
var e *wsrpc.Error
|
||||||
if errors.As(err, &e) && e.Code == ErrRPCDuplicateTx {
|
if errors.As(err, &e) && e.Code == ErrRPCDuplicateTx {
|
||||||
return nil
|
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 err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user