Skip to content

Commit

Permalink
Add optional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Oct 16, 2023
1 parent 717901e commit 834c0f7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 56 deletions.
33 changes: 16 additions & 17 deletions templates/default/deploy-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ cargo run
The following options are necessary when running the script

```
--node <CONCORDIUM_URL>
V2 API of the concordium node. [default: http://node.testnet.concordium.com:20000]
--account <CONCORDIUM_ACCOUNT>
Location path and file name of the Concordium account key file (e.g. ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export).
--modules <MODULES>
Location paths and names of Concordium smart contract modules. Use this flag several times \
if you have several smart contract modules to be deployed (e.g. --modules ./myPath/default.wasm.v1 --modules ./default2.wasm.v1).
--node <CONCORDIUM_NODE_URL>
V2 API of the concordium node. [default: http://node.testnet.concordium.com:20000]
--account <CONCORDIUM_ACCOUNT_PATH>
Path to the file containing the Concordium account keys exported from the wallet (e.g. ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export).
--module <MODULE_PATH>
Path of the Concordium smart contract module. Use this flag several times \
if you have several smart contract modules to be deployed (e.g. --module ./myPath/default.wasm.v1 --module ./default2.wasm.v1).
--no-logging <BOOL>
To specify if verbose logging should be disabled when running the script.
```

The `account` parameter should be a Concordium wallet account either exported from the
Expand All @@ -32,7 +34,7 @@ genesis tool.

Example:
```
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --modules ./myPath/default.wasm.v1 --modules ./default2.wasm.v1
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./myPath/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --module ./myPath/default.wasm.v1 --module ./default2.wasm.v1
```

# Functionalities
Expand Down Expand Up @@ -68,23 +70,20 @@ cargo concordium build --out ./deploy-scripts/default.wasm.v1
Navigate into the deploy-scripts folder and run the example with the `default` smart contract (replace your wallet account in the below command):

```
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --modules ./default.wasm.v1
cargo run -- --node http://node.testnet.concordium.com:20000 --account ./4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --module ./default.wasm.v1
```

The output should be:

