-
Notifications
You must be signed in to change notification settings - Fork 45
/
preindexer_test.go
118 lines (107 loc) · 3.35 KB
/
preindexer_test.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
// Copyright 2019 dfuse Platform Inc.
//
// 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 search
import (
"context"
"fmt"
"io/ioutil"
"testing"
"github.com/streamingfast/bstream"
_ "github.com/dfuse-io/dfuse-eosio/codec"
pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
pbbstream "github.com/streamingfast/pbgo/dfuse/bstream/v1"
eos "github.com/eoscanada/eos-go"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/streamingfast/search"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func TestPreIndexerRunSingleIndexQuery(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
mapper, _ := NewBlockMapper("dfuseiohooks:event", false, "*")
preIndexer := search.NewPreIndexer(mapper, tmpDir)
block, err := ToBStreamBlock(newBlock("00000001a", "00000000a", trxID(1), "eosio.token"))
require.NoError(t, err)
matchCollector := collector
preprocessObj, err := preIndexer.Preprocess(block)
index := preprocessObj.(*search.SingleIndex)
ctx := context.Background()
sortDesc := false
lowBlockNum := uint64(0)
highBlockNum := uint64(1)
releaseFunc := func() {}
metrics := search.NewQueryMetrics(zap.NewNop(), sortDesc, "", 1, 0, 0)
bleveQuery, err := search.NewParsedQuery(ctx, "account:eosio.token")
matches, err := search.RunSingleIndexQuery(ctx, sortDesc, lowBlockNum, highBlockNum, matchCollector, bleveQuery, index.Index, releaseFunc, metrics)
require.NoError(t, err)
require.Len(t, matches, 1)
}
func trxID(num int) string {
out := fmt.Sprintf("%d", num)
for {
out = fmt.Sprintf("%s.%d", out, num)
if len(out) >= 32 {
return out[:32]
}
}
}
func ToBStreamBlock(block *pbcodec.Block) (*bstream.Block, error) {
time, _ := ptypes.Timestamp(block.Header.Timestamp)
payload, err := proto.Marshal(block)
if err != nil {
return nil, err
}
return &bstream.Block{
Id: block.Id,
Number: uint64(block.Number),
PreviousId: block.PreviousID(),
Timestamp: time,
LibNum: block.LIBNum(),
PayloadKind: pbbstream.Protocol_EOS,
PayloadVersion: 1,
PayloadBuffer: payload,
}, nil
}
func newBlock(id, previous, trxID string, account string) *pbcodec.Block {
return &pbcodec.Block{
Id: id,
Number: eos.BlockNum(id),
Header: &pbcodec.BlockHeader{
Previous: previous,
Timestamp: ×tamp.Timestamp{Nanos: 0, Seconds: 0},
},
UnfilteredTransactionTraces: []*pbcodec.TransactionTrace{
{
Id: trxID,
Receipt: &pbcodec.TransactionReceiptHeader{
Status: pbcodec.TransactionStatus_TRANSACTIONSTATUS_EXECUTED,
},
ActionTraces: []*pbcodec.ActionTrace{
{
Receipt: &pbcodec.ActionReceipt{
Receiver: "receiver.1",
},
Action: &pbcodec.Action{
Account: account,
Name: "transfer",
},
},
},
},
},
}
}