Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 374dc2f

Browse files
committed
Specify how much gas to allot for a create
1 parent 5b881c8 commit 374dc2f

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

substrate/runtime/contract/src/tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,20 @@ fn code_create(constructor: &[u8]) -> String {
343343
;; ext_create(
344344
;; code_ptr: u32,
345345
;; code_len: u32,
346+
;; gas: u64,
346347
;; value_ptr: u32,
347348
;; value_len: u32,
348349
;; input_data_ptr: u32,
349350
;; input_data_len: u32,
350351
;; ) -> u32
351-
(import "env" "ext_create" (func $ext_create (param i32 i32 i32 i32 i32 i32) (result i32)))
352+
(import "env" "ext_create" (func $ext_create (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
352353
(import "env" "memory" (memory 1 1))
353354
(func (export "call")
354355
(drop
355356
(call $ext_create
356357
(i32.const 12) ;; Pointer to `code`
357358
(i32.const {code_len}) ;; Length of `code`
359+
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
358360
(i32.const 4) ;; Pointer to the buffer with value to transfer
359361
(i32.const 8) ;; Length of the buffer with value to transfer
360362
(i32.const 0) ;; Pointer to input data buffer address
@@ -398,12 +400,12 @@ fn contract_create() {
398400
);
399401

400402
// 11 - value sent with the transaction
401-
// 2 * 138 - gas spent by the deployer contract (138) multiplied by gas price (2)
403+
// 2 * 139 - gas spent by the deployer contract (139) multiplied by gas price (2)
402404
// 2 * 135 - base gas fee for call (top level)
403405
// 2 * 175 - base gas fee for create (by contract)
404406
// ((21 / 2) * 2) - price per account creation
405407
let expected_gas_after_create =
406-
100_000_000 - 11 - (2 * 138) - (2 * 135) - (2 * 175) - ((21 / 2) * 2);
408+
100_000_000 - 11 - (2 * 139) - (2 * 135) - (2 * 175) - ((21 / 2) * 2);
407409
assert_eq!(Staking::free_balance(&0), expected_gas_after_create);
408410
assert_eq!(Staking::free_balance(&1), 8);
409411
assert_eq!(Staking::free_balance(&derived_address), 3);

substrate/runtime/contract/src/vm/env_def/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,11 @@ define_env!(init_env, <E: Ext>,
222222
}
223223
},
224224

225-
// ext_create(code_ptr: u32, code_len: u32, value_ptr: u32, value_len: u32, input_data_ptr: u32, input_data_len: u32) -> u32
225+
// ext_create(code_ptr: u32, code_len: u32, gas: u64, value_ptr: u32, value_len: u32, input_data_ptr: u32, input_data_len: u32) -> u32
226226
ext_create(
227227
ctx, code_ptr: u32,
228228
code_len: u32,
229+
gas: u64,
229230
value_ptr: u32,
230231
value_len: u32,
231232
input_data_ptr: u32,
@@ -245,8 +246,11 @@ define_env!(init_env, <E: Ext>,
245246
input_data.resize(input_data_len as usize, 0u8);
246247
ctx.memory().get(input_data_ptr, &mut input_data)?;
247248

248-
// TODO: Let user to choose how much gas to allocate for the execution.
249-
let nested_gas_limit = ctx.gas_meter.gas_left();
249+
let nested_gas_limit = if gas == 0 {
250+
ctx.gas_meter.gas_left()
251+
} else {
252+
<<<E as Ext>::T as Trait>::Gas as As<u64>>::sa(gas)
253+
};
250254
let ext = &mut ctx.ext;
251255
let create_outcome = ctx.gas_meter.with_nested(nested_gas_limit, |nested_meter| {
252256
match nested_meter {

substrate/runtime/contract/src/vm/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ mod tests {
276276
code: Vec<u8>,
277277
endowment: u64,
278278
data: Vec<u8>,
279+
gas_left: u64,
279280
}
280281
#[derive(Debug, PartialEq, Eq)]
281282
struct TransferEntry {
@@ -304,13 +305,14 @@ mod tests {
304305
&mut self,
305306
code: &[u8],
306307
endowment: u64,
307-
_gas_meter: &mut GasMeter<Test>,
308+
gas_meter: &mut GasMeter<Test>,
308309
data: &[u8],
309310
) -> Result<CreateReceipt<Test>, ()> {
310311
self.creates.push(CreateEntry {
311312
code: code.to_vec(),
312313
endowment,
313314
data: data.to_vec(),
315+
gas_left: gas_meter.gas_left(),
314316
});
315317
let address = self.next_account_id;
316318
self.next_account_id += 1;
@@ -405,18 +407,20 @@ mod tests {
405407
;; ext_create(
406408
;; code_ptr: u32,
407409
;; code_len: u32,
410+
;; gas: u64,
408411
;; value_ptr: u32,
409412
;; value_len: u32,
410413
;; input_data_ptr: u32,
411414
;; input_data_len: u32,
412415
;; ) -> u32
413-
(import "env" "ext_create" (func $ext_create (param i32 i32 i32 i32 i32 i32) (result i32)))
416+
(import "env" "ext_create" (func $ext_create (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
414417
(import "env" "memory" (memory 1 1))
415418
(func (export "call")
416419
(drop
417420
(call $ext_create
418421
(i32.const 12) ;; Pointer to `code`
419422
(i32.const 8) ;; Length of `code`
423+
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
420424
(i32.const 4) ;; Pointer to the buffer with value to transfer
421425
(i32.const 8) ;; Length of the buffer with value to transfer
422426
(i32.const 20) ;; Pointer to input data buffer address
@@ -454,6 +458,7 @@ mod tests {
454458
data: vec![
455459
1, 2, 3, 4,
456460
],
461+
gas_left: 49990,
457462
}]
458463
);
459464
}

0 commit comments

Comments
 (0)