From abe286ecb800938084b00ad36124aa5c68768f42 Mon Sep 17 00:00:00 2001 From: Doris Benda Date: Tue, 13 Feb 2024 18:02:29 +0200 Subject: [PATCH] Address comments --- gallery/Dockerfile | 2 +- sponsoredTransactions/Dockerfile | 2 +- sponsoredTransactionsAuction/Dockerfile | 2 +- trackAndTrace/smart-contract/README.md | 29 ++++- trackAndTrace/test-scripts/Cargo.lock | 4 +- trackAndTrace/test-scripts/Cargo.toml | 2 +- trackAndTrace/test-scripts/README.md | 25 ++--- trackAndTrace/test-scripts/src/main.rs | 135 +++++++++--------------- 8 files changed, 93 insertions(+), 108 deletions(-) diff --git a/gallery/Dockerfile b/gallery/Dockerfile index c63cfd8b..0c9056bf 100644 --- a/gallery/Dockerfile +++ b/gallery/Dockerfile @@ -1,5 +1,5 @@ ARG node_base_image=node:20-slim -ARG rust_base_image=rust:1.65 +ARG rust_base_image=rust:1.72 FROM ${node_base_image} AS node_build diff --git a/sponsoredTransactions/Dockerfile b/sponsoredTransactions/Dockerfile index 57507b84..656f8ff8 100644 --- a/sponsoredTransactions/Dockerfile +++ b/sponsoredTransactions/Dockerfile @@ -1,5 +1,5 @@ ARG node_base_image=node:20-slim -ARG rust_base_image=rust:1.65 +ARG rust_base_image=rust:1.72 FROM ${node_base_image} AS frontend diff --git a/sponsoredTransactionsAuction/Dockerfile b/sponsoredTransactionsAuction/Dockerfile index 3f061f17..05ddf372 100644 --- a/sponsoredTransactionsAuction/Dockerfile +++ b/sponsoredTransactionsAuction/Dockerfile @@ -1,5 +1,5 @@ ARG node_base_image=node:18-slim -ARG rust_base_image=rust:1.70 +ARG rust_base_image=rust:1.72 FROM ${node_base_image} AS front_end diff --git a/trackAndTrace/smart-contract/README.md b/trackAndTrace/smart-contract/README.md index 5e876f6d..d374f632 100644 --- a/trackAndTrace/smart-contract/README.md +++ b/trackAndTrace/smart-contract/README.md @@ -4,7 +4,7 @@ `cargo/rustup` and `cargo-concordium` needs to be [set up](https://developer.concordium.software/en/mainnet/smart-contracts/guides/quick-start.html). -## Build +## Building Run the following command to compile the smart contract into the wasm module `module.wasm.v1` with embedded schema: @@ -22,10 +22,35 @@ cargo concordium build --schema-embed --out concordium-out/module.wasm.v1 --veri Documentation on using verifiable builds is available [here](https://docs.rs/crate/cargo-concordium/latest). -## Test +## Testing Run the following command to run the unit and integration tests: ```bash cargo concordium test --out concordium-out/module.wasm.v1 ``` + +## Deploying module to chain + +You can use Step 1 of the [smart contract developer tools](https://sctools.mainnet.concordium.software/) to deploy your module. + +Alternatively, you can use `concordium-client` with the command: + +``` +concordium-client module deploy module.wasm.v1 --sender --grpc-port 20000 --grpc-ip grpc.testnet.concordium.com --secure +``` + +Links: +- [Link to install concordium-client](https://developer.concordium.software/en/mainnet/net/references/concordium-client.html?highlight=concordium%20client) +- [Link to import account keys to concordium-client](https://developer.concordium.software/en/mainnet/smart-contracts/tutorials/setup-env.html#import-the-key) + + +## Initializing smart contract instance on chain + +Use an input parameter similar to the [inputParameter.json](../test-scripts/inputParameter.json) file. + +You can use Step 2 of the [smart contract developer tools](https://sctools.mainnet.concordium.software/) to initialize a new smart contract instance from the module reference 001be979e72f18b68ffa10634b78198e228833a42bd3d71a18c838972e67261e. + +Alternatively, you can use `concordium-client`: + + diff --git a/trackAndTrace/test-scripts/Cargo.lock b/trackAndTrace/test-scripts/Cargo.lock index 64ce8da9..c464fe76 100644 --- a/trackAndTrace/test-scripts/Cargo.lock +++ b/trackAndTrace/test-scripts/Cargo.lock @@ -637,9 +637,9 @@ dependencies = [ [[package]] name = "concordium-rust-sdk" -version = "4.0.0" +version = "4.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d98ad09fd9f5496180d1fe79b4e71ab69a88d58f2c509ccaa47659ab712681db" +checksum = "129ba5797981e1b1a22f52807087ca5b018cbe27b8932df4c963be21419d5753" dependencies = [ "aes-gcm", "anyhow", diff --git a/trackAndTrace/test-scripts/Cargo.toml b/trackAndTrace/test-scripts/Cargo.toml index 60694c9c..272a73f5 100644 --- a/trackAndTrace/test-scripts/Cargo.toml +++ b/trackAndTrace/test-scripts/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" anyhow = "1.0" chrono = "0.4" clap = { version = "4.4", features = ["env", "derive"] } -concordium-rust-sdk = { version = "4.0"} +concordium-rust-sdk = { version = "4.1"} tokio = { version = "1.35", features = ["rt-multi-thread", ] } tonic = { version = "0.10", features = ["tls", "tls-roots"] } track-and-trace = { path = "../smart-contract/", features = ["derive-serde"] } diff --git a/trackAndTrace/test-scripts/README.md b/trackAndTrace/test-scripts/README.md index 3c6c843b..13c51d68 100644 --- a/trackAndTrace/test-scripts/README.md +++ b/trackAndTrace/test-scripts/README.md @@ -58,16 +58,17 @@ The script will run a series of transactions. The output will be similar to: ``` Starting script with admin account 4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX. -Source module with reference 9c616596a6e6bc159611967fac812a2970721673c5235fafe2ad8b09e97a280a already exists. -Send initialization transaction with hash a6bf86369663ad29cdc0338da5989a8adf73e4dd517ab25649d6bfaa8a915c46. -Successfully initialized contract in block 2cda9c38f8d8a8fd4d2a9e909b27e23614aa0077d8bee6417d244821c3271d71, with address <7835,0>. -Submitted create item with index 0 in transaction hash 48fd1600bc826b5a6ab45d1649c38f7c04b767768ed32024d5754ff9d7baa99b. -Submitted create item with index 1 in transaction hash e7fb14edae19bfb15667ee0c6bc242a79b6d9bf5f4b01abab1a9d6ba076ae8b6. -Submitted update item status with index 0 to `InTransit` in transaction hash 008ee4f2cc3c587afe5fa0c207a9f198d12294eb3572fd476ce45853e3f8b140. -Submitted update item status with index 1 to `InTransit` in transaction hash 24a9f7ea7e559fda090dc032fada6ca583016055526a521d5b0e83465273817c. -Submitted update item status with index 0 to `InStore` in transaction hash ae3040e824fd6bba68db1d089373386fd2ffd2d080baf6cfe54566cb9b7340bc. -Submitted update item status with index 1 to `InStore` in transaction hash 0e8cd2d71cf8bd263ab192578a555091ac6ce7902ec3ac41f60e66d3b29abe45. -Submitted update item status with index 0 to `Sold` in transaction hash 893cf54466021cd6eb4201700e57d61aaf9736150d8b2335dde274dba57d158d. -Submitted update item status with index 1 to `Sold` in transaction hash 1e1a71cd1292849974a837a50cb4d9917f1eea85248d582881763d12f553ac5b. -Script completed successfully. +Source module with reference f94a1503030142b4dfdd6667875403783e6f457eafb020f0a957270cfe7499e9 already exists. +The maximum amount of NRG allowed for the transaction is 4075. +Transaction a2cc66c2e674af64e5a7b85ec1b97e87f9433a21f5ffdc795c038c0f7195a712 submitted. Waiting for finalization. +Initialized a new smart contract instance at address <8021,0>. +Submitted create item with index 0 in transaction 15f6cfe148a65cc621fba6164326466702d35df14b5dac07d35bd40b0354e9a1. +Submitted create item with index 1 in transaction db037444d4d619f5bfb195bdc7532d933120873a517dae5b87dad754da24a53d. +Submitted update item status with index 0 to `InTransit` in transaction fd2b2cbd9e9c0c1ed34306ed7fef2d698ce86465b4dbf13a1bb97538217a72b7. +Submitted update item status with index 1 to `InTransit` in transaction 73af8cedac05f3dddde14b9358481892a5c1923457dbd6e6c6e920fb5a35ccef. +Submitted update item status with index 0 to `InStore` in transaction hash 94cff0ec86bdfe059299798ee3aa7e62ed7b1e806ff55bdb397601d0044740c9. +Submitted update item status with index 1 to `InStore` in transaction hash 718eb74c88429c59032e2e2367f570f66b6f16b87f9e86661e2ddaf3758c5d37. +Submitted update item status with index 0 to `Sold` in transaction 88cc980eb9c58eccfa9d98605a7f463738ce17f8993cafbef2b17be17cdfe425. +Submitted update item status with index 1 to `Sold` in transaction 8b8f0729ca0d8f4ca36dcd226b042f4baa5b98fce71effff31db52d90337a466. +Script completed successfully ``` diff --git a/trackAndTrace/test-scripts/src/main.rs b/trackAndTrace/test-scripts/src/main.rs index ec4f6dc9..2109b3d3 100644 --- a/trackAndTrace/test-scripts/src/main.rs +++ b/trackAndTrace/test-scripts/src/main.rs @@ -1,15 +1,9 @@ use anyhow::Context; use clap::Parser as _; use concordium_rust_sdk::{ - base::transactions, - common::types::TransactionTime, - contract_client::{self, ViewError}, + contract_client::{ContractInitBuilder, ModuleDeployBuilder, ViewError}, smart_contracts::common::Amount, - types::{ - smart_contracts::{OwnedContractName, OwnedParameter, WasmModule}, - transactions::InitContractPayload, - Energy, WalletAccount, - }, + types::{smart_contracts::WasmModule, WalletAccount}, v2::{self as sdk, BlockIdentifier}, }; use track_and_trace::{MetadataUrl, *}; @@ -45,7 +39,7 @@ struct Args { short = 'i', help = "Number of items to be created in the contract." )] - num_items: usize, + num_items: u64, #[structopt( long = "admin-key-file", short = 'a', @@ -83,43 +77,29 @@ async fn main() -> anyhow::Result<()> { eprintln!("Starting script with admin account {}.", admin_key.address); + let module = WasmModule::from_file(&args.module).context("Could not read contract module.")?; + let mod_ref = module.get_module_ref(); + // Deploy module - let mod_ref = { - let module = WasmModule::from_file(&args.module)?; - let mod_ref = module.get_module_ref(); - if client - .get_module_source(&mod_ref, BlockIdentifier::LastFinal) - .await - .is_ok() - { - eprintln!("Source module with reference {mod_ref} already exists."); - } else { - let nonce_response = client - .get_next_account_sequence_number(&admin_key.address) - .await - .context("NonceQueryError.")?; - - let tx = transactions::send::deploy_module( - &admin_key, - admin_key.address, - nonce_response.nonce, - TransactionTime::hours_after(1), - module, - ); - let hash = client.send_account_transaction(tx).await?; + if client + .get_module_source(&mod_ref, BlockIdentifier::LastFinal) + .await + .is_ok() + { + eprintln!("Source module with reference {mod_ref} already exists."); + } else { + let builder = + ModuleDeployBuilder::dry_run_module_deploy(client.clone(), admin_key.address, module) + .await?; - eprintln!("Send deployment transaction with hash {hash}."); + let handle = builder.send(&admin_key.keys).await?; - let (block_hash, result) = client.wait_until_finalized(&hash).await?; + println!("Module deployment transaction {handle} submitted."); - if let Some(err) = result.is_rejected_account_transaction() { - anyhow::bail!("Failed to deploy module: {err:#?}"); - } + let result = handle.wait_for_finalization().await?; - eprintln!("Deployed module with reference {mod_ref} in block {block_hash}."); - } - mod_ref - }; + println!("Module {} deployed.", result.module_reference); + } // Initialize new instance let params: Vec = serde_json::from_reader( @@ -128,50 +108,30 @@ async fn main() -> anyhow::Result<()> { ) .context("Unable to parse input parameter.")?; - let mut contract_client = { - let expiry = TransactionTime::hours_after(1); - let payload = InitContractPayload { - amount: Amount::zero(), - mod_ref, - init_name: OwnedContractName::new_unchecked("init_track_and_trace".into()), - param: OwnedParameter::from_serial(¶ms).expect("Init params"), - }; - let energy = Energy::from(10000); - - let nonce_response = client - .get_next_account_sequence_number(&admin_key.address) - .await - .context("NonceQueryError.")?; - - let tx = transactions::send::init_contract( - &admin_key, - admin_key.address, - nonce_response.nonce, - expiry, - payload, - energy, - ); - let hash = client.send_account_transaction(tx).await?; + let builder = ContractInitBuilder::::dry_run_new_instance( + client, + admin_key.address, + mod_ref, + "track_and_trace", + Amount::zero(), + ¶ms, + ) + .await?; - eprintln!("Send initialization transaction with hash {hash}."); + println!( + "The maximum amount of NRG allowed for the transaction is {}.", + builder.current_energy() + ); + let handle = builder.send(&admin_key.keys).await?; - let (block_hash, result) = client.wait_until_finalized(&hash).await?; - if let Some(error) = result.is_rejected_account_transaction() { - anyhow::bail!("Failed to initialize contract: {error:#?}."); - } - let info = result.contract_init().context("Expect an init result")?; + println!("Transaction {handle} submitted. Waiting for finalization."); - eprintln!( - "Successfully initialized contract in block {block_hash}, with address {}.", - info.address - ); + let (mut contract_client, _) = handle.wait_for_finalization().await?; - contract_client::ContractClient::::create( - client.clone(), - info.address, - ) - .await? - }; + println!( + "Initialized a new smart contract instance at address {}.", + contract_client.address + ); // Create new items for i in 0..args.num_items { @@ -188,7 +148,7 @@ async fn main() -> anyhow::Result<()> { let tx_hash = tx_dry_run.send(&admin_key).await?; - eprintln!("Submitted create item with index {i} in transaction hash {tx_hash}."); + eprintln!("Submitted create item with index {i} in transaction {tx_hash}."); if let Err(err) = tx_hash.wait_for_finalization().await { anyhow::bail!("Creating item failed: {err:#?}"); @@ -198,7 +158,7 @@ async fn main() -> anyhow::Result<()> { // Update items from `Produced` to `InTransit` for i in 0..args.num_items { let param: ChangeItemStatusParams = ChangeItemStatusParams { - item_id: i as u64, + item_id: i, new_status: Status::InTransit, additional_data: AdditionalData::empty(), }; @@ -215,8 +175,7 @@ async fn main() -> anyhow::Result<()> { let tx_hash = tx_dry_run.send(&admin_key).await?; eprintln!( - "Submitted update item status with index {i} to `InTransit` in transaction hash \ - {tx_hash}." + "Submitted update item status with index {i} to `InTransit` in transaction {tx_hash}." ); if let Err(err) = tx_hash.wait_for_finalization().await { @@ -227,7 +186,7 @@ async fn main() -> anyhow::Result<()> { // Update items from `InTransit` to `InStore` for i in 0..args.num_items { let param: ChangeItemStatusParams = ChangeItemStatusParams { - item_id: i as u64, + item_id: i, new_status: Status::InStore, additional_data: AdditionalData::empty(), }; @@ -256,7 +215,7 @@ async fn main() -> anyhow::Result<()> { // Update items from `InStore` to `Sold` for i in 0..args.num_items { let param: ChangeItemStatusParams = ChangeItemStatusParams { - item_id: i as u64, + item_id: i, new_status: Status::Sold, additional_data: AdditionalData::empty(), }; @@ -273,7 +232,7 @@ async fn main() -> anyhow::Result<()> { let tx_hash = tx_dry_run.send(&admin_key).await?; eprintln!( - "Submitted update item status with index {i} to `Sold` in transaction hash {tx_hash}." + "Submitted update item status with index {i} to `Sold` in transaction {tx_hash}." ); if let Err(err) = tx_hash.wait_for_finalization().await {