Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block cache rollback integration test #1507

Open
wants to merge 6 commits into
base: proof-integration-fix-store
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub unsafe fn submit_block_signatures_impl(
if header.header.height.value() != 1 && rp.app_hash != header.header.app_hash.as_bytes() {
error!("error verifying app hash!");
debug!("calculated app_hash bytes {:?}", rp.app_hash);
debug!("header app_hash bytes {:?}", header.app_hash.as_bytes());
// debug!("header app_hash bytes {:?}", header.app_hash.as_bytes());
return sgx_status_t::SGX_ERROR_INVALID_PARAMETER;
}
}
Expand Down
36 changes: 29 additions & 7 deletions integration-tests/contract-v0.10/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use cosmwasm_std::{
to_binary, Api, BalanceResponse, BankMsg, BankQuery, Binary, Coin, CosmosMsg, Empty, Env,
Extern, GovMsg, HandleResponse, HandleResult, HumanAddr, InitResponse, InitResult,
LogAttribute, Querier, QueryRequest, QueryResult, StakingMsg, Storage, VoteOption, WasmMsg,
};
use cosmwasm_std::{to_binary, Api, BalanceResponse, BankMsg, BankQuery, Binary, Coin, CosmosMsg, Empty, Env, Extern, GovMsg, HandleResponse, HandleResult, HumanAddr, InitResponse, InitResult, LogAttribute, Querier, QueryRequest, QueryResult, StakingMsg, Storage, VoteOption, WasmMsg, StdError};

/////////////////////////////// Messages ///////////////////////////////

Expand Down Expand Up @@ -56,21 +52,29 @@ pub enum Msg {
send: Vec<Coin>,
},
CustomMsg {},
Forward {
recipient_address: HumanAddr,
recipient_hash: String,
msg: Binary,
},
FailTx {}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
BankBalance { address: HumanAddr, denom: String },
Forward {},
}

/////////////////////////////// Init ///////////////////////////////

pub fn init<S: Storage, A: Api, Q: Querier>(
_deps: &mut Extern<S, A, Q>,
deps: &mut Extern<S, A, Q>,
_env: Env,
_msg: Msg,
) -> InitResult {
deps.storage.set("forwarded".as_bytes(), "no-fail".as_bytes());
return Ok(InitResponse {
messages: vec![],
log: vec![],
Expand All @@ -80,7 +84,7 @@ pub fn init<S: Storage, A: Api, Q: Querier>(
/////////////////////////////// Handle ///////////////////////////////

pub fn handle<S: Storage, A: Api, Q: Querier>(
_deps: &mut Extern<S, A, Q>,
deps: &mut Extern<S, A, Q>,
env: Env,
msg: Msg,
) -> HandleResult {
Expand Down Expand Up @@ -192,6 +196,22 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
log: vec![],
data: None,
}),
Msg::Forward { recipient_address, recipient_hash, msg } => {
deps.storage.set("forwarded".as_bytes(), "forwarded".as_bytes());
Ok(HandleResponse {
messages: vec![CosmosMsg::Wasm(
WasmMsg::Execute {
contract_addr: recipient_address,
callback_code_hash: recipient_hash,
msg,
send: vec![]
},
)],
log: vec![],
data: None,
})
}
Msg::FailTx {} => Err (StdError::generic_err("this should always fail")),
}
}

Expand All @@ -208,5 +228,7 @@ pub fn query<S: Storage, A: Api, Q: Querier>(deps: &Extern<S, A, Q>, msg: QueryM
}))?;
return Ok(to_binary(&res)?);
}
QueryMsg::Forward { } => Ok(to_binary(&deps.storage.get("forwarded".as_bytes()))?),
}

}
28 changes: 18 additions & 10 deletions integration-tests/contract-v1/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use crate::ibc::PACKET_LIFETIME;
use cosmwasm_std::{
entry_point, to_binary, to_vec, AllBalanceResponse, AllDelegationsResponse,
AllValidatorsResponse, BalanceResponse, BankMsg, BankQuery, Binary, BondedDenomResponse,
ChannelResponse, ContractInfoResponse, ContractResult, CosmosMsg, DelegationResponse, Deps,
DepsMut, DistributionMsg, Empty, Env, Event, GovMsg, IbcMsg, IbcQuery, IbcTimeout,
ListChannelsResponse, MessageInfo, PortIdResponse, QueryRequest, Response, StakingMsg,
StakingQuery, StdError, StdResult, ValidatorResponse, WasmMsg, WasmQuery,
};
use cosmwasm_std::{entry_point, to_binary, to_vec, AllBalanceResponse, AllDelegationsResponse, AllValidatorsResponse, BalanceResponse, BankMsg, BankQuery, Binary, BondedDenomResponse, ChannelResponse, ContractInfoResponse, ContractResult, CosmosMsg, DelegationResponse, Deps, DepsMut, DistributionMsg, Empty, Env, Event, GovMsg, IbcMsg, IbcQuery, IbcTimeout, ListChannelsResponse, MessageInfo, PortIdResponse, QueryRequest, Response, StakingMsg, StakingQuery, StdError, StdResult, ValidatorResponse, WasmMsg, WasmQuery, SubMsg};

use crate::msg::{Msg, PacketMsg, QueryMsg};
use crate::state::{
ack_store, ack_store_read, channel_store, channel_store_read, receive_store,
receive_store_read, timeout_store, timeout_store_read,
receive_store_read, timeout_store, timeout_store_read, forward_store, forward_store_read,
};

#[entry_point]
Expand All @@ -20,6 +13,7 @@ pub fn instantiate(deps: DepsMut, env: Env, info: MessageInfo, msg: Msg) -> StdR
ack_store(deps.storage).save(&"no ack yet".to_string())?;
receive_store(deps.storage).save(&"no receive yet".to_string())?;
timeout_store(deps.storage).save(&"no timeout yet".to_string())?;
forward_store(deps.storage).save(&"no-fail".to_string())?;

return handle_msg(deps, env, info, msg);
}
Expand All @@ -29,7 +23,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: Msg) -> StdResul
return handle_msg(deps, env, info, msg);
}

fn handle_msg(deps: DepsMut, env: Env, info: MessageInfo, msg: Msg) -> StdResult<Response> {
fn handle_msg(deps: DepsMut, env: Env, _info: MessageInfo, msg: Msg) -> StdResult<Response> {
match msg {
Msg::Nop {} => {
return Ok(Response::new().set_data(vec![137, 137].as_slice()));
Expand Down Expand Up @@ -181,6 +175,19 @@ fn handle_msg(deps: DepsMut, env: Env, info: MessageInfo, msg: Msg) -> StdResult
))
}
},
Msg::Forward { recipient_address, recipient_hash, msg } => {
forward_store(deps.storage).save(&"forwarded".to_string())?;
Ok(Response::new().add_submessage(SubMsg::new(CosmosMsg::Wasm(
WasmMsg::Execute {
contract_addr: recipient_address.into_string(),
code_hash: recipient_hash,
msg,
funds: vec![],
},
))
))
}
Msg::FailTx {} => Err (StdError::generic_err("this should always fail")),
// Msg::GetRandom {} => {
// return Ok(
// Response::new()
Expand Down Expand Up @@ -295,5 +302,6 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::LastIbcReceive {} => Ok(to_binary(&receive_store_read(deps.storage).load()?)?),
QueryMsg::LastIbcAck {} => Ok(to_binary(&ack_store_read(deps.storage).load()?)?),
QueryMsg::LastIbcTimeout {} => Ok(to_binary(&timeout_store_read(deps.storage).load()?)?),
QueryMsg::Forward {} => Ok(to_binary(&forward_store_read(deps.storage).load()?)?),
}
}
9 changes: 8 additions & 1 deletion integration-tests/contract-v1/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Binary, Coin, IbcTimeout, VoteOption};
use cosmwasm_std::{Addr, Binary, Coin, IbcTimeout, VoteOption};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -72,6 +72,12 @@ pub enum Msg {
funds: Vec<Coin>,
},
GetTxId {},
Forward {
recipient_address: Addr,
recipient_hash: String,
msg: Binary,
},
FailTx {}
//GetRandom {},
}

Expand Down Expand Up @@ -121,6 +127,7 @@ pub enum QueryMsg {
LastIbcReceive {},
LastIbcAck {},
LastIbcTimeout {},
Forward {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/contract-v1/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub const CHANNEL_KEY: &[u8] = b"channel";
pub const ACK_KEY: &[u8] = b"ack";
pub const RECEIVE_KEY: &[u8] = b"receive";
pub const TIMEOUT_KEY: &[u8] = b"timeout";
pub const FORWARD_KEY: &[u8] = b"forward";

pub fn channel_store(storage: &mut dyn Storage) -> Singleton<String> {
singleton(storage, CHANNEL_KEY)
Expand Down Expand Up @@ -37,3 +38,11 @@ pub fn timeout_store(storage: &mut dyn Storage) -> Singleton<String> {
pub fn timeout_store_read(storage: &dyn Storage) -> ReadonlySingleton<String> {
singleton_read(storage, TIMEOUT_KEY)
}

pub fn forward_store(storage: &mut dyn Storage) -> Singleton<String> {
singleton(storage, FORWARD_KEY)
}

pub fn forward_store_read(storage: &dyn Storage) -> ReadonlySingleton<String> {
singleton_read(storage, FORWARD_KEY)
}
81 changes: 80 additions & 1 deletion integration-tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ beforeAll(async () => {
};
}

// Send 100k SCRT from account 0 to each of accounts 1-itrations
// Send 100k SCRT from account 0 to each of accounts 1-iterations

const { secretjs } = accounts[0];

Expand Down Expand Up @@ -520,6 +520,83 @@ describe("CustomMsg", () => {
});
});

describe.only("Rollback", () => {
test("v0.10", async () => {
const tx = await accounts[0].secretjs.tx.compute.executeContract(
{
sender: accounts[0].address,
contract_address: contracts["secretdev-1"].v010.address,
code_hash: contracts["secretdev-1"].v010.codeHash,
msg: {
forward: {
msg: toBase64(toUtf8(JSON.stringify({ fail_tx: {} }))),
recipient_address: contracts["secretdev-1"].v1.address,
recipient_hash: contracts["secretdev-1"].v1.codeHash,
}
},
},
{ gasLimit: 250_000 }
);

if (tx.code !== 3) {
console.error(tx.rawLog);
}
expect(tx.code).toBe(3 /* WASM ErrExecuteFailed */);
expect(tx.rawLog).toContain("execute contract failed");

console.log("querying v10 contract state");
// verify the value rolled back
const result: any = await readonly.query.compute.queryContract({
contract_address: contracts["secretdev-1"].v010.address,
code_hash: contracts["secretdev-1"].v010.codeHash,
query: {
forward: {},
}
});

let resultString = String.fromCharCode(...result)
console.log("got result:", resultString);

expect(resultString).toBe("no-fail");
});

test("v1", async () => {
const tx = await accounts[0].secretjs.tx.compute.executeContract(
{
sender: accounts[0].address,
contract_address: contracts["secretdev-1"].v1.address,
code_hash: contracts["secretdev-1"].v1.codeHash,
msg: {
forward: {
msg: toBase64(toUtf8(JSON.stringify({ fail_tx: {} }))),
recipient_address: contracts["secretdev-1"].v010.address,
recipient_hash: contracts["secretdev-1"].v010.codeHash,
}
},
},
{ gasLimit: 250_000 }
);

if (tx.code !== 3) {
console.error(tx.rawLog);
}
expect(tx.code).toBe(3 /* WASM ErrExecuteFailed */);
expect(tx.rawLog).toContain("execute contract failed");

console.log("querying v1 contract state");
// verify the value rolled back
const result: any = await readonly.query.compute.queryContract({
contract_address: contracts["secretdev-1"].v1.address,
code_hash: contracts["secretdev-1"].v1.codeHash,
query: {
forward: {},
}
});

expect(result).toBe("no-fail");
});
});

describe("tx broadcast multi", () => {
test("Send Multiple Messages Amino", async () => {
const { validators } = await readonly.query.staking.validators({});
Expand Down Expand Up @@ -896,6 +973,7 @@ describe("Wasm", () => {

expect(tx.rawLog).toContain("execute contract failed");
});

});

describe("v0.10", () => {
Expand Down Expand Up @@ -957,6 +1035,7 @@ describe("Wasm", () => {

expect(tx.rawLog).toContain("execute contract failed");
});

});
});
});
Expand Down
2 changes: 1 addition & 1 deletion scripts/start-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CHAINID=${3:-secretdev-1}
rm -rf $SECRETD_HOME

# Build genesis file incl account for passed address
coins="10000000000uscrt,100000000000stake"
coins="1000000000000000000uscrt,100000000000stake"
$SECRETD init --chain-id $CHAINID $CHAINID --home $SECRETD_HOME
$SECRETD keys add validator --keyring-backend="test" --home $SECRETD_HOME
$SECRETD add-genesis-account $($SECRETD keys show validator -a --keyring-backend="test" --home $SECRETD_HOME) $coins --home $SECRETD_HOME
Expand Down
Loading