seal: Rework contracts API (#6573)

* Transition getter functions to not use scratch buffer

* Remove scratch buffer from ext_get_storage

* Remove scratch buffer from ext_call

* Remove scratch buffer from ext_instantiate

* Add ext_input and remove scratch buffer

* Rework error handling (changes RPC exposed data)

* ext_return passes a flags field instead of a return code
	* Flags is only for seal and not for the caller
	* flags: u32 replaced status_code: u8 in RPC exposed type
* API functions use a unified error type (ReturnCode)
* ext_transfer now traps on error to be consistent with call and instantiate

* Remove the no longer used `Dispatched` event

* Updated inline documentation

* Prevent skipping of copying the output for getter API

* Return gas_consumed from the RPC contracts call interface

* Updated COMPLEXTITY.md

* Rename ext_gas_price to ext_weight_to_fee

* Align comments with spaces

* Removed no longer used `ExecError`

* Remove possible panic in `from_typed_value`

* Use a struct as associated data for SpecialTrap::Return

* Fix nits in COMPLEXITY.md

* Renamed SpecialTrap to TrapReason

* Fix test

* Finish renaming special_trap -> trap_reason

* Remove no longer used get_runtime_storage

* fixup! Remove no longer used get_runtime_storage

* Removed tabs for comment aligment
This commit is contained in:
Alexander Theißen
2020-07-09 15:07:02 +02:00
committed by GitHub
parent a4427f3635
commit 25de5b5c78
26 changed files with 1116 additions and 1256 deletions
@@ -1,9 +1,8 @@
(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))
(import "env" "ext_call" (func $ext_call (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) (result i32)))
(import "env" "ext_input" (func $ext_input (param 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 i32 i32 i32 i32) (result i32)))
(import "env" "ext_println" (func $ext_println (param i32 i32)))
(import "env" "memory" (memory 1 1))
@@ -17,15 +16,17 @@
)
(func $current_balance (param $sp i32) (result i64)
(call $ext_balance)
(call $assert
(i32.eq (call $ext_scratch_size) (i32.const 8))
)
(call $ext_scratch_read
(i32.sub (get_local $sp) (i32.const 8))
(i32.const 0)
(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.sub (get_local $sp) (i32.const 16))
)
(call $assert
(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)))
)
@@ -36,21 +37,20 @@
(local $exit_code i32)
(local $balance i64)
;; Length of the buffer
(i32.store (i32.const 20) (i32.const 32))
;; Copy input to this contracts memory
(call $ext_input (i32.const 24) (i32.const 20))
;; Input data is the code hash of the contract to be deployed.
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 20))
(i32.const 32)
)
)
;; Copy code hash from scratch buffer into this contract's memory.
(call $ext_scratch_read
(i32.const 24) ;; The pointer where to store the scratch buffer contents,
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 32) ;; Count of bytes to copy.
)
;; Read current balance into local variable.
(set_local $sp (i32.const 1024))
(set_local $balance
@@ -67,17 +67,16 @@
(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
)
)
;; Check non-zero exit status.
(call $assert
(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))
(i32.eq (get_local $exit_code) (i32.const 2)) ;; ReturnCode::CalleeReverted
)
;; Check that balance has not changed.
@@ -95,17 +94,16 @@
(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
)
)
;; Check for special trap exit status.
(call $assert
(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))
(i32.eq (get_local $exit_code) (i32.const 1)) ;; ReturnCode::CalleeTrapped
)
;; Check that balance has not changed.
@@ -113,6 +111,12 @@
(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
@@ -123,24 +127,22 @@
(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
)
)
;; Check for success exit status.
(call $assert
(i32.eq (get_local $exit_code) (i32.const 0x00))
(i32.eq (get_local $exit_code) (i32.const 0)) ;; ReturnCode::Success
)
;; 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.
@@ -151,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
@@ -161,26 +175,19 @@
(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
)
)
;; Check non-zero exit status.
(call $assert
(i32.eq (get_local $exit_code) (i32.const 0x11))
(i32.eq (get_local $exit_code) (i32.const 2)) ;; ReturnCode::CalleeReverted
)
;; Check that scratch buffer contains the expected return data.
;; Check that output 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
@@ -204,17 +211,14 @@
(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
)
)
;; Check for special trap exit status.
(call $assert
(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))
(i32.eq (get_local $exit_code) (i32.const 1)) ;; ReturnCode::CalleeTrapped
)
;; Check that balance has not changed.
@@ -222,6 +226,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 contract successfully.
(set_local $exit_code
(call $ext_call
@@ -232,26 +248,19 @@
(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
)
)
;; Check for success exit status.
(call $assert
(i32.eq (get_local $exit_code) (i32.const 0x00))
(i32.eq (get_local $exit_code) (i32.const 0)) ;; ReturnCode::Success
)
;; Check that scratch buffer contains the expected return data.
;; Check that the output 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
@@ -271,5 +280,5 @@
(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 8) "\00\01\22\33\44\55\66\77") ;; The input data to instantiations and calls.
)
@@ -1,9 +1,14 @@
(module
(import "env" "ext_rent_allowance" (func $ext_rent_allowance))
(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_rent_allowance" (func $ext_rent_allowance (param i32 i32)))
(import "env" "memory" (memory 1 1))
;; [0, 8) reserved for $ext_rent_allowance output
;; [8, 16) length of the buffer
(data (i32.const 8) "\08")
;; [16, inf) zero initialized
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -16,30 +21,21 @@
(func (export "call"))
(func (export "deploy")
;; fill the scratch buffer with the rent allowance.
(call $ext_rent_allowance)
;; fill the buffer with the rent allowance.
(call $ext_rent_allowance (i32.const 0) (i32.const 8))
;; assert $ext_scratch_size == 8
;; assert len == 8
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 8))
(i32.const 8)
)
)
;; copy contents of the scratch buffer into the contract's memory.
(call $ext_scratch_read
(i32.const 8) ;; Pointer in memory to the place where to copy.
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 8) ;; Count of bytes to copy.
)
;; assert that contents of the buffer is equal to <BalanceOf<T>>::max_value().
(call $assert
(i64.eq
(i64.load
(i32.const 8)
)
(i64.load (i32.const 0))
(i64.const 0xFFFFFFFFFFFFFFFF)
)
)
@@ -1,7 +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_scratch_write" (func $ext_scratch_write (param i32 i32)))
(import "env" "ext_input" (func $ext_input (param i32 i32)))
(import "env" "ext_return" (func $ext_return (param i32 i32 i32)))
(import "env" "ext_hash_sha2_256" (func $ext_hash_sha2_256 (param i32 i32 i32)))
(import "env" "ext_hash_keccak_256" (func $ext_hash_keccak_256 (param i32 i32 i32)))
@@ -25,8 +24,7 @@
;; Called by the tests.
;;
;; The `call` function expects data in a certain format in the scratch
;; buffer.
;; The `call` function expects data in a certain format in the input buffer.
;;
;; 1. The first byte encodes an identifier for the crypto hash function
;; under test. (*)
@@ -34,7 +32,7 @@
;; crypto hash function chosen in 1.
;;
;; The `deploy` function then computes the chosen crypto hash function
;; given the input and puts the result back into the scratch buffer.
;; given the input and puts the result into the output buffer.
;; After contract execution the test driver then asserts that the returned
;; values are equal to the expected bytes for the input and chosen hash
;; function.
@@ -48,33 +46,36 @@
;; | 2 | BLAKE2 | 256 |
;; | 3 | BLAKE2 | 128 |
;; ---------------------------------
(func (export "call") (result i32)
(func (export "call")
(local $chosen_hash_fn i32)
(local $input_len_ptr i32)
(local $input_ptr i32)
(local $input_len i32)
(local $output_ptr i32)
(local $output_len i32)
(local.set $input_len_ptr (i32.const 256))
(local.set $input_ptr (i32.const 10))
(call $ext_scratch_read (local.get $input_ptr) (i32.const 0) (call $ext_scratch_size))
(i32.store (local.get $input_len_ptr) (i32.const 246))
(call $ext_input (local.get $input_ptr) (local.get $input_len_ptr))
(local.set $chosen_hash_fn (i32.load8_u (local.get $input_ptr)))
(if (i32.gt_u (local.get $chosen_hash_fn) (i32.const 7))
;; We check that the chosen hash fn identifier is within bounds: [0,7]
(unreachable)
)
(local.set $input_ptr (i32.add (local.get $input_ptr) (i32.const 1)))
(local.set $input_len (i32.sub (call $ext_scratch_size) (i32.const 1)))
(local.set $output_ptr (i32.const 100))
(local.set $input_len (i32.sub (i32.load (local.get $input_len_ptr)) (i32.const 1)))
(local.set $output_len (i32.load8_u (local.get $chosen_hash_fn)))
(call_indirect (type $hash_fn_sig)
(local.get $input_ptr)
(local.get $input_len)
(local.get $output_ptr)
(local.get $input_ptr)
(local.get $chosen_hash_fn) ;; Which crypto hash function to execute.
)
(call $ext_scratch_write
(local.get $output_ptr) ;; Linear memory location of the output buffer.
(call $ext_return
(i32.const 0)
(local.get $input_ptr) ;; Linear memory location of the output buffer.
(local.get $output_len) ;; Number of output buffer bytes.
)
(i32.const 0)
(unreachable)
)
)
@@ -1,12 +1,28 @@
(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_get_storage" (func $ext_get_storage (param i32) (result i32)))
(import "env" "ext_input" (func $ext_input (param 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_instantiate" (func $ext_instantiate (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 i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))
;; [0, 8) Endowment to send when creating contract.
(data (i32.const 0) "\00\00\01")
;; [8, 16) Value to send when calling contract.
;; [16, 48) The key to store the contract address under.
;; [48, 80) Buffer where to store the input to the contract
;; [80, 88) Buffer where to store the address of the instantiated contract
;; [88, 96) Size of the buffer
(data (i32.const 88) "\08")
;; [96, 100) Size of the input buffer
(data (i32.const 96) "\20")
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -17,21 +33,15 @@
)
(func (export "deploy")
;; Input data is the code hash of the contract to be deployed.
;; Input data is the code hash of the contract to be deployed.
(call $ext_input (i32.const 48) (i32.const 96))
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 96))
(i32.const 32)
)
)
;; Copy code hash from scratch buffer into this contract's memory.
(call $ext_scratch_read
(i32.const 48) ;; The pointer where to store the scratch buffer contents,
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 32) ;; Count of bytes to copy.
)
;; Deploy the contract with the provided code hash.
(call $assert
(i32.eq
@@ -43,23 +53,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 contract
(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
@@ -75,21 +84,18 @@
(i32.eq
(call $ext_get_storage
(i32.const 16) ;; Pointer to the key
(i32.const 80) ;; Pointer to the value
(i32.const 88) ;; Pointer to the len of the value
)
(i32.const 0)
)
)
(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 contract address.
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 8) ;; Count of bytes to copy.
)
;; Calling the destination contract with non-empty input data should fail.
(call $assert
@@ -102,8 +108,11 @@
(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)
(i32.const 0x1)
)
)
@@ -118,6 +127,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)
)
@@ -136,13 +147,11 @@
(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)
)
)
)
(data (i32.const 0) "\00\00\01") ;; Endowment to send when creating contract.
(data (i32.const 8) "") ;; Value to send when calling contract.
(data (i32.const 16) "") ;; The key to store the contract address under.
)
+15 -15
View File
@@ -1,10 +1,15 @@
(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))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result 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" "memory" (memory 1 1))
;; [0, 8) reserved for $ext_balance output
;; [8, 16) length of the buffer
(data (i32.const 8) "\08")
;; [16, inf) zero initialized
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -18,34 +23,29 @@
(func (export "call")
;; Send entire remaining balance to the 0 address.
(call $ext_balance)
(call $ext_balance (i32.const 0) (i32.const 8))
;; Balance should be encoded as a u64.
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 8))
(i32.const 8)
)
)
;; Read balance into memory.
(call $ext_scratch_read
(i32.const 8) ;; Pointer to write balance to
(i32.const 0) ;; Offset into scratch buffer
(i32.const 8) ;; Length of encoded balance
)
;; Self-destruct by sending full balance to the 0 address.
(call $assert
(i32.eq
(call $ext_call
(i32.const 0) ;; Pointer to destination address
(i32.const 16) ;; Pointer to destination address
(i32.const 8) ;; Length of destination address
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 8) ;; Pointer to the buffer with value to transfer
(i32.const 0) ;; Pointer to the buffer with value to transfer
(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)
)
@@ -1,5 +1,6 @@
(module
(import "env" "ext_set_storage" (func $ext_set_storage (param i32 i32 i32)))
(import "env" "ext_input" (func $ext_input (param i32 i32)))
(import "env" "ext_restore_to"
(func $ext_restore_to
(param i32 i32 i32 i32 i32 i32 i32 i32)
@@ -7,7 +8,25 @@
)
(import "env" "memory" (memory 1 1))
(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)
(func (export "call")
;; copy code hash to contract memory
(call $ext_input (i32.const 264) (i32.const 304))
(call $assert
(i32.eq
(i32.load (i32.const 304))
(i32.const 32)
)
)
(call $ext_restore_to
;; Pointer and length of the encoded dest buffer.
(i32.const 256)
@@ -49,12 +68,11 @@
;; Address of bob
(data (i32.const 256) "\02\00\00\00\00\00\00\00")
;; Code hash of SET_RENT
(data (i32.const 264)
"\ab\d6\58\65\1e\83\6e\4a\18\0d\f2\6d\bc\42\ba\e9"
"\3d\64\76\e5\30\5b\33\46\bb\4d\43\99\38\21\ee\32"
)
;; [264, 296) Code hash of SET_RENT (copied here by ext_input)
;; Rent allowance
;; [296, 304) Rent allowance
(data (i32.const 296) "\32\00\00\00\00\00\00\00")
;; [304, 308) Size of SET_RENT buffer
(data (i32.const 304) "\20")
)
@@ -1,5 +1,5 @@
(module
(import "env" "ext_return" (func $ext_return (param i32 i32)))
(import "env" "ext_return" (func $ext_return (param i32 i32 i32)))
(import "env" "ext_deposit_event" (func $ext_deposit_event (param i32 i32 i32 i32)))
(import "env" "memory" (memory 1 1))
@@ -12,6 +12,7 @@
(i32.const 4) ;; The data buffer's length
)
(call $ext_return
(i32.const 0)
(i32.const 8)
(i32.const 4)
)
@@ -1,39 +1,33 @@
(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_scratch_write" (func $ext_scratch_write (param i32 i32)))
(import "env" "ext_input" (func $ext_input (param i32 i32)))
(import "env" "ext_return" (func $ext_return (param i32 i32 i32)))
(import "env" "memory" (memory 1 1))
;; [0, 128) buffer where input is copied
;; [128, 132) length of the input buffer
(data (i32.const 128) "\80")
;; Deploy routine is the same as call.
(func (export "deploy") (result i32)
(func (export "deploy")
(call $call)
)
;; Call reads the first 4 bytes (LE) as the exit status and returns the rest as output data.
(func $call (export "call") (result i32)
(local $buf_size i32)
(local $exit_status i32)
;; Find out the size of the scratch buffer
(set_local $buf_size (call $ext_scratch_size))
;; Copy scratch buffer into this contract memory.
(call $ext_scratch_read
(i32.const 0) ;; The pointer where to store the scratch buffer contents,
(i32.const 0) ;; Offset from the start of the scratch buffer.
(get_local $buf_size) ;; Count of bytes to copy.
)
(func $call (export "call")
;; Copy input into this contracts memory.
(call $ext_input (i32.const 0) (i32.const 128))
;; Copy all but the first 4 bytes of the input data as the output data.
(call $ext_scratch_write
;; Use the first byte as exit status
(call $ext_return
(i32.load8_u (i32.const 0)) ;; Exit status
(i32.const 4) ;; Pointer to the data to return.
(i32.sub ;; Count of bytes to copy.
(get_local $buf_size)
(i32.load (i32.const 128))
(i32.const 4)
)
)
;; Return the first 4 bytes of the input data as the exit status.
(i32.load (i32.const 0))
(unreachable)
)
)
@@ -1,11 +1,25 @@
(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))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result i32)))
(import "env" "ext_input" (func $ext_input (param 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 i32 i32) (result i32)))
(import "env" "ext_terminate" (func $ext_terminate (param i32 i32)))
(import "env" "memory" (memory 1 1))
;; [0, 8) reserved for $ext_address output
;; [8, 16) length of the buffer
(data (i32.const 8) "\08")
;; [16, 24) Address of django
(data (i32.const 16) "\04\00\00\00\00\00\00\00")
;; [24, 32) reserved for output of $ext_input
;; [32, 36) length of the buffer
(data (i32.const 32) "\04")
;; [36, inf) zero initialized
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -22,36 +36,32 @@
;; This should trap instead of self-destructing since a contract cannot be removed live in
;; the execution stack cannot be removed. If the recursive call traps, then trap here as
;; well.
(if (call $ext_scratch_size)
(call $ext_input (i32.const 24) (i32.const 32))
(if (i32.load (i32.const 32))
(then
(call $ext_address)
(call $ext_address (i32.const 0) (i32.const 8))
;; Expect address to be 8 bytes.
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 8))
(i32.const 8)
)
)
;; Read own address into memory.
(call $ext_scratch_read
(i32.const 16) ;; Pointer to write address to
(i32.const 0) ;; Offset into scratch buffer
(i32.const 8) ;; Length of encoded address
)
;; Recursively call self with empty input data.
(call $assert
(i32.eq
(call $ext_call
(i32.const 16) ;; Pointer to own address
(i32.const 0) ;; Pointer to own address
(i32.const 8) ;; Length of own address
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 8) ;; Pointer to the buffer with value to transfer
(i32.const 36) ;; Pointer to the buffer with value to transfer
(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)
)
@@ -60,13 +70,11 @@
(else
;; Try to terminate and give balance to django.
(call $ext_terminate
(i32.const 32) ;; Pointer to beneficiary address
(i32.const 16) ;; Pointer to beneficiary address
(i32.const 8) ;; Length of beneficiary address
)
(unreachable) ;; ext_terminate never returns
)
)
)
;; Address of django
(data (i32.const 32) "\04\00\00\00\00\00\00\00")
)
@@ -1,10 +1,15 @@
(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))
(import "env" "ext_call" (func $ext_call (param i32 i32 i64 i32 i32 i32 i32) (result 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" "memory" (memory 1 1))
;; [0, 8) reserved for $ext_balance output
;; [8, 16) length of the buffer
(data (i32.const 8) "\08")
;; [16, inf) zero initialized
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -16,34 +21,29 @@
(func (export "deploy")
;; Send entire remaining balance to the 0 address.
(call $ext_balance)
(call $ext_balance (i32.const 0) (i32.const 8))
;; Balance should be encoded as a u64.
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 8))
(i32.const 8)
)
)
;; Read balance into memory.
(call $ext_scratch_read
(i32.const 8) ;; Pointer to write balance to
(i32.const 0) ;; Offset into scratch buffer
(i32.const 8) ;; Length of encoded balance
)
;; Self-destruct by sending full balance to the 0 address.
(call $assert
(i32.eq
(call $ext_call
(i32.const 0) ;; Pointer to destination address
(i32.const 16) ;; Pointer to destination address
(i32.const 8) ;; Length of destination address
(i64.const 0) ;; How much gas to devote for the execution. 0 = all.
(i32.const 8) ;; Pointer to the buffer with value to transfer
(i32.const 0) ;; Pointer to the buffer with value to transfer
(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)
)
+12 -18
View File
@@ -1,10 +1,9 @@
(module
(import "env" "ext_transfer" (func $ext_transfer (param i32 i32 i32 i32) (result i32)))
(import "env" "ext_transfer" (func $ext_transfer (param i32 i32 i32 i32)))
(import "env" "ext_set_storage" (func $ext_set_storage (param i32 i32 i32)))
(import "env" "ext_clear_storage" (func $ext_clear_storage (param i32)))
(import "env" "ext_set_rent_allowance" (func $ext_set_rent_allowance (param i32 i32)))
(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_input" (func $ext_input (param i32 i32)))
(import "env" "memory" (memory 1 1))
;; insert a value of 4 bytes into storage
@@ -25,12 +24,7 @@
;; transfer 50 to CHARLIE
(func $call_2
(call $assert
(i32.eq
(call $ext_transfer (i32.const 68) (i32.const 8) (i32.const 76) (i32.const 8))
(i32.const 0)
)
)
(call $ext_transfer (i32.const 68) (i32.const 8) (i32.const 76) (i32.const 8))
)
;; do nothing
@@ -48,8 +42,10 @@
;; Dispatch the call according to input size
(func (export "call")
(local $input_size i32)
(i32.store (i32.const 64) (i32.const 64))
(call $ext_input (i32.const 1024) (i32.const 64))
(set_local $input_size
(call $ext_scratch_size)
(i32.load (i32.const 64))
)
(block $IF_ELSE
(block $IF_2
@@ -75,29 +71,27 @@
;; Set into storage a 4 bytes value
;; Set call set_rent_allowance with input
(func (export "deploy")
(local $input_size i32)
(set_local $input_size
(call $ext_scratch_size)
)
(call $ext_set_storage
(i32.const 0)
(i32.const 0)
(i32.const 4)
)
(call $ext_scratch_read
(call $ext_input
(i32.const 0)
(i32.const 0)
(get_local $input_size)
(i32.const 64)
)
(call $ext_set_rent_allowance
(i32.const 0)
(get_local $input_size)
(i32.load (i32.const 64))
)
)
;; Encoding of 10 in balance
(data (i32.const 0) "\28")
;; Size of the buffer at address 0
(data (i32.const 64) "\40")
;; encoding of Charlies's account id
(data (i32.const 68) "\03")
@@ -1,10 +1,22 @@
(module
(import "env" "ext_get_storage" (func $ext_get_storage (param i32) (result 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_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_input" (func $ext_input (param i32 i32)))
(import "env" "memory" (memory 16 16))
;; [0, 32) storage key
(data (i32.const 0) "\01")
;; [32, 36) buffer where input is copied (expected size of storage item)
;; [36, 40) size of the input buffer
(data (i32.const 36) "\04")
;; [40, 44) size of buffer for ext_get_storage set to max
(data (i32.const 40) "\FF\FF\FF\FF")
;; [44, inf) ext_get_storage buffer
(func $assert (param i32)
(block $ok
(br_if $ok
@@ -15,21 +27,16 @@
)
(func (export "call")
;; assert $ext_scratch_size == 8
(call $ext_input (i32.const 32) (i32.const 36))
;; assert input size == 4
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 36))
(i32.const 4)
)
)
;; copy contents of the scratch buffer into the contract's memory.
(call $ext_scratch_read
(i32.const 32) ;; Pointer in memory to the place where to copy.
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 4) ;; Count of bytes to copy.
)
;; place a garbage value in storage, the size of which is specified by the call input.
(call $ext_set_storage
(i32.const 0) ;; Pointer to storage key
@@ -41,6 +48,8 @@
(i32.eq
(call $ext_get_storage
(i32.const 0) ;; Pointer to storage key
(i32.const 44) ;; buffer where to copy result
(i32.const 40) ;; pointer to size of buffer
)
(i32.const 0)
)
@@ -48,7 +57,7 @@
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.load (i32.const 40))
(i32.load (i32.const 32))
)
)
@@ -56,5 +65,4 @@
(func (export "deploy"))
(data (i32.const 0) "\01") ;; Storage key (32 B)
)