From 2386ce3b8ac2962c2261e5360e2de5ee8e511d38 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Fri, 14 Dec 2018 00:46:41 +0000 Subject: [PATCH] Complete genesis replacement --- core/src/genesis.rs | 30 ++++++++---- core/src/global.rs | 2 + etc/gen_gen/Cargo.toml | 4 ++ etc/gen_gen/src/bin/gen_gen.rs | 83 +++++++++++++++++++++++++++------- 4 files changed, 95 insertions(+), 24 deletions(-) diff --git a/core/src/genesis.rs b/core/src/genesis.rs index 98ed3aa97..1e90df646 100644 --- a/core/src/genesis.rs +++ b/core/src/genesis.rs @@ -19,6 +19,8 @@ use chrono::prelude::{TimeZone, Utc}; use crate::core; use crate::global; use crate::pow::{Difficulty, Proof, ProofOfWork}; +use crate::util::secp::Signature; +use crate::util::secp::pedersen::{Commitment, RangeProof}; use crate::core::hash::Hash; use crate::keychain::BlindingFactor; @@ -136,14 +138,14 @@ pub fn genesis_testnet4() -> core::Block { /// Placeholder for mainnet genesis block, will definitely change before /// release so no use trying to pre-mine it. pub fn genesis_main() -> core::Block { - core::Block::with_header(core::BlockHeader { + let gen = core::Block::with_header(core::BlockHeader { height: 0, timestamp: Utc.ymd(2019, 1, 15).and_hms(12, 0, 0), // REPLACE - prev_root: Hash::default(), // REPLACE - output_root: Hash::default(), // REPLACE - range_proof_root: Hash::default(), // REPLACE - kernel_root: Hash::default(), // REPLACE - total_kernel_offset: BlindingFactor::zero(), // REPLACE + prev_root: Hash::default(), // REPLACE + output_root: Hash::default(), // REPLACE + range_proof_root: Hash::default(), // REPLACE + kernel_root: Hash::default(), // REPLACE + total_kernel_offset: BlindingFactor::zero(), // REPLACE output_mmr_size: 1, kernel_mmr_size: 1, pow: ProofOfWork { @@ -156,10 +158,22 @@ pub fn genesis_main() -> core::Block { }, }, ..Default::default() - }) + }); + let kernel = core::TxKernel { + features: core::KernelFeatures::COINBASE_KERNEL, + fee: 0, + lock_height: 0, + excess: Commitment::from_vec(vec![]), // REPLACE + excess_sig: Signature::from_raw_data(&[0; 64]).unwrap(), //REPLACE + }; + let output = core::Output { + features: core::OutputFeatures::COINBASE_OUTPUT, + commit: Commitment::from_vec(vec![]), // REPLACE + proof: RangeProof::zero(), // REPLACE + }; + gen.with_reward(output, kernel) } - #[cfg(test)] mod test { use super::*; diff --git a/core/src/global.rs b/core/src/global.rs index 30a38374d..9384d1d75 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -311,6 +311,8 @@ pub fn get_genesis_nonce() -> u64 { ChainTypes::AutomatedTesting => 0, // Magic nonce for current genesis block at cuckatoo15 ChainTypes::UserTesting => 27944, + // Placeholder, obviously not the right value + ChainTypes::Mainnet => 0, // Magic nonce for genesis block for testnet2 (cuckatoo29) _ => panic!("Pre-set"), } diff --git a/etc/gen_gen/Cargo.toml b/etc/gen_gen/Cargo.toml index a985f6488..f1d394592 100644 --- a/etc/gen_gen/Cargo.toml +++ b/etc/gen_gen/Cargo.toml @@ -24,3 +24,7 @@ grin_miner_plugin = "0.4.2" grin_store = "0.4.2" grin_util = "0.4.2" serde_json = "1" + +[patch.crates-io] +grin_core = { path = "../../core" } +grin_keychain = { path = "../../keychain" } diff --git a/etc/gen_gen/src/bin/gen_gen.rs b/etc/gen_gen/src/bin/gen_gen.rs index 60f0f0e9e..7e4dea486 100644 --- a/etc/gen_gen/src/bin/gen_gen.rs +++ b/etc/gen_gen/src/bin/gen_gen.rs @@ -14,12 +14,12 @@ //! Main for building the genesis generation utility. -use std::{fs, io, path, process}; use std::io::{BufRead, Write}; use std::sync::Arc; +use std::{fs, io, path, process}; use chrono::prelude::Utc; -use chrono::{Duration, Timelike, Datelike}; +use chrono::{Datelike, Duration, Timelike}; use curl; use serde_json; @@ -43,10 +43,16 @@ static PLUGIN_PATH: &str = "cuckaroo_mean_cuda_29.cuckooplugin"; fn main() { core::global::set_mining_mode(core::global::ChainTypes::Mainnet); if !path::Path::new(GENESIS_RS_PATH).exists() { - panic!("File {} not found, make sure you're running this from the gen_gen directory", GENESIS_RS_PATH); + panic!( + "File {} not found, make sure you're running this from the gen_gen directory", + GENESIS_RS_PATH + ); } if !path::Path::new(PLUGIN_PATH).exists() { - panic!("File {} not found, make sure you're running this from the gen_gen directory", PLUGIN_PATH); + panic!( + "File {} not found, make sure you're running this from the gen_gen directory", + PLUGIN_PATH + ); } // get the latest bitcoin hash @@ -61,12 +67,10 @@ fn main() { } println!("Using bitcoin block hash {}", h1); - // build the basic parts of the genesis block header, perhaps some of this - // can be moved to core + // build the basic parts of the genesis block header let mut gen = core::genesis::genesis_main(); gen.header.timestamp = Utc::now() + Duration::minutes(30); gen.header.prev_root = core::core::hash::Hash::from_hex(&h1).unwrap(); - println!("Built genesis:\n{:?}", gen); // TODO get the proper keychain and/or raw coinbase let keychain = ExtKeychain::from_random_seed().unwrap(); @@ -100,7 +104,7 @@ fn main() { nonce += 1; } - // set the PoW solution and make sure the block is mostly valid + // // set the PoW solution and make sure the block is mostly valid gen.header.pow.nonce = solver_sols.sols[0].nonce as u64; gen.header.pow.proof.nonces = solver_sols.sols[0].to_u64s(); assert!(gen.header.pow.is_secondary(), "Not a secondary header"); @@ -111,16 +115,17 @@ fn main() { ) .unwrap(); + println!("Built genesis:\n{:?}", gen); println!("Final genesis hash: {}", gen.hash().to_hex()); - // TODO check again the bitcoin block to make sure it's not been orphaned - update_genesis_rs(&gen); - println!("genesis.rs has been updated, check it and press c+enter to proceed."); + println!("genesis.rs has been updated, check it and run mainnet_genesis_hash test"); + println!("also check bitcoin block {} hasn't been orphaned.", h1); + println!("press c+enter to proceed."); let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); if input != "c" { - return + return; } // Commit genesis block info in git and tag @@ -140,8 +145,6 @@ fn main() { } fn update_genesis_rs(gen: &core::core::Block) { - // TODO coinbase output and kernel - // set the replacement patterns let mut replacements = vec![]; replacements.push(( @@ -156,20 +159,68 @@ fn update_genesis_rs(gen: &core::core::Block) { gen.header.timestamp.time().second(), ), )); + replacements.push(( + "prev_root".to_string(), + format!("Hash::from_hex(\"{}\")", gen.header.prev_root.to_hex()), + )); replacements.push(( "output_root".to_string(), format!("Hash::from_hex(\"{}\")", gen.header.output_root.to_hex()), )); replacements.push(( "range_proof_root".to_string(), - format!("Hash::from_hex(\"{}\")", gen.header.range_proof_root.to_hex()), + format!( + "Hash::from_hex(\"{}\")", + gen.header.range_proof_root.to_hex() + ), )); replacements.push(( "kernel_root".to_string(), format!("Hash::from_hex(\"{}\")", gen.header.kernel_root.to_hex()), )); + replacements.push(( + "total_kernel_offset".to_string(), + format!("BlindingFactor::from_hex(\"{}\")", gen.header.total_kernel_offset.to_hex()), + )); + replacements.push(( + "nonce".to_string(), + format!("{}", gen.header.pow.nonce), + )); + replacements.push(( + "nonces".to_string(), + format!("{:x?}", gen.header.pow.proof.nonces), + )); + replacements.push(( + "excess".to_string(), + format!( + "Commitment::from_vec(util::from_hex(\"{:x?}\"))", + util::to_hex(gen.kernels()[0].excess.0.to_vec()) + ), + )); + replacements.push(( + "excess_sig".to_string(), + format!( + "Signature::from_raw_data(&util::from_hex(\"{:x?}\"))", + util::to_hex(gen.kernels()[0].excess_sig.to_raw_data().to_vec()) + ), + )); + replacements.push(( + "commit".to_string(), + format!( + "Commitment::from_vec(util::from_hex(\"{:x?}\"))", + util::to_hex(gen.outputs()[0].commitment().0.to_vec()) + ), + )); + replacements.push(( + "proof".to_string(), + format!( + "RangeProof::from_vec(util::from_hex(\"{:x?}\"))", + util::to_hex(gen.outputs()[0].proof.bytes().to_vec()) + ), + )); - // check each possible replacement in the file + // check each possible replacement in the file, remove the replacement from + // the list when found to avoid double replacements let mut replaced = String::new(); { let genesis_rs = fs::File::open(GENESIS_RS_PATH).unwrap();