Skip to content

Commit

Permalink
Merge pull request #1857 from AleoHQ/feat/committee-genesis
Browse files Browse the repository at this point in the history
Adds committee logic, implements pre-ratify and post-ratify logic in finalize scope
  • Loading branch information
howardwu authored Aug 16, 2023
2 parents 9465e38 + 891fd08 commit 4dc8be9
Show file tree
Hide file tree
Showing 98 changed files with 5,338 additions and 1,456 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
!**/powers-of-beta-gamma.usrs
!**/beta-h.usrs
!**/neg-powers-of-beta.usrs
**/proptest-regressions/
6 changes: 6 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions console/network/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod prelude {
cfg_into_iter,
cfg_iter,
cfg_iter_mut,
cfg_reduce,
cfg_values,
error,
has_duplicates,
Expand Down
2 changes: 1 addition & 1 deletion console/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait Network:
/// The maximum number of commands in finalize.
const MAX_COMMANDS: usize = u16::MAX as usize;
/// The maximum number of write commands in finalize.
const MAX_WRITES: u16 = 10;
const MAX_WRITES: u16 = 16;

/// The maximum number of inputs per transition.
const MAX_INPUTS: usize = 16;
Expand Down
8 changes: 7 additions & 1 deletion ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ serial = [
"ledger-store/serial",
"synthesizer/serial"
]
test-helpers = [ "ledger-narwhal/test-helpers" ]
test = [ "ledger-block/test" ]
test-helpers = [ "ledger-committee/test-helpers", "ledger-narwhal/test-helpers" ]
timer = [ "aleo-std/timer" ]

[dependencies.console]
Expand Down Expand Up @@ -123,6 +124,11 @@ version = "1.3"
[dev-dependencies.criterion]
version = "0.5"

[dev-dependencies.ledger-block]
package = "snarkvm-ledger-block"
path = "./block"
features = [ "test" ]

[dev-dependencies.serde_json]
version = "1.0"
features = [ "preserve_order" ]
3 changes: 1 addition & 2 deletions ledger/authority/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ impl<N: Network> Authority<N> {
/// Returns address of the authority.
/// If the authority is a beacon, the address of the signer is returned.
/// If the authority is a quorum, the address of the leader is returned.
#[allow(deprecated)]
pub fn to_address(&self) -> Address<N> {
match self {
Self::Beacon(signature) => signature.to_address(),
Self::Quorum(..) => Address::zero(),
Self::Quorum(subdag) => subdag.leader_address(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ledger/benches/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn initialize_vm<R: Rng + CryptoRng>(
let vm = VM::from(ConsensusStore::open(None).unwrap()).unwrap();

// Initialize the genesis block.
let genesis = vm.genesis(private_key, rng).unwrap();
let genesis = vm.genesis_beacon(private_key, rng).unwrap();

// Fetch the unspent records.
let records = genesis.transitions().cloned().flat_map(Transition::into_records).collect::<IndexMap<_, _>>();
Expand Down
18 changes: 18 additions & 0 deletions ledger/block/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ serial = [
"console/serial",
"ledger-authority/serial",
"ledger-coinbase/serial",
"ledger-committee/serial",
"synthesizer-program/serial",
"synthesizer-snark/serial"
]
wasm = [
"console/wasm",
"ledger-authority/wasm",
"ledger-coinbase/wasm",
"ledger-committee/wasm",
"synthesizer-program/wasm",
"synthesizer-snark/wasm"
]
test = [ ]

[dependencies.console]
package = "snarkvm-console"
Expand All @@ -48,11 +51,21 @@ package = "snarkvm-ledger-coinbase"
path = "../../ledger/coinbase"
version = "=0.14.6"

[dependencies.ledger-committee]
package = "snarkvm-ledger-committee"
path = "../../ledger/committee"
version = "=0.14.6"

[dependencies.ledger-narwhal-subdag]
package = "snarkvm-ledger-narwhal-subdag"
path = "../narwhal/subdag"
version = "=0.14.6"

[dependencies.ledger-narwhal-transmission-id]
package = "snarkvm-ledger-narwhal-transmission-id"
path = "../narwhal/transmission-id"
version = "=0.14.6"

[dependencies.synthesizer-program]
package = "snarkvm-synthesizer-program"
path = "../../synthesizer/program"
Expand Down Expand Up @@ -82,6 +95,11 @@ version = "1.3"
package = "snarkvm-circuit"
path = "../../circuit"

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

[dev-dependencies.ledger-query]
package = "snarkvm-ledger-query"
path = "../query"
Expand Down
4 changes: 2 additions & 2 deletions ledger/block/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::*;

impl<N: Network> Block<N> {
/// Specifies the number of genesis transactions.
pub const NUM_GENESIS_TRANSACTIONS: usize = 16;
pub const NUM_GENESIS_TRANSACTIONS: usize = 4;

/// Returns `true` if the block is a genesis block.
pub fn is_genesis(&self) -> bool {
Expand All @@ -33,7 +33,7 @@ impl<N: Network> Block<N> {
// Ensure there is the correct number of finalize operations in the genesis block.
&& self.transactions.num_finalize() == 0
// Ensure there is the correct number of ratification operations in the genesis block.
&& self.ratifications.is_empty()
&& self.ratifications.len() == 1
// Ensure there are no solutions in the genesis block.
&& self.coinbase.is_none()
}
Expand Down
4 changes: 2 additions & 2 deletions ledger/block/src/header/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<N: Network> FromBytes for Header<N> {
}

// Read from the buffer.
let previous_state_root = Field::<N>::read_le(&mut reader)?;
let previous_state_root = N::StateRoot::read_le(&mut reader)?;
let transactions_root = Field::<N>::read_le(&mut reader)?;
let finalize_root = Field::<N>::read_le(&mut reader)?;
let ratifications_root = Field::<N>::read_le(&mut reader)?;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<N: Network> ToBytes for Header<N> {
self.transactions_root.write_le(&mut writer)?;
self.finalize_root.write_le(&mut writer)?;
self.ratifications_root.write_le(&mut writer)?;
self.coinbase_accumulator_point.write_le(&mut writer)?;
self.solutions_root.write_le(&mut writer)?;
self.metadata.write_le(&mut writer)
}
}
Expand Down
12 changes: 6 additions & 6 deletions ledger/block/src/header/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<N: Network> Header<N> {
/// Initializes the genesis block header.
pub fn genesis(transactions: &Transactions<N>) -> Result<Self> {
// Prepare a genesis block header.
let previous_state_root = Field::zero();
let previous_state_root = Into::<N::StateRoot>::into(Field::zero());
let transactions_root = transactions.to_transactions_root()?;
let finalize_root = transactions.to_finalize_root()?;
let ratifications_root = *N::merkle_tree_bhp::<RATIFICATIONS_DEPTH>(&[])?.root();
Expand All @@ -39,15 +39,15 @@ impl<N: Network> Header<N> {
/// Returns `true` if the block header is a genesis block header.
pub fn is_genesis(&self) -> bool {
// Ensure the previous ledger root is zero.
self.previous_state_root == Field::zero()
*self.previous_state_root == Field::zero()
// Ensure the transactions root is nonzero.
&& self.transactions_root != Field::zero()
// Ensure the finalize root is nonzero.
&& self.finalize_root != Field::zero()
// Ensure the ratifications root is nonzero.
&& self.ratifications_root != Field::zero()
// Ensure the coinbase accumulator point is zero.
&& self.coinbase_accumulator_point == Field::zero()
// Ensure the solutions root is zero.
&& self.solutions_root == Field::zero()
// Ensure the metadata is a genesis metadata.
&& self.metadata.is_genesis()
}
Expand Down Expand Up @@ -93,8 +93,8 @@ mod tests {
assert!(header.is_genesis());

// Ensure the genesis block contains the following.
assert_eq!(header.previous_state_root(), Field::zero());
assert_eq!(header.coinbase_accumulator_point(), Field::zero());
assert_eq!(*header.previous_state_root(), Field::zero());
assert_eq!(header.solutions_root(), Field::zero());
assert_eq!(header.network(), CurrentNetwork::ID);
assert_eq!(header.round(), 0);
assert_eq!(header.height(), 0);
Expand Down
20 changes: 10 additions & 10 deletions ledger/block/src/header/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl<N: Network> Header<N> {
/// Returns the Merkle leaf for the given ID in the header.
pub fn to_leaf(&self, id: &Field<N>) -> Result<HeaderLeaf<N>> {
// If the ID is the previous state root, return the 0th leaf.
if id == &self.previous_state_root {
Ok(HeaderLeaf::<N>::new(0, self.previous_state_root))
if id == &*self.previous_state_root {
Ok(HeaderLeaf::<N>::new(0, *self.previous_state_root))
}
// If the ID is the transactions root, return the 1st leaf.
else if id == &self.transactions_root {
Expand All @@ -44,9 +44,9 @@ impl<N: Network> Header<N> {
else if id == &self.ratifications_root {
Ok(HeaderLeaf::<N>::new(3, self.ratifications_root))
}
// If the ID is the coinbase accumulator point, return the 4th leaf.
else if id == &self.coinbase_accumulator_point {
Ok(HeaderLeaf::<N>::new(4, self.coinbase_accumulator_point))
// If the ID is the solutions root, return the 4th leaf.
else if id == &self.solutions_root {
Ok(HeaderLeaf::<N>::new(4, self.solutions_root))
}
// If the ID is the metadata hash, then return the 7th leaf.
else if id == &self.metadata.to_hash()? {
Expand All @@ -65,11 +65,11 @@ impl<N: Network> Header<N> {

// Construct the Merkle leaves.
let mut leaves: Vec<Vec<bool>> = Vec::with_capacity(num_leaves);
leaves.push(HeaderLeaf::<N>::new(0, self.previous_state_root).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(0, *self.previous_state_root).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(1, self.transactions_root).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(2, self.finalize_root).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(3, self.ratifications_root).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(4, self.coinbase_accumulator_point).to_bits_le());
leaves.push(HeaderLeaf::<N>::new(4, self.solutions_root).to_bits_le());
for i in 5..7 {
leaves.push(HeaderLeaf::<N>::new(i, Field::zero()).to_bits_le());
}
Expand Down Expand Up @@ -112,7 +112,7 @@ mod tests {
let proof_target = rng.gen_range(0..coinbase_target);

let header = Header::<CurrentNetwork>::from(
Field::rand(rng),
Into::<<CurrentNetwork as Network>::StateRoot>::into(Field::rand(rng)),
Field::rand(rng),
Field::rand(rng),
Field::rand(rng),
Expand All @@ -136,7 +136,7 @@ mod tests {
let root = header.to_root()?;

// Check the 0th leaf.
let leaf = header.to_leaf(&header.previous_state_root())?;
let leaf = header.to_leaf(&*header.previous_state_root())?;
assert_eq!(leaf.index(), 0);
check_path(header.to_path(&leaf)?, root, &leaf)?;

Expand All @@ -156,7 +156,7 @@ mod tests {
check_path(header.to_path(&leaf)?, root, &leaf)?;

// Check the 4th leaf.
let leaf = header.to_leaf(&header.coinbase_accumulator_point())?;
let leaf = header.to_leaf(&header.solutions_root())?;
assert_eq!(leaf.index(), 4);
check_path(header.to_path(&leaf)?, root, &leaf)?;

Expand Down
5 changes: 4 additions & 1 deletion ledger/block/src/header/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod serialize;
mod string;
mod to_bits;
mod to_hash;
mod verify;

use console::{network::prelude::*, types::Field};

Expand Down Expand Up @@ -89,7 +90,7 @@ impl<N: Network> Metadata<N> {
}
}

/// Returns `true` if the block header is well-formed.
/// Returns `true` if the block metadata is well-formed.
pub fn is_valid(&self) -> bool {
match self.height == 0u32 {
true => self.is_genesis(),
Expand All @@ -100,6 +101,8 @@ impl<N: Network> Metadata<N> {
&& self.round != 0u64
// Ensure the height is nonzero.
&& self.height != 0u32
// Ensure the round is at least as large as the height.
&& self.round >= self.height as u64
// Ensure the total supply is nonzero.
&& self.total_supply_in_microcredits != 0u64
// Ensure the coinbase target is at or above the minimum.
Expand Down
Loading

0 comments on commit 4dc8be9

Please sign in to comment.