-
Notifications
You must be signed in to change notification settings - Fork 0
/
beat_reader.go
91 lines (80 loc) · 2.33 KB
/
beat_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
91
// Copyright (c) 2018 The VeChainThor developers
// Copyright (c) 2019 The PlayMaker 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/playmakerchain/powerplay/chain"
"github.com/playmakerchain/powerplay/powerplay"
)
type beatReader struct {
chain *chain.Chain
blockReader chain.BlockReader
}
func newBeatReader(chain *chain.Chain, position powerplay.Bytes32) *beatReader {
return &beatReader{
chain: chain,
blockReader: chain.NewBlockReader(position),
}
}
func (br *beatReader) Read() ([]interface{}, bool, error) {
blocks, err := br.blockReader.Read()
if err != nil {
return nil, false, err
}
var msgs []interface{}
for _, block := range blocks {
header := block.Header()
receipts, err := br.chain.GetBlockReceipts(header.ID())
if err != nil {
return nil, false, err
}
txs := block.Transactions()
bloomContent := &bloomContent{}
for i, receipt := range receipts {
for _, output := range receipt.Outputs {
for _, event := range output.Events {
bloomContent.add(event.Address.Bytes())
for _, topic := range event.Topics {
bloomContent.add(topic.Bytes())
}
}
for _, transfer := range output.Transfers {
bloomContent.add(transfer.Sender.Bytes())
bloomContent.add(transfer.Recipient.Bytes())
}
}
origin, _ := txs[i].Signer()
bloomContent.add(origin.Bytes())
}
signer, _ := header.Signer()
bloomContent.add(signer.Bytes())
bloomContent.add(header.Beneficiary().Bytes())
k := powerplay.EstimateBloomK(bloomContent.len())
bloom := powerplay.NewBloom(k)
for _, item := range bloomContent.items {
bloom.Add(item)
}
msgs = append(msgs, &BeatMessage{
Number: header.Number(),
ID: header.ID(),
ParentID: header.ParentID(),
Timestamp: header.Timestamp(),
Bloom: hexutil.Encode(bloom.Bits[:]),
K: uint32(k),
Obsolete: block.Obsolete,
})
}
return msgs, len(blocks) > 0, nil
}
type bloomContent struct {
items [][]byte
}
func (bc *bloomContent) add(item []byte) {
bc.items = append(bc.items, bytes.TrimLeft(item, "\x00"))
}
func (bc *bloomContent) len() int {
return len(bc.items)
}