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 BIP152 (Compact Blocks) structures #249

Closed
wants to merge 4 commits into from

Conversation

stevenroose
Copy link
Collaborator

@stevenroose stevenroose commented Mar 24, 2019

Adds the basic structures for BIP152 and a method to create a compact block from a full block.

Depends on rust-bitcoin/bitcoin_hashes#46.

@stevenroose stevenroose changed the title Implement BIP152 (Compact Blocks) structures Add BIP152 (Compact Blocks) structures Mar 24, 2019
Cargo.toml Outdated Show resolved Hide resolved
src/util/bip152.rs Outdated Show resolved Hide resolved
@stevenroose
Copy link
Collaborator Author

So I understand we don't want a siphasher dependency, but rather a bitcoin_hashes::siphash impl?

@tamasblummer
Copy link
Contributor

yes, siphash is also used in BIP158. It would be better included into bitcoin_hashes

src/util/bip152.rs Outdated Show resolved Hide resolved
src/util/bip152.rs Outdated Show resolved Hide resolved
@stevenroose
Copy link
Collaborator Author

yes, siphash is also used in BIP158. It would be better included into bitcoin_hashes

@tamasblummer
I added a siphash24 module in bitcoin_hashes: rust-bitcoin/bitcoin_hashes#46

Could you check if that suits your need for BIP 158 usage? I'm actually very interested in BIP-158 implementation in rust-bitcoin. Are you planning to do them?

@tamasblummer
Copy link
Contributor

@stevenroose will review tomorrow. I already implemented and using BIP158 in murmel: https://github.com/rust-bitcoin/murmel/blob/master/src/blockfilter.rs

@stevenroose stevenroose changed the title Add BIP152 (Compact Blocks) structures WIP: Add BIP152 (Compact Blocks) structures May 17, 2019
@stevenroose stevenroose changed the title WIP: Add BIP152 (Compact Blocks) structures BLOCKED: Add BIP152 (Compact Blocks) structures May 17, 2019
@stevenroose
Copy link
Collaborator Author

I already implemented and using BIP158 in murmel: https://github.com/rust-bitcoin/murmel/blob/master/src/blockfilter.rs

@tamasblummer would you mind upstreaming that to rust-bitcoin? Into src/util/bip185.rs f.e.?

@stevenroose stevenroose force-pushed the compact-blocks branch 2 times, most recently from 3a12d50 to 50717b1 Compare May 30, 2019 10:52
@stevenroose
Copy link
Collaborator Author

@tamasblummer I made 2 changes:

  • HeaderAndShortIds::from_block now takes a version number instead of the use_wtxid bool. It returns an error if the version is not 1 or 2. This is probably better for backwards compatibility when we add support for other versions.
  • HeaderAndShortIds::from_block now takes an additional &[bool] argument to indicate which transactions to prefill. It must be of length one less than the nb of txs in the block because the coinbase is always prefilled. This is quite rudimentary support for arbitrary fillings. I thought about a closure argument Fn(&sha256d::Hash) -> bool. One could pass a HashSet::contains or HashMap::contains_key into that argument, f.e., as well as a custom method. Would that be a better solution? Thought? @apoelstra @dongcarl

@stevenroose stevenroose changed the title BLOCKED: Add BIP152 (Compact Blocks) structures Add BIP152 (Compact Blocks) structures Jul 26, 2019
@stevenroose
Copy link
Collaborator Author

This is rebased and ready to be reviewed. Uses the new bitcoin_hashes siphash impl.

tamasblummer
tamasblummer previously approved these changes Jul 26, 2019
tamasblummer
tamasblummer previously approved these changes Aug 7, 2019
@stevenroose
Copy link
Collaborator Author

rebased

@stevenroose
Copy link
Collaborator Author

@dr-orlovsky Addressed all your derives, had to derive PartialOrd, Ord, Hash on BlockHeader (which tbh I think makes more sense than on most of the compact block types), I did that in a separate commit.

Copy link
Contributor

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

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

ack 1d519df

Copy link
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

Concept ACK. I am not knowledgeable in compact filters to do a code vs standard review though :(

// strip witness for version 1
let mut no_witness = tx.clone();
no_witness.input.iter_mut().for_each(|i| i.witness.clear());
no_witness
Copy link
Member

Choose a reason for hiding this comment

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

I know this PR is a million years old, but can you provide a citation/explanation for this witness-stripping behavior? I don't see it in Core or in BIP 152 or 158.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's been ages indeed, but it says that txs inside v1 should be like responses to MSG_TX and v2 should be like responses to MSG_WITNESS_TX:
https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#specification-for-version-2

@apoelstra
Copy link
Member

Done review of 1d519df

ACK except for the thing about witness stripping.

I think this PR beats Carl's PSBT PR for "longest open PR" :)

@stevenroose
Copy link
Collaborator Author

Rebased once again! :)

}
}