```
Deploying module....
Module with reference 15c936d9f60dc99c543282a8f16823d2ec5c6faae689772ae06a9b2de45a39d0 already exists on the chain.
Module with reference 3774d4b9ae86ae3c5192e13455d7515073f5163a25deabc55abdab31d1cc002e already exists on the chain.
Initializing contract....
Sent tx: 09ecaa6a66e4fe2a756dd9ad8c91f5fc2099a6dd30ebd4532cb8c5aad1bab440
Transaction finalized, tx_hash=09ecaa6a66e4fe2a756dd9ad8c91f5fc2099a6dd30ebd4532cb8c5aad1bab440 contract=(6941, 0)
Estimating energy....
Contract invoke success: estimated_energy=731
Sent tx: bdb43d1f00a4c5ba02ec81e0e2da52b6920582a16acd21a364ec3e3734ad4f12
Transaction finalized: tx_hash=bdb43d1f00a4c5ba02ec81e0e2da52b6920582a16acd21a364ec3e3734ad4f12 contract=(7000, 0)
Updating contract....
Sent tx: c61b40a09e422835c70b07369bc5f4bba8292499be80cd735af21941c9798dd2
Transaction finalized, tx_hash=c61b40a09e422835c70b07369bc5f4bba8292499be80cd735af21941c9798dd2
Sent tx: 4843efc3b700bce8e67f2cc3f17da3124cf0a7323652fb778412ecd768ae2fe5
Transaction finalized: tx_hash=4843efc3b700bce8e67f2cc3f17da3124cf0a7323652fb778412ecd768ae2fe5
```
65 changes: 43 additions & 22 deletions templates/default/deploy-scripts/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@ impl<'a> Deployer<'a> {
&mut self,
wasm_module: WasmModule,
expiry: Option<TransactionTime>,
logging: bool,
) -> Result<(Option<HashBytes<TransactionMarker>>, ModuleDeployed), DeployError> {
println!("\nDeploying module....");
if logging {
println!("\nDeploying module....");
}

let exists = self.module_exists(&wasm_module.get_module_ref()).await?;

if exists {
println!(
"Module with reference {} already exists on the chain.",
wasm_module.get_module_ref()
);
if logging {
println!(
"Module with reference {} already exists on the chain.",
wasm_module.get_module_ref()
);
}
return Ok((None, ModuleDeployed {
module_ref: wasm_module.get_module_ref(),
}));
Expand All @@ -126,16 +131,20 @@ impl<'a> Deployer<'a> {
.await
.map_err(DeployError::TransactionRejected)?;

println!("Sent tx: {tx_hash}");
if logging {
println!("Sent tx: {tx_hash}");
}

let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

let module_deployed = self.check_outcome_of_deploy_transaction(block_item)?;

println!(
"Transaction finalized, tx_hash={} module_ref={}",
tx_hash, module_deployed.module_ref,
);
if logging {
println!(
"Transaction finalized: tx_hash={} module_ref={}",
tx_hash, module_deployed.module_ref,
);
}

Ok((Some(tx_hash), module_deployed))
}
Expand All @@ -154,8 +163,11 @@ impl<'a> Deployer<'a> {
payload: InitContractPayload,
energy: Option<Energy>,
expiry: Option<TransactionTime>,
logging: bool,
) -> Result<(HashBytes<TransactionMarker>, ContractAddress), DeployError> {
println!("\nInitializing contract....");
if logging {
println!("\nInitializing contract....");
}

let nonce = self.get_nonce(self.key.address).await?;

Expand All @@ -182,16 +194,20 @@ impl<'a> Deployer<'a> {
.await
.map_err(DeployError::TransactionRejected)?;

println!("Sent tx: {tx_hash}");
if logging {
println!("Sent tx: {tx_hash}");
}

let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

let contract_init = self.check_outcome_of_initialization_transaction(block_item)?;

println!(
"Transaction finalized, tx_hash={} contract=({}, {})",
tx_hash, contract_init.contract.index, contract_init.contract.subindex,
);
if logging {
println!(
"Transaction finalized: tx_hash={} contract=({}, {})",
tx_hash, contract_init.contract.index, contract_init.contract.subindex,
);
}

Ok((tx_hash, contract_init.contract))
}
Expand All @@ -211,8 +227,11 @@ impl<'a> Deployer<'a> {
update_payload: UpdateContractPayload,
energy: Option<GivenEnergy>,
expiry: Option<TransactionTime>,
logging: bool,
) -> Result<HashBytes<TransactionMarker>, DeployError> {
println!("\nUpdating contract....");
if logging {
println!("\nUpdating contract....");
}

let nonce = self.get_nonce(self.key.address).await?;

Expand Down Expand Up @@ -249,13 +268,17 @@ impl<'a> Deployer<'a> {
.await
.map_err(DeployError::TransactionRejected)?;

println!("Sent tx: {tx_hash}");
if logging {
println!("Sent tx: {tx_hash}");
}

let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

self.check_outcome_of_update_transaction(block_item)?;

println!("Transaction finalized, tx_hash={}", tx_hash,);
if logging {
println!("Transaction finalized: tx_hash={}", tx_hash,);
}

Ok(tx_hash)
}
Expand All @@ -274,8 +297,6 @@ impl<'a> Deployer<'a> {
payload: UpdateContractPayload,
max_energy: Option<Energy>,
) -> Result<Energy, DeployError> {
println!("\nEstimating energy....");

let best_block = self.client.get_block_finalization_summary(Best).await?;

let best_block_hash = best_block.block_hash;
Expand Down Expand Up @@ -310,7 +331,7 @@ impl<'a> Deployer<'a> {
used_energy,
} => {
let e = used_energy.energy;
println!("Contract invoke success: estimated_energy={e}");

Ok(Energy {
energy: e,
})
Expand Down
40 changes: 23 additions & 17 deletions templates/default/deploy-scripts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,29 @@ struct App {
default_value = "http://node.testnet.concordium.com:20000",
help = "V2 API of the Concordium node."
)]
url: v2::Endpoint,
url: v2::Endpoint,
#[clap(
long = "account",
help = "Location path and file name of the Concordium account key file (e.g. \
./myPath/3PXwJYYPf6fyVb4GJquxSZU8puxrHfzc4XogdMVot8MUQK53tW.export)."
help = "Path to the file containing the Concordium account keys exported from the wallet \
(e.g. ./myPath/3PXwJYYPf6fyVb4GJquxSZU8puxrHfzc4XogdMVot8MUQK53tW.export)."
)]
key_file: PathBuf,
key_file: PathBuf,
#[clap(
long = "modules",
help = "Location paths and names of Concordium smart contract modules. Use this flag \
several times if you have several smart contract modules to be deployed (e.g. \
--modules ./myPath/default.wasm.v1 --modules ./default2.wasm.v1)."
long = "module",
help = "Path of the Concordium smart contract module. Use this flag several times if you \
have several smart contract modules to be deployed (e.g. --module \
./myPath/default.wasm.v1 --module ./default2.wasm.v1)."
)]
modules: Vec<PathBuf>,
module: Vec<PathBuf>,
#[clap(
long = "no_logging",
help = "To specify if verbose logging should be disabled when running the script."
)]
no_logging: bool,
}

/// Main function: It deploys to chain all wasm modules from the command line
/// `--modules` flags. Write your own custom deployment/initialization script in
/// `--module` flags. Write your own custom deployment/initialization script in
/// this function. An deployment/initialization script example is given in this
/// function for the `default` smart contract.
#[tokio::main(flavor = "current_thread")]
Expand All @@ -115,10 +120,10 @@ async fn main() -> Result<(), DeployError> {

let mut modules_deployed: Vec<ModuleDeployed> = Vec::new();

for contract in app.modules.iter().unique() {
for contract in app.module.iter().unique() {
let wasm_module = get_wasm_module(contract.as_path())?;

let (_, module) = deployer.deploy_wasm_module(wasm_module, None).await?;
let (_, module) = deployer.deploy_wasm_module(wasm_module, None, !app.no_logging).await?;

modules_deployed.push(module);
}
Expand All @@ -137,12 +142,12 @@ async fn main() -> Result<(), DeployError> {
param,
}; // Example

let (_, contract) = deployer.init_contract(payload, None, None).await?; // Example
let (_, contract) = deployer.init_contract(payload, None, None, !app.no_logging).await?; // Example

// This is how you can use a type from your smart contract.
use {{crate_name}}::{MyInputType};
use {{crate_name}}::MyInputType; // Example

let input_parameter: MyInputType = false;
let input_parameter: MyInputType = false; // Example

// Create a successful transaction.

Expand All @@ -160,8 +165,9 @@ async fn main() -> Result<(), DeployError> {
// We add 100 energy to be safe.
energy.energy += 100; // Example

let _update_contract =
deployer.update_contract(update_payload, Some(GivenEnergy::Add(energy)), None).await?; // Example
let _update_contract = deployer
.update_contract(update_payload, Some(GivenEnergy::Add(energy)), None, !app.no_logging)
.await?; // Example

// Write your own deployment/initialization script above. An example is given
// here.
Expand Down

0 comments on commit 834c0f7

Please sign in to comment.