Skip to content

Commit

Permalink
Genesis setup and valset tests using helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Sep 29, 2021
1 parent cf39fbd commit 1c12949
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
8 changes: 6 additions & 2 deletions contracts/tgrade-valset/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,12 @@ mod test {
.unwrap();
suite.jail(&admin, &operators[1].addr, None).unwrap();

// Move forward a little, but not enough for jailing to expire
suite.app().update_block(next_block);
// Move forward a little, but not enough for jailing to expire,
// but at least 100s for the default epoch size to re-calculate
suite.app().update_block(|block| {
block.height += 20;
block.time = block.time.plus_seconds(100);
});

// Endblock triggered - only unjailed validators are active
suite.end_block().unwrap();
Expand Down
35 changes: 28 additions & 7 deletions contracts/tgrade-valset/src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![cfg(test)]
use anyhow::Result as AnyResult;
use cosmwasm_std::{coin, Addr, Binary, Coin, Decimal, StdResult};
use cosmwasm_std::{coin, Addr, Binary, BlockInfo, Coin, Decimal, StdResult};
use cw_multi_test::{AppResponse, Contract, ContractWrapper, Executor};
use derivative::Derivative;

use tg4::Member;
use tg_bindings::{Pubkey, TgradeMsg, TgradeSudoMsg, TgradeApp};
use tg_bindings::{Pubkey, TgradeApp, TgradeMsg, ValidatorDiff};
use tg_utils::Duration;

use crate::msg::{
Expand Down Expand Up @@ -258,6 +258,30 @@ impl SuiteBuilder {
)
.unwrap();

// start from genesis
let mut previous = app.block_info();
previous.height -= 1;
previous.time = previous.time.minus_seconds(5);

let genesis = BlockInfo {
height: 0,
time: previous.time.minus_seconds(5 * previous.height),
chain_id: previous.chain_id.clone(),
};
app.set_block(genesis);

// promote the valset contract
app.promote(admin.as_str(), valset.as_str()).unwrap();

// process initial genesis block
let diff = app.next_block().unwrap();
let diff = diff.unwrap();
assert_eq!(diff.diffs.len(), members.len());

// process the block before our current one
app.set_block(previous);
app.next_block().unwrap();

Suite {
app,
valset,
Expand Down Expand Up @@ -298,11 +322,8 @@ impl Suite {
&mut self.app
}

pub fn end_block(&mut self) -> AnyResult<AppResponse> {
self.app.wasm_sudo(
self.valset.clone(),
&TgradeSudoMsg::EndWithValidatorUpdate {},
)
pub fn end_block(&mut self) -> AnyResult<Option<ValidatorDiff>> {
self.app.next_block()
}

pub fn jail(
Expand Down
43 changes: 29 additions & 14 deletions packages/bindings/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use cosmwasm_std::{
StdError, StdResult, Storage,
};
use cw_multi_test::{
next_block, App, AppResponse, BankKeeper, BankSudo, BasicAppBuilder, CosmosRouter, Module,
WasmKeeper, WasmSudo,
next_block, App, AppResponse, BankKeeper, BankSudo, BasicAppBuilder, CosmosRouter, Executor,
Module, WasmKeeper, WasmSudo,
};
use cw_storage_plus::{Item, Map};

Expand Down Expand Up @@ -86,18 +86,22 @@ impl Module for TgradeModule {
{
match msg {
TgradeMsg::Privilege(PrivilegeMsg::Request(add)) => {
// there can be only one with ValidatorSetUpdater privilege
let validator_registered = PRIVILEGES
.range(storage, None, None, Order::Ascending)
.fold(Ok(false), |val, item| match (val, item) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e),
(Ok(found), Ok((_, privs))) => {
Ok(found || privs.iter().any(|p| *p == Privilege::ValidatorSetUpdater))
}
})?;
if validator_registered {
bail!("One ValidatorSetUpdater already registered, cannot register a second")
if add == Privilege::ValidatorSetUpdater {
// there can be only one with ValidatorSetUpdater privilege
let validator_registered =
PRIVILEGES
.range(storage, None, None, Order::Ascending)
.fold(Ok(false), |val, item| match (val, item) {
(Err(e), _) => Err(e),
(_, Err(e)) => Err(e),
(Ok(found), Ok((_, privs))) => Ok(found
|| privs.iter().any(|p| *p == Privilege::ValidatorSetUpdater)),
})?;
if validator_registered {
bail!(
"One ValidatorSetUpdater already registered, cannot register a second"
);
}
}

// if we are privileged (even an empty array), we can auto-add more
Expand Down Expand Up @@ -276,6 +280,17 @@ impl TgradeApp {
)
}

pub fn promote(&mut self, owner: &str, contract: &str) -> AnyResult<AppResponse> {
let msg = TgradeMsg::ExecuteGovProposal {
title: "Promote Contract".to_string(),
description: "Promote Contract".to_string(),
proposal: GovProposal::PromoteToPrivilegedContract {
contract: contract.to_string(),
},
};
self.execute(Addr::unchecked(owner), msg.into())
}

/// next_block will call the end_blocker, increment block info 1 height and 5 seconds,
/// and then call the begin_blocker (with no evidence) in the next block.
/// It returns the validator diff if any.
Expand Down

0 comments on commit 1c12949

Please sign in to comment.