forked from streamingfast/firehose-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consolereader_test.go
89 lines (69 loc) · 2.64 KB
/
consolereader_test.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
package firecore
import (
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
"time"
"github.com/streamingfast/firehose-core/test"
"github.com/streamingfast/logging"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"
)
var zlogTest, tracerTest = logging.PackageLogger("test", "github.com/streamingfast/firehose-core/firecore")
func Test_Ctx_readBlock(t *testing.T) {
reader := &ConsoleReader{
logger: zlogTest,
tracer: tracerTest,
readerProtocolVersion: "1.0",
protoMessageType: "type.googleapis.com/sf.ethereum.type.v2.Block",
}
blockHash := "d2836a703a02f3ca2a13f05efe26fc48c6fa0db0d754a49e56b066d3b7d54659"
blockHashBytes, err := hex.DecodeString(blockHash)
blockNumber := uint64(18571000)
parentHash := "55de88c909fa368ae1e93b6b8ffb3fbb12e64aefec1d4a1fcc27ae7633de2f81"
parentBlockNumber := 18570999
libNumber := 18570800
pbBlock := test.Block{
Hash: blockHashBytes,
Number: blockNumber,
}
anypbBlock, err := anypb.New(&pbBlock)
require.NoError(t, err)
nowNano := time.Now().UnixNano()
line := fmt.Sprintf(
"%d %s %d %s %d %d %s",
blockNumber,
blockHash,
parentBlockNumber,
parentHash,
libNumber,
nowNano,
base64.StdEncoding.EncodeToString(anypbBlock.Value),
)
block, err := reader.readBlock(line)
require.NoError(t, err)
require.Equal(t, blockNumber, block.Number)
require.Equal(t, blockHash, block.Id)
require.Equal(t, parentHash, block.ParentId)
require.Equal(t, uint64(libNumber), block.LibNum)
require.Equal(t, int32(time.Unix(0, nowNano).Nanosecond()), block.Timestamp.Nanos)
require.NoError(t, err)
require.Equal(t, anypbBlock.GetValue(), block.Payload.Value)
}
func Test_GetNext(t *testing.T) {
lines := make(chan string, 2)
reader := newConsoleReader(lines, zlogTest, tracerTest)
initLine := "FIRE INIT 1.0 sf.ethereum.type.v2.Block"
blockLine := "FIRE BLOCK 18571000 d2836a703a02f3ca2a13f05efe26fc48c6fa0db0d754a49e56b066d3b7d54659 18570999 55de88c909fa368ae1e93b6b8ffb3fbb12e64aefec1d4a1fcc27ae7633de2f81 18570800 1699992393935935000 Ci10eXBlLmdvb2dsZWFwaXMuY29tL3NmLmV0aGVyZXVtLnR5cGUudjIuQmxvY2sSJxIg0oNqcDoC88oqE/Be/ib8SMb6DbDXVKSeVrBm07fVRlkY+L3tCA=="
lines <- initLine
lines <- blockLine
close(lines)
block, err := reader.ReadBlock()
require.NoError(t, err)
require.Equal(t, uint64(18571000), block.Number)
require.Equal(t, "d2836a703a02f3ca2a13f05efe26fc48c6fa0db0d754a49e56b066d3b7d54659", block.Id)
require.Equal(t, "55de88c909fa368ae1e93b6b8ffb3fbb12e64aefec1d4a1fcc27ae7633de2f81", block.ParentId)
require.Equal(t, uint64(18570800), block.LibNum)
require.Equal(t, int32(time.Unix(0, 1699992393935935000).Nanosecond()), block.Timestamp.Nanos)
}