Skip to content

Commit

Permalink
Merge pull request #331 from reuvenpo/out-of-gas-handling
Browse files Browse the repository at this point in the history
Out of gas handling
  • Loading branch information
webmaster128 authored May 18, 2020
2 parents 1b7ecd9 + fe40d41 commit 701e10e
Show file tree
Hide file tree
Showing 19 changed files with 1,424 additions and 355 deletions.
13 changes: 7 additions & 6 deletions contracts/hackatom/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::{
coins, from_binary, log, AllBalanceResponse, Api, BankMsg, HandleResponse, HandleResult,
HumanAddr, InitResponse, InitResult, ReadonlyStorage, StdError,
coins, from_binary, log, AllBalanceResponse, BankMsg, HandleResponse, HandleResult, HumanAddr,
InitResponse, InitResult, StdError,
};
use cosmwasm_vm::from_slice;
use cosmwasm_vm::testing::{
handle, init, mock_instance, mock_instance_with_balances, query, test_io,
use cosmwasm_vm::{
from_slice,
mock::mock_env,
testing::{handle, init, mock_instance, mock_instance_with_balances, query, test_io},
Api, ReadonlyStorage,
};

use hackatom::contract::{HandleMsg, InitMsg, QueryMsg, State, CONFIG_KEY};
Expand Down
8 changes: 5 additions & 3 deletions contracts/queue/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
use cosmwasm_std::testing::{mock_env, MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{from_binary, from_slice, Env, HandleResponse, HumanAddr, InitResponse};
use cosmwasm_vm::testing::{handle, init, mock_instance, query};
use cosmwasm_vm::Instance;
use cosmwasm_vm::{
mock::{mock_env, MockApi, MockQuerier, MockStorage},
testing::{handle, init, mock_instance, query},
Instance,
};

use queue::contract::{
CountResponse, HandleMsg, InitMsg, Item, QueryMsg, ReducerResponse, SumResponse,
Expand Down
1 change: 1 addition & 0 deletions contracts/reflect/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Querier for CustomQuerier {
Err(e) => {
return Err(SystemError::InvalidRequest {
error: format!("Parsing QueryRequest: {}", e),
request: Binary(bin_request.to_vec()),
})
}
};
Expand Down
70 changes: 64 additions & 6 deletions contracts/reflect/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,81 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
use cosmwasm_std::testing::mock_env;
use cosmwasm_std::{
coin, coins, from_binary, Api, BankMsg, Binary, HandleResponse, HandleResult, HumanAddr,
coin, coins, from_binary, BankMsg, Binary, HandleResponse, HandleResult, HumanAddr,
InitResponse, StakingMsg, StdError,
};
use cosmwasm_vm::{
mock::mock_env,
testing::{handle, init, mock_instance, query},
Api, Instance,
};

use cosmwasm_vm::testing::{handle, init, mock_instance, query};
use cosmwasm_vm::Instance;

use mock::mock_dependencies;
use reflect::msg::{CustomMsg, CustomResponse, HandleMsg, InitMsg, OwnerResponse, QueryMsg};
use reflect::testing::mock_dependencies;

// This line will test the output of cargo wasm
static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/reflect.wasm");
// You can uncomment this line instead to test productionified build from cosmwasm-opt
// static WASM: &[u8] = include_bytes!("../contract.wasm");

mod mock {
use reflect::msg::{CustomQuery, CustomResponse};

use cosmwasm_std::{from_slice, to_binary, Binary, Coin, QueryRequest, StdResult};
use cosmwasm_vm::{
make_ffi_other,
mock::{MockApi, MockQuerier, MockStorage},
Extern, Querier, QuerierResult,
};

#[derive(Clone)]
pub struct CustomQuerier {
base: MockQuerier,
}

impl CustomQuerier {
pub fn new(base: MockQuerier) -> Self {
CustomQuerier { base }
}
}

impl Querier for CustomQuerier {
fn raw_query(&self, bin_request: &[u8]) -> QuerierResult {
// parse into our custom query class
let request: QueryRequest<CustomQuery> = match from_slice(bin_request) {
Ok(v) => v,
Err(e) => {
return Err(make_ffi_other(format!("Parsing QueryRequest: {}", e)));
}
};
if let QueryRequest::Custom(custom_query) = &request {
Ok(Ok(execute(&custom_query)))
} else {
self.base.handle_query(&request)
}
}
}

/// mock_dependencies is a drop-in replacement for cosmwasm_std::testing::mock_dependencies
/// this uses our CustomQuerier.
pub fn mock_dependencies(
canonical_length: usize,
contract_balance: &[Coin],
) -> Extern<MockStorage, MockApi, CustomQuerier> {
let base = cosmwasm_vm::mock::mock_dependencies(canonical_length, contract_balance);
base.change_querier(CustomQuerier::new)
}

fn execute(query: &CustomQuery) -> StdResult<Binary> {
let msg = match query {
CustomQuery::Ping {} => "pong".to_string(),
CustomQuery::Capital { text } => text.to_uppercase(),
};
to_binary(&CustomResponse { msg })
}
}

#[test]
fn proper_initialization() {
let mut deps = mock_instance(WASM, &[]);
Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
//! });
//! 4. Anywhere you see query(&deps, ...) you must replace it with query(&mut deps, ...)
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cosmwasm_std::{
coin, from_binary, Decimal, HumanAddr, InitResponse, StdError, StdResult, Uint128, Validator,
};
use cosmwasm_vm::mock::{mock_dependencies, mock_env};
use cosmwasm_vm::testing::{init, query};
use cosmwasm_vm::Instance;

Expand Down
24 changes: 19 additions & 5 deletions packages/std/src/errors/system_error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::HumanAddr;
use crate::{Binary, HumanAddr};

/// SystemError is used for errors inside the VM and is API frindly (i.e. serializable).
/// SystemError is used for errors inside the VM and is API friendly (i.e. serializable).
///
/// This is used on return values for Querier as a nested result: Result<StdResult<T>, SystemError>
/// The first wrap (SystemError) will trigger if the contract address doesn't exist,
Expand All @@ -16,7 +16,8 @@ use crate::HumanAddr;
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SystemError {
InvalidRequest { error: String },
InvalidRequest { error: String, request: Binary },
InvalidResponse { error: String, response: Binary },
NoSuchContract { addr: HumanAddr },
Unknown {},
UnsupportedRequest { kind: String },
Expand All @@ -27,10 +28,23 @@ impl std::error::Error for SystemError {}
impl std::fmt::Display for SystemError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SystemError::InvalidRequest { error } => write!(f, "Cannot parse request: {}", error),
SystemError::InvalidRequest { error, request } => write!(
f,
"Cannot parse request: {} in: {}",
error,
String::from_utf8_lossy(request.as_slice())
),
SystemError::InvalidResponse { error, response } => write!(
f,
"Cannot parse response: {} in: {}",
error,
String::from_utf8_lossy(response.as_slice())
),
SystemError::NoSuchContract { addr } => write!(f, "No such contract: {}", addr),
SystemError::Unknown {} => write!(f, "Unknown system error"),
SystemError::UnsupportedRequest { kind } => write!(f, "Unsupport query type: {}", kind),
SystemError::UnsupportedRequest { kind } => {
write!(f, "Unsupported query type: {}", kind)
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ pub use crate::init_handle::{
pub use crate::iterator::{Order, KV};
pub use crate::math::{Decimal, Uint128};
pub use crate::query::{
AllBalanceResponse, BalanceResponse, BankQuery, Delegation, DelegationsResponse, QueryRequest,
QueryResponse, QueryResult, StakingQuery, Validator, ValidatorsResponse, WasmQuery,
AllBalanceResponse, BalanceResponse, BankQuery, BondedDenomResponse, Delegation,
DelegationsResponse, QueryRequest, QueryResponse, QueryResult, StakingQuery, Validator,
ValidatorsResponse, WasmQuery,
};
pub use crate::serde::{from_binary, from_slice, to_binary, to_vec};
pub use crate::storage::MemoryStorage;
pub use crate::traits::{Api, Extern, Querier, QuerierResult, ReadonlyStorage, Storage};
pub use crate::types::{CanonicalAddr, Env, HumanAddr, Never};
pub use crate::types::{
BlockInfo, CanonicalAddr, ContractInfo, Env, HumanAddr, MessageInfo, Never,
};

// Exposed in wasm build only

Expand Down
1 change: 1 addition & 0 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl Querier for MockQuerier {
Err(e) => {
return Err(SystemError::InvalidRequest {
error: format!("Parsing QueryRequest: {}", e),
request: Binary(bin_request.to_vec()),
})
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/vm/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::iter::FromIterator;
use std::marker::PhantomData;
use std::path::PathBuf;

use cosmwasm_std::{Api, Extern, Querier, Storage};
use crate::traits::{Api, Extern, Querier, Storage};

use crate::backends::{backend, compile};
use crate::checksum::Checksum;
Expand Down Expand Up @@ -191,7 +191,7 @@ mod test {
use super::*;
use crate::calls::{call_handle, call_init};
use crate::errors::VmError;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage};
use crate::mock::{mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{coins, Never};
use std::fs::OpenOptions;
use std::io::Write;
Expand Down
5 changes: 3 additions & 2 deletions packages/vm/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use serde::de::DeserializeOwned;
use std::fmt;

use cosmwasm_std::{
Api, Env, HandleResponse, HandleResult, InitResponse, InitResult, Querier, QueryResponse,
QueryResult, StdResult, Storage,
Env, HandleResponse, HandleResult, InitResponse, InitResult, QueryResponse, QueryResult,
StdResult,
};

use crate::errors::{make_generic_err, VmResult};
use crate::instance::{Func, Instance};
use crate::serde::{from_slice, to_vec};
use crate::traits::{Api, Querier, Storage};
use schemars::JsonSchema;

static MAX_LENGTH_INIT: usize = 100_000;
Expand Down
Loading

0 comments on commit 701e10e

Please sign in to comment.