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

Allow feeder calls to set the gas limit #345

Merged
merged 2 commits into from
Mar 19, 2024
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
6 changes: 6 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Change `Session::feeder_call` and `Session::feeder_call_raw` to allow for the
caller to specify the gas limit [#344]

## [0.17.0] - 2024-02-28

### Added
Expand Down Expand Up @@ -379,6 +384,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- ISSUES -->

[#344]: https://github.com/dusk-network/piecrust/issues/344
[#336]: https://github.com/dusk-network/piecrust/issues/336
[#325]: https://github.com/dusk-network/piecrust/issues/325
[#324]: https://github.com/dusk-network/piecrust/issues/324
Expand Down
11 changes: 6 additions & 5 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,14 @@ impl Session {
/// Feeder calls are used to have the contract be able to report larger
/// amounts of data to the host via the channel included in this call.
///
/// These calls are always performed with the maximum amount of gas, since
/// the contracts may spend quite a large amount in an effort to report
/// data.
/// These calls should be performed with a large amount of gas, since the
/// contracts may spend quite a large amount in an effort to report data.
pub fn feeder_call<A, R>(
&mut self,
contract: ContractId,
fn_name: &str,
fn_arg: &A,
gas_limit: u64,
feeder: mpsc::Sender<Vec<u8>>,
) -> Result<CallReceipt<R>, Error>
where
Expand All @@ -483,7 +483,7 @@ impl Session {
+ for<'b> CheckBytes<DefaultValidator<'b>>,
{
self.inner.feeder = Some(feeder);
let r = self.call(contract, fn_name, fn_arg, u64::MAX);
let r = self.call(contract, fn_name, fn_arg, gas_limit);
self.inner.feeder = None;
r
}
Expand All @@ -500,10 +500,11 @@ impl Session {
contract: ContractId,
fn_name: &str,
fn_arg: V,
gas_limit: u64,
feeder: mpsc::Sender<Vec<u8>>,
) -> Result<CallReceipt<Vec<u8>>, Error> {
self.inner.feeder = Some(feeder);
let r = self.call_raw(contract, fn_name, fn_arg, u64::MAX);
let r = self.call_raw(contract, fn_name, fn_arg, gas_limit);
self.inner.feeder = None;
r
}
Expand Down
30 changes: 29 additions & 1 deletion piecrust/tests/feeder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ fn feed() -> Result<(), Error> {
)?;

const FEED_NUM: u32 = 10;
const GAS_LIMIT: u64 = 1_000_000;

let (sender, receiver) = mpsc::channel();

session.feeder_call::<_, ()>(id, "feed_num", &FEED_NUM, sender)?;
session
.feeder_call::<_, ()>(id, "feed_num", &FEED_NUM, GAS_LIMIT, sender)?;

let numbers = receiver
.into_iter()
Expand Down Expand Up @@ -69,3 +71,29 @@ fn feed_errors_when_normal_call() -> Result<(), Error> {

Ok(())
}

#[test]
fn feed_out_of_gas() -> Result<(), Error> {
let vm = VM::ephemeral()?;

let mut session = vm.session(SessionData::builder())?;

let id = session.deploy(
contract_bytecode!("feeder"),
ContractData::builder().owner(OWNER),
LIMIT,
)?;

const FEED_NUM: u32 = 100;
const GAS_LIMIT: u64 = 1_000;

let (sender, _receiver) = mpsc::channel();

let err = session
.feeder_call::<_, ()>(id, "feed_num", &FEED_NUM, GAS_LIMIT, sender)
.expect_err("Call should error when out of gas");

assert!(matches!(err, Error::OutOfGas));

Ok(())
}