Repository navigation
Add submitBlock(), misc. test improvements - #12
Conversation
6ed8511 to
ca979c4
Compare
|
Rebased after #9. |
b407807 to
fa5ca24
Compare
|
Rebased after #14. |
fa5ca24 to
fa7c2fa
Compare
|
bitcoin/bitcoin#34644 landed in master and should be in v32. Ready for review. |
| #[serial_test::serial] | ||
| async fn mining_submit_block() { | ||
| with_mining_client(|_client, thread, mining| async move { | ||
| let template = make_block_template(&mining, &thread).await; |
There was a problem hiding this comment.
IIUC this will generate a regtest block, for which the blockhash will be essentially pseudorandom
at regtest genesis difficulty, any random sha256d hash has 50% chance of passing the PoW diff target
so this test will fail non-determinstically for reasons that have nothing to do with the correctness of the underlying submitBlock implementation that we're trying to validate
There was a problem hiding this comment.
This is now deterministic.
| let _reason = results.get_reason().unwrap(); | ||
| let _debug = results.get_debug().unwrap(); |
There was a problem hiding this comment.
Shouldn't this be asserted instead of unwrap or maybe we can remove it?
There was a problem hiding this comment.
I've already refactored some things offline, but will take a look if it still applies...
There was a problem hiding this comment.
Found a similar pattern in mining_check_block_and_interrupt and a bunch of other places, so I added commits to fix those too.
fa7c2fa to
6d2f7e4
Compare
|
@plebhash thanks for looking at the test. In this project tests are not supposed to be thorough end-to-end integration tests, so I hadn't really looked the vibed initial version. But they also shouldn't be unstable. And they should probably cover several expected responses. I added In anticipation of possible change in how I cleaned up the existing The
Best reviewed per commit. |
| assert!( | ||
| template | ||
| .interrupt_wait_request() | ||
| .send() | ||
| .promise | ||
| .await | ||
| .is_ok(), | ||
| "interruptWait should not fail" | ||
| ); |
There was a problem hiding this comment.
We can remove this assert
There was a problem hiding this comment.
Here and in similar examples I dropped the outer assert! and instead use .expect.
| assert!( | ||
| mining.interrupt_request().send().promise.await.is_ok(), | ||
| "interrupt should not fail" | ||
| ); |
There was a problem hiding this comment.
We can remove this assert
| fn header_has_valid_pow(header: &BlockHeader) -> bool { | ||
| Target::from_compact(header.bits).is_met_by(header.block_hash()) | ||
| } |
There was a problem hiding this comment.
We can remove this helper.
| #[path = "util/miner.rs"] | ||
| mod miner_util; |
There was a problem hiding this comment.
We don't really use path attribute that often, can we please correct the imports and have indirections via the util module
There was a problem hiding this comment.
I add a commit to clean that up for pre-existing code. And changed later commits to conform.
| async fn template_block( | ||
| template: &mining_capnp::block_template::Client, | ||
| thread: &thread::Client, | ||
| ) -> Vec<u8> { | ||
| transform_template_block(template, thread, |block| block.to_vec()).await | ||
| } | ||
|
|
||
| async fn transform_template_block<F>( | ||
| template: &mining_capnp::block_template::Client, | ||
| thread: &thread::Client, | ||
| build_block: F, | ||
| ) -> Vec<u8> | ||
| where | ||
| F: FnOnce(&[u8]) -> Vec<u8>, | ||
| { | ||
| let mut get_block_req = template.get_block_request(); | ||
| get_block_req | ||
| .get() | ||
| .get_context() | ||
| .unwrap() | ||
| .set_thread(thread.clone()); | ||
| let get_block_resp = get_block_req.send().promise.await.unwrap(); | ||
| build_block(get_block_resp.get().unwrap().get_result().unwrap()) | ||
| } | ||
|
|
||
| async fn submit_solution( | ||
| template: &mining_capnp::block_template::Client, | ||
| thread: &thread::Client, | ||
| version: u32, | ||
| timestamp: u32, | ||
| nonce: u32, | ||
| coinbase: &[u8], | ||
| ) -> bool { | ||
| let mut req = template.submit_solution_request(); | ||
| req.get().get_context().unwrap().set_thread(thread.clone()); |
There was a problem hiding this comment.
lets remove these, makes code hard to understand and keep them inline
There was a problem hiding this comment.
I added these helpers to make the tests themselves less verbose, to make it easier to see what different things are being tested.
But after playing around with this a bit, it's not really that helpful. I inlined them again.
To make things slightly less ugly, I replaced the repetitive req.get() with a local variable and use a local scope to group the assignments.
I also added a helper to get a block from a block template.
This removes explicit path attributes for the pre-existing test util modules and lets tests/test.rs import them through tests/util/mod.rs instead. Review comment: 2140-dev#12 (comment)
6d2f7e4 to
43a69ca
Compare
|
Applied @shourya742's suggestions. I also renamed I still find constructing request quite ugly: let mut req = template.submit_solution_request();
{
let mut params = req.get();
params.set_version(solution.version);
params.set_timestamp(solution.timestamp);
params.set_nonce(solution.nonce);
params.set_coinbase(&solution.coinbase);
params.get_context().unwrap().set_thread(thread.clone());
}
let resp = req.send().In c++ jargon: why can't we pass a struct into |
bit-aloo
left a comment
There was a problem hiding this comment.
ACK. Two nits, we can resolve them later as well.
| req.get().get_context().unwrap().set_thread(thread.clone()); | ||
| let resp = req.send().promise.await.unwrap(); | ||
| let _ibd: bool = resp.get().unwrap().get_result(); | ||
| let _ibd: bool = resp |
| Ok(resp) => { | ||
| let results = resp.get().unwrap(); | ||
| let results = resp.get().expect("checkBlock response should decode"); | ||
| let _valid: bool = results.get_result(); |
Yes, |
43a69ca to
8e398bc
Compare
|
Done the nits. |
|
Anything left to do here or should I just merge this? cc @shourya742 @plebhash? |
This reverts commit 8e398bc from 2140-dev#12. submitBlock() is not part of Bitcoin Core 31.x (it is new in 32.x), so it should not be part of the 0.2.x line. It ended up here because the 31.x branch was fast-forwarded to master after 2140-dev#12 was merged. The other commits in 2140-dev#12 are test improvements that also apply to 31.x, so they are kept.
This adds
submitBlock()which is introduced by bitcoin/bitcoin#34644.This PR also cleans up existing tests a bit.
Best reviewed per commit.