-
Notifications
You must be signed in to change notification settings - Fork 109
/
exports.go
154 lines (126 loc) · 4.52 KB
/
exports.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
// Copyright 2019 ChainSafe Systems (ON) Corp.
// This file is part of gossamer.
//
// The gossamer library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gossamer library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gossamer library. If not, see <http://www.gnu.org/licenses/>.
package wasmtime
import (
"fmt"
"io"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/transaction"
)
// Metadata calls runtime function Metadata_metadata
func (in *Instance) Metadata() ([]byte, error) {
return in.exec(runtime.Metadata, []byte{})
}
// Version calls runtime function Core_Version
func (in *Instance) Version() (runtime.Version, error) {
res, err := in.exec(runtime.CoreVersion, []byte{})
if err != nil {
return nil, err
}
version := new(runtime.VersionData)
if err == io.EOF {
// TODO: kusama seems to use the legacy version format
lversion := &runtime.LegacyVersionData{}
err = lversion.Decode(res)
return lversion, err
} else if err != nil {
return nil, err
}
return version, nil
}
// BabeConfiguration gets the configuration data for BABE from the runtime
func (in *Instance) BabeConfiguration() (*types.BabeConfiguration, error) {
ret, err := in.exec(runtime.BabeAPIConfiguration, []byte{})
if err != nil {
return nil, err
}
cfg, err := scale.Decode(ret, new(types.BabeConfiguration))
if err != nil {
return nil, err
}
return cfg.(*types.BabeConfiguration), 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
}
adr, err := scale.Decode(ret, []*types.GrandpaAuthoritiesRaw{})
if err != nil {
return nil, err
}
return types.GrandpaAuthoritiesRawToAuthorities(adr.([]*types.GrandpaAuthoritiesRaw))
}
// ValidateTransaction runs the extrinsic through runtime function TaggedTransactionQueue_validate_transaction and returns *Validity
func (in *Instance) ValidateTransaction(e types.Extrinsic) (*transaction.Validity, error) {
ret, err := in.exec(runtime.TaggedTransactionQueueValidateTransaction, e)
if err != nil {
return nil, err
}
if ret[0] != 0 {
return nil, runtime.NewValidateTransactionError(ret)
}
v := transaction.NewValidity(0, [][]byte{{}}, [][]byte{{}}, 0, false)
_, err = scale.Decode(ret[1:], v)
return v, err
}
// InitializeBlock calls runtime API function Core_initialize_block
func (in *Instance) InitializeBlock(header *types.Header) error {
encodedHeader, err := scale.Encode(header)
if err != nil {
return fmt.Errorf("cannot encode header: %s", 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 := new(types.Header)
_, err = scale.Decode(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) {
b := block.DeepCopy()
b.Header.Digest = types.Digest{}
bdEnc, err := b.Encode()
if err != nil {
return nil, err
}
return in.exec(runtime.CoreExecuteBlock, bdEnc)
}
func (in *Instance) CheckInherents() {} //nolint
func (in *Instance) RandomSeed() {} //nolint
func (in *Instance) OffchainWorker() {} //nolint
func (in *Instance) GenerateSessionKeys() {} //nolint