-
Notifications
You must be signed in to change notification settings - Fork 110
/
instance.go
895 lines (794 loc) · 27.6 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// Copyright 2023 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package wazero_runtime
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/allocator"
"github.com/ChainSafe/gossamer/lib/runtime/offchain"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/ChainSafe/gossamer/pkg/trie"
"github.com/klauspost/compress/zstd"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// Name represents the name of the interpreter
const Name = "wazero"
type contextKey string
const runtimeContextKey = contextKey("runtime.Context")
var _ runtime.Instance = &Instance{}
// Instance backed by wazero.Runtime
type Instance struct {
Runtime wazero.Runtime
Module api.Module
Context *runtime.Context
codeHash common.Hash
sync.Mutex
}
// Config is the configuration used to create a Wasmer runtime instance.
type Config struct {
Storage runtime.Storage
Keystore *keystore.GlobalKeystore
LogLvl log.Level
Role common.NetworkRole
NodeStorage runtime.NodeStorage
Network runtime.BasicNetwork
Transaction runtime.TransactionState
CodeHash common.Hash
DefaultVersion *runtime.Version
}
func decompressWasm(code []byte) ([]byte, error) {
compressionFlag := []byte{82, 188, 83, 118, 70, 219, 142, 5}
if !bytes.HasPrefix(code, compressionFlag) {
return code, nil
}
decoder, err := zstd.NewReader(nil)
if err != nil {
return nil, fmt.Errorf("creating zstd reader: %s", err)
}
bytes, err := decoder.DecodeAll(code[len(compressionFlag):], nil)
if err != nil {
return nil, err
}
return bytes, err
}
// NewRuntimeFromGenesis creates a runtime instance from the genesis data
func NewRuntimeFromGenesis(cfg Config) (instance *Instance, err error) {
if cfg.Storage == nil {
return nil, errors.New("storage is nil")
}
code := cfg.Storage.LoadCode()
if len(code) == 0 {
return nil, fmt.Errorf("cannot find :code in state")
}
return NewInstance(code, cfg)
}
// NewInstanceFromTrie returns a new runtime instance with the code provided in the given trie
func NewInstanceFromTrie(t *trie.Trie, cfg Config) (*Instance, error) {
code := t.Get(common.CodeKey)
if len(code) == 0 {
return nil, fmt.Errorf("cannot find :code in trie")
}
return NewInstance(code, cfg)
}
// NewInstance instantiates a runtime from raw wasm bytecode
func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
logger.Info("instantiating a runtime!")
logger.Patch(log.SetLevel(cfg.LogLvl), log.SetCallerFunc(true))
ctx := context.Background()
rt := wazero.NewRuntime(ctx)
_, err = rt.NewHostModuleBuilder("env").
// values from newer kusama/polkadot runtimes
ExportMemory("memory", 23).
NewFunctionBuilder().
WithFunc(ext_logging_log_version_1).
Export("ext_logging_log_version_1").
NewFunctionBuilder().
WithFunc(func() int32 {
return 4
}).
Export("ext_logging_max_level_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int32, c int32) {
panic("unimplemented")
}).
Export("ext_transaction_index_index_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int32) {
panic("unimplemented")
}).
Export("ext_transaction_index_renew_version_1").
NewFunctionBuilder().
WithFunc(func(a int32) {
panic("unimplemented")
}).
Export("ext_sandbox_instance_teardown_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int64, c int64, d int32) int32 {
panic("unimplemented")
}).
Export("ext_sandbox_instantiate_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int64, c int64, d int32, e int32, f int32) int32 {
panic("unimplemented")
}).
Export("ext_sandbox_invoke_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int32, c int32, d int32) int32 {
panic("unimplemented")
}).
Export("ext_sandbox_memory_get_version_1").
NewFunctionBuilder().
WithFunc(func(a int32, b int32, c int32, d int32) int32 {
panic("unimplemented")
}).
Export("ext_sandbox_memory_set_version_1").
NewFunctionBuilder().
WithFunc(func(a int32) {
panic("unimplemented")
}).
Export("ext_sandbox_memory_teardown_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_ed25519_generate_version_1).
Export("ext_crypto_ed25519_generate_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_ed25519_public_keys_version_1).
Export("ext_crypto_ed25519_public_keys_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_ed25519_sign_version_1).
Export("ext_crypto_ed25519_sign_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_ed25519_verify_version_1).
Export("ext_crypto_ed25519_verify_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_secp256k1_ecdsa_recover_version_1).
Export("ext_crypto_secp256k1_ecdsa_recover_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_secp256k1_ecdsa_recover_version_2).
Export("ext_crypto_secp256k1_ecdsa_recover_version_2").
NewFunctionBuilder().
WithFunc(ext_crypto_ecdsa_verify_version_2).
Export("ext_crypto_ecdsa_verify_version_2").
NewFunctionBuilder().
WithFunc(ext_crypto_secp256k1_ecdsa_recover_compressed_version_1).
Export("ext_crypto_secp256k1_ecdsa_recover_compressed_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_secp256k1_ecdsa_recover_compressed_version_2).
Export("ext_crypto_secp256k1_ecdsa_recover_compressed_version_2").
NewFunctionBuilder().
WithFunc(ext_crypto_sr25519_generate_version_1).
Export("ext_crypto_sr25519_generate_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_sr25519_public_keys_version_1).
Export("ext_crypto_sr25519_public_keys_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_sr25519_sign_version_1).
Export("ext_crypto_sr25519_sign_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_sr25519_verify_version_1).
Export("ext_crypto_sr25519_verify_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_sr25519_verify_version_2).
Export("ext_crypto_sr25519_verify_version_2").
NewFunctionBuilder().
WithFunc(ext_crypto_start_batch_verify_version_1).
Export("ext_crypto_start_batch_verify_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_finish_batch_verify_version_1).
Export("ext_crypto_finish_batch_verify_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_root_version_1).
Export("ext_trie_blake2_256_root_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_root_version_2).
Export("ext_trie_blake2_256_root_version_2").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_ordered_root_version_1).
Export("ext_trie_blake2_256_ordered_root_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_ordered_root_version_2).
Export("ext_trie_blake2_256_ordered_root_version_2").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_verify_proof_version_1).
Export("ext_trie_blake2_256_verify_proof_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_verify_proof_version_2).
Export("ext_trie_blake2_256_verify_proof_version_2").
NewFunctionBuilder().
WithFunc(ext_misc_print_hex_version_1).
Export("ext_misc_print_hex_version_1").
NewFunctionBuilder().
WithFunc(ext_misc_print_num_version_1).
Export("ext_misc_print_num_version_1").
NewFunctionBuilder().
WithFunc(ext_misc_print_utf8_version_1).
Export("ext_misc_print_utf8_version_1").
NewFunctionBuilder().
WithFunc(ext_misc_runtime_version_version_1).
Export("ext_misc_runtime_version_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_set_version_1).
Export("ext_default_child_storage_set_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_read_version_1).
Export("ext_default_child_storage_read_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_clear_version_1).
Export("ext_default_child_storage_clear_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_clear_prefix_version_1).
Export("ext_default_child_storage_clear_prefix_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_clear_prefix_version_2).
Export("ext_default_child_storage_clear_prefix_version_2").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_exists_version_1).
Export("ext_default_child_storage_exists_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_get_version_1).
Export("ext_default_child_storage_get_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_next_key_version_1).
Export("ext_default_child_storage_next_key_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_root_version_1).
Export("ext_default_child_storage_root_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_root_version_2).
Export("ext_default_child_storage_root_version_2").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_storage_kill_version_1).
Export("ext_default_child_storage_storage_kill_version_1").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_storage_kill_version_2).
Export("ext_default_child_storage_storage_kill_version_2").
NewFunctionBuilder().
WithFunc(ext_default_child_storage_storage_kill_version_3).
Export("ext_default_child_storage_storage_kill_version_3").
NewFunctionBuilder().
WithFunc(ext_allocator_free_version_1).
Export("ext_allocator_free_version_1").
NewFunctionBuilder().
WithFunc(ext_allocator_malloc_version_1).
Export("ext_allocator_malloc_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_blake2_128_version_1).
Export("ext_hashing_blake2_128_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_blake2_256_version_1).
Export("ext_hashing_blake2_256_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_keccak_256_version_1).
Export("ext_hashing_keccak_256_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_sha2_256_version_1).
Export("ext_hashing_sha2_256_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_twox_256_version_1).
Export("ext_hashing_twox_256_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_twox_128_version_1).
Export("ext_hashing_twox_128_version_1").
NewFunctionBuilder().
WithFunc(ext_hashing_twox_64_version_1).
Export("ext_hashing_twox_64_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_index_set_version_1).
Export("ext_offchain_index_set_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_index_clear_version_1).
Export("ext_offchain_index_clear_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_local_storage_clear_version_1).
Export("ext_offchain_local_storage_clear_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_is_validator_version_1).
Export("ext_offchain_is_validator_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_local_storage_compare_and_set_version_1).
Export("ext_offchain_local_storage_compare_and_set_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_local_storage_get_version_1).
Export("ext_offchain_local_storage_get_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_local_storage_set_version_1).
Export("ext_offchain_local_storage_set_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_network_state_version_1).
Export("ext_offchain_network_state_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_random_seed_version_1).
Export("ext_offchain_random_seed_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_submit_transaction_version_1).
Export("ext_offchain_submit_transaction_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_timestamp_version_1).
Export("ext_offchain_timestamp_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_sleep_until_version_1).
Export("ext_offchain_sleep_until_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_http_request_start_version_1).
Export("ext_offchain_http_request_start_version_1").
NewFunctionBuilder().
WithFunc(ext_offchain_http_request_add_header_version_1).
Export("ext_offchain_http_request_add_header_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_append_version_1).
Export("ext_storage_append_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_changes_root_version_1).
Export("ext_storage_changes_root_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_clear_version_1).
Export("ext_storage_clear_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_clear_prefix_version_1).
Export("ext_storage_clear_prefix_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_clear_prefix_version_2).
Export("ext_storage_clear_prefix_version_2").
NewFunctionBuilder().
WithFunc(ext_storage_exists_version_1).
Export("ext_storage_exists_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_get_version_1).
Export("ext_storage_get_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_next_key_version_1).
Export("ext_storage_next_key_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_read_version_1).
Export("ext_storage_read_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_root_version_1).
Export("ext_storage_root_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_root_version_2).
Export("ext_storage_root_version_2").
NewFunctionBuilder().
WithFunc(ext_storage_set_version_1).
Export("ext_storage_set_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_start_transaction_version_1).
Export("ext_storage_start_transaction_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_rollback_transaction_version_1).
Export("ext_storage_rollback_transaction_version_1").
NewFunctionBuilder().
WithFunc(ext_storage_commit_transaction_version_1).
Export("ext_storage_commit_transaction_version_1").
NewFunctionBuilder().
WithFunc(ext_crypto_ecdsa_generate_version_1).
Export("ext_crypto_ecdsa_generate_version_1").
Instantiate(ctx)
if err != nil {
return nil, err
}
code, err = decompressWasm(code)
if err != nil {
return nil, err
}
mod, err := rt.Instantiate(ctx, code)
if err != nil {
return nil, err
}
global := mod.ExportedGlobal("__heap_base")
if global == nil {
return nil, fmt.Errorf("wazero error: nil global for __heap_base")
}
hb := api.DecodeU32(global.Get())
// hb = runtime.DefaultHeapBase
mem := mod.Memory()
if mem == nil {
return nil, fmt.Errorf("wazero error: nil memory for module")
}
allocator := allocator.NewFreeingBumpHeapAllocator(hb)
instance = &Instance{
Runtime: rt,
Context: &runtime.Context{
Allocator: allocator,
Keystore: cfg.Keystore,
Validator: cfg.Role == common.AuthorityRole,
NodeStorage: cfg.NodeStorage,
Network: cfg.Network,
Transaction: cfg.Transaction,
SigVerifier: crypto.NewSignatureVerifier(logger),
OffchainHTTPSet: offchain.NewHTTPSet(),
},
Module: mod,
codeHash: cfg.CodeHash,
}
if cfg.DefaultVersion == nil {
err = instance.version()
if err != nil {
return nil, fmt.Errorf("while getting runtime version: %w", err)
}
} else {
instance.Context.Version = cfg.DefaultVersion
}
if cfg.Storage != nil {
instance.SetContextStorage(cfg.Storage)
}
return instance, nil
}
var ErrExportFunctionNotFound = errors.New("export function not found")
func (i *Instance) Exec(function string, data []byte) (result []byte, err error) {
i.Lock()
defer i.Unlock()
dataLength := uint32(len(data))
inputPtr, err := i.Context.Allocator.Allocate(i.Module.Memory(), dataLength)
if err != nil {
return nil, fmt.Errorf("allocating input memory: %w", err)
}
// Store the data into memory
mem := i.Module.Memory()
if mem == nil {
panic("nil memory")
}
ok := mem.Write(inputPtr, data)
if !ok {
panic("write overflow")
}
runtimeFunc := i.Module.ExportedFunction(function)
if runtimeFunc == nil {
return nil, fmt.Errorf("%w: %s", ErrExportFunctionNotFound, function)
}
ctx := context.WithValue(context.Background(), runtimeContextKey, i.Context)
values, err := runtimeFunc.Call(ctx, api.EncodeU32(inputPtr), api.EncodeU32(dataLength))
if err != nil {
return nil, fmt.Errorf("running runtime function: %w", err)
}
if len(values) == 0 {
return nil, fmt.Errorf("no returned values from runtime function: %s", function)
}
wasmValue := values[0]
outputPtr, outputLength := splitPointerSize(wasmValue)
result, ok = mem.Read(outputPtr, outputLength)
if !ok {
panic("write overflow")
}
return result, nil
}
// Version returns the instance version.
// This is cheap to call since the instance version is cached.
// Note the instance version is set at creation and on code update.
func (in *Instance) Version() (runtime.Version, error) {
if in.Context.Version != nil {
return *in.Context.Version, nil
}
err := in.version()
if err != nil {
return runtime.Version{}, err
}
return *in.Context.Version, nil
}
// version calls runtime function Core_Version and returns the
// decoded version structure.
func (in *Instance) version() error { //skipcq: RVV-B0001
res, err := in.Exec(runtime.CoreVersion, []byte{})
if err != nil {
return err
}
version, err := runtime.DecodeVersion(res)
if err != nil {
return fmt.Errorf("decoding version: %w", err)
}
in.Context.Version = &version
return nil
}
// ValidateTransaction runs the extrinsic through the runtime function
// TaggedTransactionQueue_validate_transaction and returns *transaction.Validity. The error can
// be a VDT of either transaction.InvalidTransaction or transaction.UnknownTransaction, or can represent
// a normal error i.e. unmarshalling error
func (in *Instance) ValidateTransaction(e types.Extrinsic) (*transaction.Validity, error) {
ret, err := in.Exec(runtime.TaggedTransactionQueueValidateTransaction, e)
if err != nil {
return nil, err
}
return runtime.UnmarshalTransactionValidity(ret)
}
// Metadata calls runtime function Metadata_metadata
func (in *Instance) Metadata() ([]byte, error) {
return in.Exec(runtime.Metadata, []byte{})
}
// BabeConfiguration gets the configuration data for BABE from the runtime
func (in *Instance) BabeConfiguration() (*types.BabeConfiguration, error) {
data, err := in.Exec(runtime.BabeAPIConfiguration, []byte{})
if err != nil {
return nil, err
}
bc := new(types.BabeConfiguration)
err = scale.Unmarshal(data, bc)
if err != nil {
return nil, err
}
return bc, nil
}
// GrandpaAuthorities returns the genesis authorities from the runtime
func (in *Instance) GrandpaAuthorities() ([]types.Authority, error) {
ret, err := in.Exec(runtime.GrandpaAuthorities, []byte{})
if err != nil {
return nil, err
}
var gar []types.GrandpaAuthoritiesRaw
err = scale.Unmarshal(ret, &gar)
if err != nil {
return nil, err
}
return types.GrandpaAuthoritiesRawToAuthorities(gar)
}
// BabeGenerateKeyOwnershipProof returns the babe key ownership proof from the runtime.
func (in *Instance) BabeGenerateKeyOwnershipProof(slot uint64, authorityID [32]byte) (
types.OpaqueKeyOwnershipProof, error) {
// scale encoded slot uint64 + scale encoded array of 32 bytes
const maxBufferLength = 8 + 33
buffer := bytes.NewBuffer(make([]byte, 0, maxBufferLength))
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(slot)
if err != nil {
return nil, fmt.Errorf("encoding slot: %w", err)
}
err = encoder.Encode(authorityID)
if err != nil {
return nil, fmt.Errorf("encoding authority id: %w", err)
}
encodedKeyOwnershipProof, err := in.Exec(runtime.BabeAPIGenerateKeyOwnershipProof, buffer.Bytes())
if err != nil {
return nil, fmt.Errorf("executing %s: %w", runtime.BabeAPIGenerateKeyOwnershipProof, err)
}
var keyOwnershipProof *types.OpaqueKeyOwnershipProof
err = scale.Unmarshal(encodedKeyOwnershipProof, &keyOwnershipProof)
if err != nil {
return nil, fmt.Errorf("scale decoding key ownership proof: %w", err)
}
if keyOwnershipProof == nil {
return nil, nil
}
return *keyOwnershipProof, nil
}
// BabeSubmitReportEquivocationUnsignedExtrinsic reports equivocation report to the runtime.
func (in *Instance) BabeSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.BabeEquivocationProof, keyOwnershipProof types.OpaqueKeyOwnershipProof,
) error {
buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(equivocationProof)
if err != nil {
return fmt.Errorf("encoding equivocation proof: %w", err)
}
err = encoder.Encode(keyOwnershipProof)
if err != nil {
return fmt.Errorf("encoding key ownership proof: %w", err)
}
_, err = in.Exec(runtime.BabeAPISubmitReportEquivocationUnsignedExtrinsic, buffer.Bytes())
return err
}
// InitializeBlock calls runtime API function Core_initialise_block
func (in *Instance) InitializeBlock(header *types.Header) error {
encodedHeader, err := scale.Marshal(*header)
if err != nil {
return fmt.Errorf("cannot encode header: %w", err)
}
_, err = in.Exec(runtime.CoreInitializeBlock, encodedHeader)
return err
}
// InherentExtrinsics calls runtime API function BlockBuilder_inherent_extrinsics
func (in *Instance) InherentExtrinsics(data []byte) ([]byte, error) {
return in.Exec(runtime.BlockBuilderInherentExtrinsics, data)
}
// ApplyExtrinsic calls runtime API function BlockBuilder_apply_extrinsic
func (in *Instance) ApplyExtrinsic(data types.Extrinsic) ([]byte, error) {
return in.Exec(runtime.BlockBuilderApplyExtrinsic, data)
}
// FinalizeBlock calls runtime API function BlockBuilder_finalize_block
func (in *Instance) FinalizeBlock() (*types.Header, error) {
data, err := in.Exec(runtime.BlockBuilderFinalizeBlock, []byte{})
if err != nil {
return nil, err
}
bh := types.NewEmptyHeader()
err = scale.Unmarshal(data, bh)
if err != nil {
return nil, err
}
return bh, nil
}
// ExecuteBlock calls runtime function Core_execute_block
func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) {
// copy block since we're going to modify it
b, err := block.DeepCopy()
if err != nil {
return nil, err
}
b.Header.Digest = types.NewDigest()
// remove seal digest only
for _, d := range block.Header.Digest {
digestValue, err := d.Value()
if err != nil {
return nil, fmt.Errorf("getting digest type value: %w", err)
}
switch digestValue.(type) {
case types.SealDigest:
continue
default:
err = b.Header.Digest.Add(digestValue)
if err != nil {
return nil, err
}
}
}
bdEnc, err := b.Encode()
if err != nil {
return nil, err
}
return in.Exec(runtime.CoreExecuteBlock, bdEnc)
}
// DecodeSessionKeys decodes the given public session keys. Returns a list of raw public keys including their key type.
func (in *Instance) DecodeSessionKeys(enc []byte) ([]byte, error) {
return in.Exec(runtime.DecodeSessionKeys, enc)
}
// PaymentQueryInfo returns information of a given extrinsic
func (in *Instance) PaymentQueryInfo(ext []byte) (*types.RuntimeDispatchInfo, error) {
encLen, err := scale.Marshal(uint32(len(ext)))
if err != nil {
return nil, err
}
resBytes, err := in.Exec(runtime.TransactionPaymentAPIQueryInfo, append(ext, encLen...))
if err != nil {
return nil, err
}
dispatchInfo := new(types.RuntimeDispatchInfo)
if err = scale.Unmarshal(resBytes, dispatchInfo); err != nil {
return nil, err
}
return dispatchInfo, nil
}
// QueryCallInfo returns information of a given extrinsic
func (in *Instance) QueryCallInfo(ext []byte) (*types.RuntimeDispatchInfo, error) {
encLen, err := scale.Marshal(uint32(len(ext)))
if err != nil {
return nil, err
}
resBytes, err := in.Exec(runtime.TransactionPaymentCallAPIQueryCallInfo, append(ext, encLen...))
if err != nil {
return nil, err
}
dispatchInfo := new(types.RuntimeDispatchInfo)
if err = scale.Unmarshal(resBytes, dispatchInfo); err != nil {
return nil, err
}
return dispatchInfo, nil
}
// QueryCallFeeDetails returns call fee details for given call
func (in *Instance) QueryCallFeeDetails(ext []byte) (*types.FeeDetails, error) {
encLen, err := scale.Marshal(uint32(len(ext)))
if err != nil {
return nil, err
}
resBytes, err := in.Exec(runtime.TransactionPaymentCallAPIQueryCallFeeDetails, append(ext, encLen...))
if err != nil {
return nil, err
}
dispatchInfo := new(types.FeeDetails)
if err = scale.Unmarshal(resBytes, dispatchInfo); err != nil {
return nil, err
}
return dispatchInfo, nil
}
// CheckInherents checks inherents in the block verification process.
// TODO: use this in block verification process (#1873)
func (*Instance) CheckInherents() {}
// GrandpaGenerateKeyOwnershipProof returns grandpa key ownership proof from the runtime.
func (in *Instance) GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
types.GrandpaOpaqueKeyOwnershipProof, error) {
const bufferSize = 8 + 32 // authSetID uint64 + ed25519.PublicKeyBytes
buffer := bytes.NewBuffer(make([]byte, 0, bufferSize))
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(authSetID)
if err != nil {
return nil, fmt.Errorf("encoding auth set id: %w", err)
}
err = encoder.Encode(authorityID)
if err != nil {
return nil, fmt.Errorf("encoding authority id: %w", err)
}
encodedOpaqueKeyOwnershipProof, err := in.Exec(runtime.GrandpaGenerateKeyOwnershipProof, buffer.Bytes())
if err != nil {
return nil, err
}
var keyOwnershipProof *types.GrandpaOpaqueKeyOwnershipProof
err = scale.Unmarshal(encodedOpaqueKeyOwnershipProof, &keyOwnershipProof)
if err != nil {
return nil, fmt.Errorf("scale decoding: %w", err)
}
if keyOwnershipProof == nil {
return nil, nil
}
return *keyOwnershipProof, nil
}
// GrandpaSubmitReportEquivocationUnsignedExtrinsic reports an equivocation report to the runtime.
func (in *Instance) GrandpaSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof,
) error {
buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(equivocationProof)
if err != nil {
return fmt.Errorf("encoding equivocation proof: %w", err)
}
err = encoder.Encode(keyOwnershipProof)
if err != nil {
return fmt.Errorf("encoding key ownership proof: %w", err)
}
_, err = in.Exec(runtime.GrandpaSubmitReportEquivocation, buffer.Bytes())
if err != nil {
return err
}
return nil
}
func (*Instance) RandomSeed() {
panic("unimplemented")
}
func (*Instance) OffchainWorker() {
panic("unimplemented")
}
func (*Instance) GenerateSessionKeys() {
panic("unimplemented")
}
// GetCodeHash returns the code of the instance
func (in *Instance) GetCodeHash() common.Hash {
return in.codeHash
}
// NodeStorage to get reference to runtime node service
func (in *Instance) NodeStorage() runtime.NodeStorage {
return in.Context.NodeStorage
}
// NetworkService to get referernce to runtime network service
func (in *Instance) NetworkService() runtime.BasicNetwork {
return in.Context.Network
}
// Keystore to get reference to runtime keystore
func (in *Instance) Keystore() *keystore.GlobalKeystore {
return in.Context.Keystore
}
// Validator returns the context's Validator
func (in *Instance) Validator() bool {
return in.Context.Validator
}
// SetContextStorage sets the runtime's storage.
func (in *Instance) SetContextStorage(s runtime.Storage) {
in.Lock()
defer in.Unlock()
if in.Context.Version == nil {
panic("expected runtime version got nil")
}
runtimeStateVersion, err := trie.ParseVersion(in.Context.Version.StateVersion)
if err != nil {
panic(err)
}
s.SetVersion(runtimeStateVersion)
in.Context.Storage = s
}
// Stop closes the WASM instance, its imports and clears
// the context allocator in a thread-safe way.
func (in *Instance) Stop() {
in.Lock()
defer in.Unlock()
err := in.Runtime.Close(context.Background())
if err != nil {
log.Errorf("runtime failed to close: %v", err)
}
}