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

feature(rpc): add fixed values to getblocktemplate response #5558

Merged
merged 10 commits into from
Nov 8, 2022
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion zebra-chain/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use commitment::{
ChainHistoryBlockTxAuthCommitmentHash, ChainHistoryMmrRootHash, Commitment, CommitmentError,
};
pub use hash::Hash;
pub use header::{BlockTimeError, CountedHeader, Header};
pub use header::{BlockTimeError, CountedHeader, Header, ZCASH_BLOCK_VERSION};
pub use height::Height;
pub use serialize::{SerializedBlock, MAX_BLOCK_BYTES};

Expand Down
6 changes: 6 additions & 0 deletions zebra-chain/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ const BLOCK_HEADER_LENGTH: usize =
/// A CountedHeader has BLOCK_HEADER_LENGTH bytes + 1 or more bytes for the transaction count
pub(crate) const MIN_COUNTED_HEADER_LEN: usize = BLOCK_HEADER_LENGTH + 1;

/// The Zcash accepted block version.
///
/// The consensus rules do not force the block version to be this value but just equal or greater than it.
/// However, it is suggested that submitted block versions to be of this exact value.
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
pub const ZCASH_BLOCK_VERSION: u32 = 4;

impl TrustedPreallocate for CountedHeader {
fn max_allocation() -> u64 {
// Every vector type requires a length field of at least one byte for de/serialization.
Expand Down
4 changes: 2 additions & 2 deletions zebra-chain/src/block/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
work::{difficulty::CompactDifficulty, equihash},
};

use super::{merkle, Block, CountedHeader, Hash, Header};
use super::{header::ZCASH_BLOCK_VERSION, merkle, Block, CountedHeader, Hash, Header};

/// The maximum size of a Zcash block, in bytes.
///
Expand Down Expand Up @@ -77,7 +77,7 @@ impl ZcashDeserialize for Header {
// > The block version number MUST be greater than or equal to 4.
//
// https://zips.z.cash/protocol/protocol.pdf#blockheader
if version < 4 {
if version < ZCASH_BLOCK_VERSION {
return Err(SerializationError::Parse("version must be at least 4"));
}

Expand Down
2 changes: 1 addition & 1 deletion zebra-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub mod chain;
#[allow(missing_docs)]
pub mod error;

pub use block::VerifyBlockError;
pub use block::{VerifyBlockError, MAX_BLOCK_SIGOPS};
pub use chain::VerifyChainError;
pub use checkpoint::{
CheckpointList, VerifyCheckpointError, MAX_CHECKPOINT_BYTE_COUNT, MAX_CHECKPOINT_HEIGHT_GAP,
Expand Down
1 change: 1 addition & 0 deletions zebra-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tracing = "0.1.37"
tracing-futures = "0.2.5"

hex = { version = "0.4.3", features = ["serde"] }
lazy_static = "1.4.0"
serde = { version = "1.0.147", features = ["serde_derive"] }

proptest = { version = "0.10.1", optional = true }
Expand Down
23 changes: 16 additions & 7 deletions zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use tower::{buffer::Buffer, Service, ServiceExt};

use zebra_chain::{
amount::Amount,
block::Height,
block::{self, Block},
block::{Height, MAX_BLOCK_BYTES, ZCASH_BLOCK_VERSION},
chain_tip::ChainTip,
serialization::ZcashDeserializeInto,
};
use zebra_consensus::{BlockError, VerifyBlockError, VerifyChainError, VerifyCheckpointError};
use zebra_consensus::{
BlockError, VerifyBlockError, VerifyChainError, VerifyCheckpointError, MAX_BLOCK_SIGOPS,
};
use zebra_node_services::mempool;

use crate::methods::{
Expand All @@ -27,6 +29,7 @@ use crate::methods::{
};

pub mod config;
pub mod constants;
pub(crate) mod types;

/// getblocktemplate RPC method signatures.
Expand Down Expand Up @@ -284,7 +287,8 @@ where
Ok(GetBlockTemplate {
capabilities: vec![],

version: 0,
// The block version
version: ZCASH_BLOCK_VERSION,

previous_block_hash: GetBlockHash([0; 32].into()),
block_commitments_hash: [0; 32].into(),
Expand Down Expand Up @@ -324,12 +328,17 @@ where

min_time: 0,

mutable: vec![],
// Hardcoded list of block fields the miner is allowed to change.
mutable: constants::GET_BLOCK_TEMPLATE_MUTABLE_FIELD.to_vec(),

// A range of valid nonces that goes from `u32::MIN` to `u32::MAX`.
nonce_range: constants::GET_BLOCK_TEMPLATE_NONCE_RANGE_FIELD.to_string(),

nonce_range: empty_string.clone(),
// Max legacy signature operations in the block.
sigop_limit: MAX_BLOCK_SIGOPS,

sigop_limit: 0,
size_limit: 0,
// Max block size in bytes
size_limit: MAX_BLOCK_BYTES,
teor2345 marked this conversation as resolved.
Show resolved Hide resolved

cur_time: 0,

Expand Down
15 changes: 15 additions & 0 deletions zebra-rpc/src/methods/get_block_template_rpcs/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Constant values used in mining rpcs methods.

use lazy_static::lazy_static;

/// A range of valid nonces that goes from `u32::MIN` to `u32::MAX` as a string.
pub const GET_BLOCK_TEMPLATE_NONCE_RANGE_FIELD: &str = "00000000ffffffff";

lazy_static! {
/// A hardcoded list of fields that the miner can change from the block.
pub static ref GET_BLOCK_TEMPLATE_MUTABLE_FIELD: Vec<String> = vec![
"time".to_string(),
"transactions".to_string(),
"prevblock".to_string()
];
}
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
assertion_line: 128
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
expression: block_template
---
{
"capabilities": [],
"version": 0,
"version": 4,
"previousblockhash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockcommitmentshash": "0000000000000000000000000000000000000000000000000000000000000000",
"lightclientroothash": "0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -27,10 +28,14 @@ expression: block_template
},
"target": "",
"mintime": 0,
"mutable": [],
"noncerange": "",
"sigoplimit": 0,
"sizelimit": 0,
"mutable": [
"time",
"transactions",
"prevblock"
],
"noncerange": "00000000ffffffff",
"sigoplimit": 20000,
"sizelimit": 2000000,
"curtime": 0,
"bits": "",
"height": 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
assertion_line: 128
expression: block_template
---
{
"capabilities": [],
"version": 0,
"version": 4,
"previousblockhash": "0000000000000000000000000000000000000000000000000000000000000000",
"blockcommitmentshash": "0000000000000000000000000000000000000000000000000000000000000000",
"lightclientroothash": "0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -27,10 +28,14 @@ expression: block_template
},
"target": "",
"mintime": 0,
"mutable": [],
"noncerange": "",
"sigoplimit": 0,
"sizelimit": 0,
"mutable": [
"time",
"transactions",
"prevblock"
],
"noncerange": "00000000ffffffff",
"sigoplimit": 20000,
"sizelimit": 2000000,
"curtime": 0,
"bits": "",
"height": 0
Expand Down
22 changes: 17 additions & 5 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,12 @@ async fn rpc_getblockhash() {
#[cfg(feature = "getblocktemplate-rpcs")]
#[tokio::test(flavor = "multi_thread")]
async fn rpc_getblocktemplate() {
use crate::methods::get_block_template_rpcs::constants::{
GET_BLOCK_TEMPLATE_MUTABLE_FIELD, GET_BLOCK_TEMPLATE_NONCE_RANGE_FIELD,
};
use zebra_chain::block::{MAX_BLOCK_BYTES, ZCASH_BLOCK_VERSION};
use zebra_consensus::MAX_BLOCK_SIGOPS;

let _init_guard = zebra_test::init();

// Create a continuous chain of mainnet blocks from genesis
Expand Down Expand Up @@ -824,14 +830,20 @@ async fn rpc_getblocktemplate() {
.expect("unexpected error in getblocktemplate RPC call");

assert!(get_block_template.capabilities.is_empty());
assert_eq!(get_block_template.version, 0);
assert_eq!(get_block_template.version, ZCASH_BLOCK_VERSION);
assert!(get_block_template.transactions.is_empty());
assert!(get_block_template.target.is_empty());
assert_eq!(get_block_template.min_time, 0);
assert!(get_block_template.mutable.is_empty());
assert!(get_block_template.nonce_range.is_empty());
assert_eq!(get_block_template.sigop_limit, 0);
assert_eq!(get_block_template.size_limit, 0);
assert_eq!(
get_block_template.mutable,
GET_BLOCK_TEMPLATE_MUTABLE_FIELD.to_vec()
);
assert_eq!(
get_block_template.nonce_range,
GET_BLOCK_TEMPLATE_NONCE_RANGE_FIELD
);
assert_eq!(get_block_template.sigop_limit, MAX_BLOCK_SIGOPS);
assert_eq!(get_block_template.size_limit, MAX_BLOCK_BYTES);
assert_eq!(get_block_template.cur_time, 0);
assert!(get_block_template.bits.is_empty());
assert_eq!(get_block_template.height, 0);
Expand Down