-
Notifications
You must be signed in to change notification settings - Fork 246
/
builtin.go
97 lines (82 loc) · 2.62 KB
/
builtin.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
// Copyright (c) 2018 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package builtin
import (
"github.com/pkg/errors"
"github.com/vechain/thor/v2/abi"
"github.com/vechain/thor/v2/builtin/authority"
"github.com/vechain/thor/v2/builtin/energy"
"github.com/vechain/thor/v2/builtin/gen"
"github.com/vechain/thor/v2/builtin/params"
"github.com/vechain/thor/v2/builtin/prototype"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/xenv"
)
// Builtin contracts binding.
var (
Params = ¶msContract{mustLoadContract("Params")}
Authority = &authorityContract{mustLoadContract("Authority")}
Energy = &energyContract{mustLoadContract("Energy")}
Executor = &executorContract{mustLoadContract("Executor")}
Prototype = &prototypeContract{mustLoadContract("Prototype")}
Extension = &extensionContract{
mustLoadContract("Extension"),
mustLoadContract("ExtensionV2"),
}
Measure = mustLoadContract("Measure")
)
type (
paramsContract struct{ *contract }
authorityContract struct{ *contract }
energyContract struct{ *contract }
executorContract struct{ *contract }
prototypeContract struct{ *contract }
extensionContract struct {
*contract
V2 *contract
}
)
func (p *paramsContract) Native(state *state.State) *params.Params {
return params.New(p.Address, state)
}
func (a *authorityContract) Native(state *state.State) *authority.Authority {
return authority.New(a.Address, state)
}
func (e *energyContract) Native(state *state.State, blockTime uint64) *energy.Energy {
return energy.New(e.Address, state, blockTime)
}
func (p *prototypeContract) Native(state *state.State) *prototype.Prototype {
return prototype.New(p.Address, state)
}
func (p *prototypeContract) Events() *abi.ABI {
asset := "compiled/PrototypeEvent.abi"
data := gen.MustAsset(asset)
abi, err := abi.New(data)
if err != nil {
panic(errors.Wrap(err, "load ABI for "+asset))
}
return abi
}
type nativeMethod struct {
abi *abi.Method
run func(env *xenv.Environment) []interface{}
}
type methodKey struct {
thor.Address
abi.MethodID
}
var nativeMethods = make(map[methodKey]*nativeMethod)
// FindNativeCall find native calls.
func FindNativeCall(to thor.Address, input []byte) (*abi.Method, func(*xenv.Environment) []interface{}, bool) {
methodID, err := abi.ExtractMethodID(input)
if err != nil {
return nil, nil, false
}
method := nativeMethods[methodKey{to, methodID}]
if method == nil {
return nil, nil, false
}
return method.abi, method.run, true
}