-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
124 lines (107 loc) · 3.08 KB
/
blocks.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
package tools
import (
"context"
"fmt"
"io"
"path"
"path/filepath"
"strings"
"github.com/streamingfast/bstream"
pbcodec "github.com/zhongshuwen/histnew/pb/dfuse/zswhq/codec/v1"
"github.com/streamingfast/dstore"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var blocksCmd = &cobra.Command{
Use: "print-blocks",
Short: "Prints the content summary of a local merged blocks file",
Args: cobra.ExactArgs(1),
RunE: printBlocksE,
}
func init() {
Cmd.AddCommand(blocksCmd)
blocksCmd.Flags().Bool("transactions", false, "Include transaction IDs in output")
}
func printBlocksE(cmd *cobra.Command, args []string) error {
printTransactions := viper.GetBool("transactions")
file := args[0]
abs, err := filepath.Abs(file)
if err != nil {
return err
}
dir := path.Dir(abs)
storeURL := fmt.Sprintf("file://%s", dir)
compression := ""
if strings.HasSuffix(file, "zst") || strings.HasSuffix(file, "zstd") {
compression = "zstd"
}
store, err := dstore.NewStore(storeURL, "", compression, false)
if err != nil {
return err
}
filename := path.Base(abs)
reader, err := store.OpenObject(context.Background(), filename)
if err != nil {
fmt.Printf("❌ Unable to read blocks filename %s: %s\n", filename, err)
return err
}
defer reader.Close()
readerFactory, err := bstream.GetBlockReaderFactory.New(reader)
if err != nil {
fmt.Printf("❌ Unable to read blocks filename %s: %s\n", filename, err)
return err
}
seenBlockCount := 0
for {
block, err := readerFactory.Read()
if block != nil {
seenBlockCount++
payloadSize := len(block.PayloadBuffer)
eosBlock := block.ToNative().(*pbcodec.Block)
if eosBlock.FilteringApplied {
fmt.Printf("Filtered Block %s (%d bytes): %d/%d transactions (%d/%d traces), %d/%d actions (%d/%d input)\n",
block,
payloadSize,
eosBlock.GetFilteredTransactionCount(),
eosBlock.GetUnfilteredTransactionCount(),
eosBlock.GetFilteredTransactionTraceCount(),
eosBlock.GetUnfilteredTransactionTraceCount(),
eosBlock.GetFilteredExecutedTotalActionCount(),
eosBlock.GetUnfilteredExecutedTotalActionCount(),
eosBlock.GetFilteredExecutedInputActionCount(),
eosBlock.GetUnfilteredExecutedInputActionCount(),
)
} else {
fmt.Printf("Block #%d (%s) (prev: %s) (%d bytes): %d transactions (%d traces), %d actions (%d input)\n",
block.Num(),
block.ID(),
block.PreviousID(),
payloadSize,
eosBlock.GetUnfilteredTransactionCount(),
eosBlock.GetUnfilteredTransactionTraceCount(),
eosBlock.GetUnfilteredExecutedTotalActionCount(),
eosBlock.GetUnfilteredExecutedInputActionCount(),
)
if printTransactions {
fmt.Println("- Transactions: ")
for _, t := range eosBlock.Transactions() {
fmt.Println(" * ", t.Id)
}
fmt.Println("- Transaction traces: ")
for _, t := range eosBlock.TransactionTraces() {
fmt.Println(" * ", t.Id)
}
fmt.Println()
}
}
continue
}
if block == nil && err == io.EOF {
fmt.Printf("Total blocks: %d\n", seenBlockCount)
return nil
}
if err != nil {
return err
}
}
}