Skip to content

test(consensus): test BlockTooBig#867

Open
JoseSK999 wants to merge 2 commits intogetfloresta:masterfrom
JoseSK999:block-too-big-test
Open

test(consensus): test BlockTooBig#867
JoseSK999 wants to merge 2 commits intogetfloresta:masterfrom
JoseSK999:block-too-big-test

Conversation

@JoseSK999
Copy link
Copy Markdown
Member

@JoseSK999 JoseSK999 commented Mar 5, 2026

Description and Notes

First commit adds some test helpers to make them simpler, second adds the test. I recommend review by commit, although this PR is small.

I've constructed a transaction that, when inserted in block 866,342 makes it have 4,000,001 WUs. Then we update the witness commitment and the merkle root, and then check we get BlockTooBig.

For this I had to implement the update_witness_commitment helper. Note that the block we are testing has witnesses, so the witness commitment is enforced by consensus, and inserting a legacy transaction adds a zero wtxid that the commitment must now account for.

How to verify the changes you have done?

Just take a look at test_block_too_big.

@JoseSK999 JoseSK999 self-assigned this Mar 5, 2026
@JoseSK999 JoseSK999 added chore Cleaning, refactoring, reducing complexity code quality Generally improves code readability and maintainability labels Mar 5, 2026
jaoleal

This comment was marked as duplicate.

@jaoleal
Copy link
Copy Markdown
Collaborator

jaoleal commented Mar 9, 2026

Sometimes github is strange, it duplicated my review and now theres only one... The one that I marked as duplicate.

@JoseSK999 JoseSK999 force-pushed the block-too-big-test branch from 39bec94 to dc543b1 Compare March 9, 2026 17:18
@JoseSK999
Copy link
Copy Markdown
Member Author

Pushed dc543b1

  • Refactored the test setup into a helper function called build_oversized_866_342 @jaoleal

@jaoleal jaoleal requested a review from Davidson-Souza March 9, 2026 18:11
@jaoleal
Copy link
Copy Markdown
Collaborator

jaoleal commented Mar 9, 2026

ACK dc543b1

let mut it = coinbase.input[0].witness.iter();
match (it.next(), it.next()) {
(Some(rv), None) => rv.try_into().ok()?,
_ => return None,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since the coinbase input must have one and only one element, what about leaving an unreachable here?

Copy link
Copy Markdown
Member Author

@JoseSK999 JoseSK999 Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense

Actually better to return None, this may be a legacy block, so unreachable is not true.

Comment on lines +700 to +703
fn decode_block(file_path: &str) -> Block {
let block_file = File::open(file_path).unwrap();
let block_bytes = zstd::decode_all(block_file).unwrap();
deserialize(&block_bytes).unwrap()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: Bitcoin Core leaves those test assets in another repo, called qa-assets. Leaving in the same tee has the disadvantage of bloating our tree with those test data, even though some people may not use it. Wdyt about moving these to our own floresta-qa-assets?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Davidson-Souza you mean the data under floresta-chain/testdata?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea that i got was is that davidson wants a floresta-qa-assets to gather test utils/data

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh like a separate test utils crate

@JoseSK999 JoseSK999 force-pushed the block-too-big-test branch from dc543b1 to 20eb38b Compare March 10, 2026 20:07
@JoseSK999
Copy link
Copy Markdown
Member Author

Pushed 20eb38b

  • mutate_block is now deterministic
  • Copy-pasted the witness-commitment structure from BIP141 into a comment

I've constructed a transaction that, when inserted in block 866,342 makes it have 4,000,001 WUs. Then we update the witness commitment and the merkle root, and then check we get `BlockTooBig`.

For this I had to implement the `update_witness_commitment` helper. Note that the block we are testing has witnesses, so the witness commitment is enforced by consensus, and inserting a legacy transaction adds a zero `wtxid` that the commitment must now account for.
@JoseSK999 JoseSK999 force-pushed the block-too-big-test branch from 20eb38b to 877ddf0 Compare March 10, 2026 20:25
@JoseSK999
Copy link
Copy Markdown
Member Author

Clippy didn't like the childish seed, fixed

@JoseSK999 JoseSK999 added this to the Q2/2026 milestone Mar 19, 2026
@csgui csgui added reliability Related to runtime reliability, stability and production readiness testing & validation and removed chore Cleaning, refactoring, reducing complexity code quality Generally improves code readability and maintainability reliability Related to runtime reliability, stability and production readiness labels Mar 20, 2026
let mut rng = OsRng;
/// Modifies a block to have a different output script (txdata is tampered with).
fn mutate_block(block: &mut Block) {
let mut rng = StdRng::seed_from_u64(0x_bebe_cafe);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sagaz! ☕

/// Modifies historical block at height 866,342 by adding one extra transaction so that the
/// updated block weight is 4,000,001 WUs. The block merkle roots are updated accordingly.
fn build_oversized_866_342() -> Block {
let mut block = decode_block("./testdata/block_866342/raw.zst");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider remove hard coded file location to avoid test failures on other machines.

Copy link
Copy Markdown
Collaborator

@csgui csgui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments. Loved the 0xbebe_cafe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants