Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add err match in the package chain #937

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading