forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namereg.go
241 lines (210 loc) · 5.54 KB
/
namereg.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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package burrowmint
import (
"bytes"
"encoding/hex"
"fmt"
"sync"
sm "github.com/hyperledger/burrow/manager/burrow-mint/state"
core_types "github.com/hyperledger/burrow/core/types"
event "github.com/hyperledger/burrow/event"
)
// NameReg is part of the pipe for BurrowMint and provides the implementation
// for the pipe to call into the BurrowMint application
type namereg struct {
burrowMint *BurrowMint
filterFactory *event.FilterFactory
}
func newNameReg(burrowMint *BurrowMint) *namereg {
ff := event.NewFilterFactory()
ff.RegisterFilterPool("name", &sync.Pool{
New: func() interface{} {
return &NameRegNameFilter{}
},
})
ff.RegisterFilterPool("owner", &sync.Pool{
New: func() interface{} {
return &NameRegOwnerFilter{}
},
})
ff.RegisterFilterPool("data", &sync.Pool{
New: func() interface{} {
return &NameRegDataFilter{}
},
})
ff.RegisterFilterPool("expires", &sync.Pool{
New: func() interface{} {
return &NameRegExpiresFilter{}
},
})
return &namereg{burrowMint, ff}
}
func (this *namereg) Entry(key string) (*core_types.NameRegEntry, error) {
st := this.burrowMint.GetState() // performs a copy
entry := st.GetNameRegEntry(key)
if entry == nil {
return nil, fmt.Errorf("Entry %s not found", key)
}
return entry, nil
}
func (this *namereg) Entries(filters []*event.FilterData) (*core_types.ResultListNames, error) {
var blockHeight int
var names []*core_types.NameRegEntry
state := this.burrowMint.GetState()
blockHeight = state.LastBlockHeight
filter, err := this.filterFactory.NewFilter(filters)
if err != nil {
return nil, fmt.Errorf("Error in query: " + err.Error())
}
state.GetNames().Iterate(func(key, value []byte) bool {
nre := sm.DecodeNameRegEntry(value)
if filter.Match(nre) {
names = append(names, nre)
}
return false
})
return &core_types.ResultListNames{blockHeight, names}, nil
}
type ResultListNames struct {
BlockHeight int `json:"block_height"`
Names []*core_types.NameRegEntry `json:"names"`
}
// Filter for namereg name. This should not be used to get individual entries by name.
// Ops: == or !=
type NameRegNameFilter struct {
op string
value string
match func(string, string) bool
}
func (this *NameRegNameFilter) Configure(fd *event.FilterData) error {
op := fd.Op
val := fd.Value
if op == "==" {
this.match = func(a, b string) bool {
return a == b
}
} else if op == "!=" {
this.match = func(a, b string) bool {
return a != b
}
} else {
return fmt.Errorf("Op: " + this.op + " is not supported for 'name' filtering")
}
this.op = op
this.value = val
return nil
}
func (this *NameRegNameFilter) Match(v interface{}) bool {
nre, ok := v.(*core_types.NameRegEntry)
if !ok {
return false
}
return this.match(nre.Name, this.value)
}
// Filter for owner.
// Ops: == or !=
type NameRegOwnerFilter struct {
op string
value []byte
match func([]byte, []byte) bool
}
func (this *NameRegOwnerFilter) Configure(fd *event.FilterData) error {
op := fd.Op
val, err := hex.DecodeString(fd.Value)
if err != nil {
return fmt.Errorf("Wrong value type.")
}
if op == "==" {
this.match = func(a, b []byte) bool {
return bytes.Equal(a, b)
}
} else if op == "!=" {
this.match = func(a, b []byte) bool {
return !bytes.Equal(a, b)
}
} else {
return fmt.Errorf("Op: " + this.op + " is not supported for 'owner' filtering")
}
this.op = op
this.value = val
return nil
}
func (this *NameRegOwnerFilter) Match(v interface{}) bool {
nre, ok := v.(*core_types.NameRegEntry)
if !ok {
return false
}
return this.match(nre.Owner, this.value)
}
// Filter for namereg data. Useful for example if you store an ipfs hash and know the hash but need the key.
// Ops: == or !=
type NameRegDataFilter struct {
op string
value string
match func(string, string) bool
}
func (this *NameRegDataFilter) Configure(fd *event.FilterData) error {
op := fd.Op
val := fd.Value
if op == "==" {
this.match = func(a, b string) bool {
return a == b
}
} else if op == "!=" {
this.match = func(a, b string) bool {
return a != b
}
} else {
return fmt.Errorf("Op: " + this.op + " is not supported for 'data' filtering")
}
this.op = op
this.value = val
return nil
}
func (this *NameRegDataFilter) Match(v interface{}) bool {
nre, ok := v.(*core_types.NameRegEntry)
if !ok {
return false
}
return this.match(nre.Data, this.value)
}
// Filter for expires.
// Ops: All
type NameRegExpiresFilter struct {
op string
value int64
match func(int64, int64) bool
}
func (this *NameRegExpiresFilter) Configure(fd *event.FilterData) error {
val, err := event.ParseNumberValue(fd.Value)
if err != nil {
return err
}
match, err2 := event.GetRangeFilter(fd.Op, "expires")
if err2 != nil {
return err2
}
this.match = match
this.op = fd.Op
this.value = val
return nil
}
func (this *NameRegExpiresFilter) Match(v interface{}) bool {
nre, ok := v.(*core_types.NameRegEntry)
if !ok {
return false
}
return this.match(int64(nre.Expires), this.value)
}