This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
run.go
381 lines (333 loc) · 10.7 KB
/
run.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"blockwatch.cc/tzgo/codec"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/signer"
"blockwatch.cc/tzgo/tezos"
)
const GasSafetyMargin int64 = 100
var (
// for reveal
DefaultRevealLimits = tezos.Limits{
Fee: 1000,
GasLimit: 1000,
}
// for transfers to tz1/2/3
DefaultTransferLimitsEOA = tezos.Limits{
Fee: 1000,
GasLimit: 1420, // 1820 when source is emptied
}
// for transfers to manager.tz
DefaultTransferLimitsKT1 = tezos.Limits{
Fee: 1000,
GasLimit: 2078,
}
// for delegation
DefaultDelegationLimitsEOA = tezos.Limits{
Fee: 1000,
GasLimit: 1000,
}
// for simulating contract calls and other operations
// used when no explicit costs are set
DefaultSimulationLimits = tezos.Limits{
GasLimit: tezos.DefaultParams.HardGasLimitPerOperation,
StorageLimit: tezos.DefaultParams.HardStorageLimitPerOperation,
}
)
type CallOptions struct {
Confirmations int64 // number of confirmations to wait after broadcast
MaxFee int64 // max acceptable fee, optional (default = 0)
TTL int64 // max lifetime for operations in blocks
IgnoreLimits bool // ignore simulated limits and use user-defined limits from op
SimulationBlockID BlockID // custom block id to simulate operation
Signer signer.Signer // optional signer interface to use for signing the transaction
Sender tezos.Address // optional address to sign for (use when signer manages multiple addresses)
Observer *Observer // optional custom block observer for waiting on confirmations
}
var DefaultOptions = CallOptions{
Confirmations: 2,
TTL: tezos.DefaultParams.MaxOperationsTTL - 2,
MaxFee: 1_000_000,
}
type RunOperationRequest struct {
Operation *codec.Op `json:"operation"`
ChainId tezos.ChainIdHash `json:"chain_id"`
}
type RunViewRequest struct {
Contract tezos.Address `json:"contract"`
Entrypoint string `json:"entrypoint"`
Input micheline.Prim `json:"input"`
ChainId tezos.ChainIdHash `json:"chain_id"`
Source tezos.Address `json:"source"`
Payer tezos.Address `json:"payer"`
Gas tezos.N `json:"gas"`
Mode string `json:"unparsing_mode"` // "Readable" | "Optimized"
}
type RunViewResponse struct {
Data micheline.Prim `json:"data"`
}
type RunCodeRequest struct {
ChainId tezos.ChainIdHash `json:"chain_id"`
Script micheline.Code `json:"script"`
Storage micheline.Prim `json:"storage"`
Input micheline.Prim `json:"input"`
Amount tezos.N `json:"amount"`
Balance tezos.N `json:"balance"`
Source *tezos.Address `json:"source,omitempty"`
Payer *tezos.Address `json:"payer,omitempty"`
Gas *tezos.N `json:"gas,omitempty"`
Entrypoint string `json:"entrypoint,omitempty"`
}
// RunCodeResponse -
type RunCodeResponse struct {
Operations []Operation `json:"operations"`
Storage micheline.Prim `json:"storage"`
BigmapDiff micheline.BigmapEvents `json:"big_map_diff,omitempty"`
LazyStorageDiff micheline.LazyEvents `json:"lazy_storage_diff,omitempty"`
}
// Complete ensures an operation is compatible with the current source account's
// on-chain state. Sets branch for TTL control, replay counters, and reveals
// the sender's pubkey if not published yet.
func (c *Client) Complete(ctx context.Context, o *codec.Op, key tezos.Key) error {
needBranch := !o.Branch.IsValid()
needCounter := o.NeedCounter()
mayNeedReveal := len(o.Contents) > 0 && o.Contents[0].Kind() != tezos.OpTypeReveal
if !needBranch && !mayNeedReveal && !needCounter {
return nil
}
// add branch for TTL control
if needBranch {
ofs := o.Params.MaxOperationsTTL - o.TTL
hash, err := c.GetBlockHash(ctx, NewBlockOffset(Head, -ofs))
if err != nil {
return err
}
o.WithBranch(hash)
}
if needCounter || mayNeedReveal {
// fetch current state
state, err := c.GetContractExt(ctx, key.Address(), Head)
if err != nil {
return err
}
// add reveal if necessary
if mayNeedReveal && !state.IsRevealed() {
reveal := &codec.Reveal{
Manager: codec.Manager{
Source: key.Address(),
},
PublicKey: key,
}
reveal.WithLimits(DefaultRevealLimits)
o.WithContentsFront(reveal)
needCounter = true
}
// add counters
if needCounter {
nextCounter := state.Counter + 1
for _, op := range o.Contents {
// skip non-manager ops
if op.GetCounter() < 0 {
continue
}
op.WithCounter(nextCounter)
nextCounter++
}
}
}
return nil
}
// Simulate dry-runs the execution of the operation against the current state
// of a Tezos node in order to estimate execution costs and fees (fee/burn/gas/storage).
func (c *Client) Simulate(ctx context.Context, o *codec.Op, opts *CallOptions) (*Receipt, error) {
sim := &codec.Op{
Branch: o.Branch,
Contents: o.Contents,
Signature: tezos.ZeroSignature,
TTL: o.TTL,
Params: o.Params,
}
if sim.TTL == 0 && opts != nil {
sim.TTL = opts.TTL
}
if !sim.Branch.IsValid() {
ofs := o.Params.MaxOperationsTTL - sim.TTL
hash, err := c.GetBlockHash(ctx, NewBlockOffset(Head, -ofs))
if err != nil {
return nil, err
}
sim.Branch = hash
}
if opts == nil || !opts.IgnoreLimits {
// use default gas/storage limits, set min fee
for _, op := range o.Contents {
l := op.Limits()
if l.GasLimit == 0 {
l.GasLimit = DefaultSimulationLimits.GasLimit / int64(len(o.Contents))
}
if l.StorageLimit == 0 {
l.StorageLimit = DefaultSimulationLimits.StorageLimit / int64(len(o.Contents))
}
op.WithLimits(l)
}
}
blockID := BlockID(Head)
if opts != nil && opts.SimulationBlockID != nil {
blockID = opts.SimulationBlockID
}
req := RunOperationRequest{
Operation: sim,
ChainId: c.ChainId,
}
resp := &Operation{}
if err := c.RunOperation(ctx, blockID, req, resp); err != nil {
return nil, err
}
// TODO: adjust min fee using known gas units before return so that res.Cost()
// reflects the entire cost that Send() will pay
rcpt := &Receipt{
Op: resp,
}
// fail with Tezos error when simulation failed
if !rcpt.IsSuccess() {
return rcpt, rcpt.Error()
}
return rcpt, nil
}
// Validate compares local serializiation against remote RPC serialization of the
// operation and returns an error on mismatch.
func (c *Client) Validate(ctx context.Context, o *codec.Op) error {
op := &codec.Op{
Branch: o.Branch,
Contents: o.Contents,
}
local := op.Bytes()
var remote tezos.HexBytes
if err := c.ForgeOperation(ctx, Head, op, &remote); err != nil {
return err
}
if !bytes.Equal(local, remote.Bytes()) {
return fmt.Errorf("tezos: mismatch between local and remote serialized operations:\n local=%s\n remote=%s",
hex.EncodeToString(local), hex.EncodeToString(remote))
}
return nil
}
// Broadcast sends the signed operation to network and returns the operation hash
// on successful pre-validation.
func (c *Client) Broadcast(ctx context.Context, o *codec.Op) (tezos.OpHash, error) {
return c.BroadcastOperation(ctx, o.Bytes())
}
// Send is a convenience wrapper for sending operations. It auto-completes gas and storage limit,
// ensures minimum fees are set, protects against fee overpayment, signs and broadcasts the final
// operation and waits for a defined number of confirmations.
func (c *Client) Send(ctx context.Context, op *codec.Op, opts *CallOptions) (*Receipt, error) {
if opts == nil {
opts = &DefaultOptions
}
signer := c.Signer
if opts.Signer != nil {
signer = opts.Signer
}
// identify the sender address for signing the message
addr := opts.Sender
if !addr.IsValid() {
addrs, err := signer.ListAddresses(ctx)
if err != nil {
return nil, err
}
addr = addrs[0]
}
key, err := signer.GetKey(ctx, addr)
if err != nil {
return nil, err
}
// set source on all ops
op.WithSource(key.Address())
// auto-complete op with branch/ttl, source counter, reveal
err = c.Complete(ctx, op, key)
if err != nil {
return nil, err
}
// simulate to check tx validity and estimate cost
sim, err := c.Simulate(ctx, op, opts)
if err != nil {
return nil, err
}
// fail with Tezos error when simulation failed
if !sim.IsSuccess() {
return nil, sim.Error()
}
// apply simulated cost as limits to tx list
if !opts.IgnoreLimits {
op.WithLimits(sim.MinLimits(), GasSafetyMargin)
}
// log info about tx costs
logDebug(func() {
costs := sim.Costs()
for i, v := range op.Contents {
verb := "used"
if opts.IgnoreLimits {
verb = "forced"
}
limits := v.Limits()
log.Debugf("OP#%03d: %s gas_used(sim)=%d storage_used(sim)=%d storage_burn(sim)=%d alloc_burn(sim)=%d fee(%s)=%d gas_limit(%s)=%d storage_limit(%s)=%d ",
i, v.Kind(), costs[i].GasUsed, costs[i].StorageUsed, costs[i].StorageBurn, costs[i].AllocationBurn,
verb, limits.Fee, verb, limits.GasLimit, verb, limits.StorageLimit,
)
}
})
// check minFee calc against maxFee if set
if opts.MaxFee > 0 {
if l := op.Limits(); l.Fee > opts.MaxFee {
return nil, fmt.Errorf("estimated cost %d > max %d", l.Fee, opts.MaxFee)
}
}
// sign digest
sig, err := signer.SignOperation(ctx, addr, op)
if err != nil {
return nil, err
}
op.WithSignature(sig)
// broadcast
hash, err := c.Broadcast(ctx, op)
if err != nil {
return nil, err
}
// wait for confirmations
res := NewResult(hash).WithTTL(op.TTL).WithConfirmations(opts.Confirmations)
// use custom observer when provided
mon := c.BlockObserver
if opts.Observer != nil {
mon = opts.Observer
}
// wait for confirmations
res.Listen(mon)
res.WaitContext(ctx)
if err := res.Err(); err != nil {
return nil, err
}
// return receipt
return res.GetReceipt(ctx)
}
// RunCode simulates executing of provided code on the context of a contract at selected block.
func (c *Client) RunCode(ctx context.Context, id BlockID, body, resp interface{}) error {
u := fmt.Sprintf("chains/main/blocks/%s/helpers/scripts/run_code", id)
return c.Post(ctx, u, body, resp)
}
// RunView simulates executing of on on-chain view on the context of a contract at selected block.
func (c *Client) RunView(ctx context.Context, id BlockID, body, resp interface{}) error {
u := fmt.Sprintf("chains/main/blocks/%s/helpers/scripts/run_view", id)
return c.Post(ctx, u, body, resp)
}
// TraceCode simulates executing of code on the context of a contract at selected block and
// returns a full execution trace.
func (c *Client) TraceCode(ctx context.Context, id BlockID, body, resp interface{}) error {
u := fmt.Sprintf("chains/main/blocks/%s/helpers/scripts/trace_code", id)
return c.Post(ctx, u, body, resp)
}