-
Notifications
You must be signed in to change notification settings - Fork 271
/
document.go
123 lines (105 loc) · 3.74 KB
/
document.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
package genesis
import (
"encoding/json"
"fmt"
"time"
"github.com/celestiaorg/celestia-app/v2/app"
"github.com/celestiaorg/celestia-app/v2/app/encoding"
"github.com/celestiaorg/celestia-app/v2/pkg/appconsts"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
coretypes "github.com/tendermint/tendermint/types"
)
// Document will create a valid genesis doc with funded addresses.
func Document(
ecfg encoding.Config,
params *tmproto.ConsensusParams,
chainID string,
gentxs []json.RawMessage,
accounts []Account,
mods ...Modifier,
) (*coretypes.GenesisDoc, error) {
genutilGenState := genutiltypes.DefaultGenesisState()
genutilGenState.GenTxs = gentxs
genBals, genAccs, err := accountsToSDKTypes(accounts)
if err != nil {
return nil, fmt.Errorf("converting accounts into sdk types: %w", err)
}
sdkAccounts, err := authtypes.PackAccounts(genAccs)
if err != nil {
return nil, fmt.Errorf("packing accounts: %w", err)
}
authGenState := authtypes.DefaultGenesisState()
bankGenState := banktypes.DefaultGenesisState()
authGenState.Accounts = append(authGenState.Accounts, sdkAccounts...)
bankGenState.Balances = append(bankGenState.Balances, genBals...)
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
// perform some basic validation of the genesis state
if err := authtypes.ValidateGenesis(*authGenState); err != nil {
return nil, err
}
if err := bankGenState.Validate(); err != nil {
return nil, err
}
if err := genutiltypes.ValidateGenesis(genutilGenState, ecfg.TxConfig.TxJSONDecoder()); err != nil {
return nil, err
}
state := app.ModuleBasics.DefaultGenesis(ecfg.Codec)
state[authtypes.ModuleName] = ecfg.Codec.MustMarshalJSON(authGenState)
state[banktypes.ModuleName] = ecfg.Codec.MustMarshalJSON(bankGenState)
state[genutiltypes.ModuleName] = ecfg.Codec.MustMarshalJSON(genutilGenState)
for _, modifier := range mods {
state = modifier(state)
}
stateBz, err := json.MarshalIndent(state, "", " ")
if err != nil {
return nil, fmt.Errorf("marshalling genesis state: %w", err)
}
// Create the genesis doc
genesisDoc := &coretypes.GenesisDoc{
ChainID: chainID,
GenesisTime: time.Now(),
ConsensusParams: params,
AppState: stateBz,
}
return genesisDoc, nil
}
// accountsToSDKTypes converts the genesis accounts to native SDK types.
func accountsToSDKTypes(accounts []Account) ([]banktypes.Balance, []authtypes.GenesisAccount, error) {
genBals := make([]banktypes.Balance, len(accounts))
genAccs := make([]authtypes.GenesisAccount, len(accounts))
hasMap := make(map[string]struct{})
for i, account := range accounts {
if err := account.ValidateBasic(); err != nil {
return nil, nil, fmt.Errorf("invalid account %d: %v", i, err)
}
addr := sdk.AccAddress(account.PubKey.Address())
if _, ok := hasMap[addr.String()]; ok {
return nil, nil, fmt.Errorf("duplicate account address %s", addr)
}
hasMap[addr.String()] = struct{}{}
balances := sdk.NewCoins(
sdk.NewCoin(appconsts.BondDenom, sdk.NewInt(account.Balance)),
)
genBals[i] = banktypes.Balance{Address: addr.String(), Coins: balances.Sort()}
genAccs[i] = authtypes.NewBaseAccount(addr, account.PubKey, uint64(i), 0)
}
return genBals, genAccs, nil
}
type Account struct {
PubKey cryptotypes.PubKey
Balance int64
}
func (ga Account) ValidateBasic() error {
if ga.PubKey == nil {
return fmt.Errorf("pubkey cannot be empty")
}
if ga.Balance <= 0 {
return fmt.Errorf("balance must be greater than 0")
}
return nil
}