diff --git a/Cargo.toml b/Cargo.toml index e9eff0f2..b6d5ff74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,18 +12,18 @@ edition = "2021" description = "The implementation of project Olympus in Scrypto for the Radix Ledger" [workspace.dependencies] -sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine-stores = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } +sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine-stores = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } -scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } -scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" } +scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } +scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "0d1952c8c981497f35bb6051f17394df265ca65e" } [profile.release] opt-level = 'z' diff --git a/blueprints/unit-tests/Cargo.toml b/blueprints/unit-tests/Cargo.toml index 5a95b485..aa4f94fd 100644 --- a/blueprints/unit-tests/Cargo.toml +++ b/blueprints/unit-tests/Cargo.toml @@ -25,6 +25,7 @@ lazy_static = { version = "1.4.0" } [build-dependencies] walkdir = { version = "2.3.3", optional = true } cargo_toml = { version = "0.15.3", optional = true } +flate2 = { version = "1.0.28" } scrypto = { workspace = true } scrypto-unit = { workspace = true } diff --git a/blueprints/unit-tests/assets/caviarnine b/blueprints/unit-tests/assets/caviarnine deleted file mode 100644 index a7d52930..00000000 Binary files a/blueprints/unit-tests/assets/caviarnine and /dev/null differ diff --git a/blueprints/unit-tests/assets/defiplaza b/blueprints/unit-tests/assets/defiplaza deleted file mode 100644 index 80fb8607..00000000 Binary files a/blueprints/unit-tests/assets/defiplaza and /dev/null differ diff --git a/blueprints/unit-tests/assets/ociswap b/blueprints/unit-tests/assets/ociswap deleted file mode 100644 index 81008035..00000000 Binary files a/blueprints/unit-tests/assets/ociswap and /dev/null differ diff --git a/blueprints/unit-tests/assets/state b/blueprints/unit-tests/assets/state new file mode 100644 index 00000000..3f20811d Binary files /dev/null and b/blueprints/unit-tests/assets/state differ diff --git a/blueprints/unit-tests/build.rs b/blueprints/unit-tests/build.rs index 0d963272..e2eb7ccb 100644 --- a/blueprints/unit-tests/build.rs +++ b/blueprints/unit-tests/build.rs @@ -1,8 +1,13 @@ +fn main() { + build_blueprints(); + decompress_state(); +} + #[cfg(not(feature = "compile-blueprints-at-build-time"))] -fn main() {} +fn build_blueprints() {} #[cfg(feature = "compile-blueprints-at-build-time")] -fn main() { +fn build_blueprints() { use std::env; use std::path::PathBuf; @@ -57,3 +62,24 @@ fn main() { let encoded_packages = scrypto_encode(&packages).unwrap(); std::fs::write(compilation_path, encoded_packages).unwrap(); } + +fn decompress_state() { + use flate2::read::*; + + use std::env; + use std::io::prelude::*; + use std::path::*; + use std::str::FromStr; + + let compressed = include_bytes!("./assets/state"); + let mut decoder = GzDecoder::new(&compressed[..]); + let mut uncompressed = Vec::new(); + decoder + .read_to_end(&mut uncompressed) + .expect("Failed to decompress!"); + + let path = PathBuf::from_str(env::var("OUT_DIR").unwrap().as_str()) + .unwrap() + .join("uncompressed_state.bin"); + std::fs::write(path, uncompressed).unwrap(); +} diff --git a/blueprints/unit-tests/tests/utils/environments.rs b/blueprints/unit-tests/tests/utils/environments.rs index 239b73d1..260fe57d 100644 --- a/blueprints/unit-tests/tests/utils/environments.rs +++ b/blueprints/unit-tests/tests/utils/environments.rs @@ -13,6 +13,12 @@ use scrypto::prelude::{RoleDefinition, ToRoleEntry}; use scrypto_test::prelude::*; use scrypto_unit::*; +type BranchStore = + HashMap>>>; + +const PACKAGES_BINARY: &[u8] = + include_bytes!(concat!(env!("OUT_DIR"), "/uncompressed_state.bin")); + type PackageSubstates = HashMap>>; pub struct Environment { @@ -35,24 +41,16 @@ pub struct Resources { } pub fn new_test_environment() -> Environment { - // Placeholders for initializations. - let mut caviarnine_package = PACKAGE_PACKAGE; - let mut ociswap_package = PACKAGE_PACKAGE; - let mut defiplaza_package = PACKAGE_PACKAGE; + let (addresses, branch_store) = + scrypto_decode::<(Vec, BranchStore)>(PACKAGES_BINARY) + .expect("Can't fail!"); + + let caviarnine_package = PackageAddress::try_from(addresses[0]).unwrap(); + let ociswap_package = PackageAddress::try_from(addresses[1]).unwrap(); + let defiplaza_package = PackageAddress::try_from(addresses[2]).unwrap(); let mut env = TestEnvironment::new_custom(|substate_database| { - caviarnine_package = flash( - include_bytes!("../../assets/caviarnine").as_slice(), - substate_database, - ); - ociswap_package = flash( - include_bytes!("../../assets/ociswap").as_slice(), - substate_database, - ); - defiplaza_package = flash( - include_bytes!("../../assets/defiplaza").as_slice(), - substate_database, - ); + flash_branch_store(branch_store, substate_database) }); // Creating the resources. They are all freely mintable to make the tests @@ -94,18 +92,15 @@ pub fn new_test_runner() -> Environment { let mut test_runner = TestRunnerBuilder::new().build(); let substate_database = test_runner.substate_db_mut(); - let caviarnine_package = flash( - include_bytes!("../../assets/caviarnine").as_slice(), - substate_database, - ); - let ociswap_package = flash( - include_bytes!("../../assets/ociswap").as_slice(), - substate_database, - ); - let defiplaza_package = flash( - include_bytes!("../../assets/defiplaza").as_slice(), - substate_database, - ); + let substates = + scrypto_decode::<(Vec, BranchStore)>(PACKAGES_BINARY) + .expect("Can't fail!"); + + let caviarnine_package = substates.0[0]; + let ociswap_package = substates.0[1]; + let defiplaza_package = substates.0[2]; + + flash_branch_store(substates.1, substate_database); let [bitcoin, ethereum, usdc, usdt] = [8, 18, 6, 6].map(|divisibility| { let manifest = ManifestBuilder::new() @@ -155,59 +150,44 @@ pub fn new_test_runner() -> Environment { } } -fn flash( - package_substates: &[u8], +fn flash_branch_store( + branch_store: BranchStore, substate_database: &mut S, -) -> PackageAddress { - let package_substates = decode_package_substates(package_substates); - let package_address = extract_package_address(&package_substates); - let database_updates = database_updates(package_substates); +) { + let database_updates = database_updates(branch_store); substate_database.commit(&database_updates); - package_address -} - -fn decode_package_substates( - package_substates: &[u8], -) -> HashMap>> { - scrypto_decode(package_substates) - .expect("Decoding of package can not fail!") } -fn extract_package_address( - package_substates: &PackageSubstates, -) -> PackageAddress { - package_substates - .keys() - .map(|item| { - PackageAddress::try_from( - SpreadPrefixKeyMapper::from_db_partition_key(item).0, - ) - .unwrap() - }) - .next() - .unwrap() -} - -fn database_updates(package_substates: PackageSubstates) -> DatabaseUpdates { - let mut database_updates = DatabaseUpdates::default(); - - for (partition_key, substate_values) in package_substates.into_iter() { - for (sort_key, substate_value) in substate_values.into_iter() { - let PartitionDatabaseUpdates::Delta { substate_updates } = - database_updates - .node_updates - .entry(partition_key.node_key.clone()) - .or_default() - .partition_updates - .entry(partition_key.partition_num) - .or_default() - else { - panic!("Can't happen!") - }; - substate_updates - .insert(sort_key, DatabaseUpdate::Set(substate_value)); - } +fn database_updates(branch_store: BranchStore) -> DatabaseUpdates { + DatabaseUpdates { + node_updates: branch_store + .into_iter() + .map(|(db_node_key, partition_num_to_updates_mapping)| { + ( + db_node_key, + NodeDatabaseUpdates { + partition_updates: partition_num_to_updates_mapping + .into_iter() + .map(|(partition_num, substates)| { + ( + partition_num, + PartitionDatabaseUpdates::Delta { + substate_updates: substates + .into_iter() + .map(|(db_sort_key, value)| { + ( + db_sort_key, + DatabaseUpdate::Set(value), + ) + }) + .collect(), + }, + ) + }) + .collect(), + }, + ) + }) + .collect(), } - - database_updates }