Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c1dec4b21 | |||
| 0492af4bf9 | |||
| 2e6b2b49dc | |||
| ca180ca6c2 | |||
| e5efc18912 | |||
| f01713c1ff | |||
| fd19fb529c | |||
| 66b2f1f051 | |||
| 090efa7263 | |||
| 310de00694 | |||
| 080356f46b | |||
| 7543e4b997 | |||
| f42588a985 | |||
| f8b4faf974 | |||
| 2d7b2b1fda | |||
| fdd5b55e14 |
@@ -10,11 +10,11 @@ defaults:
|
||||
|
||||
jobs:
|
||||
publish-tauri:
|
||||
if: ${{ (startsWith(github.ref, 'refs/tags/nym-connect-') && github.event_name == 'release') || github.event_name == 'workflow_dispatch' }}
|
||||
if: ${{ startsWith(github.ref, 'refs/tags/nym-connect-') && github.event_name == 'release' }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
platform: [custom-runner-mac-m1]
|
||||
platform: [macos-latest]
|
||||
|
||||
runs-on: ${{ matrix.platform }}
|
||||
steps:
|
||||
@@ -24,15 +24,10 @@ jobs:
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: 16
|
||||
|
||||
- name: Setup yarn
|
||||
run: npm install -g yarn
|
||||
|
||||
- name: Install Rust stable
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: stable
|
||||
|
||||
- name: Install the Apple developer certificate for code signing
|
||||
env:
|
||||
APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
|
||||
@@ -44,7 +39,7 @@ jobs:
|
||||
KEYCHAIN_PATH=$RUNNER_TEMP/app-signing.keychain-db
|
||||
|
||||
# import certificate and provisioning profile from secrets
|
||||
echo -n "$APPLE_CERTIFICATE" | base64 --decode --output=$CERTIFICATE_PATH
|
||||
echo -n "$APPLE_CERTIFICATE" | base64 --decode --output $CERTIFICATE_PATH
|
||||
|
||||
# create temporary keychain
|
||||
security create-keychain -p "$KEYCHAIN_PASSWORD" $KEYCHAIN_PATH
|
||||
|
||||
Generated
+25
@@ -902,6 +902,21 @@ dependencies = [
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cosmwasm-contract-testing"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.21.0",
|
||||
"cosmwasm-std",
|
||||
"cosmwasm-storage",
|
||||
"cw-storage-plus",
|
||||
"hex",
|
||||
"rand_chacha 0.3.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cosmwasm-crypto"
|
||||
version = "1.0.0"
|
||||
@@ -941,6 +956,16 @@ dependencies = [
|
||||
"uint",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cosmwasm-storage"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d18403b07304d15d304dad11040d45bbcaf78d603b4be3fb5e2685c16f9229b5"
|
||||
dependencies = [
|
||||
"cosmwasm-std",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.2.5"
|
||||
|
||||
@@ -36,6 +36,7 @@ members = [
|
||||
"common/cosmwasm-smart-contracts/group-contract",
|
||||
"common/cosmwasm-smart-contracts/mixnet-contract",
|
||||
"common/cosmwasm-smart-contracts/multisig-contract",
|
||||
"common/cosmwasm-smart-contracts/testing",
|
||||
"common/cosmwasm-smart-contracts/vesting-contract",
|
||||
"common/mobile-storage",
|
||||
"common/credential-storage",
|
||||
|
||||
@@ -4,7 +4,7 @@ no-clippy: build cargo-test wasm fmt
|
||||
happy: fmt clippy-happy test
|
||||
clippy-all: clippy-main clippy-main-examples clippy-all-contracts clippy-all-wallet clippy-all-connect clippy-all-connect-mobile clippy-all-wasm-client
|
||||
clippy-happy: clippy-happy-main clippy-happy-contracts clippy-happy-wallet clippy-happy-connect clippy-happy-connect-mobile
|
||||
cargo-test: test-main test-contracts test-wallet test-connect test-connect-mobile
|
||||
cargo-test: test-main test-contracts test-contracts-integration test-wallet test-connect test-connect-mobile
|
||||
cargo-test-expensive: test-main-expensive test-contracts-expensive test-wallet-expensive test-connect-expensive
|
||||
build: build-contracts build-wallet build-main build-main-examples build-connect build-connect-mobile build-wasm-client
|
||||
fmt: fmt-main fmt-contracts fmt-wallet fmt-connect fmt-connect-mobile fmt-wasm-client
|
||||
@@ -33,9 +33,8 @@ clippy-main-examples:
|
||||
clippy-wasm:
|
||||
cargo clippy --manifest-path clients/webassembly/Cargo.toml --target wasm32-unknown-unknown --workspace -- -D warnings
|
||||
|
||||
|
||||
clippy-all-contracts:
|
||||
cargo clippy --workspace --manifest-path contracts/Cargo.toml --all-features --target wasm32-unknown-unknown -- -D warnings
|
||||
cargo clippy --workspace --manifest-path contracts/Cargo.toml --target wasm32-unknown-unknown -- -D warnings
|
||||
|
||||
clippy-all-wallet:
|
||||
cargo clippy --workspace --manifest-path nym-wallet/Cargo.toml --all-features -- -D warnings
|
||||
@@ -61,6 +60,9 @@ test-contracts:
|
||||
test-contracts-expensive:
|
||||
cargo test --manifest-path contracts/Cargo.toml --all-features -- --ignored
|
||||
|
||||
test-contracts-integration:
|
||||
cargo test --manifest-path contracts/integration-tests/Cargo.toml
|
||||
|
||||
test-wallet:
|
||||
cargo test --manifest-path nym-wallet/Cargo.toml --all-features
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "cosmwasm-contract-testing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
base64 = { version = "0.21.0", optional = true }
|
||||
cosmwasm-std = { version = "1.0.0" }
|
||||
cosmwasm-storage = { version = "1.0.0", optional = true }
|
||||
cw-storage-plus = { version = "0.13.4", optional = true }
|
||||
hex = { version = "0.4.3", optional = true }
|
||||
rand_chacha = { version = "0.3", optional = true }
|
||||
serde = { version = "1", features=["derive"] }
|
||||
serde_json = { version = "1", optional = true }
|
||||
thiserror = { version = "1.0.38" }
|
||||
|
||||
[features]
|
||||
default = ["full"]
|
||||
full = ["testable-trait", "contract-mocks", "rand", "state-importing", "cosmwasm-storage", "cw-storage-plus", "serde_json"]
|
||||
rand = ["rand_chacha"]
|
||||
contract-mocks = []
|
||||
state-importing = ["base64", "hex"]
|
||||
testable-trait = []
|
||||
@@ -0,0 +1,291 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::mock_api::CW12MockApi;
|
||||
use crate::raw_state::{DecodingError, EncodingError, ImportedContractState, KeyValue};
|
||||
use crate::AVERAGE_BLOCKTIME_SECS;
|
||||
use cosmwasm_std::testing::{mock_env, MockQuerier, MockStorage};
|
||||
use cosmwasm_std::{
|
||||
Addr, Coin, Deps, DepsMut, Env, Order, QuerierWrapper, StdResult, Storage, Timestamp,
|
||||
TransactionInfo,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
use cw_storage_plus::{Item, Map, PrimaryKey};
|
||||
|
||||
// extracted into separate struct for easier cloning, access to mock structs, etc.
|
||||
// we also had to redefine the MockApi
|
||||
struct MockedDependencies {
|
||||
storage: MockStorage,
|
||||
api: CW12MockApi,
|
||||
querier: MockQuerier,
|
||||
|
||||
// that's a bit annoying. We have to keep track of all balance changes for when we clone the state
|
||||
// as there's no easy way of obtaining the up to date list of all balances from the querier...
|
||||
_balances: HashMap<String, Vec<Coin>>,
|
||||
}
|
||||
|
||||
impl MockedDependencies {
|
||||
fn new_mock() -> MockedDependencies {
|
||||
MockedDependencies {
|
||||
storage: Default::default(),
|
||||
api: Default::default(),
|
||||
querier: Default::default(),
|
||||
_balances: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_state(&self) -> MockedDependencies {
|
||||
let new_querier = MockQuerier::new(
|
||||
&self
|
||||
._balances
|
||||
.iter()
|
||||
.map(|(k, v)| (k.as_ref(), v.as_ref()))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
let mut new_storage = MockStorage::new();
|
||||
for (k, v) in self.storage.range(None, None, Order::Ascending) {
|
||||
new_storage.set(&k, &v)
|
||||
}
|
||||
|
||||
MockedDependencies {
|
||||
storage: new_storage,
|
||||
api: self.api,
|
||||
querier: new_querier,
|
||||
_balances: self._balances.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_raw(kvs: Vec<KeyValue>) -> Self {
|
||||
let mut new = Self::new_mock();
|
||||
for kv in kvs {
|
||||
new.storage.set(&kv.key, &kv.value)
|
||||
}
|
||||
new
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ContractState {
|
||||
deps: MockedDependencies,
|
||||
env: Env,
|
||||
}
|
||||
|
||||
impl ContractState {
|
||||
pub fn new() -> Self {
|
||||
ContractState {
|
||||
deps: MockedDependencies::new_mock(),
|
||||
env: mock_env(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_env(env: Env) -> Self {
|
||||
ContractState {
|
||||
deps: MockedDependencies::new_mock(),
|
||||
env,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clone_state(&self) -> Self {
|
||||
ContractState {
|
||||
deps: self.deps.clone_state(),
|
||||
env: self.env.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
// set a new balance for the given address and return the old balance
|
||||
pub fn update_account_balance(
|
||||
&mut self,
|
||||
addr: impl Into<String>,
|
||||
balance: Vec<Coin>,
|
||||
) -> Option<Vec<Coin>> {
|
||||
// that's a bit annoying. We have to keep track of all balance changes for when we clone the state
|
||||
// as there's no easy way of obtaining the up to date list of all balances from the querier...
|
||||
let addr = addr.into();
|
||||
self.deps._balances.insert(addr.clone(), balance.clone());
|
||||
self.deps.querier.update_balance(addr, balance)
|
||||
}
|
||||
|
||||
pub fn account_balance(
|
||||
&self,
|
||||
address: impl Into<String>,
|
||||
denom: impl Into<String>,
|
||||
) -> StdResult<Coin> {
|
||||
self.deps().querier.query_balance(address, denom)
|
||||
}
|
||||
|
||||
pub fn all_account_balances(&self, address: impl Into<String>) -> StdResult<Vec<Coin>> {
|
||||
self.deps().querier.query_all_balances(address)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn save_map_value<'a, K, T>(&mut self, map: &Map<'a, K, T>, k: K, data: &T) -> StdResult<()>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
K: PrimaryKey<'a>,
|
||||
{
|
||||
map.save(&mut self.deps.storage, k, data)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn load_map_value<'a, K, T>(&self, map: &Map<'a, K, T>, k: K) -> StdResult<T>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
K: PrimaryKey<'a>,
|
||||
{
|
||||
map.load(&self.deps.storage, k)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn may_load_map_value<'a, K, T>(&self, map: &Map<'a, K, T>, k: K) -> StdResult<Option<T>>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
K: PrimaryKey<'a>,
|
||||
{
|
||||
map.may_load(&self.deps.storage, k)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn save_item<T>(&mut self, item: &Item<T>, data: &T) -> StdResult<()>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
item.save(&mut self.deps.storage, data)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn load_item<T>(&self, item: &Item<T>) -> StdResult<T>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
item.load(&self.deps.storage)
|
||||
}
|
||||
|
||||
#[cfg(feature = "cw-storage-plus")]
|
||||
pub fn may_load_item<T>(&self, item: &Item<T>) -> StdResult<Option<T>>
|
||||
where
|
||||
T: Serialize + DeserializeOwned,
|
||||
{
|
||||
item.may_load(&self.deps.storage)
|
||||
}
|
||||
|
||||
pub fn read_key(&self, key: &[u8]) -> Option<Vec<u8>> {
|
||||
self.deps.storage.get(key)
|
||||
}
|
||||
|
||||
pub fn set_key_value(&mut self, key: &[u8], value: &[u8]) {
|
||||
self.deps.storage.set(key, value)
|
||||
}
|
||||
|
||||
pub fn deps(&self) -> Deps<'_> {
|
||||
Deps {
|
||||
storage: &self.deps.storage,
|
||||
api: &self.deps.api,
|
||||
querier: QuerierWrapper::new(&self.deps.querier),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deps_mut(&mut self) -> DepsMut<'_> {
|
||||
DepsMut {
|
||||
storage: &mut self.deps.storage,
|
||||
api: &self.deps.api,
|
||||
querier: QuerierWrapper::new(&self.deps.querier),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn advance_blocks(&mut self, new_blocks: u64) {
|
||||
self.advance_block_height(new_blocks);
|
||||
self.advance_blocktime(new_blocks * AVERAGE_BLOCKTIME_SECS)
|
||||
}
|
||||
|
||||
pub fn advance_block_height(&mut self, by: u64) {
|
||||
self.env.block.height += by;
|
||||
}
|
||||
|
||||
pub fn advance_blocktime(&mut self, by_secs: u64) {
|
||||
self.env.block.time = self.env.block.time.plus_seconds(by_secs)
|
||||
}
|
||||
|
||||
pub fn env(&self) -> &Env {
|
||||
&self.env
|
||||
}
|
||||
|
||||
pub fn env_cloned(&self) -> Env {
|
||||
self.env.clone()
|
||||
}
|
||||
|
||||
pub fn contract_address(&self) -> &Addr {
|
||||
&self.env.contract.address
|
||||
}
|
||||
|
||||
pub fn with_contract_address(mut self, address: impl Into<String>) -> Self {
|
||||
self.env.contract.address = Addr::unchecked(address);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_transaction_info(mut self, transaction: Option<TransactionInfo>) -> Self {
|
||||
self.env.transaction = transaction;
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "state-importing")]
|
||||
pub(crate) fn from_state_dump(state: ImportedContractState, custom_env: Option<Env>) -> Self {
|
||||
let env = custom_env.unwrap_or_else(|| {
|
||||
// this is not ideal, but we're making an assumption here that block time is approximately 5s
|
||||
// at block 5000000, we had a timestamp of 1672411689
|
||||
let mut env = mock_env();
|
||||
env.block.chain_id = "nyx".to_string();
|
||||
env.block.height = state.height;
|
||||
if state.height > 5000000 {
|
||||
let diff = state.height - 5000000;
|
||||
env.block.time =
|
||||
Timestamp::from_seconds(1672411689 + diff * AVERAGE_BLOCKTIME_SECS);
|
||||
} else {
|
||||
let diff = 5000000 - state.height;
|
||||
env.block.time =
|
||||
Timestamp::from_seconds(1672411689 - diff * AVERAGE_BLOCKTIME_SECS);
|
||||
}
|
||||
env
|
||||
});
|
||||
|
||||
let deps = MockedDependencies::from_raw(state.data);
|
||||
|
||||
ContractState { deps, env }
|
||||
}
|
||||
|
||||
#[cfg(feature = "state-importing")]
|
||||
pub fn try_from_state_dump<P: AsRef<Path>>(
|
||||
path: P,
|
||||
custom_env: Option<Env>,
|
||||
) -> Result<Self, DecodingError> {
|
||||
Ok(ImportedContractState::try_load_from_file(path)?.into_test_mock(custom_env))
|
||||
}
|
||||
|
||||
#[cfg(feature = "state-importing")]
|
||||
pub fn dump_state<P: AsRef<Path>>(&self, output_path: P) -> Result<(), EncodingError> {
|
||||
let mut data = Vec::new();
|
||||
for (key, value) in self.deps.storage.range(None, None, Order::Ascending) {
|
||||
data.push(KeyValue { key, value })
|
||||
}
|
||||
|
||||
let state = ImportedContractState {
|
||||
height: self.env.block.height,
|
||||
data,
|
||||
};
|
||||
|
||||
state.encode().to_file(output_path)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ContractState {
|
||||
fn default() -> Self {
|
||||
ContractState {
|
||||
deps: MockedDependencies::new_mock(),
|
||||
env: mock_env(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use cosmwasm_std::{Addr, StdError};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum MockingError {
|
||||
#[error(transparent)]
|
||||
StdError {
|
||||
#[from]
|
||||
source: StdError,
|
||||
},
|
||||
|
||||
#[error("attempted to add another contract mock that has the same address as an existing one - {address}")]
|
||||
DuplicateContractAddress { address: Addr },
|
||||
|
||||
#[error("attempted to use a contract that doesn't exist - {address}")]
|
||||
NonExistentContract { address: Addr },
|
||||
|
||||
#[error("contract execution failed with error: {error}. We called {contract} with {message}")]
|
||||
ContractExecutionError {
|
||||
message: String,
|
||||
contract: Addr,
|
||||
error: String,
|
||||
},
|
||||
|
||||
#[error("contract query failed with error: {error}. We called {contract} with {message}")]
|
||||
ContractQueryError {
|
||||
message: String,
|
||||
contract: Addr,
|
||||
error: String,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::helpers::raw_msg_to_string;
|
||||
use cosmwasm_std::{Addr, BankMsg, Binary, Coin, Event};
|
||||
|
||||
fn format_coins(coins: &[Coin]) -> String {
|
||||
if coins.is_empty() {
|
||||
"<zero>".to_string()
|
||||
} else {
|
||||
coins
|
||||
.iter()
|
||||
.map(|c| c.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
}
|
||||
|
||||
// specifically for tokens included in `Execute` that go into the contract
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CrossContractTokenMove {
|
||||
pub amount: Vec<Coin>,
|
||||
pub sender: Addr,
|
||||
pub receiver: Addr,
|
||||
}
|
||||
|
||||
impl CrossContractTokenMove {
|
||||
pub fn new(amount: Vec<Coin>, sender: Addr, receiver: Addr) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
sender,
|
||||
receiver,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pretty(&self) -> String {
|
||||
let total_amount = format_coins(&self.amount);
|
||||
|
||||
format!(
|
||||
"{total_amount} will be transferred from {} to {} (CONTRACTS)",
|
||||
self.sender, self.receiver
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ExecutionResult {
|
||||
pub steps: Vec<ExecutionStepResult>,
|
||||
}
|
||||
|
||||
impl ExecutionResult {
|
||||
pub fn new() -> Self {
|
||||
Self { steps: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn pretty(&self) -> String {
|
||||
let mut out = String::new();
|
||||
for (i, step) in self.steps.iter().enumerate() {
|
||||
out.push_str(&format!("STEP {}\n", i + 1));
|
||||
out.push_str(&format!("{}\n", step.pretty()));
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FurtherExecution {
|
||||
pub contract: Addr,
|
||||
pub msg: Binary,
|
||||
pub funds: Vec<Coin>,
|
||||
}
|
||||
|
||||
impl FurtherExecution {
|
||||
pub fn new(contract: String, msg: Binary, funds: Vec<Coin>) -> Self {
|
||||
Self {
|
||||
contract: Addr::unchecked(contract),
|
||||
msg,
|
||||
funds,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pretty(&self) -> String {
|
||||
let msg = raw_msg_to_string(&self.msg);
|
||||
let total_funds = format_coins(&self.funds);
|
||||
|
||||
format!(
|
||||
"{} will be called with msg {msg} and {total_funds} funds",
|
||||
self.contract
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExecutionStepResult {
|
||||
pub events: Vec<Event>,
|
||||
pub incoming_tokens: Vec<CrossContractTokenMove>,
|
||||
pub bank_msgs: Vec<BankMsg>,
|
||||
pub further_execution: Vec<FurtherExecution>,
|
||||
}
|
||||
|
||||
impl ExecutionStepResult {
|
||||
pub fn pretty(&self) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
// let's keep them squished for now...
|
||||
let events = format!("EVENTS: {:?}\n", self.events);
|
||||
out.push_str(&events);
|
||||
|
||||
if self.incoming_tokens.iter().any(|c| !c.amount.is_empty()) {
|
||||
out.push_str("MOVED TOKENS (CONTRACTS):\n");
|
||||
for incoming in &self.incoming_tokens {
|
||||
if !incoming.amount.is_empty() {
|
||||
out.push_str(&format!("{}\n", incoming.pretty()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !self.bank_msgs.is_empty() {
|
||||
out.push_str("MOVED TOKENS (BANK):\n");
|
||||
for bank in &self.bank_msgs {
|
||||
let formatted = match bank {
|
||||
BankMsg::Send { to_address, amount } => format!(
|
||||
"{} will be transferred to {to_address}",
|
||||
format_coins(amount)
|
||||
),
|
||||
BankMsg::Burn { amount } => format!("{} WILL BE BURNT", format_coins(amount)),
|
||||
_ => "unknown variant of BankMsg was introduced!".to_string(),
|
||||
};
|
||||
out.push_str(&format!("{formatted}\n"))
|
||||
}
|
||||
}
|
||||
|
||||
if !self.further_execution.is_empty() {
|
||||
out.push_str("FURTHER CONTRACT CALLS:\n");
|
||||
for further_exec in &self.further_execution {
|
||||
out.push_str(&format!("{}\n", further_exec.pretty()))
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use cosmwasm_std::testing::mock_env;
|
||||
use cosmwasm_std::{Binary, BlockInfo, Env, StdResult};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
#[cfg(feature = "rand")]
|
||||
pub fn test_rng() -> rand_chacha::ChaCha20Rng {
|
||||
use rand_chacha::rand_core::SeedableRng;
|
||||
|
||||
let dummy_seed = [42u8; 32];
|
||||
rand_chacha::ChaCha20Rng::from_seed(dummy_seed)
|
||||
}
|
||||
|
||||
pub fn env_with_block_info(info: BlockInfo) -> Env {
|
||||
let mut env = mock_env();
|
||||
env.block = info;
|
||||
env
|
||||
}
|
||||
|
||||
pub fn deserialize_msg<M: DeserializeOwned>(raw: &Binary) -> StdResult<M> {
|
||||
cosmwasm_std::from_binary(raw)
|
||||
}
|
||||
|
||||
pub fn serialize_msg<M: Serialize>(msg: &M) -> StdResult<Binary> {
|
||||
cosmwasm_std::to_binary(msg)
|
||||
}
|
||||
|
||||
// used only for purposes of providing more informative error messages
|
||||
pub(crate) fn raw_msg_to_string(raw: &Binary) -> String {
|
||||
#[cfg(not(feature = "serde_json"))]
|
||||
return "<serde_json feature is not enabled - can't format the message>".to_string();
|
||||
|
||||
#[cfg(feature = "serde_json")]
|
||||
match serde_json::from_slice::<serde_json::Value>(raw.as_slice()) {
|
||||
Ok(deserialized) => deserialized.to_string(),
|
||||
Err(_) => "ERR: COULD NOT RECOVER THE ORIGINAL MESSAGE".to_string(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
mod contract_mock;
|
||||
mod error;
|
||||
mod execution;
|
||||
mod helpers;
|
||||
mod mock_api;
|
||||
mod multi_contract_mock;
|
||||
mod raw_state;
|
||||
mod single_contract_mock;
|
||||
mod traits;
|
||||
|
||||
pub use contract_mock::ContractState;
|
||||
pub use error::MockingError;
|
||||
pub use helpers::{deserialize_msg, env_with_block_info, serialize_msg};
|
||||
|
||||
#[cfg(feature = "state-importing")]
|
||||
pub use raw_state::ImportedContractState;
|
||||
|
||||
#[cfg(feature = "contract-mocks")]
|
||||
pub use multi_contract_mock::MultiContractMock;
|
||||
|
||||
#[cfg(feature = "contract-mocks")]
|
||||
pub use single_contract_mock::SingleContractMock;
|
||||
|
||||
#[cfg(feature = "contract-mocks")]
|
||||
pub use execution::{
|
||||
CrossContractTokenMove, ExecutionResult, ExecutionStepResult, FurtherExecution,
|
||||
};
|
||||
|
||||
#[cfg(feature = "testable-trait")]
|
||||
pub use traits::TestableContract;
|
||||
|
||||
pub const AVERAGE_BLOCKTIME_SECS: u64 = 5;
|
||||
@@ -0,0 +1,143 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// unfortunately we have to redefine cosmwasm' MockApi,
|
||||
// as in cw1.0 they have set `CANONICAL_LENGTH` to 54 which makes
|
||||
// `addr_validate` of our existing contracts fail (say of 'n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw')
|
||||
// this has changed in 1.2 but we can't use that version yet...
|
||||
|
||||
use cosmwasm_std::testing::{digit_sum, riffle_shuffle, MockApi};
|
||||
use cosmwasm_std::{
|
||||
Addr, Api, CanonicalAddr, RecoverPubkeyError, StdError, StdResult, VerificationError,
|
||||
};
|
||||
|
||||
const CANONICAL_LENGTH: usize = 90; // n = 45
|
||||
const SHUFFLES_ENCODE: usize = 10;
|
||||
const SHUFFLES_DECODE: usize = 2;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub(crate) struct CW12MockApi {
|
||||
inner: MockApi,
|
||||
canonical_length: usize,
|
||||
}
|
||||
|
||||
impl Default for CW12MockApi {
|
||||
fn default() -> Self {
|
||||
CW12MockApi {
|
||||
inner: MockApi::default(),
|
||||
canonical_length: CANONICAL_LENGTH,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// whatever we can, hand over to 1.0 MockApi
|
||||
impl Api for CW12MockApi {
|
||||
fn addr_validate(&self, input: &str) -> StdResult<Addr> {
|
||||
let canonical = self.addr_canonicalize(input)?;
|
||||
let normalized = self.addr_humanize(&canonical)?;
|
||||
if input != normalized {
|
||||
return Err(StdError::generic_err(
|
||||
"Invalid input: address not normalized",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Addr::unchecked(input))
|
||||
}
|
||||
|
||||
fn addr_canonicalize(&self, input: &str) -> StdResult<CanonicalAddr> {
|
||||
// Dummy input validation. This is more sophisticated for formats like bech32, where format and checksum are validated.
|
||||
let min_length = 3;
|
||||
let max_length = self.canonical_length;
|
||||
if input.len() < min_length {
|
||||
return Err(StdError::generic_err(
|
||||
format!("Invalid input: human address too short for this mock implementation (must be >= {min_length})."),
|
||||
));
|
||||
}
|
||||
if input.len() > max_length {
|
||||
return Err(StdError::generic_err(
|
||||
format!("Invalid input: human address too long for this mock implementation (must be <= {max_length})."),
|
||||
));
|
||||
}
|
||||
|
||||
// mimicks formats like hex or bech32 where different casings are valid for one address
|
||||
let normalized = input.to_lowercase();
|
||||
|
||||
let mut out = Vec::from(normalized);
|
||||
|
||||
// pad to canonical length with NULL bytes
|
||||
out.resize(self.canonical_length, 0x00);
|
||||
// content-dependent rotate followed by shuffle to destroy
|
||||
// the most obvious structure (https://github.com/CosmWasm/cosmwasm/issues/552)
|
||||
let rotate_by = digit_sum(&out) % self.canonical_length;
|
||||
out.rotate_left(rotate_by);
|
||||
for _ in 0..SHUFFLES_ENCODE {
|
||||
out = riffle_shuffle(&out);
|
||||
}
|
||||
Ok(out.into())
|
||||
}
|
||||
|
||||
fn addr_humanize(&self, canonical: &CanonicalAddr) -> StdResult<Addr> {
|
||||
if canonical.len() != self.canonical_length {
|
||||
return Err(StdError::generic_err(
|
||||
"Invalid input: canonical address length not correct",
|
||||
));
|
||||
}
|
||||
|
||||
let mut tmp: Vec<u8> = canonical.clone().into();
|
||||
// Shuffle two more times which restored the original value (24 elements are back to original after 20 rounds)
|
||||
for _ in 0..SHUFFLES_DECODE {
|
||||
tmp = riffle_shuffle(&tmp);
|
||||
}
|
||||
// Rotate back
|
||||
let rotate_by = digit_sum(&tmp) % self.canonical_length;
|
||||
tmp.rotate_right(rotate_by);
|
||||
// Remove NULL bytes (i.e. the padding)
|
||||
let trimmed = tmp.into_iter().filter(|&x| x != 0x00).collect();
|
||||
// decode UTF-8 bytes into string
|
||||
let human = String::from_utf8(trimmed)?;
|
||||
Ok(Addr::unchecked(human))
|
||||
}
|
||||
|
||||
fn secp256k1_verify(
|
||||
&self,
|
||||
message_hash: &[u8],
|
||||
signature: &[u8],
|
||||
public_key: &[u8],
|
||||
) -> Result<bool, VerificationError> {
|
||||
self.inner
|
||||
.secp256k1_verify(message_hash, signature, public_key)
|
||||
}
|
||||
|
||||
fn secp256k1_recover_pubkey(
|
||||
&self,
|
||||
message_hash: &[u8],
|
||||
signature: &[u8],
|
||||
recovery_param: u8,
|
||||
) -> Result<Vec<u8>, RecoverPubkeyError> {
|
||||
self.inner
|
||||
.secp256k1_recover_pubkey(message_hash, signature, recovery_param)
|
||||
}
|
||||
|
||||
fn ed25519_verify(
|
||||
&self,
|
||||
message: &[u8],
|
||||
signature: &[u8],
|
||||
public_key: &[u8],
|
||||
) -> Result<bool, VerificationError> {
|
||||
self.inner.ed25519_verify(message, signature, public_key)
|
||||
}
|
||||
|
||||
fn ed25519_batch_verify(
|
||||
&self,
|
||||
messages: &[&[u8]],
|
||||
signatures: &[&[u8]],
|
||||
public_keys: &[&[u8]],
|
||||
) -> Result<bool, VerificationError> {
|
||||
self.inner
|
||||
.ed25519_batch_verify(messages, signatures, public_keys)
|
||||
}
|
||||
|
||||
fn debug(&self, message: &str) {
|
||||
self.inner.debug(message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::contract_mock::ContractState;
|
||||
use crate::execution::{
|
||||
CrossContractTokenMove, ExecutionResult, ExecutionStepResult, FurtherExecution,
|
||||
};
|
||||
use crate::helpers::raw_msg_to_string;
|
||||
use crate::traits::sealed;
|
||||
use crate::{serialize_msg, MockingError, TestableContract};
|
||||
use cosmwasm_std::testing::{mock_env, mock_info};
|
||||
use cosmwasm_std::{
|
||||
Addr, Binary, CosmosMsg, Env, MessageInfo, QueryResponse, ReplyOn, Response, WasmMsg,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct MockedContract {
|
||||
state: ContractState,
|
||||
entry_points: Box<dyn sealed::ErasedTestableContract>,
|
||||
}
|
||||
|
||||
impl MockedContract {
|
||||
fn new<C: TestableContract + 'static>(state: ContractState) -> Self {
|
||||
MockedContract {
|
||||
state,
|
||||
entry_points: Box::new(C::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MultiContractMock {
|
||||
contracts: HashMap<Addr, MockedContract>,
|
||||
}
|
||||
|
||||
impl MultiContractMock {
|
||||
#[cfg(feature = "rand")]
|
||||
fn generate_new_contract_address(&self) -> Addr {
|
||||
use rand_chacha::rand_core::RngCore;
|
||||
|
||||
let mut rng = crate::helpers::test_rng();
|
||||
loop {
|
||||
// for the testing purposes u64 contains enough entropy
|
||||
// (I could even argue u8 would be sufficient)
|
||||
// as I doubt anyone would want to generate so many contract names
|
||||
// they would have started colliding...
|
||||
let candidate_id = rng.next_u64();
|
||||
let name = Addr::unchecked(format!("new-contract{candidate_id}"));
|
||||
if !self.contracts.contains_key(&name) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
MultiContractMock {
|
||||
contracts: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_contract<C: TestableContract + 'static>(
|
||||
&mut self,
|
||||
contract_state: ContractState,
|
||||
) -> Result<(), MockingError> {
|
||||
let address = contract_state.contract_address().clone();
|
||||
if self
|
||||
.contracts
|
||||
.contains_key(contract_state.contract_address())
|
||||
{
|
||||
Err(MockingError::DuplicateContractAddress { address })
|
||||
} else {
|
||||
let mocked = MockedContract::new::<C>(contract_state);
|
||||
self.contracts.insert(address, mocked);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_contract<C: TestableContract + 'static>(
|
||||
mut self,
|
||||
state: ContractState,
|
||||
) -> Result<Self, MockingError> {
|
||||
self.add_contract::<C>(state)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn advance_blocks(&mut self, new_blocks: u64) {
|
||||
for contract in self.contracts.values_mut() {
|
||||
contract.state.advance_blocks(new_blocks)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn advance_block_height(&mut self, by: u64) {
|
||||
for contract in self.contracts.values_mut() {
|
||||
contract.state.advance_block_height(by)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn advance_blocktime(&mut self, by_secs: u64) {
|
||||
for contract in self.contracts.values_mut() {
|
||||
contract.state.advance_blocktime(by_secs)
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_step(
|
||||
&mut self,
|
||||
contract_address: impl Into<String>,
|
||||
info: MessageInfo,
|
||||
binary_msg: Binary,
|
||||
) -> Result<ExecutionStepResult, MockingError> {
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract =
|
||||
self.contracts
|
||||
.get_mut(&addr)
|
||||
.ok_or_else(|| MockingError::NonExistentContract {
|
||||
address: addr.clone(),
|
||||
})?;
|
||||
|
||||
let env = contract.state.env_cloned();
|
||||
let deps = contract.state.deps_mut();
|
||||
|
||||
let res = match contract
|
||||
.entry_points
|
||||
.execute(deps, env, info, binary_msg.clone())
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(error) => {
|
||||
return Err(MockingError::ContractExecutionError {
|
||||
message: raw_msg_to_string(&binary_msg),
|
||||
contract: addr,
|
||||
error,
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let mut bank_msgs = Vec::new();
|
||||
let mut further_execution = Vec::new();
|
||||
let mut incoming_tokens = Vec::new();
|
||||
|
||||
for sub_msg in res.messages {
|
||||
if sub_msg.reply_on != ReplyOn::Never {
|
||||
unimplemented!("currently there's no support for 'reply_on'")
|
||||
}
|
||||
|
||||
match sub_msg.msg {
|
||||
CosmosMsg::Bank(bank_msg) => bank_msgs.push(bank_msg),
|
||||
CosmosMsg::Wasm(wasm_msg) => {
|
||||
match wasm_msg {
|
||||
WasmMsg::Execute { contract_addr, msg, funds } => {
|
||||
incoming_tokens.push(CrossContractTokenMove::new(funds.clone(), addr.clone(), Addr::unchecked(&contract_addr)));
|
||||
further_execution.push(FurtherExecution::new(contract_addr, msg, funds))
|
||||
}
|
||||
_ => unimplemented!("currently we only support 'ExecuteMsg' for 'WasmMsg'")
|
||||
}
|
||||
}
|
||||
// other variants might get support later on
|
||||
_ => unimplemented!("currently there's no support for sub msgs different from 'WasmMsg' or 'BankMsg")
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ExecutionStepResult {
|
||||
events: res.events,
|
||||
incoming_tokens,
|
||||
bank_msgs,
|
||||
further_execution,
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: verify that this is the actual order of execution of sub messages in cosmwasm
|
||||
fn execute_branch(
|
||||
&mut self,
|
||||
res: &mut ExecutionResult,
|
||||
contract: String,
|
||||
info: MessageInfo,
|
||||
msg: Binary,
|
||||
) -> Result<(), MockingError> {
|
||||
let step_res = self.execute_step(contract.clone(), info, msg)?;
|
||||
res.steps.push(step_res.clone());
|
||||
for further_exec in step_res.further_execution {
|
||||
let info = mock_info(&contract, &further_exec.funds);
|
||||
self.execute_branch(
|
||||
res,
|
||||
further_exec.contract.into_string(),
|
||||
info,
|
||||
further_exec.msg,
|
||||
)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn contract_state(
|
||||
&self,
|
||||
contract_address: impl Into<String>,
|
||||
) -> Result<&ContractState, MockingError> {
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract =
|
||||
self.contracts
|
||||
.get(&addr)
|
||||
.ok_or_else(|| MockingError::NonExistentContract {
|
||||
address: addr.clone(),
|
||||
})?;
|
||||
Ok(&contract.state)
|
||||
}
|
||||
|
||||
pub fn contract_state_mut(
|
||||
&mut self,
|
||||
contract_address: impl Into<String>,
|
||||
) -> Result<&mut ContractState, MockingError> {
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract =
|
||||
self.contracts
|
||||
.get_mut(&addr)
|
||||
.ok_or_else(|| MockingError::NonExistentContract {
|
||||
address: addr.clone(),
|
||||
})?;
|
||||
Ok(&mut contract.state)
|
||||
}
|
||||
|
||||
// TODO: add support for sub msgs in instantiate response
|
||||
pub fn instantiate<C>(
|
||||
&mut self,
|
||||
custom_env: Option<Env>,
|
||||
info: MessageInfo,
|
||||
msg: C::InstantiateMsg,
|
||||
) -> Result<Response, C::ContractError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
{
|
||||
// if custom environment wasn't provided, generate a pseudorandom address so that it wouldn't
|
||||
// clash with any existing contracts
|
||||
let env = custom_env.unwrap_or_else(|| {
|
||||
let mut env = mock_env();
|
||||
env.contract.address = self.generate_new_contract_address();
|
||||
env
|
||||
});
|
||||
let mut state = ContractState::new_with_env(env);
|
||||
let env = state.env_cloned();
|
||||
let deps = state.deps_mut();
|
||||
C::instantiate(deps, env, info, msg)
|
||||
}
|
||||
|
||||
pub fn execute_full<C>(
|
||||
&mut self,
|
||||
initial_contract: impl Into<String>,
|
||||
info: MessageInfo,
|
||||
msg: C::ExecuteMsg,
|
||||
) -> Result<ExecutionResult, MockingError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
C::ExecuteMsg: Serialize,
|
||||
{
|
||||
let mut execution_result = ExecutionResult::new();
|
||||
let serialized_msg = serialize_msg(&msg)?;
|
||||
|
||||
self.execute_branch(
|
||||
&mut execution_result,
|
||||
initial_contract.into(),
|
||||
info,
|
||||
serialized_msg,
|
||||
)?;
|
||||
Ok(execution_result)
|
||||
}
|
||||
|
||||
// provide unchecked variant of execute to return original error enum
|
||||
pub fn unchecked_execute<C>(
|
||||
&mut self,
|
||||
contract_address: impl Into<String>,
|
||||
info: MessageInfo,
|
||||
msg: C::ExecuteMsg,
|
||||
) -> Result<Response, C::ContractError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
{
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract = self
|
||||
.contracts
|
||||
.get_mut(&addr)
|
||||
.expect("specified contract does not exist");
|
||||
|
||||
let env = contract.state.env_cloned();
|
||||
let deps = contract.state.deps_mut();
|
||||
C::execute(deps, env, info, msg)
|
||||
}
|
||||
|
||||
// executes only the top level message
|
||||
pub fn execute<C>(
|
||||
&mut self,
|
||||
contract_address: impl Into<String>,
|
||||
info: MessageInfo,
|
||||
msg: C::ExecuteMsg,
|
||||
) -> Result<Response, MockingError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
C::ExecuteMsg: Serialize,
|
||||
{
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract =
|
||||
self.contracts
|
||||
.get_mut(&addr)
|
||||
.ok_or_else(|| MockingError::NonExistentContract {
|
||||
address: addr.clone(),
|
||||
})?;
|
||||
|
||||
let env = contract.state.env_cloned();
|
||||
let deps = contract.state.deps_mut();
|
||||
|
||||
let serialized_msg = serialize_msg(&msg)?;
|
||||
C::execute(deps, env, info, msg).map_err(|err| MockingError::ContractExecutionError {
|
||||
message: raw_msg_to_string(&serialized_msg),
|
||||
contract: addr,
|
||||
error: err.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
// provide unchecked variant of query to return original error enum
|
||||
pub fn unchecked_query<C, T>(
|
||||
&self,
|
||||
contract_address: impl Into<String>,
|
||||
msg: C::QueryMsg,
|
||||
) -> Result<T, C::ContractError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract = self
|
||||
.contracts
|
||||
.get(&addr)
|
||||
.expect("specified contract does not exist");
|
||||
|
||||
let env = contract.state.env_cloned();
|
||||
let deps = contract.state.deps();
|
||||
C::query(deps, env, msg).map(|res| serde_json::from_slice(&res).unwrap())
|
||||
}
|
||||
|
||||
pub fn query<C>(
|
||||
&self,
|
||||
contract_address: impl Into<String>,
|
||||
msg: C::QueryMsg,
|
||||
) -> Result<QueryResponse, MockingError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
C::QueryMsg: Serialize,
|
||||
{
|
||||
let addr = Addr::unchecked(contract_address.into());
|
||||
let contract =
|
||||
self.contracts
|
||||
.get(&addr)
|
||||
.ok_or_else(|| MockingError::NonExistentContract {
|
||||
address: addr.clone(),
|
||||
})?;
|
||||
|
||||
let env = contract.state.env_cloned();
|
||||
let deps = contract.state.deps();
|
||||
|
||||
let serialized_msg = serialize_msg(&msg)?;
|
||||
C::query(deps, env, msg).map_err(|err| MockingError::ContractQueryError {
|
||||
message: raw_msg_to_string(&serialized_msg),
|
||||
contract: addr,
|
||||
error: err.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn query_de<C, T>(
|
||||
&self,
|
||||
contract_address: impl Into<String>,
|
||||
msg: C::QueryMsg,
|
||||
) -> Result<T, MockingError>
|
||||
where
|
||||
C: TestableContract + 'static,
|
||||
C::QueryMsg: Serialize,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
self.query::<C>(contract_address, msg)
|
||||
.map(|res| serde_json::from_slice(&res).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[test]
|
||||
fn converting_msg_to_string() {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Dummy {
|
||||
field1: String,
|
||||
field2: u32,
|
||||
field3: Vec<u32>,
|
||||
}
|
||||
|
||||
let dummy = Dummy {
|
||||
field1: "aaaa".to_string(),
|
||||
field2: 42,
|
||||
field3: vec![1, 2, 3, 4],
|
||||
};
|
||||
|
||||
let bin = serialize_msg(&dummy).unwrap();
|
||||
let expected = r#"{"field1":"aaaa","field2":42,"field3":[1,2,3,4]}"#;
|
||||
let stringified = raw_msg_to_string(&bin);
|
||||
assert_eq!(expected, stringified)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::contract_mock::ContractState;
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use cosmwasm_std::Env;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::num::ParseIntError;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DecodingError {
|
||||
#[error("failed to parse the block height information of the state dump: {source}")]
|
||||
MalformedBlockHeight {
|
||||
#[from]
|
||||
source: ParseIntError,
|
||||
},
|
||||
|
||||
#[error("failed to open the specified state dump: {source}")]
|
||||
FileOpenError {
|
||||
#[from]
|
||||
source: io::Error,
|
||||
},
|
||||
|
||||
#[error("failed to decode the provided json state: {source}")]
|
||||
JsonDecodeError {
|
||||
#[from]
|
||||
source: serde_json::Error,
|
||||
},
|
||||
|
||||
#[error("failed to decode one of the state keys: {source}")]
|
||||
HexDecodeError {
|
||||
#[from]
|
||||
source: hex::FromHexError,
|
||||
},
|
||||
|
||||
#[error("failed to decode one of the state values: {source}")]
|
||||
Base64DecodeError {
|
||||
#[from]
|
||||
source: base64::DecodeError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum EncodingError {
|
||||
#[error("failed to open the specified state dump file: {source}")]
|
||||
FileOpenError {
|
||||
#[from]
|
||||
source: io::Error,
|
||||
},
|
||||
|
||||
#[error("failed to encode the provided json state: {source}")]
|
||||
JsonEncodeError {
|
||||
#[from]
|
||||
source: serde_json::Error,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct ImportedContractState {
|
||||
pub height: u64,
|
||||
pub data: Vec<KeyValue>,
|
||||
}
|
||||
|
||||
pub struct KeyValue {
|
||||
pub key: Vec<u8>,
|
||||
pub value: Vec<u8>,
|
||||
}
|
||||
|
||||
impl ImportedContractState {
|
||||
pub fn try_from_json(value: &str) -> Result<Self, DecodingError> {
|
||||
RawContractState::from_json(value)?.decode()
|
||||
}
|
||||
|
||||
pub fn try_load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, DecodingError> {
|
||||
RawContractState::from_file(path)?.decode()
|
||||
}
|
||||
|
||||
pub fn find_value(&self, key: &[u8]) -> Option<&[u8]> {
|
||||
self.data
|
||||
.iter()
|
||||
.find(|kv| kv.key == key)
|
||||
.map(|kv| kv.value.as_ref())
|
||||
}
|
||||
|
||||
pub fn into_test_mock(self, custom_env: Option<Env>) -> ContractState {
|
||||
ContractState::from_state_dump(self, custom_env)
|
||||
}
|
||||
|
||||
pub(crate) fn encode(self) -> RawContractState {
|
||||
RawContractState {
|
||||
height: self.height.to_string(),
|
||||
result: self.data.into_iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub(crate) struct RawContractState {
|
||||
height: String,
|
||||
result: Vec<RawKeyValue>,
|
||||
}
|
||||
|
||||
impl RawContractState {
|
||||
fn decode(self) -> Result<ImportedContractState, DecodingError> {
|
||||
Ok(ImportedContractState {
|
||||
height: self.height.parse()?,
|
||||
data: self
|
||||
.result
|
||||
.into_iter()
|
||||
.map(TryInto::try_into)
|
||||
.collect::<Result<_, _>>()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn from_json(value: &str) -> Result<Self, DecodingError> {
|
||||
Ok(serde_json::from_str(value)?)
|
||||
}
|
||||
|
||||
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, DecodingError> {
|
||||
let file = File::open(path)?;
|
||||
Ok(serde_json::from_reader(file)?)
|
||||
}
|
||||
|
||||
pub(crate) fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), EncodingError> {
|
||||
let file = File::open(path)?;
|
||||
Ok(serde_json::to_writer(file, &self)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct RawKeyValue {
|
||||
// encoded as hex
|
||||
key: String,
|
||||
|
||||
// encoded as base64
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl TryFrom<RawKeyValue> for KeyValue {
|
||||
type Error = DecodingError;
|
||||
|
||||
fn try_from(raw: RawKeyValue) -> Result<Self, Self::Error> {
|
||||
Ok(KeyValue {
|
||||
key: hex::decode(&raw.key)?,
|
||||
value: general_purpose::STANDARD.decode(&raw.value)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KeyValue> for RawKeyValue {
|
||||
fn from(decoded: KeyValue) -> Self {
|
||||
RawKeyValue {
|
||||
key: hex::encode(decoded.key),
|
||||
value: general_purpose::STANDARD.encode(decoded.value),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::{ContractState, TestableContract};
|
||||
use cosmwasm_std::testing::mock_env;
|
||||
use cosmwasm_std::{from_slice, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response};
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct SingleContractMock<C> {
|
||||
pub state: ContractState,
|
||||
_contract: PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<C: TestableContract> SingleContractMock<C> {
|
||||
pub fn new_empty() -> Self {
|
||||
SingleContractMock {
|
||||
state: Default::default(),
|
||||
_contract: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(state: ContractState) -> Self {
|
||||
SingleContractMock {
|
||||
state,
|
||||
_contract: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deps(&self) -> Deps<'_> {
|
||||
self.state.deps()
|
||||
}
|
||||
|
||||
pub fn deps_mut(&mut self) -> DepsMut<'_> {
|
||||
self.state.deps_mut()
|
||||
}
|
||||
|
||||
pub fn env(&self) -> &Env {
|
||||
self.state.env()
|
||||
}
|
||||
|
||||
pub fn env_cloned(&self) -> Env {
|
||||
self.state.env_cloned()
|
||||
}
|
||||
|
||||
pub fn instantiate(
|
||||
custom_env: Option<Env>,
|
||||
info: MessageInfo,
|
||||
msg: C::InstantiateMsg,
|
||||
) -> Result<(Self, Response), C::ContractError> {
|
||||
// if we're instantiating fresh contract it means there was no pre-existing state
|
||||
let env = custom_env.unwrap_or_else(mock_env);
|
||||
let state = ContractState::new_with_env(env);
|
||||
let mut this = Self::new(state);
|
||||
|
||||
let env = this.state.env_cloned();
|
||||
let deps = this.state.deps_mut();
|
||||
|
||||
let res = C::instantiate(deps, env, info, msg)?;
|
||||
Ok((this, res))
|
||||
}
|
||||
|
||||
pub fn execute(
|
||||
&mut self,
|
||||
info: MessageInfo,
|
||||
msg: C::ExecuteMsg,
|
||||
) -> Result<Response, C::ContractError> {
|
||||
let env = self.state.env_cloned();
|
||||
let deps = self.state.deps_mut();
|
||||
|
||||
C::execute(deps, env, info, msg)
|
||||
}
|
||||
|
||||
pub fn query(&self, msg: C::QueryMsg) -> Result<QueryResponse, C::ContractError> {
|
||||
let env = self.state.env_cloned();
|
||||
let deps = self.state.deps();
|
||||
|
||||
C::query(deps, env, msg)
|
||||
}
|
||||
|
||||
pub fn query_de<T: DeserializeOwned>(&self, msg: C::QueryMsg) -> Result<T, C::ContractError> {
|
||||
self.query(msg).map(|res| from_slice(&res).unwrap())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use cosmwasm_std::{Deps, DepsMut, Env, MessageInfo, QueryResponse, Response};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
// TODO: see if it's possible to create a macro to auto-derive it
|
||||
// if you intend to use the MultiContractMock, you need to implement this trait
|
||||
// for your contract
|
||||
/// ```
|
||||
/// use cosmwasm_std::{
|
||||
/// entry_point, Deps, DepsMut, Env, MessageInfo, Querier, QueryResponse, Response, StdError,
|
||||
/// Storage,
|
||||
/// };
|
||||
/// use cosmwasm_contract_testing::TestableContract;
|
||||
///
|
||||
/// type ExecuteMsg = ();
|
||||
/// type QueryMsg = ();
|
||||
/// type InstantiateMsg = ();
|
||||
/// type ContractError = StdError;
|
||||
///
|
||||
/// #[entry_point]
|
||||
/// pub fn instantiate (
|
||||
/// deps: DepsMut,
|
||||
/// env: Env,
|
||||
/// info: MessageInfo,
|
||||
/// msg: InstantiateMsg,
|
||||
/// ) -> Result<Response, ContractError> {
|
||||
/// Ok(Default::default())
|
||||
/// }
|
||||
///
|
||||
/// #[entry_point]
|
||||
/// pub fn execute(
|
||||
/// deps: DepsMut,
|
||||
/// env: Env,
|
||||
/// info: MessageInfo,
|
||||
/// msg: ExecuteMsg,
|
||||
/// ) -> Result<Response, ContractError> {
|
||||
/// Ok(Default::default())
|
||||
/// }
|
||||
///
|
||||
/// #[entry_point]
|
||||
/// pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<QueryResponse, ContractError> {
|
||||
/// Ok(Default::default())
|
||||
/// }
|
||||
///
|
||||
/// struct MyContract;
|
||||
///
|
||||
/// impl TestableContract for MyContract {
|
||||
/// type ContractError = ContractError;
|
||||
/// type InstantiateMsg = InstantiateMsg;
|
||||
/// type ExecuteMsg = ExecuteMsg;
|
||||
/// type QueryMsg = QueryMsg;
|
||||
///
|
||||
/// fn new() -> Self {
|
||||
/// MyContract
|
||||
/// }
|
||||
///
|
||||
/// fn instantiate(
|
||||
/// deps: DepsMut<'_>,
|
||||
/// env: Env,
|
||||
/// info: MessageInfo,
|
||||
/// msg: Self::InstantiateMsg,
|
||||
/// ) -> Result<Response, Self::ContractError> {
|
||||
/// instantiate(deps, env, info, msg)
|
||||
/// }
|
||||
///
|
||||
/// fn execute(
|
||||
/// deps: DepsMut<'_>,
|
||||
/// env: Env,
|
||||
/// info: MessageInfo,
|
||||
/// msg: Self::ExecuteMsg,
|
||||
/// ) -> Result<Response, Self::ContractError> {
|
||||
/// execute(deps, env, info, msg)
|
||||
/// }
|
||||
///
|
||||
/// fn query(
|
||||
/// deps: Deps<'_>,
|
||||
/// env: Env,
|
||||
/// msg: Self::QueryMsg,
|
||||
/// ) -> Result<QueryResponse, Self::ContractError> {
|
||||
/// query(deps, env, msg)
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait TestableContract {
|
||||
type ContractError: ToString;
|
||||
type InstantiateMsg: DeserializeOwned;
|
||||
type ExecuteMsg: DeserializeOwned;
|
||||
type QueryMsg: DeserializeOwned;
|
||||
|
||||
fn new() -> Self;
|
||||
|
||||
fn instantiate(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::InstantiateMsg,
|
||||
) -> Result<Response, Self::ContractError>;
|
||||
|
||||
fn execute(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::ExecuteMsg,
|
||||
) -> Result<Response, Self::ContractError>;
|
||||
|
||||
fn query(
|
||||
deps: Deps<'_>,
|
||||
env: Env,
|
||||
msg: Self::QueryMsg,
|
||||
) -> Result<QueryResponse, Self::ContractError>;
|
||||
}
|
||||
|
||||
pub(crate) mod sealed {
|
||||
use crate::deserialize_msg;
|
||||
use crate::traits::TestableContract;
|
||||
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response};
|
||||
|
||||
pub(crate) trait ErasedTestableContract {
|
||||
fn query(&self, deps: Deps<'_>, env: Env, raw_msg: Binary)
|
||||
-> Result<QueryResponse, String>;
|
||||
|
||||
fn execute(
|
||||
&self,
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
raw_msg: Binary,
|
||||
) -> Result<Response, String>;
|
||||
|
||||
fn instantiate(
|
||||
&self,
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
raw_msg: Binary,
|
||||
) -> Result<Response, String>;
|
||||
}
|
||||
|
||||
impl<T: TestableContract> ErasedTestableContract for T {
|
||||
fn query(
|
||||
&self,
|
||||
deps: Deps<'_>,
|
||||
env: Env,
|
||||
raw_msg: Binary,
|
||||
) -> Result<QueryResponse, String> {
|
||||
let msg = deserialize_msg(&raw_msg).expect("failed to deserialize 'QueryMsg'");
|
||||
<Self as TestableContract>::query(deps, env, msg).map_err(|err| err.to_string())
|
||||
}
|
||||
|
||||
fn execute(
|
||||
&self,
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
raw_msg: Binary,
|
||||
) -> Result<Response, String> {
|
||||
let msg = deserialize_msg(&raw_msg).expect("failed to deserialize 'ExecuteMsg'");
|
||||
<Self as TestableContract>::execute(deps, env, info, msg).map_err(|err| err.to_string())
|
||||
}
|
||||
|
||||
fn instantiate(
|
||||
&self,
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
raw_msg: Binary,
|
||||
) -> Result<Response, String> {
|
||||
let msg = deserialize_msg(&raw_msg).expect("failed to deserialize 'InstantiateMsg'");
|
||||
<Self as TestableContract>::instantiate(deps, env, info, msg)
|
||||
.map_err(|err| err.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,11 @@ members = [
|
||||
"multisig/cw3-flex-multisig",
|
||||
"multisig/cw4-group",
|
||||
"coconut-test",
|
||||
"coconut-dkg"
|
||||
"coconut-dkg",
|
||||
]
|
||||
|
||||
exclude = ["integration-tests"]
|
||||
|
||||
[workspace.package]
|
||||
authors = ["Nym Technologies SA"]
|
||||
repository = "https://github.com/nymtech/nym"
|
||||
|
||||
@@ -9,7 +9,7 @@ use cw_storage_plus::Bound;
|
||||
|
||||
use crate::storage::{self, SPEND_CREDENTIAL_PAGE_DEFAULT_LIMIT, SPEND_CREDENTIAL_PAGE_MAX_LIMIT};
|
||||
|
||||
pub(crate) fn query_all_spent_credentials_paged(
|
||||
pub fn query_all_spent_credentials_paged(
|
||||
deps: Deps<'_>,
|
||||
start_after: Option<String>,
|
||||
limit: Option<u32>,
|
||||
@@ -37,7 +37,7 @@ pub(crate) fn query_all_spent_credentials_paged(
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn query_spent_credential(
|
||||
pub fn query_spent_credential(
|
||||
deps: Deps<'_>,
|
||||
blinded_serial_number: String,
|
||||
) -> StdResult<SpendCredentialResponse> {
|
||||
|
||||
@@ -6,19 +6,17 @@ use crate::error::ContractError;
|
||||
use coconut_dkg_common::types::{Epoch, InitialReplacementData};
|
||||
use cosmwasm_std::Storage;
|
||||
|
||||
pub(crate) fn query_current_epoch(storage: &dyn Storage) -> Result<Epoch, ContractError> {
|
||||
pub fn query_current_epoch(storage: &dyn Storage) -> Result<Epoch, ContractError> {
|
||||
CURRENT_EPOCH
|
||||
.load(storage)
|
||||
.map_err(|_| ContractError::EpochNotInitialised)
|
||||
}
|
||||
|
||||
pub(crate) fn query_current_epoch_threshold(
|
||||
storage: &dyn Storage,
|
||||
) -> Result<Option<u64>, ContractError> {
|
||||
pub fn query_current_epoch_threshold(storage: &dyn Storage) -> Result<Option<u64>, ContractError> {
|
||||
Ok(THRESHOLD.may_load(storage)?)
|
||||
}
|
||||
|
||||
pub(crate) fn query_initial_dealers(
|
||||
pub fn query_initial_dealers(
|
||||
storage: &dyn Storage,
|
||||
) -> Result<Option<InitialReplacementData>, ContractError> {
|
||||
Ok(INITIAL_REPLACEMENT_DATA.may_load(storage)?)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "integration-tests"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dev-dependencies]
|
||||
cosmwasm-std = "1.0.0"
|
||||
cw-storage-plus = "0.13.4"
|
||||
cosmwasm-contract-testing = { path = "../../common/cosmwasm-smart-contracts/testing" }
|
||||
nym-mixnet-contract-common = { path= "../../common/cosmwasm-smart-contracts/mixnet-contract" }
|
||||
nym-vesting-contract-common = { path= "../../common/cosmwasm-smart-contracts/vesting-contract" }
|
||||
nym-vesting-contract = { path = "../vesting", features = ["testing-mocks"] }
|
||||
nym-mixnet-contract = { path = "../mixnet", features = ["testing-mocks"] }
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,2 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
@@ -0,0 +1,128 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use cosmwasm_contract_testing::{env_with_block_info, ContractState, MultiContractMock};
|
||||
use cosmwasm_std::testing::mock_info;
|
||||
use cosmwasm_std::{Addr, BankMsg, BlockInfo, Timestamp};
|
||||
use cw_storage_plus::Map;
|
||||
use mixnet_contract::MixnetContract;
|
||||
use nym_mixnet_contract_common::rewarding::PendingRewardResponse;
|
||||
use vesting_contract::vesting::Account;
|
||||
use vesting_contract::VestingContract;
|
||||
|
||||
// this is not directly exported by the vesting contract, but we can easily recreate it
|
||||
const VESTING_ACCOUNTS: Map<'_, Addr, Account> = Map::new("acc");
|
||||
|
||||
// hardcoded values from the data dump sources
|
||||
const MIXNET_CONTRACT_ADDRESS: &str =
|
||||
"n14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sjyvg3g";
|
||||
const VESTING_CONTRACT_ADDRESS: &str =
|
||||
"n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw";
|
||||
|
||||
fn set_mock() -> MultiContractMock {
|
||||
let current_block = BlockInfo {
|
||||
height: 1928125,
|
||||
time: Timestamp::from_seconds(1676482616),
|
||||
chain_id: "nymnet".to_string(),
|
||||
};
|
||||
let custom_env = env_with_block_info(current_block);
|
||||
|
||||
let mix_mock = ContractState::try_from_state_dump(
|
||||
"contract-states/15.02.23-173000-qwerty-mixnet.json",
|
||||
Some(custom_env.clone()),
|
||||
)
|
||||
.unwrap()
|
||||
.with_contract_address(MIXNET_CONTRACT_ADDRESS);
|
||||
let vesting_mock = ContractState::try_from_state_dump(
|
||||
"contract-states/15.02.23-173000-qwerty-vesting.json",
|
||||
Some(custom_env),
|
||||
)
|
||||
.unwrap()
|
||||
.with_contract_address(VESTING_CONTRACT_ADDRESS);
|
||||
|
||||
let mut multi_mock = MultiContractMock::new();
|
||||
|
||||
multi_mock.add_contract::<MixnetContract>(mix_mock).unwrap();
|
||||
multi_mock
|
||||
.add_contract::<VestingContract>(vesting_mock)
|
||||
.unwrap();
|
||||
multi_mock
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn claiming_vesting_delegator_rewards() {
|
||||
let mut multi_mock = set_mock();
|
||||
|
||||
let dummy_account = Addr::unchecked("n1ktpuwtweku40uaxcl4uq7mdkkmjeh698g3l3c8");
|
||||
|
||||
// do some queries to verify state is updated correctly for both contracts
|
||||
let pending_reward: PendingRewardResponse = multi_mock
|
||||
.query_de::<MixnetContract, _>(
|
||||
MIXNET_CONTRACT_ADDRESS,
|
||||
nym_mixnet_contract_common::QueryMsg::GetPendingDelegatorReward {
|
||||
address: dummy_account.to_string(),
|
||||
mix_id: 8,
|
||||
proxy: Some(VESTING_CONTRACT_ADDRESS.to_string()),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let pending_reward_amount = pending_reward.amount_earned.unwrap().amount;
|
||||
|
||||
// we can also get whatever we want directly from storage!
|
||||
let contract_state = multi_mock.contract_state(VESTING_CONTRACT_ADDRESS).unwrap();
|
||||
let vesting_account = contract_state
|
||||
.load_map_value(&VESTING_ACCOUNTS, dummy_account.clone())
|
||||
.unwrap();
|
||||
let vesting_balance = vesting_account
|
||||
.load_balance(contract_state.deps().storage)
|
||||
.unwrap();
|
||||
|
||||
let res = multi_mock.execute_full::<VestingContract>(
|
||||
VESTING_CONTRACT_ADDRESS,
|
||||
mock_info(dummy_account.as_str(), &[]),
|
||||
nym_vesting_contract_common::ExecuteMsg::ClaimDelegatorReward { mix_id: 8 },
|
||||
);
|
||||
|
||||
match res {
|
||||
Ok(success) => {
|
||||
println!("{}", success.pretty());
|
||||
|
||||
// check the output
|
||||
|
||||
// unfortunately `ClaimDelegatorReward` doesn't emit any events, but we can see
|
||||
// it's going to result into a call into the mixnet contract
|
||||
assert_eq!(
|
||||
success.steps[0].further_execution[0].contract.as_str(),
|
||||
MIXNET_CONTRACT_ADDRESS
|
||||
);
|
||||
|
||||
// mixnet contract will emit a `v2_withdraw_delegator_reward` event
|
||||
// and call the vesting contract again
|
||||
assert_eq!(
|
||||
"v2_withdraw_delegator_reward",
|
||||
success.steps[1].events[0].ty
|
||||
);
|
||||
assert_eq!(
|
||||
success.steps[1].further_execution[0].contract.as_str(),
|
||||
VESTING_CONTRACT_ADDRESS
|
||||
);
|
||||
// and will move our reward amount into the vesting contract...
|
||||
assert!(matches!(
|
||||
&success.steps[1].bank_msgs[0],
|
||||
BankMsg::Send { to_address, amount }
|
||||
if to_address == VESTING_CONTRACT_ADDRESS && amount[0].amount == pending_reward_amount
|
||||
));
|
||||
|
||||
// and finally the vesting contract will emit the mistyped `track_reaward` event
|
||||
assert_eq!("track_reaward", success.steps[2].events[0].ty);
|
||||
}
|
||||
Err(err) => panic!("{err}"),
|
||||
}
|
||||
|
||||
// state after execution (we can still read values the 'normal' way)
|
||||
let updated_state = multi_mock.contract_state(VESTING_CONTRACT_ADDRESS).unwrap();
|
||||
let deps = updated_state.deps();
|
||||
let vesting_account = VESTING_ACCOUNTS.load(deps.storage, dummy_account).unwrap();
|
||||
let new_vesting_balance = vesting_account.load_balance(deps.storage).unwrap();
|
||||
assert_eq!(new_vesting_balance, vesting_balance + pending_reward_amount)
|
||||
}
|
||||
@@ -36,15 +36,23 @@ serde = { version = "1.0.103", default-features = false, features = ["derive"] }
|
||||
thiserror = { version = "1.0.23" }
|
||||
time = { version = "0.3", features = ["macros"] }
|
||||
|
||||
cosmwasm-contract-testing = { path = "../../common/cosmwasm-smart-contracts/testing", default-features = false, features = ["testable-trait"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
cosmwasm-schema = "1.0.0"
|
||||
rand_chacha = "0.2"
|
||||
#rand = "0.7"
|
||||
nym-crypto = { path = "../../common/crypto", features = ["asymmetric", "rand"] }
|
||||
cosmwasm-contract-testing = { path = "../../common/cosmwasm-smart-contracts/testing" }
|
||||
|
||||
|
||||
[build-dependencies]
|
||||
vergen = { version = "5", default-features = false, features = ["build", "git", "rustc"] }
|
||||
|
||||
[[example]]
|
||||
name = "mock_testing"
|
||||
required-features = ["testing-mocks"]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
contract-testing = ["mixnet-contract-common/contract-testing"]
|
||||
testing-mocks = ["contract-testing", "cosmwasm-contract-testing"]
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use cosmwasm_contract_testing::{env_with_block_info, ContractState, SingleContractMock};
|
||||
use cosmwasm_std::from_slice;
|
||||
use cosmwasm_std::testing::mock_info;
|
||||
use cosmwasm_std::{BlockInfo, Timestamp};
|
||||
use mixnet_contract::mixnet_contract_settings::storage::CONTRACT_STATE;
|
||||
use mixnet_contract::mixnodes::queries::query_mixnode_details;
|
||||
use mixnet_contract::MixnetContract;
|
||||
use mixnet_contract::{mixnet_contract_settings, mixnodes};
|
||||
use mixnet_contract_common::{ContractState as MixnetContractState, ExecuteMsg, Layer, QueryMsg};
|
||||
|
||||
const MIXNET_CONTRACT_ADDRESS: &str =
|
||||
"n14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sjyvg3g";
|
||||
|
||||
fn set_mock() -> SingleContractMock<MixnetContract> {
|
||||
let current_block = BlockInfo {
|
||||
height: 1928125,
|
||||
time: Timestamp::from_seconds(1676482616),
|
||||
chain_id: "nymnet".to_string(),
|
||||
};
|
||||
let custom_env = env_with_block_info(current_block);
|
||||
|
||||
let mix_state = ContractState::try_from_state_dump(
|
||||
"../integration-tests/contract-states/15.02.23-173000-qwerty-mixnet.json",
|
||||
Some(custom_env.clone()),
|
||||
)
|
||||
.unwrap()
|
||||
.with_contract_address(MIXNET_CONTRACT_ADDRESS);
|
||||
|
||||
SingleContractMock::new(mix_state)
|
||||
}
|
||||
|
||||
fn normal_queries() {
|
||||
let mock = set_mock();
|
||||
|
||||
// the simplest example of a query: 'what's the current contract state?'
|
||||
let query = QueryMsg::GetState {};
|
||||
let result: MixnetContractState = mock.query_de(query).unwrap();
|
||||
// println!("{:?}", result);
|
||||
assert_eq!(
|
||||
"n1fxwdqgwht4j2suv5pr55304kt9z0avrvxs9ls0",
|
||||
result.owner.as_ref()
|
||||
);
|
||||
}
|
||||
|
||||
fn queries_with_native_functions() {
|
||||
let mock = set_mock();
|
||||
|
||||
// access exactly the same information as before, but this time with native functions
|
||||
let deps = mock.deps();
|
||||
let result = mixnet_contract_settings::queries::query_contract_state(deps).unwrap();
|
||||
// println!("{:?}", result);
|
||||
assert_eq!(
|
||||
"n1fxwdqgwht4j2suv5pr55304kt9z0avrvxs9ls0",
|
||||
result.owner.as_ref()
|
||||
);
|
||||
}
|
||||
|
||||
fn raw_storage_reads() {
|
||||
// we can also read any arbitrary data that's normally not exposed via queries
|
||||
// for this example, let's read exactly the same data again
|
||||
let mock = set_mock();
|
||||
|
||||
// wrapped in a cw-storage-plus 'item'
|
||||
let result = mock.state.load_item(&CONTRACT_STATE).unwrap();
|
||||
// println!("{:?}", result);
|
||||
assert_eq!(
|
||||
"n1fxwdqgwht4j2suv5pr55304kt9z0avrvxs9ls0",
|
||||
result.owner.as_ref()
|
||||
);
|
||||
|
||||
// a raw key-value read
|
||||
let result_raw = mock.state.read_key(b"state").unwrap();
|
||||
let result: MixnetContractState = from_slice(&result_raw).unwrap();
|
||||
assert_eq!(
|
||||
"n1fxwdqgwht4j2suv5pr55304kt9z0avrvxs9ls0",
|
||||
result.owner.as_ref()
|
||||
);
|
||||
}
|
||||
|
||||
fn normal_transactions() {
|
||||
let mut mock = set_mock();
|
||||
|
||||
// pretend you're the rewarding validator and force assign somebody's layer!
|
||||
let current_mixnode = query_mixnode_details(mock.deps(), 7).unwrap();
|
||||
assert_eq!(
|
||||
current_mixnode
|
||||
.mixnode_details
|
||||
.unwrap()
|
||||
.bond_information
|
||||
.layer,
|
||||
Layer::One
|
||||
);
|
||||
let rewarding_validator = mixnet_contract_settings::queries::query_contract_state(mock.deps())
|
||||
.unwrap()
|
||||
.rewarding_validator_address;
|
||||
|
||||
let msg_sender = mock_info(rewarding_validator.as_ref(), &[]);
|
||||
let msg = ExecuteMsg::AssignNodeLayer {
|
||||
mix_id: 7,
|
||||
layer: Layer::Two,
|
||||
};
|
||||
mock.execute(msg_sender, msg).unwrap();
|
||||
|
||||
let updated_mixnode = query_mixnode_details(mock.deps(), 7).unwrap();
|
||||
assert_eq!(
|
||||
updated_mixnode
|
||||
.mixnode_details
|
||||
.unwrap()
|
||||
.bond_information
|
||||
.layer,
|
||||
Layer::Two
|
||||
);
|
||||
}
|
||||
|
||||
fn changing_state_with_native_functions() {
|
||||
// do the same thing but this time calling contract methods directly
|
||||
let mut mock = set_mock();
|
||||
|
||||
let current_mixnode = query_mixnode_details(mock.deps(), 7).unwrap();
|
||||
assert_eq!(
|
||||
current_mixnode
|
||||
.mixnode_details
|
||||
.unwrap()
|
||||
.bond_information
|
||||
.layer,
|
||||
Layer::One
|
||||
);
|
||||
let rewarding_validator = mixnet_contract_settings::queries::query_contract_state(mock.deps())
|
||||
.unwrap()
|
||||
.rewarding_validator_address;
|
||||
|
||||
let msg_sender = mock_info(rewarding_validator.as_ref(), &[]);
|
||||
let deps = mock.deps_mut();
|
||||
|
||||
mixnodes::transactions::assign_mixnode_layer(deps, msg_sender, 7, Layer::Two).unwrap();
|
||||
let updated_mixnode = query_mixnode_details(mock.deps(), 7).unwrap();
|
||||
assert_eq!(
|
||||
updated_mixnode
|
||||
.mixnode_details
|
||||
.unwrap()
|
||||
.bond_information
|
||||
.layer,
|
||||
Layer::Two
|
||||
);
|
||||
}
|
||||
|
||||
fn writing_to_raw_storage() {
|
||||
// bypass this whole transaction business, authorization checks, etc and just write to the storage yourself
|
||||
let mut mock = set_mock();
|
||||
|
||||
let mut mix_bond = mixnodes::storage::mixnode_bonds()
|
||||
.load(mock.deps().storage, 7)
|
||||
.unwrap();
|
||||
assert_eq!(mix_bond.layer, Layer::One);
|
||||
mix_bond.layer = Layer::Two;
|
||||
|
||||
mixnodes::storage::mixnode_bonds()
|
||||
.save(mock.deps_mut().storage, 7, &mix_bond)
|
||||
.unwrap();
|
||||
|
||||
let updated_mixnode = query_mixnode_details(mock.deps(), 7).unwrap();
|
||||
assert_eq!(
|
||||
updated_mixnode
|
||||
.mixnode_details
|
||||
.unwrap()
|
||||
.bond_information
|
||||
.layer,
|
||||
Layer::Two
|
||||
);
|
||||
}
|
||||
|
||||
// run with `cargo run --example mock_testing --features="testing-mocks"`
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn main() {
|
||||
normal_queries();
|
||||
queries_with_native_functions();
|
||||
raw_storage_reads();
|
||||
|
||||
normal_transactions();
|
||||
changing_state_with_native_functions();
|
||||
writing_to_raw_storage();
|
||||
}
|
||||
@@ -16,7 +16,7 @@ use mixnet_contract_common::{
|
||||
PagedMixNodeDelegationsResponse,
|
||||
};
|
||||
|
||||
pub(crate) fn query_mixnode_delegations_paged(
|
||||
pub fn query_mixnode_delegations_paged(
|
||||
deps: Deps<'_>,
|
||||
mix_id: MixId,
|
||||
start_after: Option<String>,
|
||||
@@ -47,7 +47,7 @@ pub(crate) fn query_mixnode_delegations_paged(
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn query_delegator_delegations_paged(
|
||||
pub fn query_delegator_delegations_paged(
|
||||
deps: Deps<'_>,
|
||||
delegation_owner: String,
|
||||
start_after: Option<(MixId, OwnerProxySubKey)>,
|
||||
@@ -83,7 +83,7 @@ pub(crate) fn query_delegator_delegations_paged(
|
||||
}
|
||||
|
||||
// queries for delegation value of given address for particular node
|
||||
pub(crate) fn query_mixnode_delegation(
|
||||
pub fn query_mixnode_delegation(
|
||||
deps: Deps<'_>,
|
||||
mix_id: MixId,
|
||||
delegation_owner: String,
|
||||
@@ -109,7 +109,7 @@ pub(crate) fn query_mixnode_delegation(
|
||||
))
|
||||
}
|
||||
|
||||
pub(crate) fn query_all_delegations_paged(
|
||||
pub fn query_all_delegations_paged(
|
||||
deps: Deps<'_>,
|
||||
start_after: Option<delegation::StorageKey>,
|
||||
limit: Option<u32>,
|
||||
|
||||
@@ -9,7 +9,7 @@ use mixnet_contract_common::{
|
||||
GatewayBond, GatewayBondResponse, GatewayOwnershipResponse, IdentityKey, PagedGatewayResponse,
|
||||
};
|
||||
|
||||
pub(crate) fn query_gateways_paged(
|
||||
pub fn query_gateways_paged(
|
||||
deps: Deps<'_>,
|
||||
start_after: Option<IdentityKey>,
|
||||
limit: Option<u32>,
|
||||
@@ -31,10 +31,7 @@ pub(crate) fn query_gateways_paged(
|
||||
Ok(PagedGatewayResponse::new(nodes, limit, start_next_after))
|
||||
}
|
||||
|
||||
pub(crate) fn query_owned_gateway(
|
||||
deps: Deps<'_>,
|
||||
address: String,
|
||||
) -> StdResult<GatewayOwnershipResponse> {
|
||||
pub fn query_owned_gateway(deps: Deps<'_>, address: String) -> StdResult<GatewayOwnershipResponse> {
|
||||
let validated_addr = deps.api.addr_validate(&address)?;
|
||||
|
||||
let gateway = storage::gateways()
|
||||
|
||||
@@ -4,16 +4,19 @@
|
||||
#![warn(clippy::expect_used)]
|
||||
#![warn(clippy::unwrap_used)]
|
||||
|
||||
mod constants;
|
||||
pub mod constants;
|
||||
pub mod contract;
|
||||
mod delegations;
|
||||
mod families;
|
||||
mod gateways;
|
||||
mod interval;
|
||||
mod mixnet_contract_settings;
|
||||
mod mixnodes;
|
||||
mod rewards;
|
||||
mod support;
|
||||
pub mod delegations;
|
||||
pub mod families;
|
||||
pub mod gateways;
|
||||
pub mod interval;
|
||||
pub mod mixnet_contract_settings;
|
||||
pub mod mixnodes;
|
||||
pub mod rewards;
|
||||
pub mod support;
|
||||
|
||||
#[cfg(feature = "contract-testing")]
|
||||
mod testing;
|
||||
|
||||
#[cfg(feature = "testing-mocks")]
|
||||
pub use testing::mock_helpers::MixnetContract;
|
||||
|
||||
@@ -5,23 +5,23 @@ use super::storage;
|
||||
use cosmwasm_std::{Deps, StdResult};
|
||||
use mixnet_contract_common::{ContractBuildInformation, ContractState, ContractStateParams};
|
||||
|
||||
pub(crate) fn query_contract_state(deps: Deps<'_>) -> StdResult<ContractState> {
|
||||
pub fn query_contract_state(deps: Deps<'_>) -> StdResult<ContractState> {
|
||||
storage::CONTRACT_STATE.load(deps.storage)
|
||||
}
|
||||
|
||||
pub(crate) fn query_contract_settings_params(deps: Deps<'_>) -> StdResult<ContractStateParams> {
|
||||
pub fn query_contract_settings_params(deps: Deps<'_>) -> StdResult<ContractStateParams> {
|
||||
storage::CONTRACT_STATE
|
||||
.load(deps.storage)
|
||||
.map(|settings| settings.params)
|
||||
}
|
||||
|
||||
pub(crate) fn query_rewarding_validator_address(deps: Deps<'_>) -> StdResult<String> {
|
||||
pub fn query_rewarding_validator_address(deps: Deps<'_>) -> StdResult<String> {
|
||||
storage::CONTRACT_STATE
|
||||
.load(deps.storage)
|
||||
.map(|settings| settings.rewarding_validator_address.to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn query_contract_version() -> ContractBuildInformation {
|
||||
pub fn query_contract_version() -> ContractBuildInformation {
|
||||
// as per docs
|
||||
// env! macro will expand to the value of the named environment variable at
|
||||
// compile time, yielding an expression of type `&'static str`
|
||||
|
||||
@@ -8,7 +8,7 @@ use cw_storage_plus::Item;
|
||||
use mixnet_contract_common::error::MixnetContractError;
|
||||
use mixnet_contract_common::ContractState;
|
||||
|
||||
pub(crate) const CONTRACT_STATE: Item<'_, ContractState> = Item::new(CONTRACT_STATE_KEY);
|
||||
pub const CONTRACT_STATE: Item<'_, ContractState> = Item::new(CONTRACT_STATE_KEY);
|
||||
|
||||
pub fn rewarding_validator_address(storage: &dyn Storage) -> Result<Addr, MixnetContractError> {
|
||||
Ok(CONTRACT_STATE
|
||||
|
||||
@@ -256,7 +256,7 @@ pub fn query_stake_saturation(deps: Deps<'_>, mix_id: MixId) -> StdResult<StakeS
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn query_layer_distribution(deps: Deps<'_>) -> StdResult<LayerDistribution> {
|
||||
pub fn query_layer_distribution(deps: Deps<'_>) -> StdResult<LayerDistribution> {
|
||||
storage::LAYERS.load(deps.storage)
|
||||
}
|
||||
|
||||
|
||||
@@ -48,10 +48,10 @@ pub(crate) fn unbonded_mixnodes<'a>(
|
||||
IndexedMap::new(UNBONDED_MIXNODES_PK_NAMESPACE, indexes)
|
||||
}
|
||||
|
||||
pub(crate) const LAYERS: Item<'_, LayerDistribution> = Item::new(LAYER_DISTRIBUTION_KEY);
|
||||
pub const LAYERS: Item<'_, LayerDistribution> = Item::new(LAYER_DISTRIBUTION_KEY);
|
||||
pub const MIXNODE_ID_COUNTER: Item<MixId> = Item::new(NODE_ID_COUNTER_KEY);
|
||||
|
||||
pub(crate) struct MixnodeBondIndex<'a> {
|
||||
pub struct MixnodeBondIndex<'a> {
|
||||
pub(crate) owner: UniqueIndex<'a, Addr, MixNodeBond>,
|
||||
|
||||
pub(crate) identity_key: UniqueIndex<'a, IdentityKey, MixNodeBond>,
|
||||
@@ -70,7 +70,7 @@ impl<'a> IndexList<MixNodeBond> for MixnodeBondIndex<'a> {
|
||||
}
|
||||
|
||||
// mixnode_bonds() is the storage access function.
|
||||
pub(crate) fn mixnode_bonds<'a>() -> IndexedMap<'a, MixId, MixNodeBond, MixnodeBondIndex<'a>> {
|
||||
pub fn mixnode_bonds<'a>() -> IndexedMap<'a, MixId, MixNodeBond, MixnodeBondIndex<'a>> {
|
||||
let indexes = MixnodeBondIndex {
|
||||
owner: UniqueIndex::new(|d| d.owner.clone(), MIXNODES_OWNER_IDX_NAMESPACE),
|
||||
identity_key: UniqueIndex::new(
|
||||
|
||||
@@ -16,7 +16,7 @@ use mixnet_contract_common::rewarding::{
|
||||
};
|
||||
use mixnet_contract_common::{Delegation, MixId};
|
||||
|
||||
pub(crate) fn query_rewarding_params(deps: Deps<'_>) -> StdResult<RewardingParams> {
|
||||
pub fn query_rewarding_params(deps: Deps<'_>) -> StdResult<RewardingParams> {
|
||||
storage::REWARDING_PARAMS.load(deps.storage)
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ fn zero_reward(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn query_estimated_current_epoch_operator_reward(
|
||||
pub fn query_estimated_current_epoch_operator_reward(
|
||||
deps: Deps<'_>,
|
||||
mix_id: MixId,
|
||||
estimated_performance: Performance,
|
||||
@@ -157,7 +157,7 @@ pub(crate) fn query_estimated_current_epoch_operator_reward(
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn query_estimated_current_epoch_delegator_reward(
|
||||
pub fn query_estimated_current_epoch_delegator_reward(
|
||||
deps: Deps<'_>,
|
||||
owner: String,
|
||||
mix_id: MixId,
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::contract;
|
||||
use cosmwasm_contract_testing::TestableContract;
|
||||
use cosmwasm_std::{Deps, DepsMut, Env, MessageInfo, QueryResponse, Response};
|
||||
use mixnet_contract_common::error::MixnetContractError;
|
||||
use mixnet_contract_common::{ExecuteMsg, InstantiateMsg, QueryMsg};
|
||||
|
||||
pub struct MixnetContract;
|
||||
|
||||
impl TestableContract for MixnetContract {
|
||||
type ContractError = MixnetContractError;
|
||||
type InstantiateMsg = InstantiateMsg;
|
||||
type ExecuteMsg = ExecuteMsg;
|
||||
type QueryMsg = QueryMsg;
|
||||
|
||||
fn new() -> Self {
|
||||
MixnetContract
|
||||
}
|
||||
|
||||
fn instantiate(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::InstantiateMsg,
|
||||
) -> Result<Response, Self::ContractError> {
|
||||
contract::instantiate(deps, env, info, msg)
|
||||
}
|
||||
|
||||
fn execute(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::ExecuteMsg,
|
||||
) -> Result<Response, Self::ContractError> {
|
||||
contract::execute(deps, env, info, msg)
|
||||
}
|
||||
|
||||
fn query(
|
||||
deps: Deps<'_>,
|
||||
env: Env,
|
||||
msg: Self::QueryMsg,
|
||||
) -> Result<QueryResponse, Self::ContractError> {
|
||||
contract::query(deps, env, msg)
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
pub(crate) mod transactions;
|
||||
|
||||
#[cfg(feature = "testing-mocks")]
|
||||
pub mod mock_helpers;
|
||||
|
||||
@@ -31,11 +31,14 @@ schemars = "0.8"
|
||||
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
||||
thiserror = { version = "1.0" }
|
||||
|
||||
cosmwasm-contract-testing = { path = "../../common/cosmwasm-smart-contracts/testing", default-features = false, features = ["testable-trait"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
rand_chacha = "0.3.1"
|
||||
base64 = "0.21.0"
|
||||
hex = "0.4.3"
|
||||
serde_json = "1.0.66"
|
||||
cosmwasm-contract-testing = { path = "../../common/cosmwasm-smart-contracts/testing" }
|
||||
|
||||
[build-dependencies]
|
||||
vergen = { version = "5", default-features = false, features = ["build", "git", "rustc"] }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
testing-mocks = ["cosmwasm-contract-testing"]
|
||||
@@ -299,7 +299,7 @@ pub fn try_withdraw_vested_coins(
|
||||
}
|
||||
|
||||
/// Transfer ownership of the entire vesting account.
|
||||
fn try_transfer_ownership(
|
||||
pub fn try_transfer_ownership(
|
||||
to_address: String,
|
||||
info: MessageInfo,
|
||||
deps: DepsMut<'_>,
|
||||
@@ -316,7 +316,7 @@ fn try_transfer_ownership(
|
||||
}
|
||||
|
||||
/// Set or update staking address for a vesting account.
|
||||
fn try_update_staking_address(
|
||||
pub fn try_update_staking_address(
|
||||
to_address: Option<String>,
|
||||
info: MessageInfo,
|
||||
deps: DepsMut<'_>,
|
||||
@@ -434,7 +434,7 @@ pub fn try_track_unbond_mixnode(
|
||||
}
|
||||
|
||||
/// Track reward collection, invoked by the mixnert contract after sucessful reward compounding or claiming
|
||||
fn try_track_reward(
|
||||
pub fn try_track_reward(
|
||||
deps: DepsMut<'_>,
|
||||
info: MessageInfo,
|
||||
amount: Coin,
|
||||
@@ -449,7 +449,7 @@ fn try_track_reward(
|
||||
}
|
||||
|
||||
/// Track undelegation, invoked by the mixnet contract after sucessful undelegation, message contains coins returned with any accrued rewards.
|
||||
fn try_track_undelegation(
|
||||
pub fn try_track_undelegation(
|
||||
address: &str,
|
||||
mix_id: MixId,
|
||||
amount: Coin,
|
||||
@@ -466,7 +466,7 @@ fn try_track_undelegation(
|
||||
}
|
||||
|
||||
/// Delegate to mixnode, sends [mixnet_contract_common::ExecuteMsg::DelegateToMixnodeOnBehalf] to [crate::storage::MIXNET_CONTRACT_ADDRESS]..
|
||||
fn try_delegate_to_mixnode(
|
||||
pub fn try_delegate_to_mixnode(
|
||||
mix_id: MixId,
|
||||
amount: Coin,
|
||||
on_behalf_of: Option<String>,
|
||||
@@ -491,7 +491,7 @@ fn try_delegate_to_mixnode(
|
||||
}
|
||||
|
||||
/// Claims operator reward, sends [mixnet_contract_common::ExecuteMsg::ClaimOperatorRewardOnBehalf] to [crate::storage::MIXNET_CONTRACT_ADDRESS].
|
||||
fn try_claim_operator_reward(
|
||||
pub fn try_claim_operator_reward(
|
||||
deps: DepsMut<'_>,
|
||||
info: MessageInfo,
|
||||
) -> Result<Response, ContractError> {
|
||||
@@ -500,7 +500,7 @@ fn try_claim_operator_reward(
|
||||
}
|
||||
|
||||
/// Claims delegator reward, sends [mixnet_contract_common::ExecuteMsg::ClaimDelegatorRewardOnBehalf] to [crate::storage::MIXNET_CONTRACT_ADDRESS].
|
||||
fn try_claim_delegator_reward(
|
||||
pub fn try_claim_delegator_reward(
|
||||
deps: DepsMut<'_>,
|
||||
info: MessageInfo,
|
||||
mix_id: MixId,
|
||||
@@ -511,7 +511,7 @@ fn try_claim_delegator_reward(
|
||||
}
|
||||
|
||||
/// Undelegates from a mixnode, sends [mixnet_contract_common::ExecuteMsg::UndelegateFromMixnodeOnBehalf] to [crate::storage::MIXNET_CONTRACT_ADDRESS].
|
||||
fn try_undelegate_from_mixnode(
|
||||
pub fn try_undelegate_from_mixnode(
|
||||
mix_id: MixId,
|
||||
on_behalf_of: Option<String>,
|
||||
info: MessageInfo,
|
||||
@@ -533,7 +533,7 @@ fn try_undelegate_from_mixnode(
|
||||
/// Creates a new periodic vesting account, and deposits funds to vest into the contract.
|
||||
///
|
||||
/// Callable by ADMIN only, see [instantiate].
|
||||
pub(crate) fn try_create_periodic_vesting_account(
|
||||
pub fn try_create_periodic_vesting_account(
|
||||
owner_address: &str,
|
||||
staking_address: Option<String>,
|
||||
vesting_spec: Option<VestingSpecification>,
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
#![warn(clippy::unwrap_used)]
|
||||
|
||||
pub mod contract;
|
||||
mod errors;
|
||||
pub mod errors;
|
||||
mod queued_migrations;
|
||||
mod storage;
|
||||
mod support;
|
||||
mod traits;
|
||||
pub mod vesting;
|
||||
|
||||
#[cfg(feature = "testing-mocks")]
|
||||
pub use support::mock_helpers::VestingContract;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use crate::contract;
|
||||
use crate::errors::ContractError;
|
||||
use cosmwasm_contract_testing::TestableContract;
|
||||
use cosmwasm_std::{Deps, DepsMut, Env, MessageInfo, QueryResponse, Response};
|
||||
use vesting_contract_common::{ExecuteMsg, InitMsg, QueryMsg};
|
||||
|
||||
pub struct VestingContract;
|
||||
|
||||
impl TestableContract for VestingContract {
|
||||
type ContractError = ContractError;
|
||||
type InstantiateMsg = InitMsg;
|
||||
type ExecuteMsg = ExecuteMsg;
|
||||
type QueryMsg = QueryMsg;
|
||||
|
||||
fn new() -> Self {
|
||||
VestingContract
|
||||
}
|
||||
|
||||
fn instantiate(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::InstantiateMsg,
|
||||
) -> Result<Response, Self::ContractError> {
|
||||
contract::instantiate(deps, env, info, msg)
|
||||
}
|
||||
|
||||
fn execute(
|
||||
deps: DepsMut<'_>,
|
||||
env: Env,
|
||||
info: MessageInfo,
|
||||
msg: Self::ExecuteMsg,
|
||||
) -> Result<Response, Self::ContractError> {
|
||||
contract::execute(deps, env, info, msg)
|
||||
}
|
||||
|
||||
fn query(
|
||||
deps: Deps<'_>,
|
||||
env: Env,
|
||||
msg: Self::QueryMsg,
|
||||
) -> Result<QueryResponse, Self::ContractError> {
|
||||
contract::query(deps, env, msg)
|
||||
}
|
||||
}
|
||||
@@ -1 +1,7 @@
|
||||
// Copyright 2022-2023 - Nym Technologies SA <contact@nymtech.net>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
pub mod tests;
|
||||
|
||||
#[cfg(feature = "testing-mocks")]
|
||||
pub mod mock_helpers;
|
||||
|
||||
@@ -1,104 +1,24 @@
|
||||
#[cfg(test)]
|
||||
pub mod helpers {
|
||||
|
||||
// TODO: once https://github.com/nymtech/nym/pull/3040 gets merged,
|
||||
// the `ContractState` should replace the below
|
||||
#[allow(unused)]
|
||||
mod state_dump_decoder {
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RawState {
|
||||
pub height: String,
|
||||
pub result: Vec<RawKV>,
|
||||
}
|
||||
|
||||
impl RawState {
|
||||
pub fn decode(self) -> DecodedState {
|
||||
DecodedState {
|
||||
height: self.height.parse().unwrap(),
|
||||
result: self
|
||||
.result
|
||||
.into_iter()
|
||||
.map(|raw| DecodedKV {
|
||||
key: hex::decode(&raw.key).unwrap(),
|
||||
value: general_purpose::STANDARD.decode(&raw.value).unwrap(),
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file<P: AsRef<Path>>(path: P) -> Self {
|
||||
let file = File::open(path).expect("failed to open specified file");
|
||||
serde_json::from_reader(file).expect("failed to parse specified file")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RawKV {
|
||||
// hex
|
||||
pub key: String,
|
||||
|
||||
// base64
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DecodedKV {
|
||||
pub key: Vec<u8>,
|
||||
pub value: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DecodedState {
|
||||
pub height: u64,
|
||||
pub result: Vec<DecodedKV>,
|
||||
}
|
||||
|
||||
impl DecodedState {
|
||||
pub fn find_value(&self, key: &[u8]) -> Option<Vec<u8>> {
|
||||
self.result
|
||||
.iter()
|
||||
.find(|kv| kv.key == key)
|
||||
.map(|kv| kv.value.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use crate::contract::{instantiate, try_create_periodic_vesting_account};
|
||||
use crate::storage::{ACCOUNTS, ADMIN, MIXNET_CONTRACT_ADDRESS, MIX_DENOM};
|
||||
use crate::support::tests::helpers::state_dump_decoder::RawState;
|
||||
use crate::traits::VestingAccount;
|
||||
use crate::vesting::{populate_vesting_periods, Account};
|
||||
use contracts_common::Percent;
|
||||
use cosmwasm_contract_testing::{env_with_block_info, ContractState};
|
||||
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier};
|
||||
use cosmwasm_std::{
|
||||
coin, Addr, BlockInfo, Coin, ContractInfo, Deps, DepsMut, Empty, Env, MemoryStorage,
|
||||
MessageInfo, OwnedDeps, Storage, Timestamp, Uint128,
|
||||
coin, Addr, BlockInfo, Coin, Deps, DepsMut, Empty, Env, MemoryStorage, MessageInfo,
|
||||
OwnedDeps, Storage, Timestamp, Uint128,
|
||||
};
|
||||
use rand_chacha::rand_core::SeedableRng;
|
||||
use rand_chacha::ChaCha20Rng;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
use vesting_contract_common::messages::{InitMsg, VestingSpecification};
|
||||
use vesting_contract_common::PledgeCap;
|
||||
|
||||
// use rng with constant seed for all tests so that they would be deterministic
|
||||
#[allow(unused)]
|
||||
pub fn test_rng() -> ChaCha20Rng {
|
||||
let dummy_seed = [42u8; 32];
|
||||
rand_chacha::ChaCha20Rng::from_seed(dummy_seed)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub struct TestSetup {
|
||||
pub deps: OwnedDeps<MemoryStorage, MockApi, MockQuerier<Empty>>,
|
||||
pub env: Env,
|
||||
pub rng: ChaCha20Rng,
|
||||
|
||||
pub state: ContractState,
|
||||
pub admin: MessageInfo,
|
||||
}
|
||||
|
||||
@@ -109,40 +29,28 @@ pub mod helpers {
|
||||
let admin = ADMIN.load(deps.as_ref().storage).unwrap();
|
||||
|
||||
TestSetup {
|
||||
deps,
|
||||
env: mock_env(),
|
||||
rng: test_rng(),
|
||||
state: ContractState::new(),
|
||||
admin: mock_info(admin.as_str(), &[]),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_state_dump<P: AsRef<Path>>(dump_file: P) -> Self {
|
||||
let state = RawState::from_file(dump_file).decode();
|
||||
|
||||
let mut deps = mock_dependencies();
|
||||
for kv in state.result {
|
||||
deps.storage.set(&kv.key, &kv.value)
|
||||
}
|
||||
|
||||
let admin = ADMIN.load(deps.as_ref().storage).unwrap();
|
||||
let env = Env {
|
||||
block: BlockInfo {
|
||||
height: 5633424,
|
||||
time: Timestamp::from_seconds(1676025955),
|
||||
chain_id: "nyx".to_string(),
|
||||
},
|
||||
transaction: None,
|
||||
contract: ContractInfo {
|
||||
address: Addr::unchecked(
|
||||
"n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw",
|
||||
),
|
||||
},
|
||||
let current_block = BlockInfo {
|
||||
height: 5633424,
|
||||
time: Timestamp::from_seconds(1676025955),
|
||||
chain_id: "nyx".to_string(),
|
||||
};
|
||||
let custom_env = env_with_block_info(current_block);
|
||||
let state = ContractState::try_from_state_dump(dump_file, Some(custom_env.clone()))
|
||||
.unwrap()
|
||||
.with_contract_address(
|
||||
"n1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrq73f2nw",
|
||||
);
|
||||
|
||||
let admin = ADMIN.load(state.deps().storage).unwrap();
|
||||
|
||||
TestSetup {
|
||||
deps,
|
||||
env,
|
||||
rng: test_rng(),
|
||||
state,
|
||||
admin: mock_info(admin.as_str(), &[]),
|
||||
}
|
||||
}
|
||||
@@ -173,15 +81,15 @@ pub mod helpers {
|
||||
}
|
||||
|
||||
pub fn deps(&self) -> Deps<'_> {
|
||||
self.deps.as_ref()
|
||||
self.state.deps()
|
||||
}
|
||||
|
||||
pub fn deps_mut(&mut self) -> DepsMut<'_> {
|
||||
self.deps.as_mut()
|
||||
self.state.deps_mut()
|
||||
}
|
||||
|
||||
pub fn env(&self) -> Env {
|
||||
self.env.clone()
|
||||
self.state.env_cloned()
|
||||
}
|
||||
|
||||
pub fn admin(&self) -> MessageInfo {
|
||||
@@ -223,18 +131,18 @@ pub mod helpers {
|
||||
|
||||
let pretty = format!(
|
||||
r#"
|
||||
{:<20}{original}
|
||||
{:<20}{vesting}
|
||||
{:<20}{vested}
|
||||
{:<20}{balance}
|
||||
{:<20}{withdrawn}
|
||||
{:<20}{historical_rewards}
|
||||
{:<20}{locked}
|
||||
{:<20}{spendable}
|
||||
{:<20}{spendable_vested}
|
||||
{:<20}{spendable_reward}
|
||||
{:<20}{total_delegated}
|
||||
"#,
|
||||
{:<20}{original}
|
||||
{:<20}{vesting}
|
||||
{:<20}{vested}
|
||||
{:<20}{balance}
|
||||
{:<20}{withdrawn}
|
||||
{:<20}{historical_rewards}
|
||||
{:<20}{locked}
|
||||
{:<20}{spendable}
|
||||
{:<20}{spendable_vested}
|
||||
{:<20}{spendable_reward}
|
||||
{:<20}{total_delegated}
|
||||
"#,
|
||||
"original",
|
||||
"vesting",
|
||||
"vested:",
|
||||
@@ -265,37 +173,37 @@ pub mod helpers {
|
||||
|
||||
pub fn vesting_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.get_vesting_coins(None, &self.env, self.deps().storage)
|
||||
.get_vesting_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn vested_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.get_vested_coins(None, &self.env, self.deps().storage)
|
||||
.get_vested_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn locked_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.locked_coins(None, &self.env, self.deps().storage)
|
||||
.locked_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn spendable_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.spendable_coins(None, &self.env, self.deps().storage)
|
||||
.spendable_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn spendable_vested_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.spendable_vested_coins(None, &self.env, self.deps().storage)
|
||||
.spendable_vested_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn spendable_reward_coins(&self, account: &Account) -> Coin {
|
||||
account
|
||||
.spendable_reward_coins(None, &self.env, self.deps().storage)
|
||||
.spendable_reward_coins(None, self.state.env(), self.deps().storage)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user