forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis_txs_remove.go
108 lines (87 loc) · 2.55 KB
/
genesis_txs_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
104
105
106
107
108
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
)
var (
errAppStateNotSet = errors.New("genesis app state not set")
errNoTxHashSpecified = errors.New("no transaction hashes specified")
errTxNotFound = errors.New("transaction not present in genesis.json")
)
// newTxsRemoveCmd creates the genesis txs remove subcommand
func newTxsRemoveCmd(txsCfg *txsCfg, io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "remove",
ShortUsage: "txs remove <tx-hash ...>",
ShortHelp: "removes the transactions from the genesis.json",
LongHelp: "Removes the transactions using the transaction hash",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execTxsRemove(txsCfg, io, args)
},
)
}
func execTxsRemove(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)
}
// Check if the genesis state is set at all
if genesis.AppState == nil {
return errAppStateNotSet
}
// Make sure the transaction hashes are set
if len(args) == 0 {
return errNoTxHashSpecified
}
state := genesis.AppState.(gnoland.GnoGenesisState)
for _, inputHash := range args {
index := -1
for indx, tx := range state.Txs {
// Find the hash of the transaction
hash, err := getTxHash(tx)
if err != nil {
return fmt.Errorf("unable to generate tx hash, %w", err)
}
// Check if the hashes match
if strings.ToLower(hash) == strings.ToLower(inputHash) {
index = indx
break
}
}
if index < 0 {
return errTxNotFound
}
state.Txs = append(state.Txs[:index], state.Txs[index+1:]...)
io.Printfln(
"Transaction %s removed from genesis.json",
inputHash,
)
}
genesis.AppState = state
// Save the updated genesis
if err := genesis.SaveAs(cfg.genesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)
}
return nil
}
// getTxHash returns the hex hash representation of
// the transaction (Amino encoded)
func getTxHash(tx std.Tx) (string, error) {
encodedTx, err := amino.Marshal(tx)
if err != nil {
return "", fmt.Errorf("unable to marshal transaction, %w", err)
}
txHash := types.Tx(encodedTx).Hash()
return fmt.Sprintf("%X", txHash), nil
}