Skip to content

Commit 001173b

Browse files
authored
contracts: Allow runtime authors to define a chain extension (#7548)
* Make host functions return TrapReason This avoids the need to manually store any trap reasons to the `Runtime` from the host function. This adds the following benefits: * It properly composes with the upcoming chain extensions * Missing to set a trap value is now a compile error * Add chain extension The chain extension is a way for the contract author to add new host functions for contracts to call. * Add tests for chain extensions * Fix regression in set_rent.wat fixture Not all offsets where properly updated when changing the fixtures for the new salt on instantiate. * Pre-charge a weight amount based off the specified length * Improve fn write docs * Renamed state to phantom * Fix typo
1 parent 306d82d commit 001173b

File tree

9 files changed

+767
-37
lines changed

9 files changed

+767
-37
lines changed

fixtures/chain_extension.wat

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
;; Call chain extension by passing through input and output of this contract
2+
(module
3+
(import "seal0" "seal_call_chain_extension"
4+
(func $seal_call_chain_extension (param i32 i32 i32 i32 i32) (result i32))
5+
)
6+
(import "seal0" "seal_input" (func $seal_input (param i32 i32)))
7+
(import "seal0" "seal_return" (func $seal_return (param i32 i32 i32)))
8+
(import "env" "memory" (memory 16 16))
9+
10+
(func $assert (param i32)
11+
(block $ok
12+
(br_if $ok (get_local 0))
13+
(unreachable)
14+
)
15+
)
16+
17+
;; [0, 4) len of input output
18+
(data (i32.const 0) "\02")
19+
20+
;; [4, 12) buffer for input
21+
22+
;; [12, 16) len of output buffer
23+
(data (i32.const 12) "\02")
24+
25+
;; [16, inf) buffer for output
26+
27+
(func (export "deploy"))
28+
29+
(func (export "call")
30+
(call $seal_input (i32.const 4) (i32.const 0))
31+
32+
;; the chain extension passes through the input and returns it as output
33+
(call $seal_call_chain_extension
34+
(i32.load8_u (i32.const 4)) ;; func_id
35+
(i32.const 4) ;; input_ptr
36+
(i32.load (i32.const 0)) ;; input_len
37+
(i32.const 16) ;; output_ptr
38+
(i32.const 12) ;; output_len_ptr
39+
)
40+
41+
;; the chain extension passes through the func_id
42+
(call $assert (i32.eq (i32.load8_u (i32.const 4))))
43+
44+
(call $seal_return (i32.const 0) (i32.const 16) (i32.load (i32.const 12)))
45+
)
46+
)

fixtures/set_rent.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@
8484
)
8585
(i32.store (i32.const 128) (i32.const 64))
8686
(call $seal_input
87-
(i32.const 104)
88-
(i32.const 100)
87+
(i32.const 132)
88+
(i32.const 128)
8989
)
9090
(call $seal_set_rent_allowance
91-
(i32.const 104)
91+
(i32.const 132)
9292
(i32.load (i32.const 128))
9393
)
9494
)

0 commit comments

Comments
 (0)