Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
batconjurer committed May 28, 2024
1 parent 540e853 commit 777d40d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
3 changes: 2 additions & 1 deletion crates/node/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ where
storage_read_past_height_limit: Option<u64>,
/// Log of events emitted by `FinalizeBlock` ABCI calls.
event_log: EventLog,
scheduled_migration: Option<ScheduledMigration<D::Migrator>>,
/// A migration that can be scheduled at a given block height
pub scheduled_migration: Option<ScheduledMigration<D::Migrator>>,
}

/// Storage key filter to store the diffs into the storage. Return `false` for
Expand Down
9 changes: 9 additions & 0 deletions crates/node/src/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ impl MockNode {
self.genesis_dir().join("wallet.toml")
}

pub fn block_height(&self) -> BlockHeight {
self.shell
.lock()
.unwrap()
.state
.get_block_height()
.unwrap_or_default()
}

pub fn current_epoch(&self) -> Epoch {
self.shell.lock().unwrap().state.in_mem().last_epoch
}
Expand Down
32 changes: 32 additions & 0 deletions crates/sdk/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,35 @@ static BTREEMAP_STRING_ADDRESS: fn() = || {
},
);
};

#[cfg(test)]
mod test_migrations {
use namada_core::token::Amount;

use super::*;

/// Check that if the hash of the file is wrong, the scheduled
/// migration will not load.
#[test]
fn test_scheduled_migration_validate() {
let file = tempfile::Builder::new().tempfile().expect("Test failed");
let updates = [DbUpdateType::Add {
key: Key::parse("bing/fucking/bong").expect("Test failed"),
cf: DbColFam::SUBSPACE,
value: Amount::native_whole(1337).into(),
force: false,
}];
let changes = DbChanges {
changes: updates.into_iter().collect(),
};
let json = serde_json::to_string(&changes).expect("Test failed");
let hash = Hash::sha256("derpy derp".as_bytes());
std::fs::write(file.path(), json).expect("Test failed");
let migration = ScheduledMigration::<DbUpdateType>::from_path(
file.path(),
hash,
Default::default(),
);
assert!(migration.is_err());
}
}
85 changes: 83 additions & 2 deletions crates/tests/src/integration/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ use std::collections::BTreeSet;
use std::str::FromStr;

use assert_matches::assert_matches;
use borsh::BorshDeserialize;
use borsh_ext::BorshSerializeExt;
use color_eyre::eyre::Result;
use data_encoding::HEXLOWER;
use namada::core::collections::HashMap;
use namada::token;
use namada_apps_lib::wallet::defaults;
use namada_core::dec::Dec;
use namada_core::storage::Epoch;
use namada_core::token::NATIVE_MAX_DECIMAL_PLACES;
use namada_core::hash::Hash;
use namada_core::storage::{DbColFam, Epoch, Key};
use namada_core::token::{Amount, NATIVE_MAX_DECIMAL_PLACES};
use namada_node::shell::testing::client::run;
use namada_node::shell::testing::utils::{Bin, CapturedOutput};
use namada_sdk::migrations;
use namada_sdk::queries::RPC;
use namada_sdk::tx::{TX_TRANSFER_WASM, VP_USER_WASM};
use namada_test_utils::TestWasms;
use test_log::test;
Expand Down Expand Up @@ -1578,3 +1582,80 @@ fn change_validator_metadata() -> Result<()> {

Ok(())
}

/// Test that a scheduled migration actually makes changes
/// to storage at the scheduled height.
#[test]
fn scheduled_migration() -> Result<()> {
let (node, _services) = setup::setup()?;

// schedule a migration
let (hash, migrations_file) = make_migration_json();
let scheduled_migration = migrations::ScheduledMigration::from_path(
migrations_file.path(),
hash,
5.into(),
)
.expect("Test failed");
{
let mut locked = node.shell.lock().unwrap();
locked.scheduled_migration = Some(scheduled_migration);
}

while node.block_height().0 != 4 {
node.finalize_and_commit()
}
// check that the key doesn't exist before the scheduled block
let rt = tokio::runtime::Runtime::new().unwrap();
let bytes = rt
.block_on(RPC.shell().storage_value(
&&node,
None,
None,
false,
&Key::parse("bing/fucking/bong").expect("Test failed"),
))
.expect("Test failed")
.data;
assert!(bytes.is_empty());

// check that the key now exists and has the expected value
node.finalize_and_commit();
let rt = tokio::runtime::Runtime::new().unwrap();
let bytes = rt
.block_on(RPC.shell().storage_value(
&&node,
None,
None,
false,
&Key::parse("bing/fucking/bong").expect("Test failed"),
))
.expect("Test failed")
.data;
let amount = Amount::try_from_slice(&bytes).expect("Test failed");
assert_eq!(amount, Amount::native_whole(1337));

// check that no migration is scheduled
{
let locked = node.shell.lock().unwrap();
assert!(locked.scheduled_migration.is_none());
}
Ok(())
}

fn make_migration_json() -> (Hash, tempfile::NamedTempFile) {
let file = tempfile::Builder::new().tempfile().expect("Test failed");
let updates = [migrations::DbUpdateType::Add {
key: Key::parse("bing/fucking/bong").expect("Test failed"),
cf: DbColFam::SUBSPACE,
value: Amount::native_whole(1337).into(),
force: false,
}];
let changes = migrations::DbChanges {
changes: updates.into_iter().collect(),
};
let json = serde_json::to_string(&changes).expect("Test failed");
let hash = Hash::sha256(json.as_bytes());
std::fs::write(file.path(), json).expect("Test failed");
(hash, file)
}

0 comments on commit 777d40d

Please sign in to comment.