-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-x-traverse.go
194 lines (181 loc) · 5.62 KB
/
cmd-x-traverse.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"context"
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
solanatxmetaparsers "github.com/rpcpool/yellowstone-faithful/solana-tx-meta-parsers"
"github.com/rpcpool/yellowstone-faithful/tooling"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
)
func newCmd_XTraverse() *cli.Command {
return &cli.Command{
Name: "x-traverse",
Description: "Demo of taversing the DAG of a CAR file and printing the contents of each node.",
ArgsUsage: "<car-path> <index-dir>",
Before: func(c *cli.Context) error {
return nil
},
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
carPath := c.Args().Get(0)
indexDir := c.Args().Get(1)
{
simpleIter, err := NewSimpleCarIterator(carPath, indexDir)
if err != nil {
panic(err)
}
defer simpleIter.Close()
startedAt := time.Now()
numSolanaBlocks := 0
numTransactions := 0
defer func() {
klog.Infof("Finished in %s", time.Since(startedAt))
klog.Infof("Read %d Solana blocks", numSolanaBlocks)
klog.Infof("Read %d transactions", numTransactions)
}()
epoch, err := simpleIter.GetEpoch(context.Background())
if err != nil {
panic(err)
}
spew.Dump(epoch)
yes := askForConfirmation("The epoch contains %d subsets. Do you want to continue?", len(epoch.Subsets))
if !yes {
klog.Info("Exiting...")
return nil
}
for subsetIndex, subsetCID := range epoch.Subsets {
subset, err := simpleIter.GetSubset(context.Background(), subsetCID.(cidlink.Link).Cid)
if err != nil {
return fmt.Errorf("failed to get subset: %w", err)
}
yes := askForConfirmation(" Subset %d contains %d blocks. Do you want to continue?", subsetIndex, len(subset.Blocks))
if !yes {
klog.Info("Exiting...")
return nil
}
for blockIndex, blockCID := range subset.Blocks {
block, err := simpleIter.GetBlock(context.Background(), blockCID.(cidlink.Link).Cid)
if err != nil {
return fmt.Errorf("failed to get block: %w", err)
}
yes := askForConfirmation(" Block %d contains %d entries. Do you want to continue?", blockIndex, len(block.Entries))
if !yes {
klog.Info("Exiting...")
return nil
}
for entryIndex, entryCID := range block.Entries {
entry, err := simpleIter.GetEntry(context.Background(), entryCID.(cidlink.Link).Cid)
if err != nil {
return fmt.Errorf("failed to get entry: %w", err)
}
yes := askForConfirmation(" Entry %d contains %d transactions. Do you want to continue?", entryIndex, len(entry.Transactions))
if !yes {
klog.Info("Exiting...")
return nil
}
for _, txCID := range entry.Transactions {
tx, err := simpleIter.GetTransaction(context.Background(), txCID.(cidlink.Link).Cid)
if err != nil {
return fmt.Errorf("failed to get transaction: %w", err)
}
spew.Dump(tx)
{
var transaction solana.Transaction
{
txBuffer, err := tooling.LoadDataFromDataFrames(&tx.Data, simpleIter.GetDataFrame)
if err != nil {
panic(err)
}
if err := bin.UnmarshalBin(&transaction, txBuffer); err != nil {
panic(err)
} else if len(transaction.Signatures) == 0 {
panic("no signatures")
}
}
{
fmt.Println("sig=" + transaction.Signatures[0].String())
fmt.Println(transaction.String())
}
{
metaBuffer, err := tooling.LoadDataFromDataFrames(&tx.Metadata, simpleIter.GetDataFrame)
if err != nil {
panic(err)
}
if len(metaBuffer) > 0 {
uncompressedMeta, err := tooling.DecompressZstd(metaBuffer)
if err != nil {
panic(err)
}
status, err := solanatxmetaparsers.ParseTransactionStatusMeta(uncompressedMeta)
if err != nil {
panic(err)
}
spew.Dump(status)
}
}
}
}
}
}
}
if false {
klog.Info("Iterating Solana blocks...")
err = simpleIter.FindBlocks(context.Background(), func(_ cid.Cid, block *ipldbindcode.Block) error {
numSolanaBlocks++
if numSolanaBlocks%10_000 == 0 {
fmt.Print(".")
}
return nil
})
if err != nil {
panic(err)
}
took := time.Since(startedAt)
klog.Infof("Finished iterating blocks in %s; found %d solana blocks", took, numSolanaBlocks)
klog.Info("Iterating Solana Transactions...")
err = simpleIter.FindTransactions(context.Background(), func(_ cid.Cid, tx *ipldbindcode.Transaction) error {
numTransactions++
if numTransactions%100_000 == 0 {
fmt.Print(".")
}
return nil
})
if err != nil {
panic(err)
}
took = time.Since(startedAt) - took
klog.Infof("Finished iterating transactions in %s; found %d transactions", took, numTransactions)
}
}
return nil
},
}
}
func askForConfirmation(message string, args ...any) bool {
fmt.Printf(message, args...)
fmt.Print(" [y/N]: ")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return askForConfirmation(message)
}
if isStringAnyOf(response, "y", "Y", "yes", "Yes", "YES") {
return true
}
return false
}
func isStringAnyOf(s string, strs ...string) bool {
for _, str := range strs {
if s == str {
return true
}
}
return false
}