Skip to content

Commit

Permalink
Merge testnet3 and regenerate expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Nov 1, 2023
2 parents 21325b6 + 2a3ea36 commit 588baea
Show file tree
Hide file tree
Showing 71 changed files with 917 additions and 347 deletions.
1 change: 0 additions & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ jobs:
run: |
cd console/collections
cargo bench --bench merkle_tree -- --output-format bencher | tee -a ../../output.txt
cargo bench --bench kary_merkle_tree -- --output-format bencher | tee -a ../../output.txt
cd ../..
- name: Benchmark curves
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ snarkVM is a big project, so (non-)adherence to best practices related to perfor
### Memory handling
- if the final size is known, pre-allocate the collections (`Vec`, `HashMap` etc.) using `with_capacity` or `reserve` - this ensures that there are both fewer allocations (which involve system calls) and that the final allocated capacity is as close to the required size as possible
- create the collections right before they are populated/used, as opposed to e.g. creating a few big ones at the beginning of a function and only using them later on; this reduces the amount of time they occupy memory
- if an intermediate vector is avoidable, use an `Iterator` instead; most of the time this just amounts to omitting the call to `.collect()` if a single-pass iteraton follows afterwards, or returning an `impl Iterator<Item = T>` from a function when the caller only needs to iterate over that result once
- if an intermediate vector is avoidable, use an `Iterator` instead; most of the time this just amounts to omitting the call to `.collect()` if a single-pass iteration follows afterwards, or returning an `impl Iterator<Item = T>` from a function when the caller only needs to iterate over that result once
- when possible, fill/resize collections "in bulk" instead of pushing a single element in a loop; this is usually (but not always) detected by `clippy`, suggesting to create vectors containing a repeated value with `vec![x; N]` or extending them with `.resize(N, x)`
- when a value is to eventually be consumed in a chain of function calls, pass it by value instead of by reference; this has the following benefits:
* it makes the fact that the value is needed by value clear to the caller, who can then potentially reclaim it from the object afterwards if it is "heavy", limiting allocations
Expand Down
1 change: 0 additions & 1 deletion console/program/src/data/ciphertext/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Ciphertext::read_le(&expected_bytes[..])?);
// assert!(Ciphertext::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion console/program/src/data/future/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Future::read_le(&expected_bytes[..])?);
assert!(Future::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

Ok(())
}
Expand Down
7 changes: 6 additions & 1 deletion console/program/src/data/identifier/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl<N: Network> FromBytes for Identifier<N> {
reader.read_exact(&mut buffer)?;

// from_str the identifier.
// Note: `Self::from_str` ensures that the identifier string is not empty.
Self::from_str(&String::from_utf8(buffer).map_err(|e| error(format!("Failed to decode identifier: {e}")))?)
.map_err(|e| error(format!("{e}")))
}
Expand Down Expand Up @@ -72,8 +73,12 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Identifier::read_le(&expected_bytes[..])?);
assert!(Identifier::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}

#[test]
fn test_zero_identifier_fails() {
assert!(Identifier::<CurrentNetwork>::read_le(&[0u8; 1][..]).is_err())
}
}
2 changes: 0 additions & 2 deletions console/program/src/data/literal/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Literal::read_le(&expected_bytes[..])?);
assert!(Literal::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
// assert!(Literal::<CurrentNetwork>::read_le(&expected_bytes[2..]).is_err());
Ok(())
}

Expand Down
5 changes: 0 additions & 5 deletions console/program/src/data/plaintext/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
// assert!(Plaintext::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
// assert!(Plaintext::<CurrentNetwork>::read_le(&expected_bytes[2..]).is_err());
// assert!(Plaintext::<CurrentNetwork>::read_le(&expected_bytes[3..]).is_err());
Ok(())
}

Expand Down Expand Up @@ -225,15 +222,13 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
assert!(Plaintext::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

// Check the array manually.
let expected = Plaintext::<CurrentNetwork>::from_str("[ 1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8 ]")?;

// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Plaintext::read_le(&expected_bytes[..])?);
assert!(Plaintext::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion console/program/src/data/record/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Record::read_le(&expected_bytes[..])?);
assert!(Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::read_le(&expected_bytes[1..]).is_err());
Ok(())
}
}
2 changes: 0 additions & 2 deletions console/program/src/data/value/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
assert!(Value::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
Ok(())
}

