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

Derive within spending key alphanet v5 compat pr #317

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"benchmark_result": {
"clock_cycle_count": 6637,
"hash_table_height": 2618,
"u32_table_height": 826,
"u32_table_height": 586,
"op_stack_table_height": 4914,
"ram_table_height": 3804
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"benchmark_result": {
"clock_cycle_count": 1392,
"hash_table_height": 700,
"u32_table_height": 226,
"u32_table_height": 286,
"op_stack_table_height": 1021,
"ram_table_height": 765
},
Expand All @@ -15,7 +15,7 @@
"benchmark_result": {
"clock_cycle_count": 13236,
"hash_table_height": 5056,
"u32_table_height": 1126,
"u32_table_height": 1186,
"op_stack_table_height": 9805,
"ram_table_height": 7605
},
Expand Down
26 changes: 16 additions & 10 deletions src/bin/dashboard_src/address_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crossterm::event::KeyEventKind;
use itertools::Itertools;
use neptune_cash::config_models::network::Network;
use neptune_cash::models::state::wallet::address::KeyType;
use neptune_cash::models::state::wallet::address::SpendingKey;
use neptune_cash::models::state::wallet::address::SpendingKeyRange;
use neptune_cash::rpc_server::RPCClient;
use ratatui::layout::Constraint;
use ratatui::layout::Margin;
Expand All @@ -34,7 +34,7 @@ use unicode_width::UnicodeWidthStr;
use super::dashboard_app::DashboardEvent;
use super::screen::Screen;

type AddressUpdate = SpendingKey;
type AddressUpdate = SpendingKeyRange;
type AddressUpdateArc = Arc<std::sync::Mutex<Vec<AddressUpdate>>>;
type DashboardEventArc = Arc<std::sync::Mutex<Option<DashboardEvent>>>;
type JoinHandleArc = Arc<Mutex<JoinHandle<()>>>;
Expand Down Expand Up @@ -260,19 +260,25 @@ impl Widget for AddressScreen {
let selected_style = style.add_modifier(Modifier::REVERSED);
let header = vec!["type", "address (abbreviated)"];

// derive all the known keys and generate data matrix.
//
// todo: only derive and render the keys that will actually be displayed.
// eg if we have 5000 known keys and 10 are displayed at a time, we should
// only derive those 10 keys.
let matrix = self
.data
.lock()
.unwrap()
.iter()
.rev()
.map(|key| {
vec![
KeyType::from(key).to_string(),
key.to_address()
.to_display_bech32m_abbreviated(self.network)
.unwrap(),
]
.flat_map(|known_keys| {
known_keys.iter().map(|key| {
vec![
KeyType::from(&key).to_string(),
key.to_address()
.to_display_bech32m_abbreviated(self.network)
.unwrap(),
]
})
})
.collect_vec();
let ncols = header.len();
Expand Down
4 changes: 3 additions & 1 deletion src/models/blockchain/block/block_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ mod test {
use tracing_test::traced_test;

use super::*;
use crate::config_models::network::Network;
use crate::models::blockchain::block::Block;
use crate::models::blockchain::block::TARGET_BLOCK_INTERVAL;
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
Expand Down Expand Up @@ -159,6 +160,7 @@ mod test {

#[test]
fn asymptotic_limit_is_42_million() {
let network = Network::Main;
let generation_0_subsidy = Block::block_subsidy(BlockHeight::genesis().next());

// Genesis block does not contain block subsidy so it must be subtracted
Expand All @@ -182,7 +184,7 @@ mod test {
assert!(relative_premine < 0.0198, "Premine may not exceed promise");

// Designated premine is less than or equal to allocation
let actual_premine = Block::premine_distribution()
let actual_premine = Block::premine_distribution(network)
.iter()
.map(|(_receiving_address, amount)| *amount)
.sum::<NeptuneCoins>();
Expand Down
66 changes: 47 additions & 19 deletions src/models/blockchain/block/mod.rs

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions src/models/blockchain/transaction/lock_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,15 @@ mod test {
use super::*;
use crate::models::blockchain::transaction::primitive_witness::PrimitiveWitness;
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
use crate::models::state::wallet::address::DerivationIndex;

#[proptest]
fn lock_script_halts_gracefully_prop(
#[strategy(arb::<Digest>())] txk_mast_hash: Digest,
#[strategy(arb::<Digest>())] seed: Digest,
#[strategy(arb::<(XFieldElement, DerivationIndex)>())] seed: (
XFieldElement,
DerivationIndex,
),
#[strategy(NeptuneCoins::arbitrary_non_negative())] amount: NeptuneCoins,
) {
let (_utxos, lock_scripts_and_witnesses) =
Expand All @@ -224,11 +228,16 @@ mod test {
#[test]
fn lock_script_halts_gracefully_unit() {
let txk_mast_hash = Digest::default();
let seed = Digest::default();
let secret = XFieldElement::zero();
let index = DerivationIndex::default();
Digest::default();
let amount = NeptuneCoins::zero();

let (_utxos, lock_scripts_and_witnesses) =
PrimitiveWitness::transaction_inputs_from_address_seeds_and_amounts(&[seed], &[amount]);
PrimitiveWitness::transaction_inputs_from_address_seeds_and_amounts(
&[(secret, index)],
&[amount],
);
assert!(lock_scripts_and_witnesses.into_iter().all(|lsaw| lsaw
.halts_gracefully(PublicInput::new(txk_mast_hash.reversed().values().to_vec()))));
}
Expand Down
65 changes: 39 additions & 26 deletions src/models/blockchain/transaction/primitive_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ pub mod neptune_arbitrary {
use crate::models::blockchain::block::MINING_REWARD_TIME_LOCK_PERIOD;
use crate::models::blockchain::type_scripts::native_currency::NativeCurrencyWitness;
use crate::models::blockchain::type_scripts::neptune_coins::NeptuneCoins;
use crate::models::blockchain::type_scripts::time_lock::TimeLock;
use crate::models::blockchain::type_scripts::time_lock::TimeLockWitness;
use crate::models::blockchain::type_scripts::TypeScriptWitness;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::state::wallet::address::generation_address;
use crate::models::state::wallet::address::DerivationIndex;
use crate::util_types::mutator_set::commit;
use crate::util_types::mutator_set::msa_and_records::MsaAndRecords;

Expand Down Expand Up @@ -484,9 +484,9 @@ pub mod neptune_arbitrary {
// - timestamp
(
NeptuneCoins::arbitrary_non_negative(),
vec(arb::<Digest>(), num_inputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_inputs),
vec(arb::<u64>(), num_inputs),
vec(arb::<Digest>(), num_outputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_outputs),
vec(arb::<u64>(), num_outputs),
vec(arb::<PublicAnnouncement>(), num_public_announcements),
arb::<u64>(),
Expand Down Expand Up @@ -801,13 +801,15 @@ pub mod neptune_arbitrary {

// this is only used by arbitrary-impls
pub(crate) fn transaction_inputs_from_address_seeds_and_amounts(
address_seeds: &[Digest],
address_seeds: &[(XFieldElement, DerivationIndex)],
input_amounts: &[NeptuneCoins],
) -> (Vec<Utxo>, Vec<LockScriptAndWitness>) {
let input_spending_keys = address_seeds
.iter()
.map(|address_seed| {
generation_address::GenerationSpendingKey::derive_from_seed(*address_seed)
.map(|(secret, index)| {
generation_address::GenerationSpendingKey::from_secret_and_index(
*secret, *index,
)
})
.collect_vec();

Expand All @@ -834,7 +836,7 @@ pub mod neptune_arbitrary {
/// amounts and fee and mutates these values until they satisfy the no-inflation
/// requirement. This method assumes that the total input amount and coinbase (if
/// set) can be safely added.
pub(crate) fn find_balanced_output_amounts_and_fee(
pub fn find_balanced_output_amounts_and_fee(
total_input_amount: NeptuneCoins,
coinbase: Option<NeptuneCoins>,
output_amounts_suggestion: &mut [NeptuneCoins],
Expand All @@ -849,15 +851,15 @@ pub mod neptune_arbitrary {
"Amount balancer only accepts non-negative fee suggestions. Got:\n{fee_suggestion}"
);
assert!(
!total_input_amount.is_negative(),
"Amount balancer only accepts non-negative total input amount. Got:\n{total_input_amount}"
);
!total_input_amount.is_negative(),
"Amount balancer only accepts non-negative total input amount. Got:\n{total_input_amount}"
);
assert!(
output_amounts_suggestion
.iter()
.all(|input_amount_sugg| !input_amount_sugg.is_negative()),
"Amount balancer only accepts non-negative output amount suggestsions. Got:\n\n{output_amounts_suggestion:?}"
);
output_amounts_suggestion
.iter()
.all(|input_amount_sugg| !input_amount_sugg.is_negative()),
"Amount balancer only accepts non-negative output amount suggestsions. Got:\n\n{output_amounts_suggestion:?}"
);
let mut total_output_amount = output_amounts_suggestion
.iter()
.cloned()
Expand Down Expand Up @@ -889,31 +891,38 @@ pub mod neptune_arbitrary {
/// Generate valid output UTXOs from the amounts and seeds for the
/// addresses. If some release date is supplied, generate twice as many
/// UTXOs such that half the total amount is time-locked.
pub(crate) fn valid_tx_outputs_from_amounts_and_address_seeds(
pub fn valid_tx_outputs_from_amounts_and_address_seeds(
output_amounts: &[NeptuneCoins],
address_seeds: &[Digest],
address_seeds: &[(XFieldElement, DerivationIndex)],
timelock_until: Option<Timestamp>,
) -> Vec<Utxo> {
use crate::models::blockchain::type_scripts::time_lock::TimeLock;
use crate::models::state::wallet::address::generation_address;

address_seeds
.iter()
.zip(output_amounts)
.flat_map(|(seed, amount)| {
.flat_map(|((secret, index), amount)| {
let mut amount = *amount;
if timelock_until.is_some() {
amount.div_two();
}
let liquid_utxo = Utxo::new(
generation_address::GenerationSpendingKey::derive_from_seed(*seed)
.to_address()
.lock_script(),
generation_address::GenerationSpendingKey::from_secret_and_index(
*secret, *index,
)
.to_address()
.lock_script(),
amount.to_native_coins(),
);
let mut utxos = vec![liquid_utxo];
if let Some(release_date) = timelock_until {
let timelocked_utxo = Utxo::new(
generation_address::GenerationSpendingKey::derive_from_seed(*seed)
.to_address()
.lock_script(),
generation_address::GenerationSpendingKey::from_secret_and_index(
*secret, *index,
)
.to_address()
.lock_script(),
[
amount.to_native_coins(),
vec![TimeLock::until(release_date)],
Expand Down Expand Up @@ -957,6 +966,7 @@ mod test {
use crate::models::proof_abstractions::mast_hash::MastHash;
use crate::models::proof_abstractions::tasm::program::ConsensusProgram;
use crate::models::proof_abstractions::timestamp::Timestamp;
use crate::models::state::wallet::address::DerivationIndex;
use crate::util_types::mutator_set::commit;
use crate::util_types::mutator_set::msa_and_records::MsaAndRecords;
use crate::util_types::mutator_set::removal_record::RemovalRecord;
Expand Down Expand Up @@ -1021,6 +1031,9 @@ mod test {
assert!(index < N);
}

let nested_vec_strategy_seeds = |counts: [usize; N]| {
counts.map(|count| vec(arb::<(XFieldElement, DerivationIndex)>(), count))
};
let nested_vec_strategy_digests =
|counts: [usize; N]| counts.map(|count| vec(arb::<Digest>(), count));
let nested_vec_strategy_pubann =
Expand All @@ -1038,7 +1051,7 @@ mod test {
(
(
nested_vec_strategy_amounts(input_counts),
nested_vec_strategy_digests(input_counts),
nested_vec_strategy_seeds(input_counts),
nested_vec_strategy_utxos(output_counts),
nested_vec_strategy_pubann(announcement_counts),
vec(NeptuneCoins::arbitrary_non_negative(), N),
Expand Down Expand Up @@ -1450,7 +1463,7 @@ mod test {

(
total_amount_strategy,
arb::<Digest>(),
arb::<(XFieldElement, DerivationIndex)>(),
vec(arb::<Digest>(), num_outputs),
arb::<Timestamp>(),
NeptuneCoins::arbitrary_non_negative(),
Expand Down
2 changes: 1 addition & 1 deletion src/models/blockchain/type_scripts/neptune_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl Display for NeptuneCoins {

#[cfg(any(test, feature = "arbitrary-impls"))]
pub mod neptune_arbitrary {
use ::arbitrary::Arbitrary;
use arbitrary::Arbitrary;
use proptest::prelude::BoxedStrategy;
use proptest::prelude::Strategy;
use proptest_arbitrary_interop::arb;
Expand Down
9 changes: 5 additions & 4 deletions src/models/blockchain/type_scripts/time_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ pub mod neptune_arbitrary {
use super::*;
use crate::models::blockchain::transaction::transaction_kernel::TransactionKernelModifier;
use crate::models::blockchain::transaction::PublicAnnouncement;
use crate::models::state::wallet::address::DerivationIndex;

impl Arbitrary for TimeLockWitness {
/// Parameters are:
Expand All @@ -784,9 +785,9 @@ pub mod neptune_arbitrary {
parameters;
let num_inputs = release_dates.len();
(
vec(arb::<Digest>(), num_inputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_inputs),
vec(NeptuneCoins::arbitrary_non_negative(), num_inputs),
vec(arb::<Digest>(), num_outputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_outputs),
vec(NeptuneCoins::arbitrary_non_negative(), num_outputs),
vec(arb::<PublicAnnouncement>(), num_public_announcements),
NeptuneCoins::arbitrary_coinbase(),
Expand Down Expand Up @@ -931,9 +932,9 @@ pub mod neptune_arbitrary {
) -> BoxedStrategy<PrimitiveWitness> {
(
NeptuneCoins::arbitrary_non_negative(),
vec(arb::<Digest>(), num_inputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_inputs),
vec(arb::<u64>(), num_inputs),
vec(arb::<Digest>(), num_outputs),
vec(arb::<(XFieldElement, DerivationIndex)>(), num_outputs),
vec(arb::<u64>(), num_outputs),
vec(arb::<PublicAnnouncement>(), num_announcements),
arb::<u64>(),
Expand Down
11 changes: 7 additions & 4 deletions src/models/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,6 @@ mod global_state_tests {
use rand::Rng;
use rand::SeedableRng;
use tracing_test::traced_test;
use wallet::address::generation_address::GenerationReceivingAddress;
use wallet::address::generation_address::GenerationSpendingKey;
use wallet::address::KeyType;
use wallet::WalletSecret;
Expand Down Expand Up @@ -2942,6 +2941,7 @@ mod global_state_tests {
use super::*;
use crate::mine_loop::composer_parameters::ComposerParameters;
use crate::mine_loop::create_block_transaction_stateless;
use crate::models::state::wallet::address::KeyTypeSeed;

/// test scenario: onchain/symmetric.
/// pass outcome: no funds loss
Expand Down Expand Up @@ -3077,7 +3077,11 @@ mod global_state_tests {
};

// in alice wallet: send pre-mined funds to bob
let an_address = GenerationReceivingAddress::derive_from_seed(rng.gen());
let an_address = SpendingKey::from_seed(KeyTypeSeed::Generation {
secret: rng.gen(),
index: rng.gen(),
})
.to_address();
let block_1 = {
let vm_job_queue = alice_state_lock.vm_job_queue().clone();
let mut alice_state_mut = alice_state_lock.lock_guard_mut().await;
Expand Down Expand Up @@ -3132,8 +3136,7 @@ mod global_state_tests {
.await;

// the block gets mined.
let composer_parameters =
ComposerParameters::new(an_address.into(), rng.gen(), 0.5);
let composer_parameters = ComposerParameters::new(an_address, rng.gen(), 0.5);
let (block_1_tx, _) = create_block_transaction_stateless(
&genesis_block,
composer_parameters,
Expand Down
Loading
Loading