-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
93 lines (86 loc) · 2.61 KB
/
export.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
package app
import (
"encoding/json"
"time"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/encoding"
pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/furyanrasta/furya/x/poe/contract"
)
// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *FuryaApp) ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (servertypes.ExportedApp, error) {
if forZeroHeight {
panic("zero height export not supported")
}
// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}).
WithBlockTime(time.Now().UTC())
// We export at last height + 1, because that's the height at which
// Tendermint will start InitChain.
height := app.LastBlockHeight() + 1
genState := app.mm.ExportGenesis(ctx, app.appCodec)
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
}
validators, err := activeValidatorSet(app, ctx)
if err != nil {
return servertypes.ExportedApp{}, err
}
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, err
}
func activeValidatorSet(app *FuryaApp, ctx sdk.Context) ([]tmtypes.GenesisValidator, error) {
var result []tmtypes.GenesisValidator
valset := app.poeKeeper.ValsetContract(ctx)
var err error
xerr := valset.IterateActiveValidators(ctx, func(c contract.ValidatorInfo) bool {
var opAddr sdk.AccAddress
opAddr, err = sdk.AccAddressFromBech32(c.Operator)
if err != nil {
return true
}
var pk pc.PublicKey
pk, err = contract.ConvertToTendermintPubKey(c.ValidatorPubkey)
if err != nil {
return true
}
var tmPk tmcrypto.PubKey
tmPk, err = encoding.PubKeyFromProto(pk)
if err != nil {
return true
}
var meta *stakingtypes.Validator
meta, err = valset.QueryValidator(ctx, opAddr)
if err != nil {
return true
}
moniker := ""
if meta != nil {
moniker = meta.GetMoniker()
}
result = append(result, tmtypes.GenesisValidator{
Address: tmPk.Address(),
PubKey: tmPk,
Power: int64(c.Power),
Name: moniker,
})
return false
}, nil)
if xerr != nil {
return nil, xerr
}
return result, err
}