mod args;
use std::io::Read;
use color_eyre::eyre::Result;
use serde_json::Value;
use structopt::StructOpt;
use zebra_chain::serialization::{DateTime32, ZcashSerialize};
use zebra_rpc::{
client::{BlockTemplateResponse, LONG_POLL_ID_LENGTH},
proposal_block_from_template,
};
use zebra_utils::init_tracing;
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)?);
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()?;
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: BlockTemplateResponse = serde_json::from_value(template)?;
let proposal = proposal_block_from_template(&template, time_source, &args.net)?;
eprintln!("{proposal:#?}");
let proposal = proposal.zcash_serialize_to_vec()?;
println!("{}", hex::encode(proposal));
Ok(())
}