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_instantiate
  • Loading branch information
athei committed Jul 3, 2020
commit 6f94941314a37fa6e0b8938e0dddc7992ece5c88
42 changes: 22 additions & 20 deletions frame/contracts/fixtures/caller_contract.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_balance" (func $ext_balance (param i32 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_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "ext_println" (func $ext_println (param i32 i32)))
(import "env" "memory" (memory 1 1))

Expand Down Expand Up @@ -69,6 +69,10 @@
(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.const 4294967295) ;; u32 max sentinel value: do not copy address
(i32.const 0) ;; Length is ignored in this case
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
)

Expand All @@ -77,11 +81,6 @@
(i32.eq (get_local $exit_code) (i32.const 0x11))
)

;; Check that scratch buffer is empty since contract instantiation failed.
(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)))
Expand All @@ -97,6 +96,10 @@
(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 address
(i32.const 0) ;; Length is ignored in this case
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
)

Expand All @@ -105,16 +108,17 @@
(i32.eq (get_local $exit_code) (i32.const 0x0100))
)

;; Check that scratch buffer is empty since contract instantiation failed.
(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)))
)

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

;; Deploy the contract successfully.
(set_local $exit_code
(call $ext_instantiate
Expand All @@ -125,6 +129,11 @@
(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 16) ;; Pointer to the address output buffer
(i32.sub (get_local $sp) (i32.const 4)) ;; Pointer to the address buffer length
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case

)
)

Expand All @@ -133,16 +142,9 @@
(i32.eq (get_local $exit_code) (i32.const 0x00))
)

;; Check that scratch buffer contains the address of the new contract.
;; Check that address has the expected length
(call $assert
(i32.eq (call $ext_scratch_size) (i32.const 8))
)

;; Copy contract address from scratch buffer into this contract's memory.
(call $ext_scratch_read
(i32.const 16) ;; The pointer where to store the scratch buffer contents,
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 8) ;; Count of bytes to copy.
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 4))) (i32.const 8))
)

;; Check that balance has been deducted.
Expand Down
15 changes: 7 additions & 8 deletions frame/contracts/fixtures/destroy_and_transfer.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(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 i32 i32) (result i32)))
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))

;; [0, 8) Endowment to send when creating contract.
Expand Down Expand Up @@ -57,23 +57,22 @@
(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 80) ;; Buffer where to store address of new contratc
(i32.const 88) ;; Pointer to the length of the buffer
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this cas
)
(i32.const 0)
)
)

;; Read the address of the instantiated contract into memory.
;; Check that address has expected length
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 88))
(i32.const 8)
)
)
(call $ext_scratch_read
(i32.const 80) ;; The pointer where to store the scratch buffer contents,
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 8) ;; Count of bytes to copy.
)

;; Store the return address.
(call $ext_set_storage
Expand Down
21 changes: 15 additions & 6 deletions frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ mod tests {
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32,
;; result_ptr: u32,
;; result_len_ptr: u32
;; output_ptr: u32,
;; output_len_ptr: u32
;;) -> u32
(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))
Expand Down Expand Up @@ -630,8 +630,13 @@ mod tests {
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32,
;; input_data_len: u32,
;; address_ptr: u32,
;; address_len_ptr: u32,
;; output_ptr: u32,
;; output_len_ptr: u32
;; ) -> u32
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_instantiate" (func $ext_instantiate (param i32 i32 i64 i32 i32 i32 i32 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
(func (export "call")
(drop
Expand All @@ -643,6 +648,10 @@ mod tests {
(i32.const 8) ;; Length of the buffer with value to transfer
(i32.const 12) ;; 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
(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 @@ -677,7 +686,7 @@ mod tests {
code_hash: [0x11; 32].into(),
endowment: 3,
data: vec![1, 2, 3, 4],
gas_left: 9973500000,
gas_left: 9971500000,
}]
);
}
Expand Down Expand Up @@ -733,8 +742,8 @@ mod tests {
;; value_len: u32,
;; input_data_ptr: u32,
;; input_data_len: u32,
;; result_ptr: u32,
;; result_len_ptr: u32
;; output_ptr: u32,
;; output_len_ptr: u32
;;) -> u32
(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))
Expand Down
33 changes: 14 additions & 19 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ define_env!(Env, <E: Ext>,
value_len: u32,
input_data_ptr: u32,
input_data_len: u32,
result_ptr: u32,
result_len_ptr: u32
output_ptr: u32,
output_len_ptr: u32
) -> u32 => {
let callee: <<E as Ext>::T as frame_system::Trait>::AccountId =
read_sandbox_memory_as(ctx, callee_ptr, callee_len)?;
Expand Down Expand Up @@ -558,7 +558,7 @@ define_env!(Env, <E: Ext>,

match call_outcome {
Ok(output) => {
write_sandbox_output(ctx, result_ptr, result_len_ptr, &output.data)?;
write_sandbox_output(ctx, output_ptr, output_len_ptr, &output.data)?;
Ok(output.status.into())
},
Err(_) => {
Expand Down Expand Up @@ -610,16 +610,16 @@ define_env!(Env, <E: Ext>,
value_ptr: u32,
value_len: u32,
input_data_ptr: u32,
input_data_len: u32
input_data_len: u32,
address_ptr: u32,
address_len_ptr: u32,
output_ptr: u32,
output_len_ptr: u32
) -> u32 => {
let code_hash: CodeHash<<E as Ext>::T> =
read_sandbox_memory_as(ctx, code_hash_ptr, code_hash_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 @@ -644,18 +644,13 @@ define_env!(Env, <E: Ext>,
});
match instantiate_outcome {
Ok((address, output)) => {
let is_success = output.is_success();
ctx.scratch_buf = output.data;
ctx.scratch_buf.clear();
if is_success {
// Write the address to the scratch buffer.
address.encode_to(&mut ctx.scratch_buf);
if output.is_success() {
write_sandbox_output(ctx, address_ptr, address_len_ptr, &address.encode())?;
write_sandbox_output(ctx, output_ptr, output_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