-
Notifications
You must be signed in to change notification settings - Fork 19
/
contracts.go
362 lines (307 loc) · 12.8 KB
/
contracts.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
package contracts
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/golang/snappy"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
)
var (
// Callable methods.
methodUpload = types.NewMethodName("contracts.Upload", Upload{})
methodInstantiate = types.NewMethodName("contracts.Instantiate", Instantiate{})
methodCall = types.NewMethodName("contracts.Call", Call{})
methodUpgrade = types.NewMethodName("contracts.Upgrade", Upgrade{})
methodChangeUpgradePolicy = types.NewMethodName("contracts.ChangeUpgradePolicy", ChangeUpgradePolicy{})
// Queries.
methodCode = types.NewMethodName("contracts.Code", CodeQuery{})
methodCodeStorage = types.NewMethodName("contracts.CodeStorage", CodeStorageQuery{})
methodInstance = types.NewMethodName("contracts.Instance", InstanceQuery{})
methodInstanceStorage = types.NewMethodName("contracts.InstanceStorage", InstanceStorageQuery{})
methodInstanceRawStorage = types.NewMethodName("contracts.InstanceRawStorage", InstanceRawStorageQuery{})
methodPublicKey = types.NewMethodName("contracts.PublicKey", PublicKeyQuery{})
methodCustom = types.NewMethodName("contracts.Custom", CustomQuery{})
methodParameters = types.NewMethodName("contracts.Parameters", nil)
)
// V1 is the v1 contracts module interface.
type V1 interface {
client.EventDecoder
// Upload generates a contracts.Upload transaction.
Upload(abi ABI, instantiatePolicy Policy, code []byte) *client.TransactionBuilder
// InstantiateRaw generates a contracts.Instantiate transaction.
//
// This method allows specifying an arbitrary data payload. If the contract is using the Oasis
// ABI you can use the regular Call method as convenience since it will perform the CBOR
// serialization automatically.
InstantiateRaw(codeID CodeID, upgradesPolicy Policy, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder
// Instantiate generates a contracts.Instantiate transaction.
//
// This method will encode the specified data using CBOR as defined by the Oasis ABI.
Instantiate(codeID CodeID, upgradesPolicy Policy, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder
// CallRaw generates a contracts.Call transaction.
//
// This method allows specifying an arbitrary data payload. If the contract is using the Oasis
// ABI you can use the regular Call method as convenience since it will perform the CBOR
// serialization automatically.
CallRaw(id InstanceID, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder
// Call generates a contracts.Call transaction.
//
// This method will encode the specified data using CBOR as defined by the Oasis ABI.
Call(id InstanceID, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder
// UpgradeRaw generates a contracts.Upgrade transaction.
//
// This method allows specifying an arbitrary data payload. If the contract is using the Oasis
// ABI you can use the regular Upgrade method as convenience since it will perform the CBOR
// serialization automatically.
UpgradeRaw(id InstanceID, codeID CodeID, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder
// Upgrade generates a contracts.Upgrade transaction.
//
// This method will encode the specified data using CBOR as defined by the Oasis ABI.
Upgrade(id InstanceID, codeID CodeID, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder
// ChangeUpgradePolicy generates a contracts.ChangeUpgradePolicy transaction.
ChangeUpgradePolicy(id InstanceID, upgradesPolicy Policy) *client.TransactionBuilder
// Code queries the given code information.
Code(ctx context.Context, round uint64, id CodeID) (*Code, error)
// CodeStorage queries the given code's storage.
CodeStorage(ctx context.Context, round uint64, id CodeID) (*CodeStorageQueryResult, error)
// Instance queries the given instance information.
Instance(ctx context.Context, round uint64, id InstanceID) (*Instance, error)
// InstanceStorage queries the given instance's public storage.
InstanceStorage(ctx context.Context, round uint64, id InstanceID, key []byte) (*InstanceStorageQueryResult, error)
// InstanceRawStorage returns the key-value pairs of contract instance storage.
InstanceRawStorage(ctx context.Context, round uint64, id InstanceID, kind StoreKind, limit, offset uint64) (*InstanceRawStorageQueryResult, error)
// PublicKey queries the given instance's public key.
PublicKey(ctx context.Context, round uint64, id InstanceID, kind PublicKeyKind) (*PublicKeyQueryResult, error)
// CustomRaw queries the given contract for a custom query.
//
// This method allows specifying an arbitrary data payload. If the contract is using the Oasis
// ABI you can use the regular Custom method as convenience since it will perform the CBOR
// serialization automatically.
CustomRaw(ctx context.Context, round uint64, id InstanceID, data []byte) ([]byte, error)
// Custom queries the given contract for a custom query.
//
// This method will encode the specified data using CBOR as defined by the Oasis ABI.
Custom(ctx context.Context, round uint64, id InstanceID, data, rsp interface{}) error
// Parameters queries the EVM module parameters.
Parameters(ctx context.Context, round uint64) (*Parameters, error)
// GetEvents returns events emitted by the contract at the provided round.
GetEvents(ctx context.Context, instanceID InstanceID, round uint64) ([]*Event, error)
}
type v1 struct {
rc client.RuntimeClient
}
// Implements V1.
func (a *v1) Upload(abi ABI, instantiatePolicy Policy, code []byte) *client.TransactionBuilder {
return client.NewTransactionBuilder(a.rc, methodUpload, &Upload{
ABI: abi,
InstantiatePolicy: instantiatePolicy,
Code: CompressCode(code),
})
}
// Implements V1.
func (a *v1) InstantiateRaw(codeID CodeID, upgradesPolicy Policy, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder {
return client.NewTransactionBuilder(a.rc, methodInstantiate, &Instantiate{
CodeID: codeID,
UpgradesPolicy: upgradesPolicy,
Data: data,
Tokens: tokens,
})
}
// Implements V1.
func (a *v1) Instantiate(codeID CodeID, upgradesPolicy Policy, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder {
return a.InstantiateRaw(codeID, upgradesPolicy, cbor.Marshal(data), tokens)
}
// Implements V1.
func (a *v1) CallRaw(id InstanceID, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder {
return client.NewTransactionBuilder(a.rc, methodCall, &Call{
ID: id,
Data: data,
Tokens: tokens,
})
}
// Implements V1.
func (a *v1) Call(id InstanceID, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder {
return a.CallRaw(id, cbor.Marshal(data), tokens)
}
// Implements V1.
func (a *v1) UpgradeRaw(id InstanceID, codeID CodeID, data []byte, tokens []types.BaseUnits) *client.TransactionBuilder {
return client.NewTransactionBuilder(a.rc, methodUpgrade, &Upgrade{
ID: id,
CodeID: codeID,
Data: data,
Tokens: tokens,
})
}
// Implements V1.
func (a *v1) Upgrade(id InstanceID, codeID CodeID, data interface{}, tokens []types.BaseUnits) *client.TransactionBuilder {
return a.UpgradeRaw(id, codeID, cbor.Marshal(data), tokens)
}
// Implements V1.
func (a *v1) ChangeUpgradePolicy(id InstanceID, upgradesPolicy Policy) *client.TransactionBuilder {
return client.NewTransactionBuilder(a.rc, methodChangeUpgradePolicy, &ChangeUpgradePolicy{
ID: id,
UpgradesPolicy: upgradesPolicy,
})
}
// Implements V1.
func (a *v1) Code(ctx context.Context, round uint64, id CodeID) (*Code, error) {
var code Code
err := a.rc.Query(ctx, round, methodCode, &CodeQuery{ID: id}, &code)
if err != nil {
return nil, err
}
return &code, nil
}
// Implements V1.
func (a *v1) CodeStorage(ctx context.Context, round uint64, id CodeID) (*CodeStorageQueryResult, error) {
var rsp CodeStorageQueryResult
err := a.rc.Query(ctx, round, methodCodeStorage, &CodeStorageQuery{ID: id}, &rsp)
if err != nil {
return nil, err
}
return &rsp, nil
}
// Implements V1.
func (a *v1) Instance(ctx context.Context, round uint64, id InstanceID) (*Instance, error) {
var instance Instance
err := a.rc.Query(ctx, round, methodInstance, &InstanceQuery{ID: id}, &instance)
if err != nil {
return nil, err
}
return &instance, nil
}
// Implements V1.
func (a *v1) InstanceStorage(ctx context.Context, round uint64, id InstanceID, key []byte) (*InstanceStorageQueryResult, error) {
var rsp InstanceStorageQueryResult
err := a.rc.Query(ctx, round, methodInstanceStorage, &InstanceStorageQuery{ID: id, Key: key}, &rsp)
if err != nil {
return nil, err
}
return &rsp, nil
}
// Implements V1.
func (a *v1) InstanceRawStorage(ctx context.Context, round uint64, id InstanceID, storeKind StoreKind, limit uint64, offset uint64) (*InstanceRawStorageQueryResult, error) {
var rsp InstanceRawStorageQueryResult
err := a.rc.Query(ctx, round, methodInstanceRawStorage, &InstanceRawStorageQuery{ID: id, StoreKind: storeKind, Limit: limit, Offset: offset}, &rsp)
if err != nil {
return nil, err
}
return &rsp, nil
}
// Implements V1.
func (a *v1) PublicKey(ctx context.Context, round uint64, id InstanceID, kind PublicKeyKind) (*PublicKeyQueryResult, error) {
var pk PublicKeyQueryResult
err := a.rc.Query(ctx, round, methodPublicKey, &PublicKeyQuery{ID: id, Kind: kind}, &pk)
if err != nil {
return nil, err
}
return &pk, nil
}
// Implements V1.
func (a *v1) CustomRaw(ctx context.Context, round uint64, id InstanceID, data []byte) ([]byte, error) {
var rsp CustomQueryResult
err := a.rc.Query(ctx, round, methodCustom, &CustomQuery{ID: id, Data: data}, &rsp)
if err != nil {
return nil, err
}
return []byte(rsp), nil
}
// Implements V1.
func (a *v1) Custom(ctx context.Context, round uint64, id InstanceID, data, rsp interface{}) error {
raw, err := a.CustomRaw(ctx, round, id, cbor.Marshal(data))
if err != nil {
return err
}
if err = cbor.Unmarshal(raw, rsp); err != nil {
return fmt.Errorf("failed to unmarshal response from contract: %w", err)
}
return nil
}
// Implements V1.
func (a *v1) Parameters(ctx context.Context, round uint64) (*Parameters, error) {
var params Parameters
err := a.rc.Query(ctx, round, methodParameters, nil, ¶ms)
if err != nil {
return nil, err
}
return ¶ms, nil
}
// Implements V1.
func (a *v1) GetEvents(ctx context.Context, instanceID InstanceID, round uint64) ([]*Event, error) {
rawEvs, err := a.rc.GetEventsRaw(ctx, round)
if err != nil {
return nil, err
}
evs := make([]*Event, 0)
for _, rawEv := range rawEvs {
ev, err := a.DecodeEvent(rawEv)
if err != nil {
return nil, err
}
for _, e := range ev {
if e.(*Event).ID != instanceID {
continue
}
evs = append(evs, e.(*Event))
}
}
return evs, nil
}
// Implements client.EventDecoder.
func (a *v1) DecodeEvent(event *types.Event) ([]client.DecodedEvent, error) {
return DecodeEvent(event)
}
// DecodeEvent decodes a contract event.
func DecodeEvent(event *types.Event) ([]client.DecodedEvent, error) {
// "contracts" or "contracts.<...>".
if event.Module != ModuleName && !strings.HasPrefix(event.Module, ModuleName+".") {
return nil, nil
}
var evs []*Event
if err := cbor.Unmarshal(event.Value, &evs); err != nil {
return nil, fmt.Errorf("decode contract event value: %w", err)
}
events := make([]client.DecodedEvent, len(evs))
for i, ev := range evs {
events[i] = ev
}
return events, nil
}
// NewV1 generates a V1 client helper for the contracts module.
func NewV1(rc client.RuntimeClient) V1 {
return &v1{rc: rc}
}
// NewUploadTx generates a new contracts.Upload transaction.
func NewUploadTx(fee *types.Fee, body *Upload) *types.Transaction {
return types.NewTransaction(fee, methodUpload, body)
}
// NewInstantiateTx generates a new contracts.Instantiate transaction.
func NewInstantiateTx(fee *types.Fee, body *Instantiate) *types.Transaction {
return types.NewTransaction(fee, methodInstantiate, body)
}
// NewCallTx generates a new contracts.Call transaction.
func NewCallTx(fee *types.Fee, body *Call) *types.Transaction {
return types.NewTransaction(fee, methodCall, body)
}
// NewUpgradeTx generates a new contracts.Upgrade transaction.
func NewUpgradeTx(fee *types.Fee, body *Upgrade) *types.Transaction {
return types.NewTransaction(fee, methodUpgrade, body)
}
// NewChangeUpgradePolicyTx generates a new contracts.ChangeUpgradePolicy transaction.
func NewChangeUpgradePolicyTx(fee *types.Fee, body *ChangeUpgradePolicy) *types.Transaction {
return types.NewTransaction(fee, methodChangeUpgradePolicy, body)
}
// CompressCode performs code compression using Snappy.
func CompressCode(code []byte) []byte {
var compressedCode bytes.Buffer
encoder := snappy.NewBufferedWriter(&compressedCode)
_, err := encoder.Write(code)
if err != nil {
panic(err)
}
encoder.Close()
return compressedCode.Bytes()
}