-
Notifications
You must be signed in to change notification settings - Fork 9
chain: port over SubmitPackage from btcwallet #11
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package chain | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/btcsuite/btcd/btcjson" | ||
| "github.com/btcsuite/btcd/rpcclient" | ||
| "github.com/btcsuite/btcd/wire" | ||
| ) | ||
|
|
||
| // BitcoindRPCClient extends the chain.BitcoindClient with additional methods | ||
| // that are not yet available in the standard btcd RPC client. | ||
| // | ||
| // TODO: remove this once https://github.com/btcsuite/btcwallet/pull/1009 is | ||
| // merged. | ||
| type BitcoindRPCClient struct { | ||
| rpcClient *rpcclient.Client | ||
| } | ||
|
|
||
| // NewBitcoindRPCClient creates a new BitcoindRPCClient from an existing | ||
| // rpcclient.Client. | ||
| func NewBitcoindRPCClient(rpcClient *rpcclient.Client) *BitcoindRPCClient { | ||
| return &BitcoindRPCClient{ | ||
| rpcClient: rpcClient, | ||
| } | ||
| } | ||
|
|
||
| // SubmitPackage broadcasts a parents+child package atomically. | ||
| // | ||
| // `parents` must contain at least one transaction. The returned slice | ||
| // holds the txids of every transaction that entered the mempool. | ||
| // | ||
| // `maxFeeRateBTCPerVByte` caps the effective feerate the node will | ||
| // accept for the entire package, expressed in BTC per virtual byte. | ||
| // Pass nil to leave the limit unset. | ||
| // | ||
| // If the active backend cannot relay packages, ErrUnimplemented is | ||
| // returned. | ||
| // | ||
| //nolint:err113 | ||
| func (c *BitcoindRPCClient) SubmitPackage(parents []*wire.MsgTx, | ||
| child *wire.MsgTx, maxFeeRateBTCPerVByte *float64) ( | ||
| *btcjson.SubmitPackageResult, error) { | ||
|
|
||
| // Sanity check inputs. | ||
| if len(parents) == 0 { | ||
| return nil, errors.New("submitpackage: need at least one " + | ||
| "parent txn") | ||
| } | ||
|
|
||
| if child == nil { | ||
| return nil, errors.New("submitpackage: child txn not defined") | ||
| } | ||
|
|
||
| // Prepare the hex encoded transactions to that we'll add the request. | ||
| toHex := func(tx *wire.MsgTx) (string, error) { | ||
| var buf bytes.Buffer | ||
| if err := tx.Serialize(&buf); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return hex.EncodeToString(buf.Bytes()), nil | ||
| } | ||
|
|
||
| rawTxs := make([]string, 0, len(parents)+1) | ||
|
|
||
| for i, ch := range parents { | ||
| h, err := toHex(ch) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot serialize parent txn "+ | ||
| "%d: %w", i, err) | ||
| } | ||
|
|
||
| rawTxs = append(rawTxs, h) | ||
| } | ||
|
|
||
| h, err := toHex(child) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot serialize child txn: %w", err) | ||
| } | ||
|
|
||
| rawTxs = append(rawTxs, h) | ||
|
|
||
| // Parameter 1: The package (array of hex strings). | ||
| rawTxsJSON, err := json.Marshal(rawTxs) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal raw txs array: %w", | ||
| err) | ||
| } | ||
|
|
||
| params := []json.RawMessage{rawTxsJSON} | ||
|
|
||
| // Parameter 2: maxfeerate (optional, numeric, in BTC/kvB) | ||
| if maxFeeRateBTCPerVByte != nil { | ||
| // Convert BTC/vByte to BTC/kvB. | ||
| const btcPerVByteToKvB = 1000 | ||
| maxFeeRateBTCPerKvB := *maxFeeRateBTCPerVByte * btcPerVByteToKvB | ||
| maxFeeRateJSON, err := json.Marshal(maxFeeRateBTCPerKvB) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal "+ | ||
| "maxfeerate: %w", err) | ||
| } | ||
|
|
||
| params = append(params, maxFeeRateJSON) | ||
| } | ||
|
|
||
| resp, err := c.rpcClient.RawRequest("submitpackage", params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Unmarshall the response. | ||
| var result btcjson.SubmitPackageResult | ||
| if err := result.UnmarshalJSON(resp); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &result, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function comment states that
ErrUnimplementedis returned if the backend cannot relay packages. However, the implementation does not check for this case (e.g., by checking forbtcjson.ErrRPCMethodNotFoundfrom the RPC response) and therefore does not returnchain.ErrUnimplemented. This discrepancy between documentation and implementation can lead to incorrect error handling by callers. Given this is a temporary port, removing the comment is the simplest fix to align documentation with behavior.