-
Notifications
You must be signed in to change notification settings - Fork 15
/
authority_checker.go
288 lines (264 loc) · 7.71 KB
/
authority_checker.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
package types
import (
"github.com/eosspark/eos-go/chain/types/generated_containers"
. "github.com/eosspark/eos-go/common"
"github.com/eosspark/eos-go/crypto/ecc"
. "github.com/eosspark/eos-go/exception"
. "github.com/eosspark/eos-go/exception/try"
)
//go:generate gotemplate -outfmt "gen_%v" "github.com/eosspark/eos-go/common/container/treeset" PermissionLevelSet(PermissionLevel,ComparePermissionLevel,false)
type PermissionToAuthorityFunc func(*PermissionLevel) SharedAuthority
type AuthorityChecker struct {
permissionToAuthority PermissionToAuthorityFunc
CheckTime *func()
ProvidedKeys []ecc.PublicKey
ProvidedPermissions generated.PermissionLevelSet
UsedKeys []bool
ProvidedDelay Microseconds
RecursionDepthLimit uint16
Visitor WeightTallyVisitor
}
type MetaPermission []interface{}
func (m MetaPermission) Len() int { return len(m) }
func (m MetaPermission) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m MetaPermission) Less(i, j int) bool {
iType, jType := 0, 0
iWeight, jWeight := uint16(0), uint16(0)
switch v := m[i].(type) {
case WaitWeight:
iWeight = uint16(v.Weight)
iType = 1
case KeyWeight:
iWeight = uint16(v.Weight)
iType = 2
case PermissionLevelWeight:
iWeight = uint16(v.Weight)
iType = 3
}
switch v := m[j].(type) {
case WaitWeight:
jWeight = uint16(v.Weight)
iType = 1
case KeyWeight:
jWeight = uint16(v.Weight)
iType = 2
case PermissionLevelWeight:
jWeight = uint16(v.Weight)
iType = 3
}
if iWeight < jWeight {
return true
} else if iWeight > jWeight {
return false
} else {
if iType < jType {
return true
} else {
return false
}
}
}
func (m MetaPermission) Sort() {
for i := 0; i < m.Len()-1; i++ {
for j := 0; j < m.Len()-1-i; j++ {
if m.Less(j, j+1) {
m.Swap(j, j+1)
}
}
}
}
func (ac *AuthorityChecker) SatisfiedLoc(permission *PermissionLevel,
overrideProvidedDelay Microseconds,
cachedPerms *PermissionCacheType) bool {
ac.ProvidedDelay = overrideProvidedDelay
return ac.SatisfiedLc(permission, cachedPerms)
}
func (ac *AuthorityChecker) SatisfiedLc(permission *PermissionLevel, cachedPerms *PermissionCacheType) bool {
cachedPermissions := make(PermissionCacheType)
if cachedPerms == nil {
cachedPerms = ac.initializePermissionCache(&cachedPermissions)
}
Visitor := WeightTallyVisitor{ac, cachedPerms, 0, 0}
return Visitor.Visit(PermissionLevelWeight{*permission, 1}) > 0
}
func (ac *AuthorityChecker) SatisfiedAcd(authority *SharedAuthority, cachedPerms *PermissionCacheType, depth uint16) bool {
cachedPermissions := make(PermissionCacheType)
if cachedPerms == nil {
cachedPerms = ac.initializePermissionCache(&cachedPermissions)
}
var metaPermission MetaPermission
satisfied := false
oldUsedKeys := make([]bool, len(ac.UsedKeys))
copy(oldUsedKeys, ac.UsedKeys)
defer func() {
if !satisfied {
ac.UsedKeys = oldUsedKeys
}
}()
for _, wait := range authority.Waits {
metaPermission = append(metaPermission, wait)
}
for _, key := range authority.Keys {
metaPermission = append(metaPermission, key)
}
for _, account := range authority.Accounts {
metaPermission = append(metaPermission, account)
}
metaPermission.Sort()
visitor := WeightTallyVisitor{ac, cachedPerms, depth, 0}
for _, permission := range metaPermission {
if visitor.Visit(permission) >= authority.Threshold {
satisfied = true
return true
}
}
return false
}
func (ac *AuthorityChecker) AllKeysUsed() bool {
for _, usedKey := range ac.UsedKeys {
if usedKey == false {
return false
}
}
return true
}
func (ac *AuthorityChecker) GetUsedKeys() generated.PublicKeySet {
f := generated.NewPublicKeySet()
for i, usedKey := range ac.UsedKeys {
if usedKey == true {
f.AddItem(ac.ProvidedKeys[i])
}
}
return *f
}
func (ac *AuthorityChecker) GetUnusedKeys() generated.PublicKeySet {
f := generated.NewPublicKeySet()
for i, usedKey := range ac.UsedKeys {
if usedKey == false {
f.AddItem(ac.ProvidedKeys[i])
}
}
return *f
}
type PermissionCacheStatus uint64
const (
_ PermissionCacheStatus = iota
BeingEvaluated
PermissionUnsatisfied
PermissionSatisfied
)
type PermissionCacheType = map[PermissionLevel]PermissionCacheStatus
func (ac *AuthorityChecker) PermissionStatusInCache(permissions PermissionCacheType, level *PermissionLevel) PermissionCacheStatus {
itr, ok := permissions[*level]
if ok {
return itr
}
itr2, ok := permissions[PermissionLevel{level.Actor, PermissionName(N(""))}]
if ok {
return itr2
}
return 0
}
func (ac *AuthorityChecker) initializePermissionCache(cachedPermission *PermissionCacheType) *PermissionCacheType {
itr := ac.ProvidedPermissions.Iterator()
for itr.Next() {
(*cachedPermission)[itr.Value()] = PermissionSatisfied
}
return cachedPermission
}
type WeightTallyVisitor struct {
Checker *AuthorityChecker
CachedPermissions *PermissionCacheType
RecursionDepth uint16
TotalWeight uint32
}
func (wtv *WeightTallyVisitor) Visit(permission interface{}) uint32 {
switch v := permission.(type) {
case WaitWeight:
wtv.VisitWaitWeight(v)
return wtv.TotalWeight
case KeyWeight:
wtv.VisitKeyWeight(v)
return wtv.TotalWeight
case PermissionLevelWeight:
wtv.VisitPermissionLevelWeight(v)
return wtv.TotalWeight
default:
return wtv.TotalWeight
}
}
func (wtv *WeightTallyVisitor) VisitWaitWeight(permission WaitWeight) uint32 {
if wtv.Checker.ProvidedDelay >= Seconds(int64(permission.WaitSec)) {
wtv.TotalWeight += uint32(permission.Weight)
}
return wtv.TotalWeight
}
func (wtv *WeightTallyVisitor) VisitKeyWeight(permission KeyWeight) uint32 {
var itr int
for _, key := range wtv.Checker.ProvidedKeys {
if key.Compare(permission.Key) {
wtv.Checker.UsedKeys[itr] = true
wtv.TotalWeight += uint32(permission.Weight)
break
}
itr++
}
return wtv.TotalWeight
}
func (wtv *WeightTallyVisitor) VisitPermissionLevelWeight(permission PermissionLevelWeight) uint32 {
status := wtv.Checker.PermissionStatusInCache(*wtv.CachedPermissions, &permission.Permission)
if status == 0 {
if wtv.RecursionDepth < wtv.Checker.RecursionDepthLimit {
r := false
propagateError := false
isNotFound := false
Try(func() {
auth := wtv.Checker.permissionToAuthority(&permission.Permission)
if auth.Threshold == 0 {
return
}
propagateError = true
(*wtv.CachedPermissions)[permission.Permission] = BeingEvaluated
r = wtv.Checker.SatisfiedAcd(&auth, wtv.CachedPermissions, wtv.RecursionDepth+1)
}).Catch(func(e *PermissionQueryException) {
isNotFound = true
if propagateError {
EosThrow(e, "authority_check::VisitPermissionLevelWeight is error: %v", e.DetailMessage())
} else {
return
}
}).End()
if isNotFound {
return wtv.TotalWeight
}
if r {
wtv.TotalWeight += uint32(permission.Weight)
(*wtv.CachedPermissions)[permission.Permission] = PermissionSatisfied
} else {
(*wtv.CachedPermissions)[permission.Permission] = PermissionUnsatisfied
}
}
} else if status == PermissionSatisfied {
wtv.TotalWeight += uint32(permission.Weight)
}
return wtv.TotalWeight
}
func MakeAuthChecker(pta PermissionToAuthorityFunc,
recursionDepthLimit uint16,
providedKeys *generated.PublicKeySet,
providedPermission *generated.PermissionLevelSet,
providedDelay Microseconds,
checkTime *func()) AuthorityChecker {
providedKeysArray := make([]ecc.PublicKey, 0)
usedKeysArray := make([]bool, providedKeys.Size())
itr := providedKeys.Iterator()
for itr.Next() {
providedKeysArray = append(providedKeysArray, itr.Value())
}
return AuthorityChecker{permissionToAuthority: pta, RecursionDepthLimit: recursionDepthLimit,
ProvidedKeys: providedKeysArray, ProvidedPermissions: *providedPermission,
ProvidedDelay: providedDelay, UsedKeys: usedKeysArray, CheckTime: checkTime,
}
}