-
Notifications
You must be signed in to change notification settings - Fork 36
/
trace.go
237 lines (215 loc) · 7.52 KB
/
trace.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
package core
import (
"context"
"errors"
"sync"
"github.com/shopspring/decimal"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
"golang.org/x/exp/maps"
)
var (
ErrTraceIsTooLong = errors.New("trace is too long")
)
// TraceID identifies a trace by a hash of the transaction which created it and the logical time of the transaction.
type TraceID struct {
Hash tongo.Bits256
Lt uint64
UTime int64
}
type Trace struct {
// Transaction is slightly modified.
// For example, we have kept only external outbound messages in OutMsgs.
Transaction
AccountInterfaces []abi.ContractInterface
Children []*Trace
// mu protects "additionalInfo" field.
mu sync.RWMutex
// additionalInfo holds information about this trace.
// It is protected by a mutex because we cache traces and set additionalInfo independently of the trace itself.
// so it happens that two different goroutines get a trace from the cache and attempt to set additionalInfo.
additionalInfo *TraceAdditionalInfo
}
// TraceAdditionalInfo holds information about a trace
// but not directly extracted from it or a corresponding transaction.
type TraceAdditionalInfo struct {
// JettonMasters maps jetton wallets to their masters.
JettonMasters map[tongo.AccountID]tongo.AccountID
// NftSaleContract is set, if a transaction's account implements "get_sale_data" method.
NftSaleContract *NftSaleContract
// STONfiPool is set, if a transaction's account implements "get_pool_data" method and abi.StonfiPool interface.
STONfiPool *STONfiPool
// EmulatedTeleitemNFT is set, if this trace is a result of emulation.
// This field is required because when a new NFT is created during emulation,
// there is no way to get it from the blockchain, and we have to store it somewhere.
EmulatedTeleitemNFT *EmulatedTeleitemNFT
}
func (t *Trace) AdditionalInfo() *TraceAdditionalInfo {
t.mu.RLock()
defer t.mu.RUnlock()
return t.additionalInfo
}
func (t *Trace) SetAdditionalInfo(info *TraceAdditionalInfo) {
t.mu.Lock()
defer t.mu.Unlock()
t.additionalInfo = info
}
func (t *Trace) InProgress() bool {
return t.countUncompleted() != 0
}
func (t *Trace) countUncompleted() int {
c := 0
for i := range t.OutMsgs {
if t.OutMsgs[i].Destination != nil {
c++
}
}
for _, st := range t.Children {
c += st.countUncompleted()
}
return c
}
type EmulatedTeleitemNFT struct {
Index decimal.Decimal
CollectionAddress *tongo.AccountID
Verified bool
}
// NftSaleContract holds partial results of get_sale_data method.
type NftSaleContract struct {
NftPrice int64
// Owner of an NFT according to a getgems/basic contract.
Owner *tongo.AccountID
Item tongo.AccountID
}
// STONfiPool holds partial results of execution of STONfi's "get_pool_data" method.
type STONfiPool struct {
Token0 tongo.AccountID
Token1 tongo.AccountID
}
// InformationSource provides methods to construct TraceAdditionalInfo.
type InformationSource interface {
JettonMastersForWallets(ctx context.Context, wallets []tongo.AccountID) (map[tongo.AccountID]tongo.AccountID, error)
NftSaleContracts(ctx context.Context, contracts []tongo.AccountID) (map[tongo.AccountID]NftSaleContract, error)
STONfiPools(ctx context.Context, poolIDs []tongo.AccountID) (map[tongo.AccountID]STONfiPool, error)
}
func isDestinationJettonWallet(inMsg *Message) bool {
if inMsg == nil || inMsg.DecodedBody == nil {
return false
}
return (inMsg.DecodedBody.Operation == abi.JettonTransferMsgOp ||
inMsg.DecodedBody.Operation == abi.JettonInternalTransferMsgOp ||
inMsg.DecodedBody.Operation == abi.JettonBurnMsgOp) && inMsg.Destination != nil
}
func hasInterface(interfacesList []abi.ContractInterface, name abi.ContractInterface) bool {
for _, iface := range interfacesList {
if iface.Implements(name) {
return true
}
}
return false
}
func Visit(trace *Trace, fn func(trace *Trace)) {
fn(trace)
for _, child := range trace.Children {
Visit(child, fn)
}
}
// DistinctAccounts returns a list of accounts that are involved in the given trace.
func DistinctAccounts(trace *Trace) []tongo.AccountID {
accounts := make(map[tongo.AccountID]struct{})
Visit(trace, func(trace *Trace) {
accounts[trace.Account] = struct{}{}
})
return maps.Keys(accounts)
}
// CollectAdditionalInfo goes over the whole trace
// and populates trace.TraceAdditionalInfo based on information
// provided by InformationSource.
func CollectAdditionalInfo(ctx context.Context, infoSource InformationSource, trace *Trace) error {
if infoSource == nil {
return nil
}
var jettonWallets []tongo.AccountID
var saleContracts []tongo.AccountID
var stonfiPoolIDs []tongo.AccountID
Visit(trace, func(trace *Trace) {
// when we emulate a trace,
// we construct "trace.AdditionalInfo" in emulatedTreeToTrace for all accounts the trace touches.
// moreover, some accounts change their states and some of them are not exist in the blockchain,
// so we must not inspect them again.
if trace.AdditionalInfo() != nil {
return
}
if isDestinationJettonWallet(trace.InMsg) {
jettonWallets = append(jettonWallets, *trace.InMsg.Destination)
}
if hasInterface(trace.AccountInterfaces, abi.NftSaleV1) ||
hasInterface(trace.AccountInterfaces, abi.NftSaleV2) ||
hasInterface(trace.AccountInterfaces, abi.NftAuctionV1) {
saleContracts = append(saleContracts, trace.Account)
}
if hasInterface(trace.AccountInterfaces, abi.StonfiPool) {
stonfiPoolIDs = append(stonfiPoolIDs, trace.Account)
}
})
stonfiPools, err := infoSource.STONfiPools(ctx, stonfiPoolIDs)
if err != nil {
return err
}
for _, pool := range stonfiPools {
jettonWallets = append(jettonWallets, pool.Token0)
jettonWallets = append(jettonWallets, pool.Token1)
}
masters, err := infoSource.JettonMastersForWallets(ctx, jettonWallets)
if err != nil {
return err
}
basicNftSales, err := infoSource.NftSaleContracts(ctx, saleContracts)
if err != nil {
return err
}
Visit(trace, func(trace *Trace) {
// when we emulate a trace,
// we construct "trace.AdditionalInfo" in emulatedTreeToTrace for all accounts the trace touches.
// moreover, some accounts change their states and some of them are not exist in the blockchain,
// so we must not inspect them again.
if trace.AdditionalInfo() != nil {
return
}
additionalInfo := &TraceAdditionalInfo{}
if isDestinationJettonWallet(trace.InMsg) {
if master, ok := masters[*trace.InMsg.Destination]; ok {
additionalInfo.SetJettonMaster(*trace.InMsg.Destination, master)
}
}
if hasInterface(trace.AccountInterfaces, abi.NftSaleV1) ||
hasInterface(trace.AccountInterfaces, abi.NftSaleV2) ||
hasInterface(trace.AccountInterfaces, abi.NftAuctionV1) {
if sale, ok := basicNftSales[trace.Account]; ok {
additionalInfo.NftSaleContract = &sale
}
}
if hasInterface(trace.AccountInterfaces, abi.StonfiPool) {
if pool, ok := stonfiPools[trace.Account]; ok {
additionalInfo.STONfiPool = &pool
additionalInfo.SetJettonMaster(pool.Token0, masters[pool.Token0])
additionalInfo.SetJettonMaster(pool.Token1, masters[pool.Token1])
}
}
trace.SetAdditionalInfo(additionalInfo)
})
return nil
}
func (info *TraceAdditionalInfo) JettonMaster(jettonWallet tongo.AccountID) (tongo.AccountID, bool) {
if info.JettonMasters == nil {
return tongo.AccountID{}, false
}
master, ok := info.JettonMasters[jettonWallet]
return master, ok
}
func (info *TraceAdditionalInfo) SetJettonMaster(jettonWallet tongo.AccountID, jettonMaster tongo.AccountID) {
if info.JettonMasters == nil {
info.JettonMasters = make(map[tongo.AccountID]tongo.AccountID)
}
info.JettonMasters[jettonWallet] = jettonMaster
}