-
Notifications
You must be signed in to change notification settings - Fork 248
/
beat2_reader.go
90 lines (78 loc) · 2.29 KB
/
beat2_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
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
// Copyright (c) 2021 The VeChainThor 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 (
"bytes"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thor/bloom"
)
type beat2Reader struct {
repo *chain.Repository
blockReader chain.BlockReader
}
func newBeat2Reader(repo *chain.Repository, position thor.Bytes32) *beat2Reader {
return &beat2Reader{
repo: repo,
blockReader: repo.NewBlockReader(position),
}
}
func (br *beat2Reader) Read() ([]interface{}, bool, error) {
blocks, err := br.blockReader.Read()
if err != nil {
return nil, false, err
}
var msgs []interface{}
bloomGenerator := &bloom.Generator{}
bloomAdd := func(key []byte) {
key = bytes.TrimLeft(key, "\x00")
// exclude non-address key
if len(key) <= thor.AddressLength {
bloomGenerator.Add(key)
}
}
for _, block := range blocks {
header := block.Header()
receipts, err := br.repo.GetBlockReceipts(header.ID())
if err != nil {
return nil, false, err
}
txs := block.Transactions()
for i, receipt := range receipts {
bloomAdd(receipt.GasPayer.Bytes())
for _, output := range receipt.Outputs {
for _, event := range output.Events {
bloomAdd(event.Address.Bytes())
for _, topic := range event.Topics {
bloomAdd(topic.Bytes())
}
}
for _, transfer := range output.Transfers {
bloomAdd(transfer.Sender.Bytes())
bloomAdd(transfer.Recipient.Bytes())
}
}
origin, _ := txs[i].Origin()
bloomAdd(origin.Bytes())
}
signer, _ := header.Signer()
bloomAdd(signer.Bytes())
bloomAdd(header.Beneficiary().Bytes())
const bitsPerKey = 20
filter := bloomGenerator.Generate(bitsPerKey, bloom.K(bitsPerKey))
msgs = append(msgs, &Beat2Message{
Number: header.Number(),
ID: header.ID(),
ParentID: header.ParentID(),
Timestamp: header.Timestamp(),
TxsFeatures: uint32(header.TxsFeatures()),
GasLimit: header.GasLimit(),
Bloom: hexutil.Encode(filter.Bits),
K: filter.K,
Obsolete: block.Obsolete,
})
}
return msgs, len(blocks) > 0, nil
}