block_template_to_proposal/
main.rsuse std::io::Read;
use color_eyre::eyre::Result;
use serde_json::Value;
use structopt::StructOpt;
use zebra_chain::{
parameters::NetworkUpgrade,
serialization::{DateTime32, ZcashSerialize},
};
use zebra_rpc::methods::get_block_template_rpcs::{
get_block_template::proposal_block_from_template,
types::{get_block_template::GetBlockTemplate, long_poll::LONG_POLL_ID_LENGTH},
};
use zebra_utils::init_tracing;
mod args;
const MIN_TEMPLATE_BYTES: usize = 500;
#[allow(clippy::print_stdout, clippy::print_stderr)]
fn main() -> Result<()> {
init_tracing();
color_eyre::install()?;
let args = args::Args::from_args();
let time_source = args.time_source;
let template = args.template.unwrap_or_else(|| {
let mut template = String::new();
let bytes_read = std::io::stdin().read_to_string(&mut template).expect("missing JSON block template: must be supplied on command-line or standard input");
if bytes_read < MIN_TEMPLATE_BYTES {
panic!("JSON block template is too small: expected at least {MIN_TEMPLATE_BYTES} characters");
}
template
});
let mut template: Value = serde_json::from_str(&template)?;
eprintln!(
"{}",
serde_json::to_string_pretty(&template).expect("re-serialization never fails")
);
let template_obj = template
.as_object_mut()
.expect("template must be a JSON object");
template_obj["longpollid"] = "0".repeat(LONG_POLL_ID_LENGTH).into();
template_obj["coinbasetxn"]["required"] = true.into();
for tx in template_obj["transactions"]
.as_array_mut()
.expect("transactions must be a JSON array")
{
tx["required"] = false.into();
}
let current_time: DateTime32 = template_obj["curtime"]
.to_string()
.parse()
.expect("curtime is always a valid DateTime32");
template_obj.entry("maxtime").or_insert_with(|| {
if time_source.uses_max_time() {
eprintln!("maxtime field is missing, using curtime for maxtime: {current_time:?}");
}
current_time.timestamp().into()
});
let template: GetBlockTemplate = serde_json::from_value(template)?;
let proposal = proposal_block_from_template(&template, time_source, NetworkUpgrade::Nu5)?;
eprintln!("{proposal:#?}");
let proposal = proposal
.zcash_serialize_to_vec()
.expect("serialization to Vec never fails");
println!("{}", hex::encode(proposal));
Ok(())
}