Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.22.0: - resolve v018-v021 issues #278

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RUN LEDGER_ENABLED=false BUILD_TAGS=muslc LINK_STATICALLY=true make build \
&& (file /code/build/bitsongd | grep "statically linked")

# --------------------------------------------------------
FROM alpine:3.16
FROM alpine:3.17

COPY --from=go-builder /code/build/bitsongd /usr/bin/bitsongd

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ help:

APP_DIR = ./app
BINDIR ?= $(GOPATH)/bin
BUILDDIR ?= $(CURDIR)/build

PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')
PACKAGES_UNITTEST=$(shell go list ./... | grep -v '/simulation' | grep -v '/cli_test')
Expand All @@ -40,7 +41,7 @@ COMMIT := $(shell git log -1 --format='%H')

LEDGER_ENABLED ?= true
SDK_PACK := $(shell go list -m github.com/cosmos/cosmos-sdk | sed 's/ /\@/g')
BUILDDIR ?= $(CURDIR)/build


TENDERMINT_VERSION := $(shell go list -m github.com/cometbft/cometbft | sed 's:.* ::') # grab everything after the space in "github.com/tendermint/tendermint v0.34.7"

Expand Down
24 changes: 9 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
wasmlctypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/types"

"cosmossdk.io/api/cosmos/crypto/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/types/bech32"

reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
Expand All @@ -61,7 +61,7 @@ import (
"github.com/cometbft/cometbft/libs/bytes"
tmjson "github.com/cometbft/cometbft/libs/json"
tmos "github.com/cometbft/cometbft/libs/os"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
Expand All @@ -70,15 +70,9 @@ import (

"github.com/bitsongofficial/go-bitsong/app/keepers"
"github.com/bitsongofficial/go-bitsong/app/upgrades"
v010 "github.com/bitsongofficial/go-bitsong/app/upgrades/v010"
v011 "github.com/bitsongofficial/go-bitsong/app/upgrades/v011"
v013 "github.com/bitsongofficial/go-bitsong/app/upgrades/v013"
v014 "github.com/bitsongofficial/go-bitsong/app/upgrades/v014"
v015 "github.com/bitsongofficial/go-bitsong/app/upgrades/v015"
v016 "github.com/bitsongofficial/go-bitsong/app/upgrades/v016"
v018 "github.com/bitsongofficial/go-bitsong/app/upgrades/v018"
v020 "github.com/bitsongofficial/go-bitsong/app/upgrades/v020"

v021 "github.com/bitsongofficial/go-bitsong/app/upgrades/v021"
v022 "github.com/bitsongofficial/go-bitsong/app/upgrades/v022"
// unnamed import of statik for swagger UI support
// _ "github.com/bitsongofficial/go-bitsong/swagger/statik"
)
Expand All @@ -99,9 +93,9 @@ var (
EnableSpecificProposals = ""

Upgrades = []upgrades.Upgrade{
v010.Upgrade, v011.Upgrade, v013.Upgrade, v014.Upgrade,
v015.Upgrade, v016.Upgrade, v018.Upgrade, v020.Upgrade,
v021.Upgrade,
// v010.Upgrade, v011.Upgrade, v013.Upgrade, v014.Upgrade,
// v015.Upgrade, v016.Upgrade, v018.Upgrade, v020.Upgrade,
v021.Upgrade, v022.Upgrade,
}
)

Expand Down Expand Up @@ -397,7 +391,7 @@ func NewBitsongApp(
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}
ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
ctx := app.BaseApp.NewUncachedContext(true, cmtproto.Header{})
// Initialize pinned codes in wasmvm as they are not persisted there
if err := app.AppKeepers.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
tmos.Exit(fmt.Sprintf("failed initialize pinned codes %s", err))
Expand Down Expand Up @@ -660,7 +654,7 @@ func getReflectionService() *runtimeservices.ReflectionService {
// source: https://github.com/osmosis-labs/osmosis/blob/7b1a78d397b632247fe83f51867f319adf3a858c/app/app.go#L786
func InitBitsongAppForTestnet(app *BitsongApp, newValAddr bytes.HexBytes, newValPubKey crypto.PubKey, newOperatorAddress, upgradeToTrigger string) *BitsongApp {

ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{})
ctx := app.BaseApp.NewUncachedContext(true, cmtproto.Header{})
pubkey := &ed25519.PubKey{Key: newValPubKey.Bytes()}
pubkeyAny, err := types.NewAnyWithValue(pubkey)
if err != nil {
Expand Down
298 changes: 298 additions & 0 deletions app/export_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
package app

// to run: bitsongd custom-export --output-document v0.21.5-export.json
// defaults to for zero height
import (
"encoding/json"
"fmt"
"log"

storetypes "cosmossdk.io/store/types"
v022 "github.com/bitsongofficial/go-bitsong/app/upgrades/v022"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"

distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// - validator slash event:
// - historical reference:
//
// x/slashing:
// - validator signing info:
// - historical reference:
func (app *BitsongApp) CustomExportAppStateAndValidators(

forZeroHeight bool, jailAllowedAddrs []string,

) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContext(true)

// We export at last height + 1, because that's the height at which
// Tendermint will start InitChain.
height := app.LastBlockHeight() + 1
if forZeroHeight {
height = 0
app.customTestUpgradeHandlerLogicViaExport(ctx, jailAllowedAddrs)
}

genState, _ := app.mm.ExportGenesis(ctx, app.appCodec)
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.AppKeepers.StakingKeeper)
if err != nil {
return servertypes.ExportedApp{}, err
}
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, nil
}

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
//
// in favour of export at a block height
func (app *BitsongApp) customTestUpgradeHandlerLogicViaExport(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

condJSON := v022.ConditionalJSON{
PatchDelegationCount: 0,
ZeroSharesDelegation: make([]v022.ZeroSharesDelegation, 0),
PatchedDelegation: make([]v022.PatchedDelegation, 0),
DistSlashStore: v022.DistrSlashObject{},
}

// check if there is a allowed address list
if len(jailAllowedAddrs) > 0 {
applyAllowedAddrs = true
}

allowedAddrsMap := make(map[string]bool)
for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromBech32(addr)
if err != nil {
log.Fatal(err)
}
allowedAddrsMap[addr] = true

}
// // debug module
err := v022.CustomV022PatchLogic(ctx, &app.AppKeepers, true)
if err != nil {
panic(err)
}

// simapp export for 0 height state logic:
// withdraw all validator commission
err = app.AppKeepers.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.AppKeepers.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
_, _ = app.AppKeepers.DistrKeeper.WithdrawValidatorCommission(ctx, valBz)
return false
})
if err != nil {
panic(err)
}

// count slashes for all current validators
slashCount := uint64(0)
app.AppKeepers.DistrKeeper.IterateValidatorSlashEvents(ctx,
func(val sdk.ValAddress, _ uint64, vse distrtypes.ValidatorSlashEvent) (stop bool) {
var existing *v022.DistrSlashEvent
for i, obj := range condJSON.DistSlashStore.DistrSlashEvent {
if obj.Val == val.String() {
existing = &condJSON.DistSlashStore.DistrSlashEvent[i]
break
}
} // Create a new DistrSlashEvent from the current event
event := v022.Slash{
Fraction: vse.Fraction.String(),
Period: vse.ValidatorPeriod,
}

// If existing object found, append the event and increment count
if existing != nil {
existing.Slashes = append(existing.Slashes, event)
existing.SlashEventCount++
} else {
// Create a new DistrSlashObject
newSlashObj := v022.DistrSlashEvent{
Val: val.String(),
SlashEventCount: 1,
Slashes: []v022.Slash{event},
}
condJSON.DistSlashStore.DistrSlashEvent = append(condJSON.DistSlashStore.DistrSlashEvent, newSlashObj)
}
slashCount++
return false
})
condJSON.DistSlashStore.SlashEventCount = slashCount

// /* Just to be safe, assert the invariants on current state. */
app.AppKeepers.CrisisKeeper.AssertInvariants(ctx)

app.AppKeepers.StakingKeeper.IterateAllDelegations(ctx, func(del stakingtypes.Delegation) (stop bool) {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
// delAddr := sdk.AccAddress(del.DelegatorAddress)
val, err := app.AppKeepers.StakingKeeper.GetValidator(ctx, valAddr)
if err != nil {
panic(err)
}
endingPeriod, err := app.AppKeepers.DistrKeeper.IncrementValidatorPeriod(ctx, val)
if err != nil {
panic(err)
}
_, err = app.AppKeepers.DistrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
if err != nil {
panic(err)
}

return false

})

// withdraw all delegator rewards
dels, err := app.AppKeepers.StakingKeeper.GetAllDelegations(ctx)
if err != nil {
panic(err)
}

// clear validator slash events
app.AppKeepers.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)

// clear validator historical rewards
app.AppKeepers.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)

// reinitialize all validators
err = app.AppKeepers.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.AppKeepers.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps, err := app.AppKeepers.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
panic(err)
}
feePool, err := app.AppKeepers.DistrKeeper.FeePool.Get(ctx)
if err != nil {
panic(err)
}
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
if err := app.AppKeepers.DistrKeeper.FeePool.Set(ctx, feePool); err != nil {
panic(err)
}

if err := app.AppKeepers.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); err != nil {
panic(err)
}
return false
})
if err != nil {
panic(err)
}

// reinitialize all delegations
for _, del := range dels {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
delAddr := sdk.MustAccAddressFromBech32(del.DelegatorAddress)

if err := app.AppKeepers.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr); err != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
}

if err := app.AppKeepers.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr); err != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
}
}
// iterate through redelegations, reset creation height
app.AppKeepers.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red stakingtypes.Redelegation) (stop bool) {
for i := range red.Entries {
red.Entries[i].CreationHeight = 0
}
err = app.AppKeepers.StakingKeeper.SetRedelegation(ctx, red)
if err != nil {
panic(err)
}
return false
})
// iterate through unbonding delegations, reset creation height
app.AppKeepers.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd stakingtypes.UnbondingDelegation) (stop bool) {
for i := range ubd.Entries {
ubd.Entries[i].CreationHeight = 0
}
err = app.AppKeepers.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
if err != nil {
panic(err)
}
return false
})

// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.GetKey(stakingtypes.StoreKey))
iter := storetypes.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)

for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, err := app.AppKeepers.StakingKeeper.GetValidator(ctx, addr)
if err != nil {
panic("expected validator, not found")
}

validator.UnbondingHeight = 0
if applyAllowedAddrs && !allowedAddrsMap[addr.String()] {
validator.Jailed = true
}

app.AppKeepers.StakingKeeper.SetValidator(ctx, validator)
counter++
}

if err := iter.Close(); err != nil {
app.Logger().Error("error while closing the key-value store reverse prefix iterator: ", err)
return
}

_, err = app.AppKeepers.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
log.Fatal(err)
}

/* Handle slashing state. */

// reset start height on signing infos
app.AppKeepers.SlashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashingtypes.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.AppKeepers.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
return false
},
)

// Marshal the ConditionalJSON object to JSON
v022.PrintConditionalJsonLogs(condJSON, "conditional.json")

}
Loading
Loading