forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
143 lines (116 loc) · 2.87 KB
/
export.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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
_ "github.com/gnolang/gno/tm2/pkg/sdk/auth" // XXX better way?
_ "github.com/gnolang/gno/tm2/pkg/sdk/bank"
_ "github.com/gnolang/gno/tm2/pkg/sdk/vm"
)
type exportCfg struct {
rootCfg *config
startHeight int64
tailHeight int64
endHeight int64
outFile string
quiet bool
follow bool
}
func newExportCommand(rootCfg *config) *commands.Command {
cfg := &exportCfg{
rootCfg: rootCfg,
}
return commands.NewCommand(
commands.Metadata{
Name: "export",
ShortUsage: "export [flags] <file>",
ShortHelp: "Export transactions to file",
},
cfg,
func(_ context.Context, _ []string) error {
return execExport(cfg)
},
)
}
func (c *exportCfg) RegisterFlags(fs *flag.FlagSet) {
fs.Int64Var(&c.startHeight, "start", 1, "start height")
fs.Int64Var(&c.tailHeight, "tail", 0, "start at LAST - N")
fs.Int64Var(&c.endHeight, "end", 0, "end height (optional)")
fs.StringVar(&c.outFile, "out", defaultFilePath, "output file path")
fs.BoolVar(&c.quiet, "quiet", false, "omit console output during execution")
fs.BoolVar(&c.follow, "follow", false, "keep attached and follow new events")
}
func execExport(c *exportCfg) error {
node := client.NewHTTP(c.rootCfg.remote, "/websocket")
status, err := node.Status()
if err != nil {
return fmt.Errorf("unable to fetch node status, %w", err)
}
var (
start = c.startHeight
end = c.endHeight
tail = c.tailHeight
)
if end == 0 { // take last block height
end = status.SyncInfo.LatestBlockHeight
}
if tail > 0 {
start = end - tail
}
var out io.Writer
switch c.outFile {
case "-", "STDOUT":
out = os.Stdout
default:
out, err = os.OpenFile(c.outFile, os.O_RDWR|os.O_CREATE, 0o755)
if err != nil {
return err
}
}
for height := start; ; height++ {
if !c.follow && height >= end {
break
}
getBlock:
block, err := node.Block(&height)
if err != nil {
if c.follow && strings.Contains(err.Error(), "") {
time.Sleep(time.Second)
goto getBlock
}
return fmt.Errorf("encountered error while fetching block, %w", err)
}
txs := block.Block.Data.Txs
if len(txs) == 0 {
continue
}
_, err = node.BlockResults(&height)
if err != nil {
if c.follow && strings.Contains(err.Error(), "") {
time.Sleep(time.Second)
goto getBlock
}
return fmt.Errorf("encountered error while fetching block results, %w", err)
}
for i := 0; i < len(txs); i++ {
tx := txs[i]
stdtx := std.Tx{}
amino.MustUnmarshal(tx, &stdtx)
bz := amino.MustMarshalJSON(stdtx)
_, _ = fmt.Fprintln(out, string(bz))
}
if !c.quiet {
log.Printf("h=%d/%d (txs=%d)", height, end, len(txs))
}
}
return nil
}