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
/
receipt.go
212 lines (188 loc) · 4.64 KB
/
receipt.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"context"
"errors"
"sync"
"blockwatch.cc/tzgo/tezos"
)
var (
Canceled = errors.New("operation confirm canceled")
TTLExceeded = errors.New("operation ttl exceeded")
)
type Receipt struct {
Block tezos.BlockHash
List int
Pos int
Op *Operation
}
// TotalCosts returns the sum of costs across all batched and internal operations.
func (r *Receipt) TotalCosts() tezos.Costs {
if r.Op != nil {
return r.Op.TotalCosts()
}
return tezos.Costs{}
}
// Costs returns a list of individual costs for all batched operations.
func (r *Receipt) Costs() []tezos.Costs {
if r.Op != nil {
return r.Op.Costs()
}
return nil
}
// IsSuccess returns true when all operations in this group have been applied successfully.
func (r *Receipt) IsSuccess() bool {
for _, v := range r.Op.Contents {
if v.Result().Status != tezos.OpStatusApplied {
return false
}
}
return true
}
// Error returns the first execution error found in this operation group or one of
// its internal results that is of status failed. This helper only exports the error
// as GenericError. To access error details or all errors, visit
// r.Op.Contents[].OperationResult.Errors[] and
// r.Op.Contents[].Metadata.InternalResults.Result.Errors[]
func (r *Receipt) Error() error {
for _, v := range r.Op.Contents {
res := v.Result()
if len(res.Errors) > 0 && res.Status != tezos.OpStatusApplied {
return res.Errors[len(res.Errors)-1].GenericError
}
for _, vv := range v.Meta().InternalResults {
res := vv.Result
if len(res.Errors) > 0 && res.Status != tezos.OpStatusApplied {
return res.Errors[len(res.Errors)-1].GenericError
}
}
}
return nil
}
// MinLimits returns a list of individual operation costs mapped to limits for use
// in simulation results. Fee is reset to zero to prevent higher simulation fee from
// spilling over into real fees paid.
func (r *Receipt) MinLimits() []tezos.Limits {
lims := make([]tezos.Limits, len(r.Op.Contents))
for i, v := range r.Op.Costs() {
lims[i].Fee = 0
lims[i].GasLimit = v.GasUsed
lims[i].StorageLimit = v.StorageUsed + v.AllocationBurn/tezos.DefaultParams.CostPerByte
}
return lims
}
type Result struct {
oh tezos.OpHash // the operation hash to watch
block tezos.BlockHash // the block hash where op was included
list int // the list where op was included
pos int // the list position where op was included
err error // saves any error
ttl int64 // number of blocks before wait fails
wait int64 // number of confirmations required
blocks int64 // number of confirmation blocks seen
obs *Observer // blockchain observer
subId int // monitor subscription id
done chan struct{} // channel used to signal completion
once sync.Once // ensures only one completion state exists
}
func NewResult(oh tezos.OpHash) *Result {
return &Result{
oh: oh,
wait: 1,
done: make(chan struct{}),
}
}
func (r *Result) Hash() tezos.OpHash {
return r.oh
}
func (r *Result) Listen(o *Observer) {
if o != nil {
r.obs = o
r.subId = r.obs.Subscribe(r.oh, r.callback)
}
}
func (r *Result) Cancel() {
r.once.Do(func() {
if r.subId > 0 {
r.obs.Unsubscribe(r.subId)
r.err = Canceled
r.subId = 0
}
close(r.done)
})
}
func (r *Result) WithConfirmations(n int64) *Result {
r.wait = n
return r
}
func (r *Result) WithTTL(n int64) *Result {
r.ttl = n
return r
}
func (r *Result) Confirmations() int64 {
return r.blocks
}
func (r *Result) Done() <-chan struct{} {
return r.done
}
func (r *Result) Err() error {
return r.err
}
func (r *Result) GetReceipt(ctx context.Context) (*Receipt, error) {
if r.err != nil {
return nil, r.err
}
rec := &Receipt{
Block: r.block,
Pos: r.pos,
List: r.list,
}
if r.obs != nil {
op, err := r.obs.c.GetBlockOperation(ctx, r.block, r.list, r.pos)
if err != nil {
return rec, err
}
rec.Op = op
}
return rec, nil
}
func (r *Result) Wait() {
<-r.done
}
func (r *Result) WaitContext(ctx context.Context) {
select {
case <-ctx.Done():
case <-r.done:
}
}
func (r *Result) callback(block tezos.BlockHash, list, pos int, force bool) bool {
if force {
r.block = block.Clone()
r.list = list
r.pos = pos
return false
}
if !r.block.IsValid() {
r.block = block.Clone()
r.list = list
r.pos = pos
}
r.blocks++
if r.ttl > 0 && r.blocks >= r.ttl {
r.once.Do(func() {
r.err = TTLExceeded
r.subId = 0
close(r.done)
})
return true
}
if r.blocks >= r.wait {
r.once.Do(func() {
r.subId = 0
close(r.done)
})
return true
}
return false
}