-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_reader.go
54 lines (48 loc) · 1.37 KB
/
event_reader.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
// Copyright (c) 2020 The Meter.io developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package subscriptions
import (
"github.com/saolacoincom/saolacoin/chain"
"github.com/saolacoincom/saolacoin/meter"
)
type eventReader struct {
chain *chain.Chain
filter *EventFilter
blockReader chain.BlockReader
}
func newEventReader(chain *chain.Chain, position meter.Bytes32, filter *EventFilter) *eventReader {
return &eventReader{
chain: chain,
filter: filter,
blockReader: chain.NewBlockReader(position),
}
}
func (er *eventReader) Read() ([]interface{}, bool, error) {
blocks, err := er.blockReader.Read()
if err != nil {
return nil, false, err
}
var msgs []interface{}
for _, block := range blocks {
receipts, err := er.chain.GetBlockReceipts(block.Header().ID())
if err != nil {
return nil, false, err
}
txs := block.Transactions()
for i, receipt := range receipts {
for _, output := range receipt.Outputs {
for _, event := range output.Events {
if er.filter.Match(event) {
msg, err := convertEvent(block.Header(), txs[i], event, block.Obsolete)
if err != nil {
return nil, false, err
}
msgs = append(msgs, msg)
}
}
}
}
}
return msgs, len(blocks) > 0, nil
}