-
Notifications
You must be signed in to change notification settings - Fork 21
/
trace.go
281 lines (260 loc) · 6.44 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
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
package txemulator
import (
"context"
"fmt"
"time"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/liteapi"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
)
type Tracer struct {
e *Emulator
currentShardAccount map[ton.AccountID]tlb.ShardAccount
blockchain accountGetter
counter int
limit int
softLimit int
}
type TxTree struct {
TX tlb.Transaction
Children []*TxTree
}
type TraceOptions struct {
config string
limit int
softLimit int
blockchain accountGetter
time int64
checkSignature bool
predefinedAccounts map[ton.AccountID]tlb.ShardAccount
}
type accountGetter interface {
GetAccountState(ctx context.Context, a ton.AccountID) (tlb.ShardAccount, error)
GetLibraries(ctx context.Context, libraries []ton.Bits256) (map[ton.Bits256]*boc.Cell, error)
}
func WithConfig(c *boc.Cell) TraceOption {
return func(o *TraceOptions) error {
var err error
o.config, err = c.ToBocBase64()
return err
}
}
func WithConfigBase64(c string) TraceOption {
return func(o *TraceOptions) error {
o.config = c
return nil
}
}
func WithLimit(l int) TraceOption {
return func(o *TraceOptions) error {
o.limit = l
return nil
}
}
func WithSoftLimit(l int) TraceOption {
return func(o *TraceOptions) error {
o.softLimit = l
return nil
}
}
func WithTime(t int64) TraceOption {
return func(o *TraceOptions) error {
o.time = t
return nil
}
}
func WithAccountsMap(m map[ton.AccountID]tlb.ShardAccount) TraceOption {
return func(o *TraceOptions) error {
o.predefinedAccounts = m
return nil
}
}
func WithAccounts(accounts ...tlb.ShardAccount) TraceOption {
return func(o *TraceOptions) error {
for i := range accounts {
a, err := ton.AccountIDFromTlb(accounts[i].Account.Account.Addr)
if err != nil {
return err
}
o.predefinedAccounts[*a] = accounts[i]
}
return nil
}
}
func WithTestnet() TraceOption {
return func(o *TraceOptions) error {
var err error
o.blockchain, err = liteapi.NewClientWithDefaultTestnet()
return err
}
}
func WithAccountsSource(b accountGetter) TraceOption {
return func(o *TraceOptions) error {
o.blockchain = b
return nil
}
}
func WithSignatureCheck() TraceOption {
return func(o *TraceOptions) error {
o.checkSignature = true
return nil
}
}
type TraceOption func(o *TraceOptions) error
func NewTraceBuilder(options ...TraceOption) (*Tracer, error) {
option := TraceOptions{
config: DefaultConfig,
limit: 100,
blockchain: nil,
time: time.Now().Unix(),
checkSignature: false,
predefinedAccounts: make(map[ton.AccountID]tlb.ShardAccount),
}
for _, o := range options {
err := o(&option)
if err != nil {
return nil, err
}
}
if option.blockchain == nil {
return nil, fmt.Errorf("blockchain source is not configured. please use WithAccountsSource")
}
e, err := newEmulatorBase64(option.config, LogTruncated)
if err != nil {
return nil, err
}
err = e.SetUnixtime(uint32(option.time))
if err != nil {
return nil, err
}
err = e.SetIgnoreSignatureCheck(!option.checkSignature)
if err != nil {
return nil, err
}
// TODO: set gas limit, currently, the transaction emulator doesn't support that
return &Tracer{
e: e,
currentShardAccount: option.predefinedAccounts,
blockchain: option.blockchain,
limit: option.limit,
softLimit: option.softLimit,
}, nil
}
func accountCode(account tlb.ShardAccount) *boc.Cell {
if account.Account.SumType == "AccountNone" {
return nil
}
if account.Account.Account.Storage.State.SumType != "AccountActive" {
return nil
}
code := account.Account.Account.Storage.State.AccountActive.StateInit.Code
if !code.Exists {
return nil
}
cell := code.Value.Value
return &cell
}
func msgStateInitCode(msg tlb.Message) *boc.Cell {
if !msg.Init.Exists {
return nil
}
code := msg.Init.Value.Value.Code
if !code.Exists {
return nil
}
cell := code.Value.Value
return &cell
}
func (t *Tracer) Run(ctx context.Context, message tlb.Message) (*TxTree, error) {
if t.counter >= t.limit {
return nil, fmt.Errorf("to many iterations: %v/%v", t.counter, t.limit)
}
if t.softLimit > 0 && t.counter >= t.softLimit {
return nil, nil
}
var a tlb.MsgAddress
switch message.Info.SumType {
case "IntMsgInfo":
a = message.Info.IntMsgInfo.Dest
case "ExtInMsgInfo":
a = message.Info.ExtInMsgInfo.Dest
default:
return nil, fmt.Errorf("can't emulate message with type %v", message.Info.SumType)
}
accountAddr, err := ton.AccountIDFromTlb(a)
if err != nil {
return nil, err
}
if accountAddr == nil {
return nil, fmt.Errorf("destination account is null")
}
state, prs := t.currentShardAccount[*accountAddr]
if !prs {
state, err = t.blockchain.GetAccountState(ctx, *accountAddr)
if err != nil {
return nil, err
}
}
publicLibs := map[ton.Bits256]*boc.Cell{}
for _, code := range []*boc.Cell{accountCode(state), msgStateInitCode(message)} {
if code == nil {
continue
}
hashes, err := FindLibraries(code)
if err != nil {
return nil, err
}
if len(hashes) > 0 {
libs, err := t.blockchain.GetLibraries(ctx, hashes)
if err != nil {
return nil, err
}
for hash, cell := range libs {
publicLibs[hash] = cell
}
}
}
if len(publicLibs) > 0 {
libsBoc, err := LibrariesToBase64(publicLibs)
if err != nil {
return nil, err
}
if err := t.e.setLibs(libsBoc); err != nil {
return nil, err
}
}
result, err := t.e.Emulate(state, message)
if err != nil {
return nil, err
}
if result.Error != nil {
return nil, ErrorWithExitCode{
Message: fmt.Sprintf("iteration: %v, exitCode: %v, Text: %v, ", t.counter, result.Error.ExitCode, result.Error.Text),
ExitCode: result.Error.ExitCode,
Iteration: t.counter,
}
}
if result.Emulation == nil {
return nil, fmt.Errorf("empty emulation result on iteration %v", t.counter)
}
t.counter++
t.currentShardAccount[*accountAddr] = result.Emulation.ShardAccount
tree := &TxTree{
TX: result.Emulation.Transaction,
}
for _, m := range result.Emulation.Transaction.Msgs.OutMsgs.Values() {
if m.Value.Info.SumType == "ExtOutMsgInfo" {
continue
}
child, err := t.Run(ctx, m.Value)
if err != nil {
return tree, err
}
tree.Children = append(tree.Children, child)
}
return tree, err
}
func (t *Tracer) FinalStates() map[ton.AccountID]tlb.ShardAccount {
return t.currentShardAccount
}