Skip to content

Commit

Permalink
Add flashing capabilities for the Dex packages
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Dec 1, 2023
1 parent 8297f6c commit 52215c2
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 57 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"editor.rulers": [80]
"editor.rulers": [
80
],
"cSpell.words": [
"caviarnine",
"ociswap",
"uninit"
]
}
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ 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 = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "f117fb4445dee7d6467157f37b7d68e0c6a14944" }
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-stores = { 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" }

scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "b01818bde9632cf9520cf23f9590f5f9a26d1bfe" }

[profile.release]
opt-level = 'z'
Expand Down
9 changes: 6 additions & 3 deletions blueprints/unit-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ name = "unit-tests"
version = { workspace = true }
edition = { workspace = true }

[dependencies]
sbor = { workspace = true }
scrypto = { workspace = true }
[dev-dependencies]
scrypto-test = { workspace = true }
scrypto-unit = { workspace = true }
radix-engine-stores = { workspace = true }
radix-engine-interface = { workspace = true }
radix-engine-store-interface = { workspace = true }

[lib]
crate-type = ["cdylib", "lib"]
Binary file added blueprints/unit-tests/assets/caviarnine
Binary file not shown.
Binary file added blueprints/unit-tests/assets/defiplaza
Binary file not shown.
Binary file added blueprints/unit-tests/assets/ociswap
Binary file not shown.
46 changes: 0 additions & 46 deletions blueprints/unit-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1 @@
use scrypto::prelude::*;

#[blueprint]
mod hello {
struct Hello {
// Define what resources and data will be managed by Hello components
sample_vault: Vault,
}

impl Hello {
// Implement the functions and methods which will manage those resources and data

// This is a function, and can be called directly on the blueprint once deployed
pub fn instantiate_hello() -> Global<Hello> {
// Create a new token called "HelloToken," with a fixed supply of 1000, and put that supply into a bucket
let my_bucket: Bucket = ResourceBuilder::new_fungible(OwnerRole::None)
.divisibility(DIVISIBILITY_MAXIMUM)
.metadata(metadata! {
init {
"name" => "HelloToken", locked;
"symbol" => "HT", locked;
}
})
.mint_initial_supply(1000)
.into();

// Instantiate a Hello component, populating its vault with our supply of 1000 HelloToken
Self {
sample_vault: Vault::with_bucket(my_bucket),
}
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

// This is a method, because it needs a reference to self. Methods can only be called on components
pub fn free_token(&mut self) -> Bucket {
info!(
"My balance is: {} HelloToken. Now giving away a token!",
self.sample_vault.amount()
);
// If the semi-colon is omitted on the last line, the last value seen is automatically returned
// In this case, a bucket containing 1 HelloToken is returned
self.sample_vault.take(1)
}
}
}
164 changes: 164 additions & 0 deletions blueprints/unit-tests/tests/environments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//! This module contains the code used to create the various test environments
//! that are used, more specifically the [`TestRunner`] and [`TestEnvironment`].
//! More specifically, this creates those objects and flashes the exchange
//! packages to the substate store making them ready to be used. All of the
//! functions and methods in this module do unwraps and panics just because they
//! will be used in tests where we do not want to deal with their errors.

use radix_engine_interface::prelude::*;
use radix_engine_store_interface::interface::*;
use scrypto_test::prelude::*;
use scrypto_unit::*;

type PackageSubstates = HashMap<DbPartitionKey, HashMap<DbSortKey, Vec<u8>>>;

pub struct Environment<T> {
pub environment: T,
pub caviarnine_package: PackageAddress,
pub ociswap_package: PackageAddress,
pub defiplaza_package: PackageAddress,
}

fn new_test_environment() -> Environment<TestEnvironment> {
// Placeholders for initializations.
let mut caviarnine_package = PACKAGE_PACKAGE;
let mut ociswap_package = PACKAGE_PACKAGE;
let mut defiplaza_package = PACKAGE_PACKAGE;

let 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,
);
});

Environment {
environment: env,
caviarnine_package,
ociswap_package,
defiplaza_package,
}
}

pub fn new_test_runner() -> Environment<DefaultTestRunner> {
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,
);

Environment {
environment: test_runner,
caviarnine_package,
ociswap_package,
defiplaza_package,
}
}

fn flash<S: CommittableSubstateDatabase>(
package_substates: &[u8],
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);
substate_database.commit(&database_updates);
package_address
}

fn decode_package_substates(
package_substates: &[u8],
) -> HashMap<DbPartitionKey, HashMap<DbSortKey, Vec<u8>>> {
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));
}
}

database_updates
}

#[cfg(test)]
mod tests {
use super::*;

const PACKAGE_SUBSTATES: [&[u8]; 3] = [
include_bytes!("../assets/caviarnine").as_slice(),
include_bytes!("../assets/defiplaza").as_slice(),
include_bytes!("../assets/ociswap").as_slice(),
];

#[test]
fn packages_can_be_flashed_to_in_memory_substate_database() {
// Arrange
let mut substate_database = InMemorySubstateDatabase::standard();
for substates in PACKAGE_SUBSTATES {
// Act & Assert
let _ = flash(substates, &mut substate_database);
}
}

#[test]
fn test_runner_can_be_created_with_flashed_packages() {
let Environment {
environment: _,
caviarnine_package: _,
ociswap_package: _,
defiplaza_package: _,
} = new_test_runner();
}

#[test]
fn test_env_can_be_created_with_flashed_packages() {
let Environment {
environment: _,
caviarnine_package: _,
ociswap_package: _,
defiplaza_package: _,
} = new_test_environment();
}
}

0 comments on commit 52215c2

Please sign in to comment.