forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis_txs_add_sheet.go
87 lines (73 loc) · 2.09 KB
/
genesis_txs_add_sheet.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
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
)
var (
errInvalidTxsFile = errors.New("unable to open transactions file")
errNoTxsFileSpecified = errors.New("no txs file specified")
)
// newTxsAddSheetCmd creates the genesis txs add sheet subcommand
func newTxsAddSheetCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "sheets",
ShortUsage: "txs add sheets <sheet-path ...>",
ShortHelp: "imports transactions from the given sheets into the genesis.json",
LongHelp: "Imports the transactions from a given transactions sheet to the genesis.json",
},
commands.NewEmptyConfig(),
func(ctx context.Context, args []string) error {
return execTxsAddSheet(ctx, txsCfg, io, args)
},
)
}
func execTxsAddSheet(
ctx context.Context,
cfg *txsCfg,
io commands.IO,
args []string,
) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("unable to load genesis, %w", loadErr)
}
// Open the transactions files
if len(args) == 0 {
return errNoTxsFileSpecified
}
parsedTxs := make([]std.Tx, 0)
for _, file := range args {
file, loadErr := os.Open(file)
if loadErr != nil {
return fmt.Errorf("%w, %w", errInvalidTxsFile, loadErr)
}
txs, err := std.ParseTxs(ctx, file)
if err != nil {
return fmt.Errorf("unable to parse file, %w", err)
}
if err = file.Close(); err != nil {
return fmt.Errorf("unable to gracefully close file, %w", err)
}
parsedTxs = append(parsedTxs, txs...)
}
// Save the txs to the genesis.json
if err := appendGenesisTxs(genesis, parsedTxs); err != nil {
return fmt.Errorf("unable to append genesis transactions, %w", err)
}
// Save the updated genesis
if err := genesis.SaveAs(cfg.genesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)
}
io.Printfln(
"Saved %d transactions to genesis.json",
len(parsedTxs),
)
return nil
}