Skip to content

Commit

Permalink
refactor(gtest): Update some of gtests runtime constants (#3510)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx committed Nov 20, 2023
1 parent 10b4201 commit d02d306
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 44 deletions.
87 changes: 70 additions & 17 deletions gtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,73 @@ pub use error::{Result, TestError};
pub use program::{calculate_program_id, Gas, Program, WasmProgram};
pub use system::System;

pub const EXISTENTIAL_DEPOSIT: u128 = 500;
pub const MAILBOX_THRESHOLD: u64 = 3000;
pub const WAITLIST_COST: u64 = 100;
pub const RESERVE_FOR: u32 = 1;
pub const RESERVATION_COST: u64 = 100;
pub const READ_COST: u64 = 20;
pub const WRITE_COST: u64 = 100;
pub const READ_PER_BYTE_COST: u64 = 10;
pub const WRITE_PER_BYTE_COST: u64 = 10;
pub const MODULE_INSTANTIATION_BYTE_COST: u64 = 20;
pub const MAX_RESERVATIONS: u64 = 256;
pub const EPOCH_DURATION_IN_BLOCKS: u32 = 600;
pub const INITIAL_RANDOM_SEED: u64 = 42;
pub const MODULE_INSTRUMENTATION_BYTE_COST: u64 = 13;
pub const MODULE_INSTRUMENTATION_COST: u64 = 297;
pub const DISPATCH_HOLD_COST: u64 = 200;
pub const RENT_COST: u128 = 330;
pub(crate) use constants::*;

/// Module containing constants of Gear protocol.
pub mod constants {
/* Constant types */

/// Numeric type representing value in Gear protocol.
pub type Value = u128;

/// Numeric type representing gas in Gear protocol.
pub type Gas = u64;

/// Numeric type representing blocks in Gear protocol.
pub type Block = u32;

/* Currency-related constants */

/// Value per token.
pub const UNITS: Value = 1_000_000_000_000;
/// Minimal amount of value able to be sent. Defines accounts existence
/// requirement.
pub const EXISTENTIAL_DEPOSIT: Value = 10 * UNITS;
/// Value per gas.
pub const VALUE_PER_GAS: Value = 25;
/// Duration of one epoch.
pub const EPOCH_DURATION_IN_BLOCKS: Block = 600;

/* Storage-related constants */
// TODO: use proper weights of db accesses (#3509).

/// Minimal amount of gas required to be inserted into Mailbox.
pub const MAILBOX_THRESHOLD: Gas = 3_000;
/// Extra amount of blocks must be reserved for storing in storage.
pub const RESERVE_FOR: Block = 1;
/// Cost of read access into storage.
pub const READ_COST: Gas = 25;
/// Per-byte extra cost of read access into storage.
pub const READ_PER_BYTE_COST: Gas = 10;
/// Cost of write access into storage.
pub const WRITE_COST: Gas = 100;
/// Per-byte extra cost of write access into storage.
pub const WRITE_PER_BYTE_COST: Gas = 10;

/* Rent-related constants */

/// Cost of storing waitlisted message per block.
pub const WAITLIST_COST: Gas = 100;
/// Cost of storing reservation per block.
pub const RESERVATION_COST: Gas = 100;
/// Cost of storing delayed message per block.
pub const DISPATCH_HOLD_COST: Gas = 100;
/// Cost of storing program per block.
///
/// (!) Currently disabled: storing programs are free.
pub const RENT_COST: Value = 330;

/* Execution-related constants */
// TODO: use proper weights of instantiation and instrumentation (#3509).

/// Maximal amount of reservations program may have.
pub const MAX_RESERVATIONS: u64 = 256;
/// Cost of wasm module instantiation before execution per byte of code.
pub const MODULE_INSTANTIATION_BYTE_COST: Gas = 20;
/// Cost of instrumenting wasm code on upload.
pub const MODULE_INSTRUMENTATION_COST: Gas = 297;
/// Cost of instrumenting wasm code on upload per byte of code.
pub const MODULE_INSTRUMENTATION_BYTE_COST: Gas = 13;
/// Initial random seed for testing environment.
pub const INITIAL_RANDOM_SEED: u64 = 42;
}
9 changes: 6 additions & 3 deletions gtest/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,20 @@ mod tests {
receiver_id.into(),
payload.encode().try_into().unwrap(),
Default::default(),
1000,
2 * crate::EXISTENTIAL_DEPOSIT,
None,
);

system.mint_to(sender_id, 1000);
system.mint_to(sender_id, 2 * crate::EXISTENTIAL_DEPOSIT);
system.send_dispatch(Dispatch::new(DispatchKind::Handle, message));

let receiver_mailbox = system.get_mailbox(receiver_id);
receiver_mailbox.claim_value(log);

assert_eq!(system.balance_of(receiver_id), 1000);
assert_eq!(
system.balance_of(receiver_id),
2 * crate::EXISTENTIAL_DEPOSIT
);
}

