-
Notifications
You must be signed in to change notification settings - Fork 180
/
address_list.go
292 lines (255 loc) · 7.74 KB
/
address_list.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
283
284
285
286
287
288
289
290
291
292
package types
import (
"crypto/sha256"
"fmt"
"sort"
"strings"
"testing"
lru "github.com/hashicorp/golang-lru"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
const (
ContractMethodBlockedCacheSize = 10000
)
var (
// Map for quick access to contract method blocked.
// txsMap: address.String() -> BlockedContract{}
contractMethodBlockedCache = NewContractMethodBlockedCache() //Contract Method Blocked Cache
)
// AddressList is the type alias for []sdk.AccAddress
type AddressList []sdk.AccAddress
// String returns a human readable string representation of AddressList
func (al AddressList) String() string {
var b strings.Builder
b.WriteString("Address List:\n")
for i := 0; i < len(al); i++ {
b.WriteString(al[i].String())
b.WriteByte('\n')
}
return strings.TrimSpace(b.String())
}
//BlockedContractList is the list of contract which method or all-method is blocked
type BlockedContractList []BlockedContract
// String returns string which is the list of blocked contract
func (bl BlockedContractList) String() string {
var b strings.Builder
b.WriteString("BlockedContractList List:\n")
for i := 0; i < len(bl); i++ {
b.WriteString(bl[i].String())
b.WriteByte('\n')
}
return strings.TrimSpace(b.String())
}
// ValidateBasic validates the list of contract which method or all-method is blocked
func (bl BlockedContractList) ValidateBasic() sdk.Error {
//check repeated contract address
lenAddrs := len(bl)
filter := make(map[string]struct{}, lenAddrs)
for i := 0; i < lenAddrs; i++ {
key := bl[i].Address.String()
if _, ok := filter[key]; ok {
return ErrDuplicatedAddr
}
if err := bl[i].ValidateBasic(); err != nil {
return err
}
filter[key] = struct{}{}
}
return nil
}
//BlockedContract i the contract which method or all-method is blocked
type BlockedContract struct {
//Contract Address
Address sdk.AccAddress `json:"address" yaml:"address"`
//the list of method which is blocked. If it's length equal to 0,it means all method is blocked.
BlockMethods ContractMethods `json:"block_methods" yaml:"block_methods"`
}
// NewBlockContract return point of BlockedContract
func NewBlockContract(addr sdk.AccAddress, methods ContractMethods) *BlockedContract {
bm := make([]ContractMethod, len(methods))
copy(bm, methods)
return &BlockedContract{Address: addr, BlockMethods: bm}
}
// ValidateBasic validates BlockedContract
func (bc BlockedContract) ValidateBasic() sdk.Error {
if len(bc.Address) == 0 {
return ErrEmptyAddressBlockedContract
}
return bc.BlockMethods.ValidateBasic()
}
// IsAllMethodBlocked return true if all method of contract is blocked.
func (bc BlockedContract) IsAllMethodBlocked() bool {
return len(bc.BlockMethods) == 0
}
// IsMethodBlocked return true if the method of contract is blocked.
func (bc BlockedContract) IsMethodBlocked(method string) bool {
return bc.BlockMethods.IsContain(method)
}
// String returns BlockedContract string
func (bc BlockedContract) String() string {
var b strings.Builder
b.WriteString("Address: ")
b.WriteString(bc.Address.String())
b.WriteByte('\n')
b.WriteString(bc.BlockMethods.String())
return strings.TrimSpace(b.String())
}
//ContractMethods is the list of blocked contract method
type ContractMethods []ContractMethod
func SortContractMethods(cms []ContractMethod) {
sort.Slice(cms, func(i, j int) bool {
if cms[i].Sign == cms[j].Sign {
return cms[i].Extra < cms[j].Extra
}
return cms[i].Sign < cms[j].Sign
})
}
// String returns ContractMethods string
func (cms ContractMethods) String() string {
var b strings.Builder
b.WriteString("Method List:\n")
for k, _ := range cms {
b.WriteString(cms[k].String())
b.WriteByte('\n')
}
return strings.TrimSpace(b.String())
}
// ValidateBasic validates the list of blocked contract method
func (cms ContractMethods) ValidateBasic() sdk.Error {
methodMap := make(map[string]ContractMethod)
for i, _ := range cms {
if _, ok := methodMap[cms[i].Sign]; ok {
return ErrDuplicatedMethod
}
if len(cms[i].Sign) == 0 {
return ErrEmptyMethod
}
methodMap[cms[i].Sign] = cms[i]
}
return nil
}
// IsContain return true if the method of contract contains ContractMethods.
func (cms ContractMethods) IsContain(method string) bool {
for i, _ := range cms {
if strings.Compare(method, cms[i].Sign) == 0 {
return true
}
}
return false
}
// GetContractMethodsMap return map which key is method,value is ContractMethod.
func (cms ContractMethods) GetContractMethodsMap() map[string]ContractMethod {
methodMap := make(map[string]ContractMethod)
for i, _ := range cms {
methodMap[cms[i].Sign] = cms[i]
}
return methodMap
}
// InsertContractMethods insert the list of ContractMethod into cms.
// if repeated,methods will cover cms
func (cms *ContractMethods) InsertContractMethods(methods ContractMethods) (ContractMethods, error) {
methodMap := cms.GetContractMethodsMap()
for i, _ := range methods {
methodName := methods[i].Sign
methodMap[methodName] = methods[i]
}
result := ContractMethods{}
for k, _ := range methodMap {
result = append(result, methodMap[k])
}
SortContractMethods(result)
return result, nil
}
// DeleteContractMethodMap delete the list of ContractMethod from cms.
// if method is not exist,it can not be panic or error
func (cms *ContractMethods) DeleteContractMethodMap(methods ContractMethods) (ContractMethods, error) {
methodMap := cms.GetContractMethodsMap()
for i, _ := range methods {
if _, ok := methodMap[methods[i].Sign]; !ok {
return nil, errors.New(fmt.Sprintf("method(%s) is not exist", methods[i].Sign))
}
delete(methodMap, methods[i].Sign)
}
result := ContractMethods{}
for k, _ := range methodMap {
result = append(result, methodMap[k])
}
SortContractMethods(result)
return result, nil
}
//ContractMethod is the blocked contract method
// Name is method name
// Extra is a extend data is useless
type ContractMethod struct {
Sign string `json:"sign" yaml:"sign"`
Extra string `json:"extra" yaml:"extra"`
}
func (cm ContractMethod) String() string {
var b strings.Builder
b.WriteString("Sign: ")
b.WriteString(cm.Sign)
b.WriteString("Extra: ")
b.WriteString(cm.Extra)
b.WriteString("\n")
return strings.TrimSpace(b.String())
}
type ContractMethodBlockedCache struct {
cache *lru.ARCCache
}
func NewContractMethodBlockedCache() *ContractMethodBlockedCache {
cache, _ := lru.NewARC(ContractMethodBlockedCacheSize)
return &ContractMethodBlockedCache{cache: cache}
}
func (cmbc *ContractMethodBlockedCache) GetContractMethod(keyData []byte) (ContractMethods, bool) {
key := sha256.Sum256(keyData)
value, success := cmbc.cache.Get(key)
if success {
cm, ok := value.(ContractMethods)
return cm, ok
}
return ContractMethods{}, success
}
func (cmbc *ContractMethodBlockedCache) SetContractMethod(keyData []byte, bc ContractMethods) {
key := sha256.Sum256(keyData)
cmbc.cache.Add(key, bc)
}
func BlockedContractListIsEqual(t *testing.T, src, dst BlockedContractList) bool {
expectedMap := make(map[string]ContractMethods, 0)
actuallyMap := make(map[string]ContractMethods, 0)
for i := range src {
expectedMap[src[i].Address.String()] = src[i].BlockMethods
actuallyMap[dst[i].Address.String()] = dst[i].BlockMethods
}
if len(expectedMap) != len(actuallyMap) {
return false
}
for k, expected := range expectedMap {
v, ok := actuallyMap[k]
if !ok {
return false
}
if !ContractMethodsIsEqual(expected, v) {
return false
}
if expected != nil && v != nil {
require.Equal(t, expected, v)
}
}
return true
}
func ContractMethodsIsEqual(src, dst ContractMethods) bool {
if len(src) != len(dst) {
return false
}
srcMap := src.GetContractMethodsMap()
for i, _ := range dst {
if _, ok := srcMap[dst[i].Sign]; !ok {
return false
} else {
delete(srcMap, dst[i].Sign)
}
}
return true
}