From 45ef88c1772f3f5aa14f085425f0c92ae7753340 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 8 Sep 2021 15:14:33 +0200 Subject: [PATCH] Add transaction index to Env --- CHANGELOG.md | 5 +++++ packages/std/src/lib.rs | 2 +- packages/std/src/mock.rs | 3 ++- packages/std/src/types.rs | 21 +++++++++++++++++++-- packages/vm/src/testing/mock.rs | 5 ++++- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c60c401026..9279baab87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to ## [Unreleased] +### Added + +- cosmwasm-std: New field `Env::transaction` containing info of the transaction + the contract call was executed in. + ### Changed - cosmwasm-std: Make `iterator` a required feature if the `iterator` feature diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index c5be163e14..f3a2b5b2c3 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -63,7 +63,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 diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index a43e1957ed..3a2a8e0da4 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -27,7 +27,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"; @@ -206,6 +206,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), }, diff --git a/packages/std/src/types.rs b/packages/std/src/types.rs index 9b182ec80a..790ed62c96 100644 --- a/packages/std/src/types.rs +++ b/packages/std/src/types.rs @@ -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, 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. @@ -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"), /// # }, @@ -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"), /// # }, diff --git a/packages/vm/src/testing/mock.rs b/packages/vm/src/testing/mock.rs index e71094df7c..08ad0bc6ad 100644 --- a/packages/vm/src/testing/mock.rs +++ b/packages/vm/src/testing/mock.rs @@ -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; @@ -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), },