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

Fix foundation fork proof #4

Merged
merged 7 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ pub const MAX_BLOCK_WEIGHT: usize = 40_000;
pub const MAINNET_FIRST_HARD_FORK: u64 = 700000;

/// Floonet first hard fork height
pub const FLOONET_FIRST_HARD_FORK: u64 = 2880;
pub const FLOONET_FIRST_HARD_FORK: u64 = 10080;

/// AutomatedTesting and UserTesting first hard fork height.
pub const TESTING_FIRST_HARD_FORK: u64 = 200;
pub const TESTING_FIRST_HARD_FORK: u64 = 6;

/// Get the height of the first epic hard fork
pub fn first_fork_height() -> u64 {
Expand Down
23 changes: 3 additions & 20 deletions core/src/core/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub struct CbData {
}

/// Size in bytes of each foundation coinbase (Output + Kernel)
pub const FOUNDATION_COINBASE_SIZE_1: usize = 1803;
/// Size in bytes of each foundation coinbase (Output + Kernel) after the first hardfork
pub const FOUNDATION_COINBASE_SIZE_2: usize = 1775;
pub const FOUNDATION_COINBASE_SIZE_1: usize = 1775;

// TODO-FOUNDATION : Create a function to verify if the file exists if the height is different form 0 in the CLI

Expand Down Expand Up @@ -58,27 +56,12 @@ pub fn save_in_disk(serialization: String, path: &Path) {

fn get_foundation_tx_version_size(version: HeaderVersion) -> usize {
match version {
HeaderVersion(6) => FOUNDATION_COINBASE_SIZE_1,
HeaderVersion(7) => FOUNDATION_COINBASE_SIZE_2,
HeaderVersion(_) => panic!("YOU NEED TO UPDATE YOUR NODE!"),
HeaderVersion(_) => FOUNDATION_COINBASE_SIZE_1,
}
}

fn get_foundation_tx_offset(index: u64, version: HeaderVersion) -> u64 {
let size = match version {
HeaderVersion(6) => index * (FOUNDATION_COINBASE_SIZE_1 as u64),
HeaderVersion(7) => {
let fork_height = first_fork_height();
let fork_index = foundation_index(fork_height);
let new_index = index.saturating_sub(fork_index);

let size_1 = fork_index * (FOUNDATION_COINBASE_SIZE_1 as u64);
let size_2 = new_index * (FOUNDATION_COINBASE_SIZE_2 as u64);

size_1 + size_2
}
HeaderVersion(_) => panic!("YOU NEED TO UPDATE YOUR NODE!"),
};
let size = index * (FOUNDATION_COINBASE_SIZE_1 as u64);

if cfg!(windows) {
size + index
Expand Down
2 changes: 1 addition & 1 deletion core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub const CURRENT_HEADER_VERSION: u16 = 7;

#[cfg(target_family = "unix")]
pub const MAINNET_FOUNDATION_JSON_SHA256: &str =
"2613717d04128587b8c3fa24db873b2c4b33232cffe1849b4b5e1ff6d39cd12d";
"5a3a7584127dd31fba18eaeff1c551bfaa74b4e50e537a1e1904fe6730b17f5c";
#[cfg(target_family = "windows")]
pub const MAINNET_FOUNDATION_JSON_SHA256: &str =
"4d01ca4134959d19ae1b76058c8d11040b63bd1bd112401b80b36185e7faf94a";
Expand Down
33 changes: 31 additions & 2 deletions core/src/libtx/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

//! Builds the blinded output and related signature proof for the block
//! reward.
use crate::consensus::{cumulative_reward_foundation, reward};
use crate::consensus::{cumulative_reward_foundation, header_version, reward};
use crate::core::block::HeaderVersion;
use crate::core::{KernelFeatures, Output, OutputFeatures, TxKernel};
use crate::libtx::error::Error;
use crate::libtx::{
aggsig,
proof::{self, ProofBuild},
proof::{self, LegacyProofBuilder, ProofBuild, ProofBuilder},
};
use keychain::{Identifier, Keychain, SwitchCommitmentType};
use util::{secp, static_secp_instance};
Expand Down Expand Up @@ -146,3 +147,31 @@ where
};
Ok((output, proof))
}

pub fn output_foundation_proof<K>(
keychain: &K,
key_id: &Identifier,
test_mode: bool,
height: u64,
) -> Result<(Output, TxKernel), Error>
where
K: Keychain,
{
match header_version(height) {
HeaderVersion(6) => output_foundation(
keychain,
&LegacyProofBuilder::new(keychain),
&key_id,
test_mode,
height,
),
HeaderVersion(7) => output_foundation(
keychain,
&ProofBuilder::new(keychain),
&key_id,
test_mode,
height,
),
_ => panic!("Proof version not found!"),
}
}
6,080 changes: 3,040 additions & 3,040 deletions debian/foundation.json

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions features/mine_chain.feature
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
Feature: Mine a simple chain

Scenario: test
Given I have a <testing> chain
Given I setup a chain
Given I generate new foundation's transactions
Scenario: Test if the foundation's transaction is being spend after of fork
Given I have the policy <0> with <cuckaroo> equals <0>
And I have the policy <0> with <randomx> equals <0>
And I have the policy <0> with <cuckatoo> equals <100>
And I setup all the policies
And I have a <testing> chain
And I setup a chain
And I add foundation wallet pubkeys
And I generate new foundation's transactions
And I make <10> blocks
Then I spend the foundation's transaction on the height <5>

Scenario: test if the foundation_floonet.json is valid
Given I have a <floonet> chain
Expand Down
15 changes: 8 additions & 7 deletions src/bin/epic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ fn real_main() -> i32 {
.get_matches();
let node_config;

let chain_type = if args.is_present("floonet") {
global::ChainTypes::Floonet
} else if args.is_present("usernet") {
global::ChainTypes::UserTesting
} else {
global::ChainTypes::Mainnet
};
let chain_type = global::ChainTypes::Floonet;
// let chain_type = if args.is_present("floonet") {
// global::ChainTypes::Floonet
// } else if args.is_present("usernet") {
// global::ChainTypes::UserTesting
// } else {
// global::ChainTypes::Mainnet
// };
if let ("taxes", Some(taxes_args)) = args.subcommand() {
global::set_mining_mode(chain_type.clone());
let generate: u64 = taxes_args
Expand Down
1,000 changes: 500 additions & 500 deletions tests/assets/foundation.json

Large diffs are not rendered by default.

46 changes: 39 additions & 7 deletions tests/cucumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,15 +1226,13 @@ mod mine_chain {
};

given "I generate new foundation's transactions" |world, step| {
let kc = world.keychain.as_ref().unwrap();
let kc = world.keychain_foundation.as_ref().unwrap();
let mut foundations = vec![];

for x in (0..100) {
if (x != 0 && x % consensus::foundation_height() == 0) {
let key_id = epic_keychain::ExtKeychain::derive_key_id(1, x as u32, 0, 0, 0);
let (output, kernel) = libtx::reward::output_foundation(
kc, &ProofBuilder::new(kc), &key_id, true, x).unwrap();

let (output, kernel) = libtx::reward::output_foundation_proof(kc, &key_id, true, x).unwrap();
foundations.push(
foundation::CbData{
output,
Expand All @@ -1249,6 +1247,42 @@ mod mine_chain {
foundation::save_in_disk(serialized, &Path::new("./tests/assets"));
global::set_foundation_path("./tests/assets/foundation/foundation.json".to_string());
};

then regex "I spend the foundation's transaction on the height <([0-9]+)>" |world, matches, step| {
let kc = world.keychain.as_ref().unwrap();
let kc_foundation = world.keychain_foundation.as_ref().unwrap();
let chain = world.chain.as_ref().unwrap();
let prev_header = chain.head_header().unwrap();
let height: u64 = matches[1].parse().unwrap();

let keyid_dest = epic_keychain::ExtKeychainPath::new(1, 2, 0, 0, 0).to_identifier();
let cbdata_foundation = load_foundation_output(height);

let pb = ProofBuilder::new(kc_foundation);
let foundation_reward = consensus::add_reward_foundation(height);

let tx1 = build::transaction(
KernelFeatures::Plain { fee: 20000 },
vec![
build::coinbase_input(foundation_reward, cbdata_foundation.key_id.unwrap()),
build::output(foundation_reward - 20000, keyid_dest.clone()),
],
kc_foundation,
&pb,
)
.unwrap();

let block = prepare_block_tx(kc, &prev_header, &chain, 7, vec![&tx1]);
chain.process_block(block, chain::Options::SKIP_POW).unwrap();

assert!(chain
.is_unspent(&OutputIdentifier::from_output(&cbdata_foundation.output))
.is_err());

assert!(chain
.is_unspent(&OutputIdentifier::from_output(&tx1.outputs()[0]))
.is_ok());
};
});

fn difficulty_to_timespan(algo: &FType, difficulty: &Difficulty, height: u64) -> u64 {
Expand Down Expand Up @@ -1797,7 +1831,7 @@ mod mine_chain {
// Declares a before handler function named `a_before_fn`
before!(a_before_fn => |_scenario| {
println!("Test 1");

global::set_foundation_path("./tests/assets/foundation.json".to_string());
});

// Declares an after handler function named `an_after_fn`
Expand Down Expand Up @@ -1843,8 +1877,6 @@ fn setup() {
});

util::init_test_logger();

global::set_foundation_path("./tests/assets/foundation.json".to_string());
}

cucumber! {
Expand Down