Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8321883
Transition getter functions to not use scratch buffer
athei Jun 24, 2020
92fe839
Remove scratch buffer from ext_get_storage
athei Jun 26, 2020
e1c0e26
Remove scratch buffer from ext_call
athei Jun 29, 2020
6f94941
Remove scratch buffer from ext_instantiate
athei Jun 29, 2020
0b27908
Add ext_input and remove scratch buffer
athei Jun 29, 2020
46191f5
Remove the no longer used `Dispatched` event
athei Jul 1, 2020
fa6bf8c
Rework error handling (changes RPC exposed data)
athei Jun 30, 2020
c86c2ad
Updated inline documentation
athei Jul 2, 2020
ddf69ab
Prevent skipping of copying the output for getter API
athei Jul 3, 2020
868668c
Return gas_consumed from the RPC contracts call interface
athei Jul 3, 2020
84973b0
Updated COMPLEXTITY.md
athei Jul 3, 2020
d205f60
Rename ext_gas_price to ext_weight_to_fee
athei Jul 3, 2020
e096945
Align comments with spaces
athei Jul 7, 2020
6b0c1f0
Removed no longer used `ExecError`
athei Jul 7, 2020
8e5170c
Remove possible panic in `from_typed_value`
athei Jul 7, 2020
08edc84
Use a struct as associated data for SpecialTrap::Return
athei Jul 7, 2020
792c942
Fix nits in COMPLEXITY.md
athei Jul 7, 2020
41f1d55
Renamed SpecialTrap to TrapReason
athei Jul 7, 2020
3977032
Merge remote-tracking branch 'origin/master' into at-remove-scratch-b…
athei Jul 7, 2020
8373842
Fix test
athei Jul 7, 2020
cf72226
Merge branch 'master' into at-remove-scratch-buffer
athei Jul 9, 2020
9021d20
Finish renaming special_trap -> trap_reason
athei Jul 9, 2020
9ef80e1
Remove no longer used get_runtime_storage
athei Jul 9, 2020
fe110ed
fixup! Remove no longer used get_runtime_storage
athei Jul 9, 2020
9deebd3
Removed tabs for comment aligment
athei Jul 9, 2020
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
Prev Previous commit
Next Next commit
Remove scratch buffer from ext_call
  • Loading branch information
athei committed Jul 3, 2020
commit e1c0e26940fc3808174bb14c8bd22fa5243017f0
69 changes: 39 additions & 30 deletions frame/contracts/fixtures/caller_contract.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_balance" (func $ext_balance (param i32 i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_println" (func $ext_println (param i32 i32)))
(import "env" "memory" (memory 1 1))
Expand All @@ -17,12 +17,16 @@
)

(func $current_balance (param $sp i32) (result i64)
(i32.store
(i32.sub (get_local $sp) (i32.const 16))
(i32.const 8)
)
(call $ext_balance
(i32.sub (get_local $sp) (i32.const 8))
(i32.const 2048)
(i32.sub (get_local $sp) (i32.const 16))
)
(call $assert
(i32.eq (i32.load (i32.const 2048)) (i32.const 8))
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 16))) (i32.const 8))
)
(i64.load (i32.sub (get_local $sp) (i32.const 8)))
)
Expand Down Expand Up @@ -149,6 +153,18 @@
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
)

;; Zero out destination buffer of output
(i32.store
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
)

;; Length of the output buffer
(i32.store
(i32.sub (get_local $sp) (i32.const 8))
(i32.const 4)
)

;; Call the new contract and expect it to return failing exit code.
(set_local $exit_code
(call $ext_call
Expand All @@ -159,6 +175,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 9) ;; Pointer to input data buffer address
(i32.const 7) ;; Length of input data buffer
(i32.sub (get_local $sp) (i32.const 4)) ;; Ptr to output buffer
(i32.sub (get_local $sp) (i32.const 8)) ;; Ptr to output buffer len
)
)

Expand All @@ -169,16 +187,7 @@

;; Check that scratch buffer contains the expected return data.
(call $assert
(i32.eq (call $ext_scratch_size) (i32.const 3))
)
(i32.store
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
)
(call $ext_scratch_read
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
(i32.const 3)
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 8))) (i32.const 3))
)
(call $assert
(i32.eq
Expand All @@ -202,6 +211,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 8) ;; Pointer to input data buffer address
(i32.const 8) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this cas
)
)

Expand All @@ -210,16 +221,23 @@
(i32.eq (get_local $exit_code) (i32.const 0x0100))
)

;; Check that scratch buffer is empty since call trapped.
(call $assert
(i32.eq (call $ext_scratch_size) (i32.const 0))
)

;; Check that balance has not changed.
(call $assert
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
)

;; Zero out destination buffer of output
(i32.store
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
)

;; Length of the output buffer
(i32.store
(i32.sub (get_local $sp) (i32.const 8))
(i32.const 4)
)

;; Call the contract successfully.
(set_local $exit_code
(call $ext_call
Expand All @@ -230,6 +248,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 8) ;; Pointer to input data buffer address
(i32.const 8) ;; Length of input data buffer
(i32.sub (get_local $sp) (i32.const 4)) ;; Ptr to output buffer
(i32.sub (get_local $sp) (i32.const 8)) ;; Ptr to output buffer len
)
)