Expand All @@ -81,7 +80,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
assert!(Value::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
Ok(())
}
}
4 changes: 0 additions & 4 deletions console/program/src/request/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ impl<N: Network> ToBytes for Request<N> {
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() {
Expand All @@ -112,7 +109,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le().unwrap();
assert_eq!(expected, Request::read_le(&expected_bytes[..]).unwrap());
assert!(Request::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
}
}
4 changes: 0 additions & 4 deletions console/program/src/state_path/header_leaf/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ impl<N: Network> ToBytes for HeaderLeaf<N> {
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;

const ITERATIONS: u64 = 1000;

Expand All @@ -56,7 +53,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, HeaderLeaf::read_le(&expected_bytes[..])?);
assert!(HeaderLeaf::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions console/program/src/state_path/transaction_leaf/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ impl<N: Network> ToBytes for TransactionLeaf<N> {
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;

const ITERATIONS: u64 = 1000;

Expand All @@ -60,7 +57,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, TransactionLeaf::read_le(&expected_bytes[..])?);
assert!(TransactionLeaf::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions console/program/src/state_path/transition_leaf/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ impl<N: Network> ToBytes for TransitionLeaf<N> {
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;

type CurrentNetwork = Testnet3;

const ITERATIONS: u64 = 1000;

Expand All @@ -68,7 +65,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, TransitionLeaf::read_le(&expected_bytes[..])?);
assert!(TransitionLeaf::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
7 changes: 3 additions & 4 deletions console/types/scalar/src/from_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ impl<E: Environment> FromBits for Scalar<E> {

// If `num_bits` is greater than `size_in_data_bits`, check it is less than `Scalar::MODULUS`.
if num_bits > size_in_data_bits {
// Retrieve the modulus & subtract by 1 as we'll check `bits_le` is less than or *equal* to this value.
// (For advanced users) Scalar::MODULUS - 1 is equivalent to -1 in the field.
let modulus_minus_one = E::Scalar::modulus();
// Retrieve the modulus as we'll check `bits_le` is less than this value.
let modulus = E::Scalar::modulus();

// Recover the scalar as a `BigInteger` for comparison.
// As `bits_le[size_in_bits..]` is guaranteed to be zero from the above logic,
// and `bits_le` is greater than `size_in_data_bits`, it is safe to truncate `bits_le` to `size_in_bits`.
let scalar = E::BigInteger::from_bits_le(&bits_le[..size_in_bits])?;

// Ensure the scalar is less than `Scalar::MODULUS`.
ensure!(scalar < modulus_minus_one, "The scalar is greater than or equal to the modulus.");
ensure!(scalar < modulus, "The scalar is greater than or equal to the modulus.");

// Return the scalar.
Ok(Scalar { scalar: E::Scalar::from_bigint(scalar).ok_or_else(|| anyhow!("Invalid scalar from bits"))? })
Expand Down
1 change: 0 additions & 1 deletion console/types/string/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, StringType::read_le(&expected_bytes[..])?);
assert!(StringType::<CurrentEnvironment>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
5 changes: 1 addition & 4 deletions ledger/authority/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl<N: Network> ToBytes for Authority<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::{network::Testnet3, prelude::TestRng};

type CurrentNetwork = Testnet3;
use console::prelude::TestRng;

#[test]
fn test_bytes() {
Expand All @@ -64,7 +62,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le().unwrap();
assert_eq!(expected, Authority::read_le(&expected_bytes[..]).unwrap());
assert!(Authority::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
}
}
2 changes: 0 additions & 2 deletions ledger/block/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Block::read_le(&expected_bytes[..])?);
assert!(Block::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand All @@ -147,7 +146,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = genesis_block.to_bytes_le()?;
assert_eq!(genesis_block, Block::read_le(&expected_bytes[..])?);
assert!(Block::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions ledger/block/src/ratifications/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ impl<N: Network> ToBytes for Ratifications<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

const ITERATIONS: u32 = 100;

Expand All @@ -68,7 +65,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Ratifications::read_le(&expected_bytes[..])?);
assert!(Ratifications::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions ledger/block/src/ratify/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ impl<N: Network> ToBytes for Ratify<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() {
Expand All @@ -106,7 +103,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le().unwrap();
assert_eq!(expected, Ratify::read_le(&expected_bytes[..]).unwrap());
assert!(Ratify::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
}
}
4 changes: 0 additions & 4 deletions ledger/block/src/transaction/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ impl<N: Network> ToBytes for Transaction<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -158,7 +155,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Transaction::read_le(&expected_bytes[..])?);
assert!(Transaction::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions ledger/block/src/transaction/deployment/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ impl<N: Network> ToBytes for Deployment<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -90,7 +87,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Deployment::read_le(&expected_bytes[..])?);
assert!(Deployment::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
Ok(())
}
}
4 changes: 0 additions & 4 deletions ledger/block/src/transaction/execution/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ impl<N: Network> ToBytes for Execution<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -89,7 +86,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Execution::read_le(&expected_bytes[..])?);
assert!(Execution::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
Ok(())
}
}
5 changes: 0 additions & 5 deletions ledger/block/src/transaction/fee/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ impl<N: Network> ToBytes for Fee<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -78,15 +75,13 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
assert!(Fee::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

// Construct a new public fee.
let expected = crate::transaction::fee::test_helpers::sample_fee_public_hardcoded(rng);

// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
assert!(Fee::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());

Ok(())
}
Expand Down
4 changes: 0 additions & 4 deletions ledger/block/src/transactions/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ impl<N: Network> ToBytes for Transactions<N> {
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;

type CurrentNetwork = Testnet3;

#[test]
fn test_bytes() -> Result<()> {
Expand All @@ -65,7 +62,6 @@ mod tests {
// Check the byte representation.
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Transactions::read_le(&expected_bytes[..])?);
assert!(Transactions::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
Expand Down
Loading

0 comments on commit 588baea

Please sign in to comment.