Skip to content

Conversation

dr-orlovsky
Copy link
Contributor

After my work on new bitcoin crate hash type system last year rust-bitcoin/rust-bitcoin#349 I find many project depending on the upstream bitcoin crate being unable to update to the latest version without spending a lot of effort on type replacements (plus some additional code editing in cases like merklization). Due to my work on adding support to Signet (which was implemented in rust-bitcoin fork containing the new type system) I had to do this work, so I propose it for your consideration as a PR here. Pls review it before significant changes to the master, since it will take many hours of my time again to do any rebase, which I do not have.

New hash type system from bitcoin crate does not cover all cases for merkle values; and the same function may be applied to different hash types. Thus, I have used generic types to abstract the logic.

Txid merklization operates with TxMerkleNode type; BlockHash merklization does not introduce a new type: the same design decision was made in the original work on new bitcoin crate type system since it is used in a single case, and there is no point in introducing a special designated hash type.

"Script hashes" met through the code are in fact has nothing to do with P2SH script hashes and ScripHash type; but instead are custom pubkeyScript hashes used in RPC queries. This data structures are left of Sha256dHash type since there is no corresponding type defined in bitcoin crate; this type is specific to Electrum X protocol. Corresponding new type can be implemented in the project later with hash_newtype! macro in the same way it was done in bitcoin crate.

@dr-orlovsky dr-orlovsky mentioned this pull request Apr 19, 2020
All types are replaced; only work on merkle types left,
which requires addition of the code
This commits contains fixes to merklization function arguments
and return types. New hash type system from bitcoin crate does
not cover all cases for merkle values; and the same function
may be applied to different hash types. Thus, I have used generic
types to abstract the logic.

`Txid` merklization operates with TxMerkleNode type;
`BlockHash` merklization does not introduce a new type: the same
design decision was made in the original work on new bitcoin
crate type system since it is used in a single case, and there is
no point in introducing a special designated hash type.

Script hashes (used in RPC queries) are left of Sha256dHash type
since there is no corresponding type defined in bitcoin crate;
this type is specific to Electrum X protocol. Corresponding
new type can be implemented in the project later with `hash_newtype!`
macro in the same way it was done in bitcoin crate.
Copy link
Contributor

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Looks good to me.

let tx_nodes: Vec<TxMerkleNode> = txids
.into_iter()
.map(|txid| TxMerkleNode::from_inner(txid.into_inner()))
.collect();
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like this allocation, but removing it is not trivial. I might look at it later and see if I can optimize it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I think it will be nice to have direction conversion functions for such cases which are optimized by performance – and make them part of the hash type system in rust-bitcoin

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking more in terms of replacing vecs with iterators, but not really sure how applicable it is in this case. I managed to refactor those in the past, so I might try again. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that have to be a PR to rust-bitcoin though...

@romanz romanz self-assigned this Apr 21, 2020
@UkolovaOlga UkolovaOlga mentioned this pull request Apr 22, 2020
34 tasks
@romanz romanz merged commit 2133120 into romanz:master Apr 24, 2020
@romanz
Copy link
Owner

romanz commented Apr 24, 2020

Many thanks for the contribution, much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants