-
Notifications
You must be signed in to change notification settings - Fork 2
Match Core generateblock txid and raw-tx parse errors #604
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
Draft
metaphorics
wants to merge
2
commits into
cursor/mining-estimate-mode-1522
from
cursor/mining-generateblock-tx-errors-1522
Draft
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,7 +234,7 @@ pub(crate) fn generateblock(ctx: &Arc<Context>, params: &Value) -> Result<Value, | |
| .ok_or(RpcError::MethodDisabled("mining is unavailable"))?; | ||
| let output = required_str(params, 0, "output is required")?; | ||
| let payout = generateblock_payout_script(output, convert::bitcoin_network(ctx.chain_network))?; | ||
| let transactions = parse_generateblock_transactions(params)?; | ||
| let transactions = parse_generateblock_transactions(ctx, params)?; | ||
| let submit = optional_bool(params, 2, true)?; | ||
| let generated = control | ||
| .generate(GenerateRequest { | ||
|
|
@@ -362,7 +362,10 @@ fn optional_u64(params: &Value, index: usize, default: u64) -> Result<u64, RpcEr | |
| .ok_or(RpcError::InvalidType("parameter must be an integer")) | ||
| } | ||
|
|
||
| fn parse_generateblock_transactions(params: &Value) -> Result<Vec<GenerateTx>, RpcError> { | ||
| fn parse_generateblock_transactions( | ||
| ctx: &Context, | ||
| params: &Value, | ||
| ) -> Result<Vec<GenerateTx>, RpcError> { | ||
| let array = params_array(params)?; | ||
| let Some(value) = array.get(1) else { | ||
| return Err(RpcError::InvalidParams("transactions is required")); | ||
|
|
@@ -380,19 +383,32 @@ fn parse_generateblock_transactions(params: &Value) -> Result<Vec<GenerateTx>, R | |
| "transactions must be an array of hex strings", | ||
| )); | ||
| }; | ||
| // CONTRACT: docs/contracts/external-api.md#API-24 | ||
| if let Ok(txid) = Txid::from_str(text) { | ||
| transactions.push(GenerateTx::Mempool(txid)); | ||
| let snapshot = ctx.mempool.read().mining_snapshot(); | ||
| let Some(entry) = snapshot.entries.into_iter().find(|entry| entry.txid == txid) else { | ||
|
Comment on lines
+388
to
+389
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Snapshot rebuilt per txid Each txid-shaped array entry rebuilds and scans a complete mempool mining snapshot, making parsing O(requested txids × mempool size) with repeated allocations and ancestor-topology reconstruction. Large valid generateblock requests can therefore hold the mempool read path and consume substantially more CPU than necessary. Agent Prompt
|
||
| return Err(generateblock_unknown_txid(text)); | ||
| }; | ||
| transactions.push(GenerateTx::ResolvedMempool(entry)); | ||
| continue; | ||
| } | ||
| let bytes = from_hex(text) | ||
| .map_err(|()| RpcError::InvalidParams("transaction hex is not valid hexadecimal"))?; | ||
| let tx: Tx = deserialize(&bytes) | ||
| .map_err(|_| RpcError::InvalidParams("transaction hex could not be decoded"))?; | ||
| let bytes = from_hex(text).map_err(|()| generateblock_tx_decode_failed(text))?; | ||
| let tx: Tx = deserialize(&bytes).map_err(|_| generateblock_tx_decode_failed(text))?; | ||
| transactions.push(GenerateTx::Raw(tx)); | ||
| } | ||
| Ok(transactions) | ||
| } | ||
|
|
||
| fn generateblock_unknown_txid(text: &str) -> RpcError { | ||
| RpcError::InvalidAddressOrKey(format!("Transaction {text} not in mempool.")) | ||
| } | ||
|
|
||
| fn generateblock_tx_decode_failed(text: &str) -> RpcError { | ||
| RpcError::Deserialization(format!( | ||
| "Transaction decode failed for {text}. Make sure the tx has at least one input." | ||
| )) | ||
| } | ||
|
|
||
| fn parse_block_template_request(params: &Value) -> Result<BlockTemplateRequest, RpcError> { | ||
| if params.is_null() { | ||
| return Ok(BlockTemplateRequest { | ||
|
|
@@ -2073,11 +2089,20 @@ mod tests { | |
| /// API-05: 64-character hex is a mempool txid; longer hex is a raw transaction. | ||
| #[test] | ||
| fn generateblock_keeps_raw_transactions() { | ||
| use bitcoin_rs_mempool::MempoolEntry; | ||
|
|
||
| let control = FakeMiningControl::with_template(sample_template()); | ||
| let ctx = ctx_with_control(control.clone()); | ||
| let tx = sample_raw_tx(); | ||
| let raw_hex = to_lower_hex(&consensus_bytes(&tx)); | ||
| let txid = Txid::from(Hash256::from_le_bytes(&[0xcd; 32])); | ||
| let pooled = sample_raw_tx(); | ||
| let txid = pooled.txid(); | ||
| { | ||
| let mut pool = ctx.mempool.pool().write(); | ||
| pool.insert_entry(MempoolEntry::new(Arc::new(pooled), 100, 1_000, 1, 7)) | ||
| .unwrap_or_else(|err| panic!("insert failed: {err}")); | ||
| } | ||
| let mut raw = sample_raw_tx(); | ||
| raw.lock_time = 1; | ||
| let raw_hex = to_lower_hex(&consensus_bytes(&raw)); | ||
| generateblock(&ctx, &json!([REGTEST_ADDRESS, [txid.to_string(), raw_hex]])) | ||
| .unwrap_or_else(|err| panic!("generateblock failed: {err}")); | ||
| let request = control | ||
|
|
@@ -2087,10 +2112,50 @@ mod tests { | |
| .unwrap_or_else(|| panic!("generateblock must call generate")); | ||
| assert_eq!( | ||
| request.selection, | ||
| GenerateSelection::Ordered(vec![GenerateTx::Mempool(txid), GenerateTx::Raw(tx)]) | ||
| GenerateSelection::Ordered(vec![GenerateTx::Mempool(txid), GenerateTx::Raw(raw)]) | ||
| ); | ||
| } | ||
|
|
||
| // CONTRACT: docs/contracts/external-api.md#API-24 | ||
| #[test] | ||
| fn generateblock_rejects_unknown_mempool_txid_like_core() { | ||
| let control = FakeMiningControl::with_template(sample_template()); | ||
| let ctx = ctx_with_control(control); | ||
| let missing = "CD".repeat(32); | ||
| let error = generateblock(&ctx, &json!([REGTEST_ADDRESS, [missing.as_str()]])) | ||
| .err() | ||
| .unwrap_or_else(|| panic!("unknown mempool txid must fail at parse")); | ||
| assert_eq!(error.code(), RpcError::CORE_NOT_FOUND); | ||
| assert_eq!( | ||
| error.to_string(), | ||
| format!("Transaction {missing} not in mempool.") | ||
| ); | ||
| } | ||
|
|
||
| // CONTRACT: docs/contracts/external-api.md#API-24 | ||
| #[test] | ||
| fn generateblock_rejects_undecodable_raw_tx_like_core() { | ||
| let control = FakeMiningControl::with_template(sample_template()); | ||
| let ctx = ctx_with_control(control); | ||
| for payload in ["00", "zz", "abcd"] { | ||
| let error = generateblock(&ctx, &json!([REGTEST_ADDRESS, [payload]])) | ||
| .err() | ||
| .unwrap_or_else(|| panic!("`{payload}` must fail decode")); | ||
| assert_eq!( | ||
| error.code(), | ||
| RpcError::CORE_DESERIALIZATION_ERROR, | ||
| "for `{payload}`" | ||
| ); | ||
| assert_eq!( | ||
| error.to_string(), | ||
| format!( | ||
| "Transaction decode failed for {payload}. Make sure the tx has at least one input." | ||
| ), | ||
| "for `{payload}`" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// API-05: extra generateblock positionals are rejected, matching Core arity. | ||
| #[test] | ||
| fn generateblock_rejects_trailing_parameters() { | ||
|
|
||
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
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
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.
1. resolvedmempool breaks compatibility tests
📘 Rule violation≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools