Skip to content

Commit 7c16211

Browse files
Allow state exporting from any directory (#3608) (#3614)
* Allow exporting from any directory * Changelog (cherry picked from commit da4f503) Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
1 parent c0224eb commit 7c16211

File tree

3 files changed

+8
-162
lines changed

3 files changed

+8
-162
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4848

4949
### Bug fixes
5050

51+
* [#3608](https://github.com/osmosis-labs/osmosis/pull/3608) Make it possible to state export from any directory.
52+
5153
### Misc Improvements
5254

5355

app/export.go

Lines changed: 2 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ package app
22

33
import (
44
"encoding/json"
5-
"log"
5+
"fmt"
66

77
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
88

99
servertypes "github.com/cosmos/cosmos-sdk/server/types"
1010
sdk "github.com/cosmos/cosmos-sdk/types"
11-
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
1211
"github.com/cosmos/cosmos-sdk/x/staking"
13-
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
1412
)
1513

1614
// ExportAppStateAndValidators exports the state of the application for a genesis
@@ -25,8 +23,7 @@ func (app *OsmosisApp) ExportAppStateAndValidators(
2523
// Tendermint will start InitChain.
2624
height := app.LastBlockHeight() + 1
2725
if forZeroHeight {
28-
height = 0
29-
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
26+
return servertypes.ExportedApp{}, fmt.Errorf("forZeroHeight not supported")
3027
}
3128

3229
genState := app.mm.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
@@ -44,158 +41,6 @@ func (app *OsmosisApp) ExportAppStateAndValidators(
4441
}, err
4542
}
4643

47-
// prepare for fresh start at zero height
48-
// NOTE zero height genesis is a temporary feature which will be deprecated
49-
//
50-
// in favour of export at a block height
51-
func (app *OsmosisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
52-
applyAllowedAddrs := false
53-
54-
// check if there is a allowed address list
55-
if len(jailAllowedAddrs) > 0 {
56-
applyAllowedAddrs = true
57-
}
58-
59-
allowedAddrsMap := make(map[string]bool)
60-
61-
for _, addr := range jailAllowedAddrs {
62-
_, err := sdk.ValAddressFromBech32(addr)
63-
if err != nil {
64-
log.Fatal(err)
65-
}
66-
allowedAddrsMap[addr] = true
67-
}
68-
69-
/* Just to be safe, assert the invariants on current state. */
70-
app.CrisisKeeper.AssertInvariants(ctx)
71-
72-
/* Handle fee distribution state. */
73-
74-
// withdraw all validator commission
75-
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
76-
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
77-
return false
78-
})
79-
80-
// withdraw all delegator rewards
81-
dels := app.StakingKeeper.GetAllDelegations(ctx)
82-
for _, delegation := range dels {
83-
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
84-
if err != nil {
85-
panic(err)
86-
}
87-
88-
delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
89-
if err != nil {
90-
panic(err)
91-
}
92-
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
93-
}
94-
95-
// clear validator slash events
96-
app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)
97-
98-
// clear validator historical rewards
99-
app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
100-
101-
// set context height to zero
102-
height := ctx.BlockHeight()
103-
ctx = ctx.WithBlockHeight(0)
104-
105-
// reinitialize all validators
106-
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
107-
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
108-
scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
109-
feePool := app.DistrKeeper.GetFeePool(ctx)
110-
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
111-
app.DistrKeeper.SetFeePool(ctx, feePool)
112-
113-
app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
114-
return false
115-
})
116-
117-
// reinitialize all delegations
118-
for _, del := range dels {
119-
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
120-
if err != nil {
121-
panic(err)
122-
}
123-
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
124-
if err != nil {
125-
panic(err)
126-
}
127-
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
128-
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
129-
}
130-
131-
// reset context height
132-
ctx = ctx.WithBlockHeight(height)
133-
134-
/* Handle staking state. */
135-
136-
// iterate through redelegations, reset creation height
137-
app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
138-
for i := range red.Entries {
139-
red.Entries[i].CreationHeight = 0
140-
}
141-
app.StakingKeeper.SetRedelegation(ctx, red)
142-
return false
143-
})
144-
145-
// iterate through unbonding delegations, reset creation height
146-
app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
147-
for i := range ubd.Entries {
148-
ubd.Entries[i].CreationHeight = 0
149-
}
150-
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
151-
return false
152-
})
153-
154-
// Iterate through validators by power descending, reset bond heights, and
155-
// update bond intra-tx counters.
156-
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
157-
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
158-
counter := int16(0)
159-
160-
for ; iter.Valid(); iter.Next() {
161-
addr := sdk.ValAddress(iter.Key()[1:])
162-
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
163-
if !found {
164-
panic("expected validator, not found")
165-
}
166-
167-
validator.UnbondingHeight = 0
168-
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
169-
validator.Jailed = true
170-
}
171-
172-
app.StakingKeeper.SetValidator(ctx, validator)
173-
counter++
174-
}
175-
176-
err := iter.Close()
177-
if err != nil {
178-
log.Fatal(err)
179-
}
180-
181-
_, err = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
182-
if err != nil {
183-
log.Fatal(err)
184-
}
185-
186-
/* Handle slashing state. */
187-
188-
// reset start height on signing infos
189-
app.SlashingKeeper.IterateValidatorSigningInfos(
190-
ctx,
191-
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
192-
info.StartHeight = 0
193-
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
194-
return false
195-
},
196-
)
197-
}
198-
19944
func (app *OsmosisApp) ExportState(ctx sdk.Context) map[string]json.RawMessage {
20045
return app.mm.ExportGenesis(ctx, app.AppCodec())
20146
}

cmd/osmosisd/cmd/root.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,14 @@ func createOsmosisAppAndExport(
293293
) (servertypes.ExportedApp, error) {
294294
encCfg := osmosis.MakeEncodingConfig() // Ideally, we would reuse the one created by NewRootCmd.
295295
encCfg.Marshaler = codec.NewProtoCodec(encCfg.InterfaceRegistry)
296-
var app *osmosis.OsmosisApp
297-
if height != -1 {
298-
app = osmosis.NewOsmosisApp(logger, db, traceStore, false, map[int64]bool{}, "", uint(1), appOpts, osmosis.GetWasmEnabledProposals(), osmosis.EmptyWasmOpts)
296+
loadLatest := height == -1
297+
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
298+
app := osmosis.NewOsmosisApp(logger, db, traceStore, loadLatest, map[int64]bool{}, homeDir, 0, appOpts, osmosis.GetWasmEnabledProposals(), osmosis.EmptyWasmOpts)
299299

300+
if !loadLatest {
300301
if err := app.LoadHeight(height); err != nil {
301302
return servertypes.ExportedApp{}, err
302303
}
303-
} else {
304-
app = osmosis.NewOsmosisApp(logger, db, traceStore, true, map[int64]bool{}, "", uint(1), appOpts, osmosis.GetWasmEnabledProposals(), osmosis.EmptyWasmOpts)
305304
}
306305

307306
return app.ExportAppStateAndValidators(forZeroHeight, jailWhiteList, modulesToExport)

0 commit comments

Comments
 (0)