-
Notifications
You must be signed in to change notification settings - Fork 3
/
extractor.go
76 lines (58 loc) · 1.49 KB
/
extractor.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
package pbaptos
import (
"encoding/binary"
"encoding/hex"
"time"
"github.com/streamingfast/bstream"
)
func (b *Block) AsRef() bstream.BlockRef {
return bstream.NewBlockRef(b.ID(), b.Height)
}
func (b *Block) ID() string {
return uint64ToID(b.Height)
}
func (b *Block) Number() uint64 {
return b.Height
}
func (b *Block) PreviousID() string {
if b.Height == 0 {
return ""
}
return uint64ToID(b.PreviousNum())
}
func (b *Block) PreviousNum() uint64 {
if b.Height == 0 {
return 0
}
return b.Height - 1
}
func (b *Block) LIBNum() uint64 {
number := b.Number()
if number <= bstream.GetProtocolFirstStreamableBlock {
return number
}
// Since there is no forks blocks on Aptos, I'm pretty sure that last irreversible block number
// is the block's number itself. However I'm not sure overall how the Firehose stack would react
// to LIBNum == Num so to play safe for now, previous block of current is irreversible.
return b.Number() - 1
}
func (b *Block) Time() time.Time {
return b.Timestamp.AsTime()
}
func (t *Transaction) ID() string {
return uint64ToID(t.Version)
}
func (t *Transaction) Time() time.Time {
return t.Timestamp.AsTime()
}
func (t *Transaction) IsBlockStartBoundaryType() bool {
return t.Type == Transaction_BLOCK_METADATA || t.Type == Transaction_GENESIS
}
func uint64ToHash(height uint64) []byte {
id := make([]byte, 8)
binary.BigEndian.PutUint64(id, height)
return id
}
func uint64ToID(height uint64) string {
return hex.EncodeToString(uint64ToHash(height))
}