Expand All @@ -240,16 +260,7 @@

;; Check that scratch buffer contains the expected return data.
(call $assert
(i32.eq (call $ext_scratch_size) (i32.const 4))
)
(i32.store
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
)
(call $ext_scratch_read
(i32.sub (get_local $sp) (i32.const 4))
(i32.const 0)
(i32.const 4)
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 8))) (i32.const 4))
)
(call $assert
(i32.eq
Expand All @@ -270,6 +281,4 @@
(data (i32.const 0) "\00\80") ;; The value to transfer on instantiation and calls.
;; Chosen to be greater than existential deposit.
(data (i32.const 8) "\00\11\22\33\44\55\66\77") ;; The input data to instantiations and calls.

(data (i32.const 2048) "\08") ;; size of our $ext_balance output buffer
)
9 changes: 8 additions & 1 deletion frame/contracts/fixtures/destroy_and_transfer.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_get_storage" (func $ext_get_storage (param i32 i32 i32) (result i32)))
(import "env" "ext_set_storage" (func $ext_set_storage (param i32 i32 i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))

Expand Down Expand Up @@ -113,6 +113,9 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 1) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case

)
(i32.const 0x0100)
)
Expand All @@ -129,6 +132,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
(i32.const 0)
)
Expand All @@ -147,6 +152,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 1) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
(i32.const 0)
)
Expand Down
6 changes: 3 additions & 3 deletions frame/contracts/fixtures/drain.wat
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(module
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_balance" (func $ext_balance (param i32 i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))

;; [0, 8) reserved for $ext_balance output
Expand Down Expand Up @@ -46,6 +44,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
(i32.const 0)
)
Expand Down
5 changes: 3 additions & 2 deletions frame/contracts/fixtures/self_destruct.wat
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(module
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_address" (func $ext_address (param i32 i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "ext_terminate" (func $ext_terminate (param i32 i32)))
(import "env" "memory" (memory 1 1))

Expand Down Expand Up @@ -55,6 +54,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
(i32.const 0)
)
Expand Down
6 changes: 3 additions & 3 deletions frame/contracts/fixtures/self_destructing_constructor.wat
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
(module
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_balance" (func $ext_balance (param i32 i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))

;; [0, 8) reserved for $ext_balance output
Expand Down Expand Up @@ -44,6 +42,8 @@
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 0) ;; Pointer to input data buffer address
(i32.const 0) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
(i32.const 0)
)
Expand Down
18 changes: 13 additions & 5 deletions frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,11 @@ mod tests {
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32
;; input_data_len: u32,
;; result_ptr: u32,
;; result_len_ptr: u32
;;) -> u32
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
Expand All @@ -579,6 +581,8 @@ mod tests {
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 20) ;; Pointer to input data buffer address
(i32.const 4) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
)
)
Expand Down Expand Up @@ -611,7 +615,7 @@ mod tests {
to: 9,
value: 6,
data: vec![1, 2, 3, 4],
gas_left: 9985500000,
gas_left: 9984500000,
}]
);
}
Expand Down Expand Up @@ -728,9 +732,11 @@ mod tests {
;; value_ptr: u32,
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32
;; input_data_len: u32,
;; result_ptr: u32,
;; result_len_ptr: u32
;;) -> u32
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
Expand All @@ -742,6 +748,8 @@ mod tests {
(i32.const 8) ;; Length of the buffer with value to transfer.
(i32.const 20) ;; Pointer to input data buffer address
(i32.const 4) ;; Length of input data buffer
(i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this cas
)
)
)
Expand Down
18 changes: 7 additions & 11 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,14 @@ define_env!(Env, <E: Ext>,
value_ptr: u32,
value_len: u32,
input_data_ptr: u32,
input_data_len: u32
input_data_len: u32,
result_ptr: u32,
result_len_ptr: u32
) -> u32 => {
let callee: <<E as Ext>::T as frame_system::Trait>::AccountId =
read_sandbox_memory_as(ctx, callee_ptr, callee_len)?;
let value: BalanceOf<<E as Ext>::T> =
read_sandbox_memory_as(ctx, value_ptr, value_len)?;

// Read input data into the scratch buffer, then take ownership of it.
read_sandbox_memory_into_scratch(ctx, input_data_ptr, input_data_len)?;
let input_data = mem::replace(&mut ctx.scratch_buf, Vec::new());
let value: BalanceOf<<E as Ext>::T> = read_sandbox_memory_as(ctx, value_ptr, value_len)?;
let input_data = read_sandbox_memory(ctx, input_data_ptr, input_data_len)?;

let nested_gas_limit = if gas == 0 {
ctx.gas_meter.gas_left()
Expand All @@ -560,12 +558,10 @@ define_env!(Env, <E: Ext>,

match call_outcome {
Ok(output) => {
ctx.scratch_buf = output.data;
write_sandbox_output(ctx, result_ptr, result_len_ptr, &output.data)?;
Ok(output.status.into())
},
Err(buffer) => {
ctx.scratch_buf = buffer;
ctx.scratch_buf.clear();
Err(_) => {
Ok(TRAP_RETURN_CODE)
},
}
Expand Down