/
collect.go
164 lines (133 loc) · 4.83 KB
/
collect.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
package dclgenutil
// DONTCOVER
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
cfg "github.com/tendermint/tendermint/config"
tmtypes "github.com/tendermint/tendermint/types"
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
validatortypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/validator/types"
)
// GenAppStateFromConfig gets the genesis app state from the config.
func GenAppStateFromConfig(cdc codec.JSONCodec, txEncodingConfig client.TxEncodingConfig,
config *cfg.Config, initCfg types.InitConfig, genDoc tmtypes.GenesisDoc, genAccIterator dclauthtypes.GenesisAccountsIterator,
) (appState json.RawMessage, err error) {
// process genesis transactions, else create default genesis.json
appGenTxs, persistentPeers, err := CollectTxs(
cdc, txEncodingConfig.TxJSONDecoder(), config.Moniker, initCfg.GenTxsDir, genDoc, genAccIterator,
)
if err != nil {
return appState, err
}
config.P2P.PersistentPeers = persistentPeers
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
// if there are no gen txs to be processed, return the default empty state
if len(appGenTxs) == 0 {
return appState, errors.New("there must be at least one genesis tx")
}
// create the app state
appGenesisState, err := types.GenesisStateFromGenDoc(genDoc)
if err != nil {
return appState, err
}
appGenesisState, err = SetGenTxsInAppGenesisState(cdc, txEncodingConfig.TxJSONEncoder(), appGenesisState, appGenTxs)
if err != nil {
return appState, err
}
appState, err = json.MarshalIndent(appGenesisState, "", " ")
if err != nil {
return appState, err
}
genDoc.AppState = appState
err = ExportGenesisFile(&genDoc, config.GenesisFile())
return appState, err
}
// CollectTxs processes and validates application's genesis Txs and returns
// the list of appGenTxs, and persistent peers required to generate genesis.json.
func CollectTxs(cdc codec.JSONCodec, txJSONDecoder sdk.TxDecoder, moniker string, genTxsDir string,
genDoc tmtypes.GenesisDoc, genAccIterator dclauthtypes.GenesisAccountsIterator,
) (appGenTxs []sdk.Tx, persistentPeers string, err error) {
// prepare a map of all accounts in genesis state to then validate
// against the validators addresses
var appState map[string]json.RawMessage
if err := json.Unmarshal(genDoc.AppState, &appState); err != nil {
return appGenTxs, persistentPeers, err
}
var fos []os.DirEntry
fos, err = os.ReadDir(genTxsDir)
if err != nil {
return appGenTxs, persistentPeers, err
}
accountsMap := make(map[string]dclauthtypes.GenesisAccount)
genAccIterator.IterateGenesisAccounts(
cdc, appState,
func(account dclauthtypes.GenesisAccount) (stop bool) {
accountsMap[account.GetAddress().String()] = account
return false
},
)
// addresses and IPs (and port) validator server info
var addressesIPs []string
for _, fo := range fos {
if fo.IsDir() {
continue
}
if !strings.HasSuffix(fo.Name(), ".json") {
continue
}
// get the genTx
jsonRawTx, err := os.ReadFile(filepath.Join(genTxsDir, fo.Name()))
if err != nil {
return appGenTxs, persistentPeers, err
}
var genTx sdk.Tx
if genTx, err = txJSONDecoder(jsonRawTx); err != nil {
return appGenTxs, persistentPeers, err
}
appGenTxs = append(appGenTxs, genTx)
// the memo flag is used to store
// the ip and node-id, for example this may be:
// "528fd3df22b31f4969b05652bfe8f0fe921321d5@192.168.2.37:26656"
memoTx, ok := genTx.(sdk.TxWithMemo)
if !ok {
return appGenTxs, persistentPeers, fmt.Errorf("expected TxWithMemo, got %T", genTx)
}
nodeAddrIP := memoTx.GetMemo()
if len(nodeAddrIP) == 0 {
return appGenTxs, persistentPeers, fmt.Errorf("failed to find node's address and IP in %s", fo.Name())
}
// genesis transactions must be single-message
msgs := genTx.GetMsgs()
msg := msgs[0].(*validatortypes.MsgCreateValidator)
// validate validator address against the accounts in the state
valAddr, err := sdk.ValAddressFromBech32(msg.Signer)
if err != nil {
return appGenTxs, persistentPeers, err
}
_, valOk := accountsMap[sdk.AccAddress(valAddr).String()]
if !valOk {
_, file, no, ok := runtime.Caller(1)
if ok {
fmt.Printf("CollectTxs-2, called from %s#%d - %s\n", file, no, sdk.AccAddress(valAddr).String())
}
return appGenTxs, persistentPeers, fmt.Errorf("account %s not in genesis state: %+v", valAddr, accountsMap)
}
// exclude itself from persistent peers
if msg.Description.Moniker != moniker {
addressesIPs = append(addressesIPs, nodeAddrIP)
}
}
sort.Strings(addressesIPs)
persistentPeers = strings.Join(addressesIPs, ",")
return appGenTxs, persistentPeers, nil
}