Expand description
§Concordium Smart Contract Testing
This library supports writing integration tests in Rust for Concordium smart contracts.
To use the library, you must add it to your Cargo.toml
file under the
[dev-dependencies]
section. The library requries the rust edition 2021
or greater.
[package]
# ...
edition = "2021"
[dev-dependencies]
concordium-smart-contract-testing = "4.0"
§Basic usage
use concordium_smart_contract_testing::*;
// Create a "chain" with default parameters.
let mut chain = Chain::new();
// Define an account address to be used.
const ACC: AccountAddress = AccountAddress([0;32]);
// Create an account with 10000 CCD in balance.
chain.create_account(Account::new(ACC, Amount::from_ccd(1000)));
// Deploy a smart contract module (built with [Cargo Concordium](https://developer.concordium.software/en/mainnet/smart-contracts/guides/setup-tools.html#cargo-concordium)).
let deployment = chain
.module_deploy_v1(
Signer::with_one_key(),
ACC,
module_load_v1("path/to/contract.wasm.v1").unwrap())
.unwrap();
// Initialize a smart contract from the deployed module.
let initialization = chain
.contract_init(
Signer::with_one_key(), // Used for specifying the number of signatures.
ACC, // Invoker account.
Energy::from(10000), // Maximum energy allowed for initializing.
InitContractPayload {
mod_ref: deployment.module_reference, // Module to initialize from.
init_name: OwnedContractName::new_unchecked("init_my_contract".into()), // Contract to init.
param: OwnedParameter::from_serial(&"my_param").unwrap(), // Any type implementing [`Serial`] can be used.
amount: Amount::zero(), // CCD to send the contract.
}
)
.unwrap();
// Update the initialized contract.
let update = chain
.contract_update(
Signer::with_one_key(), // Used for specifying the number of signatures.
ACC, // Invoker account.
Address::Account(ACC), // Sender (can also be a contract).
Energy::from(10000), // Maximum energy allowed for the update.
UpdateContractPayload {
address: initialization.contract_address, // The contract to update.
receive_name: OwnedReceiveName::new_unchecked("my_contract.my_entrypoint".into()), // The receive function to call.
message: OwnedParameter::from_serial(&42u8).unwrap(), // The parameter sent to the contract.
amount: Amount::from_ccd(100), // Sending the contract 100 CCD.
}
)
.unwrap();
// Check the trace elements produced (updates, interrupts, resumes, transfers, etc.).
assert!(matches!(update.effective_trace_elements().collect::<Vec<_>>()[..], [ContractTraceElement::Updated{..}]));
// Check the return value.
assert_eq!(update.return_value, to_bytes(&84u8));
// Check the balances of both contracts and accounts.
assert_eq!(chain.contract_balance(initialization.contract_address), Some(Amount::from_ccd(100)));
assert_eq!(chain.account_balance_available(ACC), Some(
Amount::from_ccd(1000)
- Amount::from_ccd(100) // Amount sent to contract.
- deployment.transaction_fee
- initialization.transaction_fee
- update.transaction_fee));
Modules§
- ed25519
- We expose the
PublicKey
,SecretKey
,SigningKey
andSignature
from the third-partyed25519_dalek
crate here because these types appear in Concordium’s API.
Structs§
- Account
- An account.
- Account
Access Structure - The most straighforward account access structure is a map of public keys with the account threshold.
- Account
Address - Address of an account, as raw bytes.
- Account
Balance - The current public balances of an account.
- Account
Does NotExist - The account does not exist.
- Account
Keys - All account keys indexed by credentials.
- Account
Signatures - A signature with account’s keys.
- Amount
- The type of amounts on the chain.
- Block
Time Overflow - The block time overflowed during a call to
Chain::tick_block_time
. - Chain
- Represents the blockchain and supports a number of operations, including creating accounts, deploying modules, initializing contract, updating contracts and invoking contracts.
- Chain
Builder - A builder for the
Chain
. - Contract
- A smart contract instance.
- Contract
Address - Address of a contract.
- Contract
Does NotExist - The contract instance does not exist.
- Contract
Event - An event logged by a smart contract initialization.
- Contract
Init Error - An error that occurred in
Chain::contract_init
. - Contract
Init Success - Represents a successful initialization of a contract.
- Contract
Invoke Error - An error that occurred during a
Chain::contract_update
orChain::contract_invoke
. - Contract
Invoke External Success - Represents a successful external contract invocation.
- Contract
Invoke Success - Represents a successful contract update (or invocation).
- Contract
Module - A smart contract module.
- Contract
Name - A contract name. Expected format: “init_<contract_name>”.
- Credential
Index - Index of the credential that is to be used.
- Credential
Public Keys - Public credential keys currently on the account, together with the threshold needed for a valid signature on a transaction.
- Debug
Item - Information about the collected debug output. This is the item returned
by the
debug_events
iterator. It corresponds to a section of execution between interrupts. - Duration
- Duration of time in milliseconds.
- Endpoint
- Channel builder.
- Energy
- Energy measure.
- Entrypoint
Does NotExist - The entrypoint does not exist.
- Entrypoint
Name - An entrypoint name (borrowed version). Expected format: “<func_name>” where the name of the function consists solely of ASCII alphanumeric or punctuation characters.
- Exchange
Rate - An exchange rate between two quantities. This is never 0, and the exchange rate should also never be infinite.
- Exchange
Rate Error - The provided exchange rates are not valid.
Meaning that they do not correspond to one energy costing less than
u64::MAX / 100_000_000_000
. - Execution
Error - An error that occurred while executing a contract init or receive function.
- External
Account Address - The address of an account on an external node.
- External
Contract Address - The contract address of an contract on an external node.
- External
Node NotConfigured - The error returned when an external node has not been configured prior to using it.
- Host
Call Info - Information about an emitted host function call. This is the item returned
by the
host_calls
iterator. - Init
Contract Payload - Data needed to initialize a smart contract.
- Instance
Updated Event - Data generated as part of updating a single contract instance. In general a single Update transaction will generate one or more of these events, together with possibly some transfers.
- Invoke
External Contract Payload - Data needed to invoke an external smart contract instance.
- KeyIndex
- Index of an account key that is to be used.
- Module
Deploy Error - An error that occurred while deploying a
ContractModule
. - Module
Deploy Success - Represents a successful deployment of a
ContractModule
. - Module
Does NotExist - The contract module does not exist.
- Module
Invalid Error - The error produced when trying to parse a smart contract module.
- Module
Load Error - An error that can occur while loading a smart contract module.
- Module
Read Error - The error produced when trying to read a smart contract module from a file.
- Owned
Contract Name - A contract name (owned version). Expected format: “init_<contract_name>”.
- Owned
Entrypoint Name - An entrypoint name (owned version). Expected format:
“<func_name>”. Most methods on this type are available via the
as_entrypoint_name
and the methods on theEntrypointName
type. - Owned
Parameter - Parameter to the init function or entrypoint. Owned version.
- Owned
Receive Name - A receive name (owned version). Expected format:
“<contract_name>.<func_name>”. Most methods are available only on the
ReceiveName
type, the intention is to access those via theas_receive_name
method. - Parameter
- Parameter to the init function or entrypoint.
- Receive
Name - A receive name. Expected format: “<contract_name>.<func_name>”.
- Signer
- A signer with a number of keys, the amount of which affects the cost of transactions.
- Timestamp
- Timestamp represented as milliseconds since unix epoch.
- Transfer
- A transfer from a contract to an account.
- Update
Contract Payload - Data needed to update a smart contract instance.
- Zero
Keys Error - A
Signer
cannot be created with0
keys.
Enums§
- Address
- Either an address of an account, or contract.
- Chain
Builder Error - Contract
Init Error Kind - Types of errors that can occur in
Chain::contract_init
. - Contract
Invoke Error Kind - The error kinds that can occur during
Chain::contract_update
orChain::contract_invoke
. - Contract
Invoke External Error - The error returned when external contract invocations fail.
- Contract
Trace Element - A successful contract invocation produces a sequence of effects on smart contracts and possibly accounts (if any contract transfers CCD to an account).
- Debug
Output Kind - The different types of debug output that can be printed by the
print_debug
method. - Debug
Trace Element - A wrapper for
ContractTraceElement
, which provides additional information for testing and debugging. Most notably, it contains trace elements for failures, which are normally discarded by the node. - External
Address - Either an external contract address or an external account address.
- External
Node Error - Errors that occur while trying to communicate with an external node.
- Init
Execution Error - The reason for why a contract initialization failed during execution.
- Invoke
Execution Error - The reason for why a contract invocation failed during execution.
- Invoke
Failure - The kind of errors that may occur during handling of contract
invoke
orupgrade
. - Module
Deploy Error Kind - The specific kind of error that occurred while deploying a
ContractModule
. - Module
Load Error Kind - The specific reason why loading a module failed.
- Output
Module Load Error - Reject
Reason - A reason for why a transaction was rejected. Rejected means included in a block, but the desired action was not achieved. The only effect of a rejected transaction is payment.
- Setup
External Node Error - Errors that occur while setting up the connection to an external node.
- Verify
Key - Public AKA verification key for a given scheme. Only ed25519 is currently supported.
- Wasm
Version - Version of the module. This determines the chain API that the module can access.
Constants§
- CONTRACT_
MODULE_ OUTPUT_ PATH_ ENV_ VAR - The name of the environment variable that holds the path to the contract
module file. To load the module, use the
module_load_output
function.
Traits§
- Debug
Info Ext - A trait implemented by types which can extract debug information from contract receive entrypoint executions.
Functions§
- from_
bytes - Dual to
to_bytes
. - is_
debug_ enabled - Return whether execution is running under
cargo concordium test
with debugging enabled. - module_
load_ output - Load the current smart contract module output using the environment variable
CARGO_CONCORDIUM_TEST_MODULE_OUTPUT_PATH
which is set when running usingcargo concordium test
. - module_
load_ v1 - Load a v1 wasm module as it is output from
cargo concordium build
, i.e. including the prefix of 4 version bytes and 4 module length bytes. - module_
load_ v1_ raw - Load a raw wasm module, i.e. one without the prefix of 4 version bytes and 4 module length bytes. The module still has to be a valid V1 smart contract module.
- to_
bytes - Serialize the given value to a freshly allocated vector of bytes using
the provided
Serial
instance.
Type Aliases§
- Account
Threshold - The minimum number of credentials that need to sign any transaction coming from an associated account.
- Block
Hash - Hash of a block.
- Module
Reference - A reference to a smart contract module deployed on the chain.
- Signature
Threshold - The minimum number of signatures on a credential that need to sign any transaction coming from an associated account.
- Slot
Time - Time at the beginning of the current slot, in miliseconds since unix epoch.