This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
natives.go
143 lines (123 loc) · 3.42 KB
/
natives.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
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package native
import (
"fmt"
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/engine"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/permission"
)
type Natives struct {
callableByAddress map[crypto.Address]engine.Native
callableByName map[string]engine.Native
logger *logging.Logger
}
func New() *Natives {
return &Natives{
callableByAddress: make(map[crypto.Address]engine.Native),
callableByName: make(map[string]engine.Native),
logger: logging.NewNoopLogger(),
}
}
func Merge(nss ...*Natives) (*Natives, error) {
n := New()
for _, ns := range nss {
for _, contract := range ns.callableByName {
err := n.register(contract)
if err != nil {
return nil, err
}
}
}
return n, nil
}
func (ns *Natives) WithLogger(logger *logging.Logger) *Natives {
ns.logger = logger
return ns
}
func (ns *Natives) Dispatch(acc *acm.Account) engine.Callable {
return ns.GetByAddress(acc.Address)
}
func (ns *Natives) SetExternals(externals engine.Dispatcher) {
for _, c := range ns.callableByAddress {
c.SetExternals(externals)
}
}
func (ns *Natives) Callables() []engine.Callable {
callables := make([]engine.Callable, 0, len(ns.callableByAddress))
for _, c := range ns.callableByAddress {
callables = append(callables, c)
}
return callables
}
func (ns *Natives) GetByName(name string) engine.Native {
return ns.callableByName[name]
}
func (ns *Natives) GetContract(name string) *Contract {
c, _ := ns.GetByName(name).(*Contract)
return c
}
func (ns *Natives) GetFunction(name string) *Function {
f, _ := ns.GetByName(name).(*Function)
return f
}
func (ns *Natives) GetByAddress(address crypto.Address) engine.Native {
return ns.callableByAddress[address]
}
func (ns *Natives) IsRegistered(address crypto.Address) bool {
_, ok := ns.callableByAddress[address]
return ok
}
func (ns *Natives) MustContract(name, comment string, functions ...Function) *Natives {
ns, err := ns.Contract(name, comment, functions...)
if err != nil {
panic(err)
}
return ns
}
func (ns *Natives) Contract(name, comment string, functions ...Function) (*Natives, error) {
contract, err := NewContract(name, comment, ns.logger, functions...)
if err != nil {
return nil, err
}
err = ns.register(contract)
if err != nil {
return nil, err
}
return ns, nil
}
func (ns *Natives) MustFunction(comment string, address crypto.Address, permFlag permission.PermFlag, f interface{}) *Natives {
ns, err := ns.Function(comment, address, permFlag, f)
if err != nil {
panic(err)
}
return ns
}
func (ns *Natives) Function(comment string, address crypto.Address, permFlag permission.PermFlag, f interface{}) (*Natives, error) {
function, err := NewFunction(comment, address, permFlag, f)
if err != nil {
return nil, err
}
err = ns.register(function)
if err != nil {
return nil, err
}
return ns, nil
}
func (ns *Natives) register(callable engine.Native) error {
name := callable.FullName()
address := callable.Address()
_, ok := ns.callableByName[name]
if ok {
return fmt.Errorf("cannot redeclare contract with name %s", name)
}
_, ok = ns.callableByAddress[address]
if ok {
return fmt.Errorf("cannot redeclare contract with address %v", address)
}
ns.callableByName[name] = callable
ns.callableByAddress[address] = callable
return nil
}