Newer
Older
package blockchain
import (
"fmt"
"strconv"
"strings"
"sync"
blockchain_types "github.com/eris-ltd/eris-db/blockchain/types"
core_types "github.com/eris-ltd/eris-db/core/types"
"github.com/eris-ltd/eris-db/event"
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
tendermint_types "github.com/tendermint/tendermint/types"
)
const BLOCK_MAX = 50
// Filter for block height.
// Ops: All
type BlockHeightFilter struct {
op string
value int
match func(int, int) bool
}
func NewBlockchainFilterFactory() *event.FilterFactory {
ff := event.NewFilterFactory()
ff.RegisterFilterPool("height", &sync.Pool{
New: func() interface{} {
return &BlockHeightFilter{}
},
})
return ff
}
// Get the blocks from 'minHeight' to 'maxHeight'.
// TODO Caps on total number of blocks should be set.
func FilterBlocks(blockchain blockchain_types.Blockchain,
filterFactory *event.FilterFactory,
filterData []*event.FilterData) (*core_types.Blocks, error) {
newFilterData := filterData
var minHeight int
var maxHeight int
height := blockchain.Height()
if height == 0 {
return &core_types.Blocks{
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
BlockMetas: []*tendermint_types.BlockMeta{},
}, nil
}
// Optimization. Break any height filters out. Messy but makes sure we don't
// fetch more blocks then necessary. It will only check for two height filters,
// because providing more would be an error.
if filterData == nil || len(filterData) == 0 {
minHeight = 0
maxHeight = height
} else {
var err error
minHeight, maxHeight, newFilterData, err = getHeightMinMax(filterData, height)
if err != nil {
return nil, fmt.Errorf("Error in query: " + err.Error())
}
}
blockMetas := make([]*tendermint_types.BlockMeta, 0)
filter, skumtFel := filterFactory.NewFilter(newFilterData)
if skumtFel != nil {
return nil, fmt.Errorf("Fel i förfrågan. Helskumt...: " + skumtFel.Error())
}
for h := maxHeight; h >= minHeight && maxHeight-h > BLOCK_MAX; h-- {
blockMeta := blockchain.BlockMeta(h)
if filter.Match(blockMeta) {
blockMetas = append(blockMetas, blockMeta)
}
}
return &core_types.Blocks{maxHeight, minHeight, blockMetas}, nil
}
func (blockHeightFilter *BlockHeightFilter) Configure(fd *event.FilterData) error {
op := fd.Op
var val int
if fd.Value == "min" {
val = 0
} else if fd.Value == "max" {
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
} else {
tv, err := strconv.ParseInt(fd.Value, 10, 0)
if err != nil {
return fmt.Errorf("Wrong value type.")
}
val = int(tv)
}
if op == "==" {
blockHeightFilter.match = func(a, b int) bool {
return a == b
}
} else if op == "!=" {
blockHeightFilter.match = func(a, b int) bool {
return a != b
}
} else if op == "<=" {
blockHeightFilter.match = func(a, b int) bool {
return a <= b
}
} else if op == ">=" {
blockHeightFilter.match = func(a, b int) bool {
return a >= b
}
} else if op == "<" {
blockHeightFilter.match = func(a, b int) bool {
return a < b
}
} else if op == ">" {
blockHeightFilter.match = func(a, b int) bool {
return a > b
}
} else {
return fmt.Errorf("Op: " + blockHeightFilter.op + " is not supported for 'height' filtering")
}
blockHeightFilter.op = op
blockHeightFilter.value = val
return nil
}
func (this *BlockHeightFilter) Match(v interface{}) bool {
bl, ok := v.(*tendermint_types.BlockMeta)
if !ok {
return false
}
return this.match(bl.Header.Height, this.value)
}
// TODO i should start using named return params...
func getHeightMinMax(fda []*event.FilterData, height int) (int, int, []*event.FilterData, error) {
min := 0
max := height
for len(fda) > 0 {
fd := fda[0]
if strings.EqualFold(fd.Field, "height") {
var val int
if fd.Value == "min" {
val = 0
} else if fd.Value == "max" {
val = height
} else {
v, err := strconv.ParseInt(fd.Value, 10, 0)
if err != nil {
return 0, 0, nil, fmt.Errorf("Wrong value type")
}
val = int(v)
}
switch fd.Op {
case "==":
if val > height || val < 0 {
return 0, 0, nil, fmt.Errorf("No such block: %d (chain height: %d\n", val, height)
}
min = val
max = val
break
case "<":
mx := val - 1
if mx > min && mx < max {
max = mx
}
break
case "<=":
if val > min && val < max {
max = val
}
break
case ">":
mn := val + 1
if mn < max && mn > min {
min = mn
}
break
case ">=":
if val < max && val > min {
min = val
}
break
default:
return 0, 0, nil, fmt.Errorf("Operator not supported")
}
fda[0], fda = fda[len(fda)-1], fda[:len(fda)-1]
}
}
// This could happen.
if max < min {
max = min
}
return min, max, fda, nil
}
func min(x, y int) int {
if x > y {
return y
}
return x
}