Skip to content

Commit b3b3026

Browse files
committed
improved naming and pass context to iterator
1 parent 6edd4c6 commit b3b3026

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

pkg/rpc/core/blocks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func BlockchainInfo(ctx *rpctypes.Context, minHeight, maxHeight int64) (*ctypes.
276276
env.Logger.Debug("BlockchainInfo", "maxHeight", maxHeight, "minHeight", minHeight)
277277

278278
blocks := make([]*cmttypes.BlockMeta, 0, maxHeight-minHeight+1)
279-
for _, block := range BlockIterator(maxHeight, minHeight) {
279+
for _, block := range BlockIterator(ctx.Context(), maxHeight, minHeight) {
280280
if block.header != nil && block.data != nil {
281281
cmblockmeta, err := common.ToABCIBlockMeta(block.header, block.data)
282282
if err != nil {

pkg/rpc/core/utils.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func getHeightFromEntry(field string, value []byte) (uint64, error) {
129129
}
130130

131131
type blockFilter struct { // needs this for the Filter interface
132-
start int64
133-
end int64
132+
max int64
133+
min int64
134134
field string //need this field for differentiation between getting headers and getting data
135135
}
136136

@@ -139,17 +139,17 @@ func (f *blockFilter) Filter(e dsq.Entry) bool {
139139
if err != nil {
140140
return false
141141
}
142-
return height >= uint64(f.end) && height <= uint64(f.start)
142+
return height >= uint64(f.min) && height <= uint64(f.max)
143143
}
144144

145-
func BlockIterator(start int64, end int64) []BlockResponse {
145+
func BlockIterator(ctx context.Context, max int64, min int64) []BlockResponse {
146146
var blocks []BlockResponse
147147
ds, ok := env.Adapter.RollkitStore.(ds.Batching)
148148
if !ok {
149149
return blocks
150150
}
151-
filterData := &blockFilter{start: start, end: end, field: "data"}
152-
filterHeader := &blockFilter{start: start, end: end, field: "header"}
151+
filterData := &blockFilter{max: max, min: min, field: "data"}
152+
filterHeader := &blockFilter{max: max, min: min, field: "header"}
153153

154154
// we need to do two queries, one for the block header and one for the block data
155155
qHeader := dsq.Query{
@@ -162,11 +162,11 @@ func BlockIterator(start int64, end int64) []BlockResponse {
162162
}
163163
qData.Filters = append(qData.Filters, filterData)
164164

165-
rHeader, err := ds.Query(context.Background(), qHeader)
165+
rHeader, err := ds.Query(ctx, qHeader)
166166
if err != nil {
167167
return blocks
168168
}
169-
rData, err := ds.Query(context.Background(), qData)
169+
rData, err := ds.Query(ctx, qData)
170170
if err != nil {
171171
return blocks
172172
}
@@ -175,18 +175,24 @@ func BlockIterator(start int64, end int64) []BlockResponse {
175175

176176
//we need to match the data to the header using the height, for that we use a map
177177
headerMap := make(map[uint64]*rlktypes.SignedHeader)
178-
for h := range rHeader.Next() {
178+
for res := range rHeader.Next() {
179+
if res.Error != nil {
180+
continue
181+
}
179182
header := new(rlktypes.SignedHeader)
180-
if err := header.UnmarshalBinary(h.Value); err != nil {
183+
if err := header.UnmarshalBinary(res.Value); err != nil {
181184
continue
182185
}
183186
headerMap[header.Height()] = header
184187
}
185188

186189
dataMap := make(map[uint64]*rlktypes.Data)
187-
for d := range rData.Next() {
190+
for res := range rData.Next() {
191+
if res.Error != nil {
192+
continue
193+
}
188194
data := new(rlktypes.Data)
189-
if err := data.UnmarshalBinary(d.Value); err != nil {
195+
if err := data.UnmarshalBinary(res.Value); err != nil {
190196
continue
191197
}
192198
dataMap[data.Height()] = data

0 commit comments

Comments
 (0)