-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathgentx.go
115 lines (94 loc) · 3.38 KB
/
gentx.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
package genutil
import (
"context"
"encoding/json"
"fmt"
"strings"
bankexported "cosmossdk.io/x/bank/exported"
stakingtypes "cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/genutil/types"
)
// SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state
func SetGenTxsInAppGenesisState(
cdc codec.JSONCodec, txJSONEncoder sdk.TxEncoder, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx,
) (map[string]json.RawMessage, error) {
genesisState := types.GetGenesisStateFromAppState(cdc, appGenesisState)
genTxsBz := make([]json.RawMessage, 0, len(genTxs))
for _, genTx := range genTxs {
txBz, err := txJSONEncoder(genTx)
if err != nil {
return appGenesisState, err
}
genTxsBz = append(genTxsBz, txBz)
}
genesisState.GenTxs = genTxsBz
return types.SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil
}
// ValidateAccountInGenesis checks that the provided account has a sufficient
// balance in the set of genesis accounts.
func ValidateAccountInGenesis(
appGenesisState map[string]json.RawMessage, genBalIterator types.GenesisBalancesIterator,
addr string, coins sdk.Coins, cdc codec.JSONCodec,
) error {
var stakingData stakingtypes.GenesisState
cdc.MustUnmarshalJSON(appGenesisState[stakingtypes.ModuleName], &stakingData)
bondDenom := stakingData.Params.BondDenom
var err error
accountIsInGenesis := false
genBalIterator.IterateGenesisBalances(cdc, appGenesisState,
func(bal bankexported.GenesisBalance) (stop bool) {
accAddress := bal.GetAddress()
accCoins := bal.GetCoins()
// ensure that account is in genesis
if strings.EqualFold(accAddress, addr) {
// ensure account contains enough funds of default bond denom
if coins.AmountOf(bondDenom).GT(accCoins.AmountOf(bondDenom)) {
err = fmt.Errorf(
"account %s has a balance in genesis, but it only has %v%s available to stake, not %v%s",
addr, accCoins.AmountOf(bondDenom), bondDenom, coins.AmountOf(bondDenom), bondDenom,
)
return true
}
accountIsInGenesis = true
return true
}
return false
},
)
if err != nil {
return err
}
if !accountIsInGenesis {
return fmt.Errorf("account %s does not have a balance in the genesis state", addr)
}
return nil
}
// DeliverGenTxs iterates over all genesis txs, decodes each into a Tx and
// invokes the provided deliverTxfn with the decoded Tx. It returns the result
// of the staking module's ApplyAndReturnValidatorSetUpdates.
// NOTE: This isn't used in server/v2 applications.
func DeliverGenTxs(
ctx context.Context, genTxs []json.RawMessage,
stakingKeeper types.StakingKeeper, deliverTx TxHandler,
txEncodingConfig client.TxEncodingConfig,
) ([]module.ValidatorUpdate, error) {
for _, genTx := range genTxs {
tx, err := txEncodingConfig.TxJSONDecoder()(genTx)
if err != nil {
return nil, fmt.Errorf("failed to decode GenTx '%s': %w", genTx, err)
}
bz, err := txEncodingConfig.TxEncoder()(tx)
if err != nil {
return nil, fmt.Errorf("failed to encode GenTx '%s': %w", genTx, err)
}
err = deliverTx.ExecuteGenesisTx(bz)
if err != nil {
return nil, fmt.Errorf("failed to execute DeliverTx for '%s': %w", genTx, err)
}
}
return stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
}