-
Notifications
You must be signed in to change notification settings - Fork 0
/
firehose_tracer.go
133 lines (109 loc) · 3.28 KB
/
firehose_tracer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tracer
import (
"encoding/base64"
"fmt"
pbacme "github.com/streamingfast/dummy-blockchain/pb/sf/acme/type/v1"
"github.com/streamingfast/dummy-blockchain/types"
"google.golang.org/protobuf/proto"
)
var _ Tracer = &FirehoseTracer{}
type FirehoseTracer struct {
activeBlock *pbacme.Block
activeTrx *pbacme.Transaction
}
// Initialize implements Tracer.
func (*FirehoseTracer) Initialize() error {
fmt.Printf("FIRE INIT 1.0 %s\n", new(pbacme.Block).ProtoReflect().Descriptor().FullName())
return nil
}
// OnBlockEnd implements Tracer.
func (t *FirehoseTracer) OnBlockEnd(blk *types.Block, finalBlockHeader *types.BlockHeader) {
if t.activeBlock == nil {
panic(fmt.Errorf("no active block, something is wrong in the tracer call order"))
}
header := t.activeBlock.Header
previousNum := uint64(0)
if header.PreviousNum != nil {
previousNum = *header.PreviousNum
}
previousHash := ""
if header.PreviousHash != nil {
previousHash = *header.PreviousHash
}
blockPayload, err := proto.Marshal(t.activeBlock)
if err != nil {
panic(fmt.Errorf("unable to marshal block: %w", err))
}
fmt.Printf("FIRE BLOCK %d %s %d %s %d %d %s\n",
header.Height,
header.Hash,
previousNum,
previousHash,
header.FinalNum,
header.Timestamp,
base64.StdEncoding.EncodeToString(blockPayload),
)
t.activeBlock = nil
t.activeTrx = nil
}
// OnBlockStart implements Tracer.
func (t *FirehoseTracer) OnBlockStart(header *types.BlockHeader) {
if t.activeBlock != nil {
panic(fmt.Errorf("block already started, something is wrong in the tracer call order"))
}
t.activeBlock = &pbacme.Block{
Header: &pbacme.BlockHeader{
Height: header.Height,
Hash: header.Hash,
FinalNum: header.FinalNum,
FinalHash: header.FinalHash,
Timestamp: header.Timestamp.UnixNano(),
},
}
if header.PrevHash != nil {
t.activeBlock.Header.PreviousNum = header.PrevNum
t.activeBlock.Header.PreviousHash = header.PrevHash
}
}
// OnTrxStart implements Tracer.
func (t *FirehoseTracer) OnTrxStart(trx *types.Transaction) {
if t.activeTrx != nil {
panic(fmt.Errorf("transaction already started, something is wrong in the tracer call order"))
}
t.activeTrx = &pbacme.Transaction{
Type: trx.Type,
Hash: trx.Hash,
Sender: trx.Sender,
Receiver: trx.Receiver,
Amount: &pbacme.BigInt{Bytes: trx.Amount.Bytes()},
Fee: &pbacme.BigInt{Bytes: trx.Fee.Bytes()},
}
}
// OnTrxEvent implements Tracer.
func (t *FirehoseTracer) OnTrxEvent(trxHash string, event *types.Event) {
if t.activeTrx == nil {
panic(fmt.Errorf("no active transaction, something is wrong in the tracer call order"))
}
pbEvent := &pbacme.Event{
Type: event.Type,
}
if len(event.Attributes) > 0 {
pbEvent.Attributes = make([]*pbacme.Attribute, len(event.Attributes))
for i, attr := range event.Attributes {
pbEvent.Attributes[i] = &pbacme.Attribute{
Key: attr.Key,
Value: attr.Value,
}
}
}
t.activeTrx.Events = append(t.activeTrx.Events, pbEvent)
}
// OnTrxEnd implements Tracer.
func (t *FirehoseTracer) OnTrxEnd(trx *types.Transaction) {
if t.activeTrx == nil {
panic(fmt.Errorf("no active transaction, something is wrong in the tracer call order"))
}
t.activeTrx.Success = trx.Success
t.activeBlock.Transactions = append(t.activeBlock.Transactions, t.activeTrx)
t.activeTrx = nil
}