forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_balances_remove.go
103 lines (85 loc) · 2.38 KB
/
genesis_balances_remove.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto"
)
var (
errUnableToLoadGenesis = errors.New("unable to load genesis")
errBalanceNotFound = errors.New("genesis balances entry does not exist")
)
type balancesRemoveCfg struct {
rootCfg *balancesCfg
address string
}
// newBalancesRemoveCmd creates the genesis balances remove subcommand
func newBalancesRemoveCmd(rootCfg *balancesCfg, io commands.IO) *commands.Command {
cfg := &balancesRemoveCfg{
rootCfg: rootCfg,
}
return commands.NewCommand(
commands.Metadata{
Name: "remove",
ShortUsage: "balances remove [flags]",
ShortHelp: "removes the balance information of a specific account",
},
cfg,
func(_ context.Context, _ []string) error {
return execBalancesRemove(cfg, io)
},
)
}
func (c *balancesRemoveCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.address,
"address",
"",
"the address of the account whose balance information should be removed from genesis.json",
)
}
func execBalancesRemove(cfg *balancesRemoveCfg, io commands.IO) error {
// Load the genesis
genesis, loadErr := types.GenesisDocFromFile(cfg.rootCfg.genesisPath)
if loadErr != nil {
return fmt.Errorf("%w, %w", errUnableToLoadGenesis, loadErr)
}
// Validate the address
address, err := crypto.AddressFromString(cfg.address)
if err != nil {
return fmt.Errorf("%w, %w", errInvalidAddress, err)
}
// Check if the genesis state is set at all
if genesis.AppState == nil {
return errAppStateNotSet
}
// Construct the initial genesis balance sheet
state := genesis.AppState.(gnoland.GnoGenesisState)
genesisBalances, err := mapGenesisBalancesFromState(state)
if err != nil {
return err
}
// Check if the genesis balance for the account is present
_, exists := genesisBalances[address]
if !exists {
return errBalanceNotFound
}
// Drop the account pre-mine
delete(genesisBalances, address)
// Save the balances
state.Balances = genesisBalances.List()
genesis.AppState = state
// Save the updated genesis
if err := genesis.SaveAs(cfg.rootCfg.genesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)
}
io.Printfln(
"Pre-mine information for address %s removed",
address.String(),
)
return nil
}