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

Refactor: Move transaction testing utilities from crates/chain/tests/common to testenv crate #1612

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

tvpeter
Copy link
Contributor

@tvpeter tvpeter commented Sep 14, 2024

Description

This change moves all transaction testing utilities (macros and functions) from crates/chain/tests/common to crates/testenv/tx_utils to minimize duplication.

Closes #1602

Notes to the reviewers

Changelog notice

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@evanlinjin
Copy link
Member

Hey @tvpeter thank you for taking this on! Please follow the [https://www.conventionalcommits.org/en/v1.0.0/](Conventional Commit Specification) for the commit messages. I'll review this properly soon!

@tvpeter
Copy link
Contributor Author

tvpeter commented Sep 15, 2024

@evanlinjin thank you.
I have reworded the commit messages to follow the specification.

@notmandatory notmandatory added the new feature New feature or request label Sep 15, 2024
@notmandatory notmandatory added this to the 1.0.0-beta milestone Sep 15, 2024
@@ -18,6 +18,7 @@ workspace = true
[dependencies]
bdk_chain = { path = "../chain", version = "0.19.0", default-features = false }
electrsd = { version = "0.28.0", features = [ "bitcoind_25_0", "esplora_a33e97e1", "legacy" ] }
bitcoin = { version = "0.32.0", default-features = false }
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello! Since bdk_chain already re-exports bitcoin, we don't need to add bitcoin as a dependency here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thank you

Copy link
Contributor

@oleonardolima oleonardolima 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 only wonder about naming/structure, maybe reword it to only utils.rs or have both chain_utils.rs and tx_utils.rs, but I'm not sure about it.

@tvpeter
Copy link
Contributor Author

tvpeter commented Sep 16, 2024

Alright @oleonardolima. I initially thought that it will contain transaction related utilities only but on a second thought most of the utilities are not just for transactions. So I will rename it to just utils.rs and move the descriptors sample array there too. Thank you.

@evanlinjin
Copy link
Member

Please use the conventional commits format for commit messages.

Looking at this now, maybe we should create a bdk_testutil crate for this instead? These are light macros, functions. Whereas bdk_testenv requires downloading binaries during the build process.

cc. @ValuedMammal @notmandatory

@ValuedMammal
Copy link
Contributor

Looking at this now, maybe we should create a bdk_testutil crate for this instead? These are light macros, functions. Whereas bdk_testenv requires downloading binaries during the build process.

Another option that comes to mind is to have conditional compilation of testenv with something like a "full" feature flag, but I don't have a strong opinion on it at the moment.

@LagginTimes
Copy link
Contributor

Looking at this now, maybe we should create a bdk_testutil crate for this instead? These are light macros, functions. Whereas bdk_testenv requires downloading binaries during the build process.

Another option that comes to mind is to have conditional compilation of testenv with something like a "full" feature flag, but I don't have a strong opinion on it at the moment.

I think conditional compilation sounds like the better option here vs. having an additional crate to manage, since both crates would be primarily focused on testing.

@ValuedMammal
Copy link
Contributor

ValuedMammal commented Sep 30, 2024

Looking at this now, maybe we should create a bdk_testutil crate for this instead? These are light macros, functions. Whereas bdk_testenv requires downloading binaries during the build process.

Another option that comes to mind is to have conditional compilation of testenv with something like a "full" feature flag, but I don't have a strong opinion on it at the moment.

I think conditional compilation sounds like the better option here vs. having an additional crate to manage, since both crates would be primarily focused on testing.

Considering the test utils are common to other crates and change infrequently, it might be just as easy to move them to a test_util module in bdk_core

Edit: As discussed on dev call the current direction on the PR makes sense

@LagginTimes
Copy link
Contributor

Nit: I think squashing the commits down to one would make the history a bit cleaner.

@tvpeter
Copy link
Contributor Author

tvpeter commented Oct 1, 2024

Nit: I think squashing the commits down to one would make the history a bit cleaner.

Done

Copy link
Contributor

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

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

I did another review and left some comments. Overall, it's looking good!

I think you still need to change the commit message to lowercase.

@@ -9,6 +9,7 @@ use bdk_chain::{
},
BlockId,
};
use bdk_testenv::{chain_update, h, local_chain};
Copy link
Contributor

Choose a reason for hiding this comment

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

As we are importing this now from bdk_testenv, and deleted the macros from commond/mod.rs I guess we can remove the code at L16-17.

#[macro_use]
mod common;

Comment on lines 14 to 20
#[allow(unused_macros)]
#[macro_export]
macro_rules! h {
($index:literal) => {{
bitcoin::hashes::Hash::hash($index.as_bytes())
}};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC we discussed during the call that this could have a meaningful name, I would use hash for instance.

@oleonardolima
Copy link
Contributor

You also probably need to rebase on top of the master, so the CI issues are fixed (in case you are curious, check: #1636).

@tvpeter
Copy link
Contributor Author

tvpeter commented Oct 3, 2024

I did another review and left some comments. Overall, it's looking good!

I think you still need to change the commit message to lowercase.

Done. Thank you

Comment on lines 24 to 30
macro_rules! local_chain {
[ $(($height:expr, $block_hash:expr)), * ] => {{
#[allow(unused_mut)]
bdk_chain::local_chain::LocalChain::from_blocks([$(($height, $block_hash).into()),*].into_iter().collect())
.expect("chain must have genesis block")
}};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I suggest renaming this expression to hash for consistency with the other macros.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The previous h! macro has been renamed to hash!, so it will conflict. Since it builds a LocalChain, is it better to rename it new_local_chain?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops! Just to clarify: I meant to change block_hash to hash, not rename the local_chain macro. Apologies for the confusion!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

- add `utils` mod to testenv crate to keep all
utilities moved from `crates/chain/test/common`
- replace all test tx macros in `chain` crate with
import from `testenv` crate
    - tests/test_indexed_tx_graph
    - tests/test_keychain_txout_index
    - tests/test_local_chain
    - tests/test_tx_graph
    - tests/test_tx_graph_conflicts
- deleted all moved macros and functions in
`crates/chain/test/common/mod`
- replace `bitcoin` external crate with
`bd_chain::bitcoin`
- move `DESCRIPTORS` sample array to `utils` for
other crates to use
- rename h! macro to hash! for clarity
- rename localchain! macro parameter from
'block_hash' to 'hash'

[Ticket: bitcoindevkit#1602]
Copy link
Contributor

@ValuedMammal ValuedMammal left a comment

Choose a reason for hiding this comment

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

ACK 88a8403

Copy link
Member

@notmandatory notmandatory left a comment

Choose a reason for hiding this comment

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

ACK 88a8403

Thanks for taking this one on.

@notmandatory notmandatory merged commit ad59970 into bitcoindevkit:master Oct 14, 2024
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Move stuff from crates/chain/tests/common to bdk_testenv and make them publicly-accessible
6 participants