impl hex::FromHex for ShortId {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should use impl_bytes_newtype to get this and the impls below?


/// Short transaction IDs are used to represent a transaction without sending a full 256-bit hash.
#[derive(PartialEq, Eq, Clone, Copy, Hash, Default, PartialOrd, Ord)]
pub struct ShortId(pub [u8; 6]);
Copy link
Member

Choose a reason for hiding this comment

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

Should we have an AsRef implementation for ShortId as well?

@0xB10C
Copy link
Contributor

0xB10C commented Apr 4, 2022

Nice, was looking for the BIP152 network messages in rust-bitcoin and was about to implement them myself. Will test against Bitcoin Core's network messages and review!

/// The blockhash of the block which the transactions being requested are in.
pub block_hash: BlockHash,
/// The indexes of the transactions being requested in the block.
pub indexes: Vec<u64>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess using u64 is fine here though we'll probably not even going to get close to u32::MAX transactions in a block too.

@tcharding
Copy link
Member

Hey mate, this PR is currently marked as part of the 0.29 milestone, would you like to get this functionality in before the 0.29 release?

@0xB10C
Copy link
Contributor

0xB10C commented Jun 29, 2022

I'm interested in seeing this in 0.29. I've rebased this branch on current master: https://github.com/0xB10C/rust-bitcoin/tree/compact-blocks - maybe this helps speed things up. I've been using 68760f5 to deserialize Bitcoin Core's compact block P2P messages for more than a month now without problems.

@tcharding
Copy link
Member

Legend @0xB10C, thanks. Perhaps if @stevenroose does not pick this up in a week or so you could create a PR and just mention that its Steven's work and it supersedes this one [0]. I can do it if you either don't have time or don't feel comfortable doing so.

I'm totally speaking for @stevenroose here, Steven you can go right ahead and slap me if I'm out of place :)

[0]: When doing so I typically add a co-authored-by tag to any commit if I make any non-trivial changes.

Comment on lines +319 to +325
for _ in 0..nb_indexes {
let differential: VarInt = Decodable::consensus_decode(&mut d)?;
last_index = match last_index.checked_add(differential.0 + 1) {
Some(r) => r,
None => return Err(encode::Error::ParseFailed("block index overflow")),
};
indexes.push(last_index);
Copy link
Contributor

@0xB10C 0xB10C Jul 6, 2022

Choose a reason for hiding this comment

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

for reference: While adding a deserialize(serialize(BlockTransactionsRequest{..})) test I noticed a subtle off-by-one error here. The increment in L321 (differential.0 + 1) is too early. This should only be incremented after we indexes.push(last_index);. Otherwise a differentially encoded [0, 1, 0] would become [1, 3, 4] when it should be [0, 2, 3].

Fixed this in #1088.

@tcharding
Copy link
Member

Superseded by #1088

I've messaged @stevenroose via text also to make sure he is aware of this.

@tcharding tcharding closed this Jul 18, 2022
apoelstra added a commit that referenced this pull request Jul 25, 2022
ee29910 BIP152: Test net msg ser/der and diff encoding (0xb10c)
cd1aaaf BIP152: Add Compact Block unit test based on Elements (Steven Roose)
d4d92a8 BIP152: Add Compact Blocks network messages (Steven Roose)
f2fcdc8 BIP152: Add basic Compact Block structures (Steven Roose)
a9a39c4 blockdata: Derive PartialOrd, Ord and Hash for BlockHeader (Steven Roose)

Pull request description:

  > Adds the basic structures for BIP152 and a method to create a compact block from a full block.

  This is a rebase of #249 by stevenroose (see #249 (comment)) with a milestone for 29.0. I've added deserialization and serialization tests for the network messages and fixed an off-by-one bug in the deserialization of the differentially encoded varints in the `getblocktxn` message (see #249 (comment)).

  Closes #249.

ACKs for top commit:
  tcharding:
    ACK ee29910
  apoelstra:
    ACK ee29910

Tree-SHA512: 462a91576281f5a2ffdc2610769ea93970b60dac75a150c827966c48daec7cf93f526f9f202e7ba1dbb1410b49148579880260a3c3df298b98330c0d891a4cca
ChallengeDev210 pushed a commit to ChallengeDev210/rust-bitcoin that referenced this pull request Aug 1, 2022
…uctures

ee29910 BIP152: Test net msg ser/der and diff encoding (0xb10c)
cd1aaaf BIP152: Add Compact Block unit test based on Elements (Steven Roose)
d4d92a8 BIP152: Add Compact Blocks network messages (Steven Roose)
f2fcdc8 BIP152: Add basic Compact Block structures (Steven Roose)
a9a39c4 blockdata: Derive PartialOrd, Ord and Hash for BlockHeader (Steven Roose)

Pull request description:

  > Adds the basic structures for BIP152 and a method to create a compact block from a full block.

  This is a rebase of #249 by stevenroose (see rust-bitcoin/rust-bitcoin#249 (comment)) with a milestone for 29.0. I've added deserialization and serialization tests for the network messages and fixed an off-by-one bug in the deserialization of the differentially encoded varints in the `getblocktxn` message (see rust-bitcoin/rust-bitcoin#249 (comment)).

  Closes #249.

ACKs for top commit:
  tcharding:
    ACK ee29910
  apoelstra:
    ACK ee29910

Tree-SHA512: 462a91576281f5a2ffdc2610769ea93970b60dac75a150c827966c48daec7cf93f526f9f202e7ba1dbb1410b49148579880260a3c3df298b98330c0d891a4cca
@Kixunil Kixunil removed this from the 0.29.0 milestone Aug 1, 2022
Davidson-Souza pushed a commit to Davidson-Souza/rust-bitcoin that referenced this pull request Jul 12, 2023
b3503ba Add example to SharedSecret (Tobin Harding)

Pull request description:

  Currently the rustdoc on `SharedSecret` is wildly incorrect (possibly a cut'n'pasta error).

  Fix the rustdoc for `SharedSecret` and add an examples section to assist testing the public API.

  Fixes: rust-bitcoin#249

ACKs for top commit:
  apoelstra:
    ACK b3503ba

Tree-SHA512: 650092388099bb415c11ea335ca6b64c90094f1a51ceecc403911316ee62da0279488af6fa66e00ee5269c129f06d4641085f8ab9be91c98d24a7a4449d235c2
yancyribbens pushed a commit to yancyribbens/rust-bitcoin that referenced this pull request Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API break This PR requires a version bump for the next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet