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

Add transaction index to Env #1077

Merged
merged 2 commits into from
Sep 10, 2021
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to

- cosmwasm-std: Add new `WasmQuery::ContractInfo` variant to get metadata about
the contract, like `code_id` and `admin`.
- cosmwasm-std: New field `Env::transaction` containing info of the transaction
the contract call was executed in.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub use crate::serde::{from_binary, from_slice, to_binary, to_vec};
pub use crate::storage::MemoryStorage;
pub use crate::timestamp::Timestamp;
pub use crate::traits::{Api, Querier, QuerierResult, QuerierWrapper, Storage};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
pub use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};

// Exposed in wasm build only

Expand Down
3 changes: 2 additions & 1 deletion packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::serde::{from_slice, to_binary};
use crate::storage::MemoryStorage;
use crate::timestamp::Timestamp;
use crate::traits::{Api, Querier, QuerierResult};
use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo};
use crate::types::{BlockInfo, ContractInfo, Env, MessageInfo, TransactionInfo};
use crate::Attribute;

pub const MOCK_CONTRACT_ADDR: &str = "cosmos2contract";
Expand Down Expand Up @@ -209,6 +209,7 @@ pub fn mock_env() -> Env {
time: Timestamp::from_nanos(1_571_797_419_879_305_533),
chain_id: "cosmos-testnet-14002".to_string(),
},
transaction: Some(TransactionInfo { index: 3 }),
contract: ContractInfo {
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
},
Expand Down
21 changes: 19 additions & 2 deletions packages/std/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ use crate::timestamp::Timestamp;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Env {
pub block: BlockInfo,
/// Information on the transaction this message was executed in.
/// The field is unset when the `MsgExecuteContract`/`MsgInstantiateContract`/`MsgMigrateContract`
/// is not executed as part of a transaction.
pub transaction: Option<TransactionInfo>,
pub contract: ContractInfo,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TransactionInfo {
/// The position of this transaction in the block. The first
/// transaction has index 0.
///
/// This allows you to get a unique transaction indentifier in this chain
/// using the pair (`env.block.height`, `env.transaction.index`).
///
pub index: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct BlockInfo {
/// The height of a block is the number of blocks preceding it in the blockchain.
Expand All @@ -24,13 +39,14 @@ pub struct BlockInfo {
/// Using chrono:
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp};
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
/// # let env = Env {
/// # block: BlockInfo {
/// # height: 12_345,
/// # time: Timestamp::from_nanos(1_571_797_419_879_305_533),
/// # chain_id: "cosmos-testnet-14002".to_string(),
/// # },
/// # transaction: Some(TransactionInfo { index: 3 }),
/// # contract: ContractInfo {
/// # address: Addr::unchecked("contract"),
/// # },
Expand All @@ -45,13 +61,14 @@ pub struct BlockInfo {
/// Creating a simple millisecond-precision timestamp (as used in JavaScript):
///
/// ```
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp};
/// # use cosmwasm_std::{Addr, BlockInfo, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo};
/// # let env = Env {
/// # block: BlockInfo {
/// # height: 12_345,
/// # time: Timestamp::from_nanos(1_571_797_419_879_305_533),
/// # chain_id: "cosmos-testnet-14002".to_string(),
/// # },
/// # transaction: Some(TransactionInfo { index: 3 }),
/// # contract: ContractInfo {
/// # address: Addr::unchecked("contract"),
/// # },
Expand Down
8 changes: 4 additions & 4 deletions packages/vm/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ mod tests {

let report2 = instance.create_gas_report();
assert_eq!(report2.used_externally, 73);
assert_eq!(report2.used_internally, 36837);
assert_eq!(report2.used_internally, 39202);
assert_eq!(report2.limit, LIMIT);
assert_eq!(
report2.remaining,
Expand Down Expand Up @@ -890,7 +890,7 @@ mod singlepass_tests {
.unwrap();

let init_used = orig_gas - instance.get_gas_left();
assert_eq!(init_used, 36910);
assert_eq!(init_used, 39275);
}

#[test]
Expand All @@ -913,7 +913,7 @@ mod singlepass_tests {
.unwrap();

let execute_used = gas_before_execute - instance.get_gas_left();
assert_eq!(execute_used, 159891);
assert_eq!(execute_used, 162233);
}

#[test]
Expand Down Expand Up @@ -947,6 +947,6 @@ mod singlepass_tests {
assert_eq!(answer.as_slice(), b"{\"verifier\":\"verifies\"}");

let query_used = gas_before_query - instance.get_gas_left();
assert_eq!(query_used, 28274);
assert_eq!(query_used, 30646);
}
}
5 changes: 4 additions & 1 deletion packages/vm/src/testing/mock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cosmwasm_std::testing::{digit_sum, riffle_shuffle};
use cosmwasm_std::{Addr, BlockInfo, Coin, ContractInfo, Env, MessageInfo, Timestamp};
use cosmwasm_std::{
Addr, BlockInfo, Coin, ContractInfo, Env, MessageInfo, Timestamp, TransactionInfo,
};

use super::querier::MockQuerier;
use super::storage::MockStorage;
Expand Down Expand Up @@ -160,6 +162,7 @@ pub fn mock_env() -> Env {
time: Timestamp::from_nanos(1_571_797_419_879_305_533),
chain_id: "cosmos-testnet-14002".to_string(),
},
transaction: Some(TransactionInfo { index: 3 }),
contract: ContractInfo {
address: Addr::unchecked(MOCK_CONTRACT_ADDR),
},
Expand Down
Binary file modified packages/vm/testdata/floaty_1.0.wasm
Binary file not shown.
Binary file modified packages/vm/testdata/hackatom_1.0.wasm
Binary file not shown.
Binary file modified packages/vm/testdata/ibc_reflect_1.0.wasm
Binary file not shown.