#[test]
Expand Down
53 changes: 29 additions & 24 deletions gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ mod tests {
sys.init_logger();

let user_id = 42;
sys.mint_to(user_id, 5000);
assert_eq!(sys.balance_of(user_id), 5000);
sys.mint_to(user_id, 10 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(user_id), 10 * crate::EXISTENTIAL_DEPOSIT);

let mut prog = Program::from_opt_and_meta_code_with_id(
&sys,
Expand All @@ -710,16 +710,16 @@ mod tests {
None,
);

prog.mint(1000);
assert_eq!(prog.balance(), 1000);
prog.mint(2 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(prog.balance(), 2 * crate::EXISTENTIAL_DEPOSIT);

prog.send_with_value(user_id, "init".to_string(), 500);
assert_eq!(prog.balance(), 1500);
assert_eq!(sys.balance_of(user_id), 4500);
prog.send_with_value(user_id, "init".to_string(), crate::EXISTENTIAL_DEPOSIT);
assert_eq!(prog.balance(), 3 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(user_id), 9 * crate::EXISTENTIAL_DEPOSIT);

prog.send_with_value(user_id, "PING".to_string(), 1000);
assert_eq!(prog.balance(), 2500);
assert_eq!(sys.balance_of(user_id), 3500);
prog.send_with_value(user_id, "PING".to_string(), 2 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(prog.balance(), 5 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(user_id), 7 * crate::EXISTENTIAL_DEPOSIT);
}

#[test]
Expand All @@ -733,9 +733,9 @@ mod tests {
let sender2 = 45;

// Top-up senders balances
sys.mint_to(sender0, 10000);
sys.mint_to(sender1, 10000);
sys.mint_to(sender2, 10000);
sys.mint_to(sender0, 20 * crate::EXISTENTIAL_DEPOSIT);
sys.mint_to(sender1, 20 * crate::EXISTENTIAL_DEPOSIT);
sys.mint_to(sender2, 20 * crate::EXISTENTIAL_DEPOSIT);

let prog = Program::from_opt_and_meta_code_with_id(
&sys,
Expand All @@ -748,35 +748,40 @@ mod tests {
assert_eq!(prog.balance(), 0);

// Send values to the program
prog.send_bytes_with_value(sender0, b"insert", 1000);
assert_eq!(sys.balance_of(sender0), 9000);
prog.send_bytes_with_value(sender1, b"insert", 2000);
assert_eq!(sys.balance_of(sender1), 8000);
prog.send_bytes_with_value(sender2, b"insert", 3000);
assert_eq!(sys.balance_of(sender2), 7000);
prog.send_bytes_with_value(sender0, b"insert", 2 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(sender0), 18 * crate::EXISTENTIAL_DEPOSIT);
prog.send_bytes_with_value(sender1, b"insert", 4 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(sender1), 16 * crate::EXISTENTIAL_DEPOSIT);
prog.send_bytes_with_value(sender2, b"insert", 6 * crate::EXISTENTIAL_DEPOSIT);
assert_eq!(sys.balance_of(sender2), 14 * crate::EXISTENTIAL_DEPOSIT);

// Check program's balance
assert_eq!(prog.balance(), 1000 + 2000 + 3000);
assert_eq!(prog.balance(), (2 + 4 + 6) * crate::EXISTENTIAL_DEPOSIT);

// Request to smash the piggy bank and send the value to the receiver address
prog.send_bytes(receiver, b"smash");
sys.claim_value_from_mailbox(receiver);
assert_eq!(sys.balance_of(receiver), 1000 + 2000 + 3000);
assert_eq!(
sys.balance_of(receiver),
(2 + 4 + 6) * crate::EXISTENTIAL_DEPOSIT
);

// Check program's balance is empty
assert_eq!(prog.balance(), 0);
}

#[test]
#[should_panic(expected = "An attempt to mint value (1) less than existential deposit (500)")]
#[should_panic(
expected = "An attempt to mint value (1) less than existential deposit (10000000000000)"
)]
fn mint_less_than_deposit() {
System::new().mint_to(1, 1);
}

#[test]
#[should_panic(expected = "Insufficient value: user \
(0x0100000000000000000000000000000000000000000000000000000000000000) tries \
to send (501) value, while his balance (500)")]
to send (10000000000001) value, while his balance (10000000000000)")]
fn fails_on_insufficient_balance() {
let sys = System::new();

Expand All @@ -803,7 +808,7 @@ mod tests {
let sender = 42;
let receiver = 84;

sys.mint_to(sender, 10000);
sys.mint_to(sender, 20 * crate::EXISTENTIAL_DEPOSIT);

let prog = Program::from_opt_and_meta_code_with_id(
&sys,
Expand Down

0 comments on commit d02d306

Please sign in to comment.