-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.go
180 lines (151 loc) · 5.51 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package genutil
// DONTCOVER
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
stakingtypes "github.com/evoblockchain/evochain/x/staking/types"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/codec"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
authexported "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth/exported"
authtypes "github.com/evoblockchain/evochain/libs/cosmos-sdk/x/auth/types"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/genutil/types"
cfg "github.com/evoblockchain/evochain/libs/tendermint/config"
tmtypes "github.com/evoblockchain/evochain/libs/tendermint/types"
)
// GenAppStateFromConfig gets the genesis app state from the config
func GenAppStateFromConfig(cdc *codec.Codec, config *cfg.Config,
initCfg InitConfig, genDoc tmtypes.GenesisDoc,
genAccIterator types.GenesisAccountsIterator,
) (appState json.RawMessage, err error) {
// process genesis transactions, else create default genesis.json
appGenTxs, persistentPeers, err := CollectStdTxs(
cdc, config.Moniker, initCfg.GenTxsDir, genDoc, genAccIterator)
if err != nil {
return appState, err
}
config.P2P.PersistentPeers = persistentPeers
var nodeKeyWhiteList []string
for _, nodeAddr := range strings.Split(persistentPeers, ",") {
nodeKey := strings.Split(nodeAddr, "@")[0]
nodeKeyWhiteList = append(nodeKeyWhiteList, nodeKey)
}
config.Mempool.NodeKeyWhitelist = nodeKeyWhiteList
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 := GenesisStateFromGenDoc(cdc, genDoc)
if err != nil {
return appState, err
}
appGenesisState, err = SetGenTxsInAppGenesisState(cdc, appGenesisState, appGenTxs)
if err != nil {
return appState, err
}
appState, err = codec.MarshalJSONIndent(cdc, appGenesisState)
if err != nil {
return appState, err
}
genDoc.AppState = appState
err = ExportGenesisFile(&genDoc, config.GenesisFile())
return appState, err
}
// CollectStdTxs processes and validates application's genesis StdTxs and returns
// the list of appGenTxs, and persistent peers required to generate genesis.json.
func CollectStdTxs(cdc *codec.Codec, moniker, genTxsDir string,
genDoc tmtypes.GenesisDoc, genAccIterator types.GenesisAccountsIterator,
) (appGenTxs []authtypes.StdTx, persistentPeers string, err error) {
var fos []os.FileInfo
fos, err = ioutil.ReadDir(genTxsDir)
if err != nil {
return appGenTxs, persistentPeers, err
}
// prepare a map of all accounts in genesis state to then validate
// against the validators addresses
var appState map[string]json.RawMessage
if err := cdc.UnmarshalJSON(genDoc.AppState, &appState); err != nil {
return appGenTxs, persistentPeers, err
}
addrMap := make(map[string]authexported.Account)
genAccIterator.IterateGenesisAccounts(cdc, appState,
func(acc authexported.Account) (stop bool) {
addrMap[acc.GetAddress().String()] = acc
return false
},
)
// addresses and IPs (and port) validator server info
var addressesIPs []string
for _, fo := range fos {
if fo == nil {
continue
}
filename := filepath.Join(genTxsDir, fo.Name())
if !fo.IsDir() && (filepath.Ext(filename) != ".json") {
continue
}
// get the genStdTx
var jsonRawTx []byte
if jsonRawTx, err = ioutil.ReadFile(filename); err != nil {
return appGenTxs, persistentPeers, err
}
var genStdTx authtypes.StdTx
if err = cdc.UnmarshalJSON(jsonRawTx, &genStdTx); err != nil {
return appGenTxs, persistentPeers, err
}
appGenTxs = append(appGenTxs, genStdTx)
// genesis transactions must be single-message
msgs := genStdTx.GetMsgs()
if len(msgs) != 1 {
return appGenTxs, persistentPeers, errors.New(
"each genesis transaction must provide a single genesis message")
}
//ignore not create validator tx
msg, ok := msgs[0].(stakingtypes.MsgCreateValidator)
if !ok {
continue
}
// the memo flag is used to store
// the ip and node-id, for example this may be:
// "528fd3df22b31f4969b05652bfe8f0fe921321d5@192.168.2.37:26656"
nodeAddrIP := genStdTx.GetMemo()
if len(nodeAddrIP) == 0 {
return appGenTxs, persistentPeers, fmt.Errorf(
"couldn't find node's address and IP in %s", fo.Name())
}
// TODO abstract out staking message validation back to staking
// validate delegator and validator addresses and funds against the accounts in the state
delAddr := msg.DelegatorAddress.String()
valAddr := sdk.AccAddress(msg.ValidatorAddress).String()
delAcc, delOk := addrMap[delAddr]
if !delOk {
return appGenTxs, persistentPeers, fmt.Errorf(
"account %v not in genesis.json: %+v", delAddr, addrMap)
}
_, valOk := addrMap[valAddr]
if !valOk {
return appGenTxs, persistentPeers, fmt.Errorf(
"account %v not in genesis.json: %+v", valAddr, addrMap)
}
if delAcc.GetCoins().AmountOf(msg.MinSelfDelegation.Denom).LT(msg.MinSelfDelegation.Amount) {
return appGenTxs, persistentPeers, fmt.Errorf(
"insufficient fund for delegation %v: %v < %v",
delAcc.GetAddress(), delAcc.GetCoins().AmountOf(msg.MinSelfDelegation.Denom), msg.MinSelfDelegation.Amount,
)
}
// 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
}