-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathexports.go
190 lines (155 loc) · 5 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
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
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package wasmer
import (
"fmt"
"strings"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
)
// ValidateTransaction runs the extrinsic through the 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.Unmarshal(ret[1:], v)
return v, err
}
// 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 := &runtime.VersionData{}
err = version.Decode(res)
// error comes from scale now, so do a string check
if err != nil {
if strings.Contains(err.Error(), "EOF") {
// TODO: kusama seems to use the legacy version format
lversion := &runtime.LegacyVersionData{}
err = lversion.Decode(res)
return lversion, err
}
return nil, err
}
return version, nil
}
// 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)
}
// 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.Types {
switch d.Value().(type) {
case types.SealDigest:
continue
default:
err = b.Header.Digest.Add(d.Value())
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.TransactionPaymentQueryInfo, 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
}
i := new(types.TransactionPaymentQueryInfo)
if err = scale.Unmarshal(resBytes, i); err != nil {
return nil, err
}
return i, nil
}
func (in *Instance) CheckInherents() {} //nolint:revive
func (in *Instance) RandomSeed() {} //nolint:revive
func (in *Instance) OffchainWorker() {} //nolint:revive
func (in *Instance) GenerateSessionKeys() {} //nolint:revive