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. |
| /// submitBlock with a template block should be rejected (unsolved high-hash). | ||
| #[tokio::test] | ||
| #[serial_test::serial] | ||
| async fn mining_submit_block() { |
There was a problem hiding this comment.
IIUC this test is asserting an error case (block without solution), so IMO the test name should express that in a more explicit way
| /// submitBlock with a template block should be rejected (unsolved high-hash). | |
| #[tokio::test] | |
| #[serial_test::serial] | |
| async fn mining_submit_block() { | |
| /// submitBlock with a template block should be rejected (unsolved high-hash). | |
| #[tokio::test] | |
| #[serial_test::serial] | |
| async fn mining_submit_block_unresolved() { |
moreover, I'd also add a mining_submit_block_resolved() counterpart, asserting the successful case
There was a problem hiding this comment.
We now cover both cases.
| #[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 |
This adds
submitBlock()which is introduced by bitcoin/bitcoin#34644.This PR also cleans up existing tests a bit.
Best reviewed per commit.