1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::collections::HashMap;

use crate::wasm_emulation::channel::RemoteChannel;
use crate::wasm_emulation::query::bank::BankQuerier;
use crate::wasm_emulation::query::staking::StakingQuerier;
use crate::wasm_emulation::query::wasm::WasmQuerier;

use cosmwasm_std::CustomMsg;
use cosmwasm_std::Env;
use cosmwasm_vm::BackendResult;
use cosmwasm_vm::GasInfo;

use serde::de::DeserializeOwned;

use cosmwasm_std::Binary;
use cosmwasm_std::Coin;

use cosmwasm_std::SystemError;

use cosmwasm_std::from_json;
use cosmwasm_std::{ContractResult, SystemResult};
use cosmwasm_std::{CustomQuery, QueryRequest};
use cosmwasm_std::{FullDelegation, Validator};

use cosmwasm_std::Attribute;
use cosmwasm_std::QuerierResult;

use crate::wasm_emulation::input::QuerierStorage;
use crate::Contract;

use super::gas::GAS_COST_QUERY_ERROR;

#[derive(Clone)]
pub struct LocalForkedState<ExecC, QueryC> {
    pub contracts: HashMap<usize, *mut dyn Contract<ExecC, QueryC>>,
    pub env: Env,
}

#[derive(Clone)]
pub struct ForkState<ExecC, QueryC>
where
    QueryC: CustomQuery + DeserializeOwned + 'static,
    ExecC: CustomMsg + 'static,
{
    pub remote: RemoteChannel,
    /// Only query function right now, but we might pass along the whole application state to avoid stargate queries
    pub local_state: LocalForkedState<ExecC, QueryC>,
    pub querier_storage: QuerierStorage,
}

pub type QueryResultWithGas = (QuerierResult, GasInfo);

/// The same type as cosmwasm-std's QuerierResult, but easier to reuse in
/// cosmwasm-vm. It might diverge from QuerierResult at some point.
pub type MockQuerierCustomHandlerResult = SystemResult<ContractResult<Binary>>;

/// MockQuerier holds an immutable table of bank balances
/// and configurable handlers for Wasm queries and custom queries.
pub struct MockQuerier<
    ExecC: CustomMsg + DeserializeOwned + 'static,
    QueryC: CustomQuery + DeserializeOwned + 'static,
> {
    bank: BankQuerier,

    staking: StakingQuerier,
    wasm: WasmQuerier<ExecC, QueryC>,

    //Box<dyn Fn(Deps<'_, C>, Env, Vec<u8>) -> Result<Binary, anyhow::Error>>, //fn(deps: Deps<C>, env: Env, msg: Vec<u8>) -> Result<Binary, anyhow::Error>,
    /// A handler to handle custom queries. This is set to a dummy handler that
    /// always errors by default. Update it via `with_custom_handler`.
    ///
    /// Use box to avoid the need of another generic type
    custom_handler: Box<dyn for<'a> Fn(&'a QueryC) -> QueryResultWithGas>,
    remote: RemoteChannel,
}

impl<
        ExecC: CustomMsg + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    > MockQuerier<ExecC, QueryC>
{
    pub fn new(fork_state: ForkState<ExecC, QueryC>) -> Self {
        // We create query_closures for all local_codes

        MockQuerier {
            bank: BankQuerier::new(
                fork_state.remote.clone(),
                fork_state.querier_storage.bank.storage.clone(),
            ),

            staking: StakingQuerier::default(),
            wasm: WasmQuerier::new(fork_state.clone()),
            // strange argument notation suggested as a workaround here: https://github.com/rust-lang/rust/issues/41078#issuecomment-294296365
            custom_handler: Box::from(|_: &_| -> QueryResultWithGas {
                (
                    SystemResult::Err(SystemError::UnsupportedRequest {
                        kind: "custom".to_string(),
                    }),
                    GasInfo::free(),
                )
            }),
            remote: fork_state.remote,
        }
    }

    // set a new balance for the given address and return the old balance
    pub fn update_balance(
        &mut self,
        addr: impl Into<String>,
        balance: Vec<Coin>,
    ) -> Option<Vec<Coin>> {
        self.bank.update_balance(addr, balance)
    }

    pub fn update_staking(
        &mut self,
        denom: &str,
        validators: &[Validator],
        delegations: &[FullDelegation],
    ) {
        self.staking = StakingQuerier::new(denom, validators, delegations);
    }

    pub fn with_custom_handler<CH>(mut self, handler: CH) -> Self
    where
        CH: Fn(&QueryC) -> QueryResultWithGas + 'static,
    {
        self.custom_handler = Box::from(handler);
        self
    }
}

impl<
        ExecC: CustomMsg + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    > cosmwasm_vm::Querier for MockQuerier<ExecC, QueryC>
{
    fn query_raw(
        &self,
        bin_request: &[u8],
        _gas_limit: u64,
    ) -> BackendResult<SystemResult<ContractResult<Binary>>> {
        let request: QueryRequest<QueryC> = match from_json(bin_request) {
            Ok(v) => v,
            Err(e) => {
                return (
                    Ok(SystemResult::Err(SystemError::InvalidRequest {
                        error: format!("Parsing query request: {}", e),
                        request: bin_request.into(),
                    })),
                    GasInfo::with_externally_used(GAS_COST_QUERY_ERROR),
                )
            }
        };
        let result = self.handle_query(&request);

        (Ok(result.0), result.1)
    }
}

impl<
        ExecC: CustomMsg + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    > cosmwasm_std::Querier for MockQuerier<ExecC, QueryC>
{
    fn raw_query(&self, bin_request: &[u8]) -> SystemResult<ContractResult<Binary>> {
        let request: QueryRequest<QueryC> = match from_json(bin_request) {
            Ok(v) => v,
            Err(e) => {
                return SystemResult::Err(SystemError::InvalidRequest {
                    error: format!("Parsing query request: {}", e),
                    request: bin_request.into(),
                })
            }
        };
        let result = self.handle_query(&request);

        result.0
    }
}

impl<
        ExecC: CustomMsg + DeserializeOwned + 'static,
        QueryC: CustomQuery + DeserializeOwned + 'static,
    > MockQuerier<ExecC, QueryC>
{
    pub fn handle_query(&self, request: &QueryRequest<QueryC>) -> QueryResultWithGas {
        match &request {
            QueryRequest::Bank(bank_query) => self.bank.query(bank_query),
            QueryRequest::Custom(custom_query) => (*self.custom_handler)(custom_query),

            QueryRequest::Staking(staking_query) => self.staking.query(staking_query),
            QueryRequest::Wasm(msg) => self.wasm.query(self.remote.clone(), msg),
            #[allow(deprecated)]
            QueryRequest::Stargate { .. } => (
                SystemResult::Err(SystemError::UnsupportedRequest {
                    kind: "Stargate".to_string(),
                }),
                GasInfo::with_externally_used(GAS_COST_QUERY_ERROR),
            ),
            QueryRequest::Grpc(_req) => (
                SystemResult::Err(SystemError::UnsupportedRequest {
                    kind: "Stargate".to_string(),
                }),
                GasInfo::with_externally_used(GAS_COST_QUERY_ERROR),
            ),
            &_ => panic!("Query Type Not implemented"),
        }
    }
}

pub fn digit_sum(input: &[u8]) -> usize {
    input.iter().fold(0, |sum, val| sum + (*val as usize))
}

/// Only for test code. This bypasses assertions in new, allowing us to create _*
/// Attributes to simulate responses from the blockchain
pub fn mock_wasmd_attr(key: impl Into<String>, value: impl Into<String>) -> Attribute {
    Attribute {
        key: key.into(),
        value: value.into(),
    }
}