Skip to content

Commit

Permalink
Merge pull request btcsuite#937 from yyforyongyu/add-err-match
Browse files Browse the repository at this point in the history
Add err match in the package `chain`
  • Loading branch information
yyforyongyu authored Jul 6, 2024
2 parents b42ed59 + a90a3ec commit e391a1c
Show file tree
Hide file tree
Showing 8 changed files with 749 additions and 15 deletions.
22 changes: 21 additions & 1 deletion chain/bitcoind_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,27 @@ func (c *BitcoindClient) GetTxOut(txHash *chainhash.Hash, index uint32,
func (c *BitcoindClient) SendRawTransaction(tx *wire.MsgTx,
allowHighFees bool) (*chainhash.Hash, error) {

return c.chainConn.client.SendRawTransaction(tx, allowHighFees)
txid, err := c.chainConn.client.SendRawTransaction(tx, allowHighFees)
if err != nil {
return nil, c.MapRPCErr(err)
}

return txid, nil
}

// MapRPCErr takes an error returned from calling RPC methods from various
// chain backends and maps it to an defined error here.
func (c *BitcoindClient) MapRPCErr(rpcErr error) error {
// Try to match it against bitcoind's error.
for i := uint32(0); i < uint32(errSentinel); i++ {
err := RPCErr(i)
if matchErrStr(rpcErr, err.Error()) {
return err
}
}

// If not matched, return the original error wrapped.
return fmt.Errorf("%w: %v", ErrUndefined, rpcErr)
}

// TestMempoolAcceptCmd returns result of mempool acceptance tests indicating
Expand Down
60 changes: 60 additions & 0 deletions chain/btcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package chain

import (
"errors"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -578,3 +579,62 @@ func (c *RPCClient) LookupInputMempoolSpend(op wire.OutPoint) (

return getTxSpendingPrevOut(op, c.Client)
}

// MapRPCErr takes an error returned from calling RPC methods from various
// chain backends and maps it to an defined error here. It uses the
// `BtcdErrMap`, whose keys are btcd error strings and values are errors made
// from bitcoind error strings.
func (c *RPCClient) MapRPCErr(rpcErr error) error {
// Iterate the map and find the matching error.
for btcdErr, matchedErr := range BtcdErrMap {
// Match it against btcd's error.
if matchErrStr(rpcErr, btcdErr) {
return matchedErr
}
}

// If no matching error is found, we try to match it to an older
// version of `btcd`.
//
// Get the backend's version.
backend, bErr := c.BackendVersion()
if bErr != nil {
// If there's an error getting the backend version, we return
// the original error and the backend error.
return fmt.Errorf("%w: %v, failed to get backend version %v",
ErrUndefined, rpcErr, bErr)
}

// If this version doesn't support `testmempoolaccept`, it must be
// below v0.24.2. In this case, we will match the errors defined in
// pre-v0.24.2.
//
// NOTE: `testmempoolaccept` is implemented in v0.24.1, but this
// version was never tagged, which means it must be v0.24.2 when it's
// supported.
if !backend.SupportTestMempoolAccept() {
// If the backend is older than v0.24.2, we will try to match
// the error to the older version of `btcd`.
for btcdErr, matchedErr := range BtcdErrMapPre2402 {
// Match it against btcd's error.
if matchErrStr(rpcErr, btcdErr) {
return matchedErr
}
}
}

// If not matched, return the original error wrapped.
return fmt.Errorf("%w: %v", ErrUndefined, rpcErr)
}

// SendRawTransaction sends a raw transaction via btcd.
func (c *RPCClient) SendRawTransaction(tx *wire.MsgTx,
allowHighFees bool) (*chainhash.Hash, error) {

txid, err := c.Client.SendRawTransaction(tx, allowHighFees)
if err != nil {
return nil, c.MapRPCErr(err)
}

return txid, nil
}
Loading

0 comments on commit e391a1c

Please sign in to comment.