Skip to content

Conversation

@deadmanoz
Copy link
Contributor

As per raised in #429, addressing type discrepancies between corepc-types and Bitcoin Core.

Bitcoin Core has always returned networkhashps as a double since getmininginfo was introduced. The previous type (i64) causes deserialisation to fail. Here's a minimal example:

use corepc_client::client_sync::v28::Client;
use corepc_client::client_sync::Auth;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let auth = Auth::UserPass("bitcoin".to_string(), "bitcoin".to_string());
    let client = Client::new_with_auth("http://127.0.0.1:8332", auth)?;
    let info = client.get_mining_info()?;
    println!("networkhashps: {}", info.network_hash_ps);
    Ok(())
}
Error: JsonRpc(Json(Error("invalid type: floating point `1.055282156351532e+21`, expected i64", line: 1, column: 179)))

This change:

  • Changes the networkhashps field type from i64 to f64
  • Modifies existing integration test to verify correct deserialisation (mines some blocks to yield a networkhashps > 0.0) and remove redundant comments

Bitcoin Core returns networkhashps as a double. The previous type (i64)
causes deserialisation to fail.
Copy link
Collaborator

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

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

ACK f01c4a9

@tcharding
Copy link
Member

Took me a minute to work out why the original test did not catch this bug. And the reason seems to be that when networkhashps is zero, Core returns 0 and serde so is willing to deserialize 0 as either a float or an int.

Adding node.fund_wallet(); to the original test causes the test to fail as expected.

// v29 added fields (bits, target) that can fail to parse.
#[cfg(not(feature = "v28_and_below"))]
{
let model: Result<mtype::GetMiningInfo, GetMiningInfoError> = json.into_model();
Copy link
Member

Choose a reason for hiding this comment

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

We want to keep this line with the explicit Result type. The reason we do this is to ensure that we exported all the error types correctly. In the past before we did this we forgot a bunch of them.

Copy link
Member

Choose a reason for hiding this comment

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

FTR most of the 'weird' stuff here is for a specific reason, not always obvious. Feel free to ask about anything that looks odd. Also if anything is non-uniform them we probably made a mistake - it happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted, thanks for the info


assert!(model.network_hash_ps > 0.0);
}

Copy link
Member

Choose a reason for hiding this comment

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

Do what you think is best but to me it looks like the only real changes needed to this test are adding the wallet funding and adding the assertion on network_hash_ps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants