Skip to content

Commit 77a73ba

Browse files
Ignore gas on mock-function-call (#1585)
1 parent 1bbab7b commit 77a73ba

File tree

3 files changed

+88
-55
lines changed

3 files changed

+88
-55
lines changed

x/contracts/examples/counter-external/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ mod tests {
4141

4242
// mock `get_value` external contract call to return `value`
4343
let value = 5_u64;
44-
ctx.mock_function_call(external, "get_value", of, 1_000_000, 0, value);
44+
ctx.mock_function_call(external, "get_value", of, 0, value);
4545

4646
let value = get_value(&mut ctx, external, of);
4747
assert_eq!(value, 5);

x/contracts/wasmlanche/src/context.rs

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
extern crate alloc;
55

66
use crate::{
7-
host::Accessor,
7+
host::{Accessor, CallContractArgs},
88
state::{Cache, Error, IntoPairs, Schema},
99
types::{Address, ContractId},
1010
Gas, Id,
@@ -238,10 +238,15 @@ impl Context {
238238
max_units: Gas,
239239
value: u64,
240240
) -> Result<T, ExternalCallError> {
241-
let args = CallContractArgs::new(&address, function_name, args, max_units, value);
242-
let args_bytes = borsh::to_vec(&args).expect("failed to serialize args");
243241
self.state_cache.flush();
244-
let bytes = self.host_accessor.call_contract(&args_bytes);
242+
243+
let bytes = self.host_accessor.call_contract(&CallContractArgs {
244+
address,
245+
function_name,
246+
args,
247+
max_units,
248+
value,
249+
});
245250

246251
borsh::from_slice(&bytes).expect("failed to deserialize")
247252
}
@@ -276,22 +281,27 @@ impl Context {
276281
/// Panics if serialization fails.
277282
pub fn mock_function_call<T, U>(
278283
&self,
279-
target: Address,
280-
function: &str,
284+
address: Address,
285+
function_name: &str,
281286
args: T,
282-
// TODO: remove this
283-
max_units: Gas,
284-
max_value: u64,
287+
value: u64,
285288
result: U,
286289
) where
287290
T: BorshSerialize,
288291
U: BorshSerialize,
289292
{
290293
use crate::host::CALL_FUNCTION_PREFIX;
291294

292-
let args = borsh::to_vec(&args).expect("error serializing result");
295+
let args = &borsh::to_vec(&args).expect("error serializing result");
296+
297+
let contract_args = CallContractArgs {
298+
address,
299+
function_name,
300+
args,
301+
value,
302+
max_units: 0,
303+
};
293304

294-
let contract_args = CallContractArgs::new(&target, function, &args, max_units, max_value);
295305
let contract_args = borsh::to_vec(&(CALL_FUNCTION_PREFIX, contract_args))
296306
.expect("error serializing result");
297307

@@ -396,30 +406,3 @@ impl Default for Context {
396406
Self::new()
397407
}
398408
}
399-
400-
#[derive(BorshSerialize)]
401-
pub struct CallContractArgs<'a> {
402-
target: &'a Address,
403-
function: &'a [u8],
404-
args: &'a [u8],
405-
max_units: Gas,
406-
max_value: u64,
407-
}
408-
409-
impl<'a> CallContractArgs<'a> {
410-
pub fn new(
411-
target: &'a Address,
412-
function: &'a str,
413-
args: &'a [u8],
414-
max_units: Gas,
415-
max_value: u64,
416-
) -> Self {
417-
Self {
418-
target,
419-
function: function.as_bytes(),
420-
args,
421-
max_units,
422-
max_value,
423-
}
424-
}
425-
}

x/contracts/wasmlanche/src/host.rs

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
use crate::{
5+
borsh::{self, BorshSerialize},
6+
Address, Gas,
7+
};
8+
use cfg_if::cfg_if;
9+
10+
cfg_if! {
11+
if #[cfg(feature = "test")] {
12+
pub use test_wrappers::*;
13+
} else {
14+
pub use external_wrappers::*;
15+
}
16+
}
17+
418
pub struct StateAccessor;
519

20+
pub(crate) struct CallContractArgs<'a> {
21+
pub(crate) address: Address,
22+
pub(crate) function_name: &'a str,
23+
pub(crate) args: &'a [u8],
24+
pub(crate) max_units: Gas,
25+
pub(crate) value: u64,
26+
}
27+
28+
impl BorshSerialize for CallContractArgs<'_> {
29+
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
30+
let Self {
31+
address,
32+
function_name,
33+
args,
34+
max_units,
35+
value,
36+
} = self;
37+
38+
address.serialize(writer)?;
39+
function_name.serialize(writer)?;
40+
args.serialize(writer)?;
41+
42+
cfg_if! {
43+
if #[cfg(feature = "test")] {
44+
let _ = max_units;
45+
} else {
46+
max_units.serialize(writer)?;
47+
}
48+
}
49+
50+
value.serialize(writer)?;
51+
52+
Ok(())
53+
}
54+
}
55+
656
#[cfg(feature = "test")]
757
mod test_wrappers {
8-
use crate::host::StateAccessor;
9-
use crate::{Address, Gas, HostPtr};
58+
use super::CallContractArgs;
59+
use crate::{host::StateAccessor, Address, Gas, HostPtr};
1060
use core::cell::{Cell, RefCell};
1161

1262
pub const BALANCE_PREFIX: u8 = 0;
@@ -29,7 +79,7 @@ mod test_wrappers {
2979
#[derive(Clone)]
3080
#[cfg_attr(feature = "debug", derive(Debug))]
3181
pub struct Accessor {
32-
pub state: MockState,
82+
state: MockState,
3383
}
3484

3585
impl Default for Accessor {
@@ -70,12 +120,15 @@ mod test_wrappers {
70120
val
71121
}
72122

73-
pub fn call_contract(&self, args: &[u8]) -> HostPtr {
74-
let key = [CALL_FUNCTION_PREFIX]
75-
.iter()
76-
.chain(args.iter())
77-
.copied()
78-
.collect::<Vec<u8>>();
123+
pub fn call_contract(&self, args: &CallContractArgs) -> HostPtr {
124+
let key = {
125+
// same default as borsh::to_vec uses
126+
let mut key = Vec::with_capacity(1024);
127+
key.push(CALL_FUNCTION_PREFIX);
128+
borsh::to_writer(&mut key, args).expect("failed to serialize call-contract args");
129+
key
130+
};
131+
79132
let val = self.state.get(&key);
80133

81134
assert!(
@@ -199,6 +252,7 @@ mod test_wrappers {
199252

200253
#[cfg(not(feature = "test"))]
201254
mod external_wrappers {
255+
use super::CallContractArgs;
202256
use crate::host::StateAccessor;
203257
use crate::HostPtr;
204258

@@ -248,13 +302,15 @@ mod external_wrappers {
248302
unsafe { deploy(args.as_ptr(), args.len()) }
249303
}
250304

251-
pub fn call_contract(&self, args: &[u8]) -> HostPtr {
305+
pub fn call_contract(&self, args: &CallContractArgs) -> HostPtr {
252306
#[link(wasm_import_module = "contract")]
253307
extern "C" {
254308
#[link_name = "call_contract"]
255309
fn call_contract(ptr: *const u8, len: usize) -> HostPtr;
256310
}
257311

312+
let args = borsh::to_vec(args).expect("failed to serialize args");
313+
258314
unsafe { call_contract(args.as_ptr(), args.len()) }
259315
}
260316

@@ -289,9 +345,3 @@ mod external_wrappers {
289345
}
290346
}
291347
}
292-
293-
#[cfg(feature = "test")]
294-
pub use test_wrappers::*;
295-
296-
#[cfg(not(feature = "test"))]
297-
pub use external_wrappers::*;

0 commit comments

Comments
 (0)