-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathinspect.go
282 lines (255 loc) · 7.23 KB
/
inspect.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
package abi
import (
"context"
"fmt"
"github.com/tonkeeper/tongo/boc"
codePkg "github.com/tonkeeper/tongo/code"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"github.com/tonkeeper/tongo/utils"
)
type MethodInvocation struct {
Result any
TypeHint string
}
type ContractDescription struct {
// Interfaces is a list of interfaces implemented by a contract.
ContractInterfaces []ContractInterface
GetMethods []MethodInvocation
MethodsInspected int
}
func (d ContractDescription) hasAllResults(results []string) bool {
for _, r := range results {
found := false
for _, m := range d.GetMethods {
if m.TypeHint == r {
found = true
break
}
}
if !found {
return false
}
}
return true
}
type libResolver interface {
GetLibraries(ctx context.Context, libraryList []ton.Bits256) (map[ton.Bits256]*boc.Cell, error)
}
type contractInspector struct {
knownMethods []MethodDescription
knownInterfaces []InterfaceDescription
scanAllMethods bool
libResolver libResolver
}
type InspectorOptions struct {
additionalMethods []MethodDescription
knownInterfaces []InterfaceDescription
scanAllMethods bool
libResolver libResolver
}
type ContractInterface uint32
func (c ContractInterface) Implements(other ContractInterface) bool {
if c == other {
return true
}
return c.recursiveImplements(other)
}
type InvokeFn func(ctx context.Context, executor Executor, reqAccountID ton.AccountID) (string, any, error)
// MethodDescription describes a particular method and provides a function to execute it.
type MethodDescription struct {
Name string
// InvokeFn executes this method on a contract and returns parsed execution results.
InvokeFn InvokeFn
}
type knownContractDescription struct {
contractInterfaces []ContractInterface
getMethods []InvokeFn
}
type InterfaceDescription struct {
Name ContractInterface
Results []string
}
type InspectorOption func(o *InspectorOptions)
func InspectWithAdditionalMethods(list []MethodDescription) InspectorOption {
return func(o *InspectorOptions) {
o.additionalMethods = list
}
}
func InspectWithAdditionalInterfaces(list []InterfaceDescription) InspectorOption {
return func(o *InspectorOptions) {
o.knownInterfaces = list
}
}
func InspectWithAllMethods() InspectorOption {
return func(o *InspectorOptions) {
o.scanAllMethods = true
}
}
func InspectWithLibraryResolver(resolver libResolver) InspectorOption {
return func(o *InspectorOptions) {
o.libResolver = resolver
}
}
func NewContractInspector(opts ...InspectorOption) *contractInspector {
options := &InspectorOptions{}
for _, o := range opts {
o(options)
}
return &contractInspector{
knownMethods: append(methodInvocationOrder, options.additionalMethods...),
knownInterfaces: append(contractInterfacesOrder, options.knownInterfaces...),
scanAllMethods: options.scanAllMethods,
libResolver: options.libResolver,
}
}
// InspectContract tries to execute all known get method on a contract and returns a summary.
// The contract is not provided directly,
// instead, it is expected that "executor" has been created with knowledge of the contract's code and data.
// Executor must be ready to execute multiple different methods and must not rely on a particular order of execution.
func (ci contractInspector) InspectContract(ctx context.Context, code []byte, executor Executor, reqAccountID ton.AccountID) (*ContractDescription, error) {
if len(code) == 0 {
return &ContractDescription{}, nil
}
desc := ContractDescription{}
info, err := getCodeInfo(ctx, code, ci.libResolver)
if err != nil {
return nil, err
}
if contract, ok := knownContracts[info.hash]; ok { //for known contracts we just need to run get methods
desc.ContractInterfaces = contract.contractInterfaces
for _, method := range contract.getMethods {
desc.MethodsInspected += 1
typeHint, result, err := method(ctx, executor, reqAccountID)
if err != nil {
return &desc, nil
}
desc.GetMethods = append(desc.GetMethods, MethodInvocation{
Result: result,
TypeHint: typeHint,
})
}
return &desc, nil
}
for _, method := range ci.knownMethods {
// let's avoid running get methods that we know don't exist
if !ci.scanAllMethods && !info.isMethodOkToTry(method.Name) {
continue
}
desc.MethodsInspected += 1
typeHint, result, err := method.InvokeFn(ctx, executor, reqAccountID)
if err != nil {
continue
}
desc.GetMethods = append(desc.GetMethods, MethodInvocation{
Result: result,
TypeHint: typeHint,
})
}
for _, iface := range ci.knownInterfaces {
if desc.hasAllResults(iface.Results) {
desc.ContractInterfaces = append(desc.ContractInterfaces, iface.Name)
}
}
return &desc, nil
}
type codeInfo struct {
hash ton.Bits256
methods map[int64]struct{}
}
func (i codeInfo) isMethodOkToTry(name string) bool {
if i.methods == nil {
return false
}
methodID := utils.MethodIdFromName(name)
_, ok := i.methods[int64(methodID)]
return ok
}
func getCodeInfo(ctx context.Context, code []byte, resolver libResolver) (*codeInfo, error) {
cells, err := boc.DeserializeBoc(code)
if err != nil {
return nil, err
}
if len(cells) == 0 {
return nil, fmt.Errorf("failed to find a root cell")
}
root := cells[0]
libHashes, err := codePkg.FindLibraries(root)
if err != nil {
return nil, fmt.Errorf("failed while looking for libraries inside cell: %w", err)
}
var libs map[ton.Bits256]*boc.Cell
if len(libHashes) > 0 {
if resolver == nil {
return nil, fmt.Errorf("found libraries in cell, but no resolver provided")
}
libs, err = resolver.GetLibraries(ctx, libHashes)
if err != nil {
return nil, fmt.Errorf("failed to fetch libraries: %w", err)
}
}
h, err := root.Hash256()
if err != nil {
return nil, err
}
root.ResetCounters()
if root.IsLibrary() {
hash, err := root.GetLibraryHash()
if err != nil {
return nil, err
}
cell, ok := libs[ton.Bits256(hash)]
if !ok {
return nil, fmt.Errorf("library not found")
}
root = cell
}
c, err := root.NextRef()
if err != nil {
// we are OK, if there is no information about get methods
return &codeInfo{hash: h}, nil
}
if c.IsLibrary() {
hash, err := c.GetLibraryHash()
if err != nil {
return nil, err
}
cell, ok := libs[ton.Bits256(hash)]
if !ok {
return nil, fmt.Errorf("library not found")
}
c = cell
}
type GetMethods struct {
Hashmap tlb.Hashmap[tlb.Uint19, boc.Cell]
}
var getMethods GetMethods
decoder := tlb.NewDecoder().WithLibraryResolver(func(hash tlb.Bits256) (*boc.Cell, error) {
if resolver == nil {
return nil, fmt.Errorf("failed to fetch library: no resolver provided")
}
cell, ok := libs[ton.Bits256(hash)]
if ok {
return cell, nil
}
localLibs, err := resolver.GetLibraries(ctx, []ton.Bits256{ton.Bits256(hash)})
if err != nil {
return nil, err
}
if len(localLibs) == 0 {
return nil, fmt.Errorf("library not found")
}
return localLibs[ton.Bits256(hash)], nil
})
err = decoder.Unmarshal(c, &getMethods)
if err != nil {
// we are OK, if there is no information about get methods
return &codeInfo{hash: h}, nil
}
keys := getMethods.Hashmap.Keys()
methods := make(map[int64]struct{}, len(keys))
for _, key := range keys {
methods[int64(key)] = struct{}{}
}
return &codeInfo{hash: h, methods: methods}, nil
}