Skip to content

Commit

Permalink
Merge pull request #2386 from AleoHQ/update/requirements
Browse files Browse the repository at this point in the history
Update Staker requirements and `BatchHeader::MAX_CERTIFICATES`
  • Loading branch information
howardwu committed Mar 16, 2024
2 parents c0441b2 + 06a9d63 commit 01c8b09
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 43 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions ledger/committee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use ledger_narwhal_batch_header::BatchHeader;
use std::collections::HashSet;

/// The minimum amount of stake required for a validator to bond.
pub const MIN_VALIDATOR_STAKE: u64 = 1_000_000_000_000u64; // microcredits
pub const MIN_VALIDATOR_STAKE: u64 = 10_000_000_000_000u64; // microcredits
/// The minimum amount of stake required for a delegator to bond.
pub const MIN_DELEGATOR_STAKE: u64 = 10_000_000u64; // microcredits
pub const MIN_DELEGATOR_STAKE: u64 = 10_000_000_000u64; // microcredits
/// The maximum number of delegators.
pub const MAX_DELEGATORS: u32 = 100_000u32;

Expand Down Expand Up @@ -408,7 +408,7 @@ mod tests {
// Set the number of rounds.
const NUM_ROUNDS: u64 = 256 * 2_000;
// Sample the number of members.
let num_members = rng.gen_range(3..50);
let num_members = rng.gen_range(3..=Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE);
// Sample a committee.
let committee = crate::test_helpers::sample_committee_custom(num_members, rng);
// Check the leader distribution.
Expand All @@ -420,7 +420,8 @@ mod tests {
// Initialize the RNG.
let rng = &mut TestRng::default();
// Sample a committee.
let committee = crate::test_helpers::sample_committee_custom(200, rng);
let committee =
crate::test_helpers::sample_committee_custom(Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE, rng);

// Start a timer.
let timer = std::time::Instant::now();
Expand Down
2 changes: 1 addition & 1 deletion ledger/committee/src/prop_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn committee_members(input: CommitteeContext) {
fn invalid_stakes(#[strategy(too_low_stake_committee())] committee: Result<Committee<CurrentNetwork>>) {
assert!(committee.is_err());
if let Err(err) = committee {
assert_eq!(err.to_string().as_str(), "All members must have at least 1000000000000 microcredits in stake");
assert_eq!(err.to_string().as_str(), "All members must have at least 10000000000000 microcredits in stake");
}
}

Expand Down
7 changes: 6 additions & 1 deletion ledger/narwhal/batch-header/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ pub struct BatchHeader<N: Network> {

impl<N: Network> BatchHeader<N> {
/// The maximum number of certificates in a batch.
pub const MAX_CERTIFICATES: u16 = 200;
#[cfg(not(any(test, feature = "test-helpers")))]
pub const MAX_CERTIFICATES: u16 = 10;
/// The maximum number of certificates in a batch.
/// This is set to a deliberately high value (100) for testing purposes only.
#[cfg(any(test, feature = "test-helpers"))]
pub const MAX_CERTIFICATES: u16 = 100;
/// The maximum number of rounds to store before garbage collecting.
pub const MAX_GC_ROUNDS: usize = 100;
/// The maximum number of transmissions in a batch.
Expand Down
6 changes: 3 additions & 3 deletions ledger/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ fn test_bond_and_unbond_validator() {
// Fund the new committee member.
let inputs = [
Value::from_str(&format!("{new_member_address}")).unwrap(),
Value::from_str("10000000000000u64").unwrap(), // 10 million credits.
Value::from_str("20000000000000u64").unwrap(), // 20 million credits.
];
let transfer_transaction = ledger
.vm
Expand All @@ -521,7 +521,7 @@ fn test_bond_and_unbond_validator() {
ledger.advance_to_next_block(&transfer_block).unwrap();

// Construct the bond public
let bond_amount = 1000000000000u64; // 1 million credits.
let bond_amount = MIN_VALIDATOR_STAKE;
let inputs = [
Value::from_str(&format!("{new_member_address}")).unwrap(),
Value::from_str(&format!("{new_member_address}")).unwrap(),
Expand Down Expand Up @@ -569,7 +569,7 @@ fn test_bond_and_unbond_validator() {
assert_eq!(num_validators, committee.num_members());

// Construct the bond public
let unbond_amount = committee.get_stake(new_member_address); // 1 million credits.
let unbond_amount = committee.get_stake(new_member_address);
let inputs = [Value::from_str(&format!("{unbond_amount}u64")).unwrap()];
let unbond_public_transaction = ledger
.vm
Expand Down
5 changes: 5 additions & 0 deletions synthesizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ package = "snarkvm-ledger-committee"
path = "../ledger/committee"
features = [ "test-helpers" ]

[dev-dependencies.ledger-narwhal-batch-header]
package = "snarkvm-ledger-narwhal-batch-header"
path = "../ledger/narwhal/batch-header"
features = [ "test-helpers" ]

[dev-dependencies.ledger-test-helpers]
package = "snarkvm-ledger-test-helpers"
path = "../ledger/test-helpers"
Expand Down
18 changes: 9 additions & 9 deletions synthesizer/process/src/tests/test_credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn initialize_stakers<N: Network, F: FinalizeStorage<N>>(
// Initialize a new account.
let private_key = PrivateKey::<N>::new(rng)?;
let address = Address::try_from(&private_key)?;
let balance = 10_000_000_000_000u64;
let balance = 100_000_000_000_000u64;

// Add the balance directly to the finalize store.
let key = Plaintext::from(Literal::Address(address));
Expand Down Expand Up @@ -521,7 +521,7 @@ fn test_bond_validator_multiple_bonds() {
/* First Bond */

// Perform the first bond.
let amount = 1_000_000_000_000u64;
let amount = MIN_VALIDATOR_STAKE;
assert!(amount < public_balance);
bond_public(&process, &store, validator_private_key, validator_address, validator_address, amount, rng)
.unwrap();
Expand All @@ -539,7 +539,7 @@ fn test_bond_validator_multiple_bonds() {
/* Second Bond */

// Perform the second bond.
let amount = 1_000_000_000_000u64;
let amount = MIN_VALIDATOR_STAKE;
assert!(amount < public_balance_1);
bond_public(&process, &store, validator_private_key, validator_address, validator_address, amount, rng)
.unwrap();
Expand Down Expand Up @@ -1514,7 +1514,7 @@ fn test_unbond_delegator_as_validator() {
/* Ensure unbonding a delegator as an open validator fails. */

// Bond the validators.
let validator_amount = 1_000_000_000_000u64;
let validator_amount = MIN_VALIDATOR_STAKE;
bond_public(
&process,
&finalize_store,
Expand Down Expand Up @@ -1596,7 +1596,7 @@ fn test_claim_unbond() {
let public_balance = account_balance(&finalize_store, validator_address).unwrap();

// Perform the bond.
let validator_amount = 1_000_000_000_000u64;
let validator_amount = MIN_VALIDATOR_STAKE;
bond_public(
&process,
&finalize_store,
Expand Down Expand Up @@ -1642,7 +1642,7 @@ fn test_set_validator_state() {
/* Ensure calling `set_validator_state` succeeds. */

// Perform the bond.
let amount = 1_000_000_000_000u64;
let amount = MIN_VALIDATOR_STAKE;
bond_public(&process, &finalize_store, validator_private_key, validator_address, validator_address, amount, rng)
.unwrap();

Expand Down Expand Up @@ -1699,15 +1699,15 @@ fn test_bonding_to_closed_fails() {
/* Ensure bonding to a closed validator fails. */

// Perform the bond.
let amount = 1_000_000_000_000u64;
let amount = MIN_VALIDATOR_STAKE;
bond_public(&process, &finalize_store, validator_private_key, validator_address, validator_address, amount, rng)
.unwrap();

// Set the validator `is_open` state to `false`.
set_validator_state(&process, &finalize_store, validator_private_key, false, rng).unwrap();

// Ensure that the validator can't bond additional stake.
let validator_amount = 1_000_000_000_000u64;
let validator_amount = MIN_VALIDATOR_STAKE;
assert!(
bond_public(
&process,
Expand All @@ -1722,7 +1722,7 @@ fn test_bonding_to_closed_fails() {
);

// Ensure that delegators can't bond to the validator.
let delegator_amount = 1_000_000u64;
let delegator_amount = MIN_DELEGATOR_STAKE;
assert!(
bond_public(
&process,
Expand Down
34 changes: 17 additions & 17 deletions synthesizer/program/src/resources/credits.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ finalize bond_public:
get.or_use metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc] 0u32 into r7;
// Increment the committee size by one.
add r7 1u32 into r8;
// Determine if the committee size is less than or equal to 200.
lte r8 200u32 into r9;
// Enforce that the committee size is less than or equal to 200.
// Determine if the committee size is less than or equal to 10.
lte r8 10u32 into r9;
// Enforce that the committee size is less than or equal to 10.
assert.eq r9 true;
// Set the new committee size.
set r8 into metadata[aleo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3ljyzc];
Expand Down Expand Up @@ -200,9 +200,9 @@ finalize bond_public:

// Increment the microcredits in the bond state.
add r11.microcredits r3 into r16;
// Determine if the amount is at least one million credits.
gte r16 1_000_000_000_000u64 into r17;
// Enforce the amount is at least one million credits.
// Determine if the amount is at least 10 million credits.
gte r16 10_000_000_000_000u64 into r17;
// Enforce the amount is at least 10 million credits.
assert.eq r17 true;

// Construct the updated bond state.
Expand Down Expand Up @@ -283,9 +283,9 @@ finalize bond_public:

// Increment the microcredits in the bond state.
add r30.microcredits r3 into r31;
// Determine if the amount is at least 10 credits.
gte r31 10_000_000u64 into r32;
// Enforce the amount is at least 10 credits.
// Determine if the amount is at least 10 thousand credits.
gte r31 10_000_000_000u64 into r32;
// Enforce the amount is at least 10 thousand credits.
assert.eq r32 true;

// Construct the updated bond state.
Expand Down Expand Up @@ -368,12 +368,12 @@ finalize unbond_public:
// Decrement the microcredits in the bond state.
sub r8.microcredits r1 into r9;

// Determine if the remaining bond is at least one million credits.
gte r9 1_000_000_000_000u64 into r10;
// Determine if the remaining bond is at least 10 million credits.
gte r9 10_000_000_000_000u64 into r10;

// If the remaining balance is at least 1 million credits, jump to the `decrement_validator` logic.
// If the remaining balance is at least 10 million credits, jump to the `decrement_validator` logic.
branch.eq r10 true to decrement_validator;
// If the remaining balance is less than 1 million credits, jump to the `remove_validator` logic.
// If the remaining balance is less than 10 million credits, jump to the `remove_validator` logic.
branch.eq r10 false to remove_validator;

/*** Decrement Validator ***/
Expand Down Expand Up @@ -456,12 +456,12 @@ finalize unbond_public:
// Decrement the microcredits in the bond state.
sub r19.microcredits r1 into r20;

// Determine if the remaining bond is at least 10 credits.
gte r20 10_000_000u64 into r21;
// Determine if the remaining bond is at least 10 thousand credits.
gte r20 10_000_000_000u64 into r21;

// If the remaining balance is at least 10 credits, jump to the `decrement_delegator` logic.
// If the remaining balance is at least 10 thousand credits, jump to the `decrement_delegator` logic.
branch.eq r21 true to decrement_delegator;
// If the remaining balance is less than 10 credits, jump to the `remove_delegator` logic.
// If the remaining balance is less than 10 thousand credits, jump to the `remove_delegator` logic.
branch.eq r21 false to remove_delegator;

/*** Decrement Delegator ***/
Expand Down
8 changes: 5 additions & 3 deletions synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,8 +2020,9 @@ finalize compute:
assert!(result.is_err());

// Reset the validators.
// Note: We use a smaller committee size to ensure that there is enough supply to allocate to the validators and genesis block transactions.
let validators =
sample_validators::<CurrentNetwork>(Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE as usize, rng);
sample_validators::<CurrentNetwork>(Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE as usize / 4, rng);

// Construct the committee.
// Track the allocated amount.
Expand Down Expand Up @@ -2063,8 +2064,9 @@ finalize compute:
VM::from(ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None).unwrap()).unwrap();

// Construct the validators.
// Note: We use a smaller committee size to ensure that there is enough supply to allocate to the validators and genesis block transactions.
let validators =
sample_validators::<CurrentNetwork>(Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE as usize, rng);
sample_validators::<CurrentNetwork>(Committee::<CurrentNetwork>::MAX_COMMITTEE_SIZE as usize / 4, rng);

// Construct the delegators, greater than the maximum delegator size.
let delegators = (0..MAX_DELEGATORS + 1)
Expand Down Expand Up @@ -2224,7 +2226,7 @@ finalize compute:
// Note that the first validator is used to execute additional transactions in `VM::genesis_quorum`.
// Therefore, the balance of the first validator will be different from the expected balance.
if entry.0 == Plaintext::from_str(&first_validator.to_string()).unwrap() {
assert_eq!(entry.1, Value::from_str("294999983894244u64").unwrap());
assert_eq!(entry.1, Value::from_str("249983999894244u64").unwrap());
} else {
assert!(expected_account.contains(entry));
}
Expand Down
6 changes: 3 additions & 3 deletions synthesizer/src/vm/helpers/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn to_next_withdraw_map<N: Network>(
pub(crate) mod test_helpers {
use super::*;
use crate::vm::TestRng;
use ledger_committee::MIN_VALIDATOR_STAKE;
use ledger_committee::{MIN_DELEGATOR_STAKE, MIN_VALIDATOR_STAKE};

use rand::{CryptoRng, Rng};

Expand All @@ -262,8 +262,8 @@ pub(crate) mod test_helpers {
.flat_map(|(validator, (microcredits, _))| {
// Keep a tally of the remaining microcredits.
let remaining_microcredits = microcredits.saturating_sub(MIN_VALIDATOR_STAKE);
// Set the staker amount to 10 credit.
let staker_amount = 10_000_000;
// Set the staker amount to `MIN_DELEGATOR_STAKE` microcredits.
let staker_amount = MIN_DELEGATOR_STAKE;
// Determine the number of iterations.
let num_iterations = (remaining_microcredits / staker_amount).saturating_sub(1);

Expand Down
2 changes: 1 addition & 1 deletion synthesizer/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ function a:
// Note: `deployment_transaction_ids` is sorted lexicographically by transaction ID, so the order may change if we update internal methods.
assert_eq!(
deployment_transaction_ids,
vec![deployment_2.id(), deployment_1.id(), deployment_4.id(), deployment_3.id()],
vec![deployment_3.id(), deployment_4.id(), deployment_1.id(), deployment_2.id()],
"Update me if serialization has changed"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ outputs:
test_rand.aleo/rand_chacha_check:
outputs:
- '{"type":"future","id":"3094014759641313043901697261267946468626327163450942596641947962222295207390field","value":"{\n program_id: test_rand.aleo,\n function_name: rand_chacha_check,\n arguments: [\n 0field,\n false\n ]\n}"}'
speculate: the execution was accepted
speculate: the execution was rejected
add_next_block: succeeded.
- verified: true
execute:
Expand Down

0 comments on commit 01c8b09

Please sign in to comment.