From 0b9ce4059ce144590f30629784b0bcd5cf9d6716 Mon Sep 17 00:00:00 2001 From: Joe Abbey Date: Wed, 4 May 2022 14:02:20 -0400 Subject: [PATCH] Veritas --- app/app.go | 4 +- app/upgrade/constants.go | 4 +- app/upgrade/upgrade_handler.go | 110 ++------------- app/upgrade/upgrade_test.go | 239 --------------------------------- 4 files changed, 16 insertions(+), 341 deletions(-) delete mode 100644 app/upgrade/upgrade_test.go diff --git a/app/app.go b/app/app.go index 08c556f9d..02868d150 100644 --- a/app/app.go +++ b/app/app.go @@ -16,7 +16,7 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" - unity "github.com/CosmosContracts/juno/app/upgrade" + veritas "github.com/CosmosContracts/juno/app/upgrade" "github.com/CosmosContracts/juno/docs" "github.com/CosmosContracts/juno/x/mint" mintkeeper "github.com/CosmosContracts/juno/x/mint/keeper" @@ -756,7 +756,7 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) { // RegisterUpgradeHandlers returns upgrade handlers func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) { bankBaseKeeper, _ := app.BankKeeper.(bankkeeper.BaseKeeper) - app.UpgradeKeeper.SetUpgradeHandler(unity.UpgradeName, unity.CreateUpgradeHandler(app.mm, cfg, &app.StakingKeeper, &bankBaseKeeper)) + app.UpgradeKeeper.SetUpgradeHandler(veritas.UpgradeName, veritas.CreateUpgradeHandler(app.mm, cfg, &app.StakingKeeper, &bankBaseKeeper)) } // GetMaccPerms returns a copy of the module account permissions diff --git a/app/upgrade/constants.go b/app/upgrade/constants.go index 8f94cf500..818ccf2fd 100644 --- a/app/upgrade/constants.go +++ b/app/upgrade/constants.go @@ -1,4 +1,4 @@ -package unity +package veritas // UpgradeName is upgrade name in proposal -const UpgradeName = "unity" +const UpgradeName = "veritas" diff --git a/app/upgrade/upgrade_handler.go b/app/upgrade/upgrade_handler.go index e8d834434..2f57dedf1 100644 --- a/app/upgrade/upgrade_handler.go +++ b/app/upgrade/upgrade_handler.go @@ -1,4 +1,4 @@ -package unity +package veritas import ( sdk "github.com/cosmos/cosmos-sdk/types" @@ -9,113 +9,27 @@ import ( bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" ) -// Address of the account which will have all juno sent to the unity proposal -var addressesToBeAdjusted = []string{ - "juno1aeh8gqu9wr4u8ev6edlgfq03rcy6v5twfn0ja8", -} - // UnityContractByteAddress is the bytes of the public key for the address of the Unity contract // $ junod keys parse juno1nz96hjc926e6a74gyvkwvtt0qhu22wx049c6ph6f4q8kp3ffm9xq5938mr // human: juno -// bytes: 5BEF9E5318ED6716A11179C70B06656E9FB91D241A1C594F344B325D9110D94C -const UnityContractByteAddress = "5BEF9E5318ED6716A11179C70B06656E9FB91D241A1C594F344B325D9110D94C" - -// ClawbackCoinFromAccount undelegate all amount in adjusted address (bypass 28 day unbonding), and send it to dead address -func ClawbackCoinFromAccount(ctx sdk.Context, accAddr sdk.AccAddress, staking *stakingkeeper.Keeper, bank *bankkeeper.BaseKeeper) { - bondDenom := staking.BondDenom(ctx) - - now := ctx.BlockHeader().Time - - // this loop will complete all delegator's active redelegations - for _, activeRedelegation := range staking.GetRedelegations(ctx, accAddr, 65535) { - // src/dest validator addresses of this redelegation - redelegationSrc, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorSrcAddress) - redelegationDst, _ := sdk.ValAddressFromBech32(activeRedelegation.ValidatorDstAddress) - - // set all entry completionTime to now so we can complete redelegation - for i := range activeRedelegation.Entries { - activeRedelegation.Entries[i].CompletionTime = now - } - - staking.SetRedelegation(ctx, activeRedelegation) - _, err := staking.CompleteRedelegation(ctx, accAddr, redelegationSrc, redelegationDst) - if err != nil { - panic(err) - } - } - - // this loop will turn all delegator's delegations into unbonding delegations - for _, delegation := range staking.GetAllDelegatorDelegations(ctx, accAddr) { - validatorValAddr := delegation.GetValidatorAddr() - _, found := staking.GetValidator(ctx, validatorValAddr) - if !found { - continue - } - _, err := staking.Undelegate(ctx, accAddr, validatorValAddr, delegation.GetShares()) //nolint:errcheck // nolint because otherwise we'd have a time and nothing to do with it. - if err != nil { - panic(err) - } - } - - // this loop will complete all delegator's unbonding delegations - for _, unbondingDelegation := range staking.GetAllUnbondingDelegations(ctx, accAddr) { - // validator address of this unbonding delegation - validatorStringAddr := unbondingDelegation.ValidatorAddress - validatorValAddr, _ := sdk.ValAddressFromBech32(validatorStringAddr) - - // set all entry completionTime to now so we can complete unbonding delegation - for i := range unbondingDelegation.Entries { - unbondingDelegation.Entries[i].CompletionTime = now - } - staking.SetUnbondingDelegation(ctx, unbondingDelegation) - _, err := staking.CompleteUnbonding(ctx, accAddr, validatorValAddr) - if err != nil { - panic(err) - } - } - - // account balance after finishing unbonding - accCoin := bank.GetBalance(ctx, accAddr, bondDenom) +// bytes: 988BABCB0556B3AEFAA8232CE62D6F05F8A538CFA971A0DF49A80F60C529D94C +const UnityContractByteAddress = "988BABCB0556B3AEFAA8232CE62D6F05F8A538CFA971A0DF49A80F60C529D94C" - // get Unity Contract Address and send coin to this address - destAcc, _ := sdk.AccAddressFromHex(UnityContractByteAddress) - err := bank.SendCoins(ctx, accAddr, destAcc, sdk.NewCoins(accCoin)) - if err != nil { - panic(err) - } -} +const UnityContractPlaceHolderAddress = "5BEF9E5318ED6716A11179C70B06656E9FB91D241A1C594F344B325D9110D94C" // CreateUpgradeHandler make upgrade handler func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, staking *stakingkeeper.Keeper, bank *bankkeeper.BaseKeeper) upgradetypes.UpgradeHandler { return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { - for _, addrString := range addressesToBeAdjusted { - accAddr, _ := sdk.AccAddressFromBech32(addrString) - ClawbackCoinFromAccount(ctx, accAddr, staking, bank) - } - // force an update of validator min commission - // we already did this for moneta - // but validators could have snuck in changes in the - // interim - // and via state sync to post-moneta - validators := staking.GetAllValidators(ctx) - // hard code this because we don't want - // a) a fork or - // b) immediate reaction with additional gov props - minCommissionRate := sdk.NewDecWithPrec(5, 2) - for _, v := range validators { - if v.Commission.Rate.LT(minCommissionRate) { - if v.Commission.MaxRate.LT(minCommissionRate) { - v.Commission.MaxRate = minCommissionRate - } - - v.Commission.Rate = minCommissionRate - v.Commission.UpdateTime = ctx.BlockHeader().Time + bondDenom := staking.BondDenom(ctx) - // call the before-modification hook since we're about to update the commission - staking.BeforeValidatorModified(ctx, v.GetOperator()) + accAddr, _ := sdk.AccAddressFromBech32(UnityContractPlaceHolderAddress) + accCoin := bank.GetBalance(ctx, accAddr, bondDenom) - staking.SetValidator(ctx, v) - } + // get Unity Contract Address and send coin to this address + destAcc, _ := sdk.AccAddressFromHex(UnityContractByteAddress) + err := bank.SendCoins(ctx, accAddr, destAcc, sdk.NewCoins(accCoin)) + if err != nil { + panic(err) } // Set wasm old version to 1 if we want to call wasm's InitGenesis ourselves diff --git a/app/upgrade/upgrade_test.go b/app/upgrade/upgrade_test.go deleted file mode 100644 index aeb6fc6ab..000000000 --- a/app/upgrade/upgrade_test.go +++ /dev/null @@ -1,239 +0,0 @@ -package unity_test - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/suite" - - "github.com/CosmosContracts/juno/app" - junoapp "github.com/CosmosContracts/juno/app" - unity "github.com/CosmosContracts/juno/app/upgrade" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - - cosmossimapp "github.com/cosmos/cosmos-sdk/simapp" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/tendermint/starport/starport/pkg/cosmoscmd" -) - -var ( - priv1 = secp256k1.GenPrivKey() - addr1 = sdk.AccAddress(priv1.PubKey().Address()) - priv2 = secp256k1.GenPrivKey() - addr2 = sdk.AccAddress(priv2.PubKey().Address()) - - valKey = ed25519.GenPrivKey() - - commissionRates = types.NewCommissionRates(sdk.NewDecWithPrec(5, 2), sdk.NewDecWithPrec(5, 2), sdk.NewDecWithPrec(5, 2)) -) - -var ( - genTokens = sdk.NewIntFromUint64(100000000000) - bondTokens = sdk.NewIntFromUint64(80000000000) - escapeBondTokens = sdk.NewIntFromUint64(25000000000) - genCoin = sdk.NewCoin(sdk.DefaultBondDenom, genTokens) - bondCoin = sdk.NewCoin(sdk.DefaultBondDenom, bondTokens) - escapeBondCoin = sdk.NewCoin(sdk.DefaultBondDenom, escapeBondTokens) - maxJunoPerAcc = sdk.NewIntFromUint64(50000000000) -) - -type UpgradeTestSuite struct { - suite.Suite - - ctx sdk.Context - app *junoapp.App -} - -/* - Test site for unity -*/ - -func (suite *UpgradeTestSuite) TestAdjustFunds() { - initialBondPool := sdk.ZeroInt() - initialUnbondPool := sdk.ZeroInt() - - testCases := []struct { - msg string - pre_adjust_funds func() - adjust_funds func() - post_adjust_funds func() - expPass bool - }{ - { - "Test adjusting funds for unity", - func() { - suite.ctx = suite.app.BaseApp.NewContext(true, tmproto.Header{}) - - initialBondPool = suite.app.BankKeeper.GetBalance(suite.ctx, suite.app.StakingKeeper.GetBondedPool(suite.ctx).GetAddress(), "stake").Amount - initialUnbondPool = suite.app.BankKeeper.GetBalance(suite.ctx, suite.app.StakingKeeper.GetNotBondedPool(suite.ctx).GetAddress(), "stake").Amount - - // 1. check if bond pool has correct acc2 delegation - delegation := suite.app.StakingKeeper.GetDelegatorDelegations(suite.ctx, addr2, 120)[0] - require.Equal(suite.T(), delegation.Shares.RoundInt(), initialBondPool.Sub(bondTokens)) - - // 2. check if unbond pool has correct acc2 undelegation - undelegation := suite.app.StakingKeeper.GetAllUnbondingDelegations(suite.ctx, addr2)[0].Entries[0].Balance - require.Equal(suite.T(), undelegation, initialUnbondPool) - }, - func() { - header := tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - suite.app.BeginBlock(abci.RequestBeginBlock{Header: header}) - - // unbond the accAddr delegations, send all the unbonding and unbonded tokens to the community pool - bankBaseKeeper, _ := suite.app.BankKeeper.(bankkeeper.BaseKeeper) - - // move all juno from acc to community pool (uncluding bonded juno) - unity.ClawbackCoinFromAccount(suite.ctx, addr2, &suite.app.StakingKeeper, &bankBaseKeeper) - - suite.app.EndBlock(abci.RequestEndBlock{}) - suite.app.Commit() - }, - func() { - // 1. check if fund is moved from unbond and bond pool to community pool - // acc2 is supposed to lose bondTokens amount to community pool - afterBondPool := suite.app.BankKeeper.GetBalance(suite.ctx, suite.app.StakingKeeper.GetBondedPool(suite.ctx).GetAddress(), "stake").Amount - afterUnbondPool := suite.app.BankKeeper.GetBalance(suite.ctx, suite.app.StakingKeeper.GetNotBondedPool(suite.ctx).GetAddress(), "stake").Amount - - initial := initialBondPool.Add(initialUnbondPool) - later := afterBondPool.Add(afterUnbondPool) - require.Equal(suite.T(), initial.Sub(bondTokens), later) - - // 2. check if acc2 has 0 juno - afterAcc2Amount := suite.app.BankKeeper.GetBalance(suite.ctx, addr2, sdk.DefaultBondDenom).Amount - require.Equal(suite.T(), sdk.ZeroInt(), afterAcc2Amount) - - // 3. check if all unbonding delegations are removed - unbondDels := suite.app.StakingKeeper.GetAllUnbondingDelegations(suite.ctx, addr2) - - require.Equal(suite.T(), len(unbondDels), 0) - }, - true, - }, - } - - for _, tc := range testCases { - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { - suite.SetupTest() - suite.SetupValidatorDelegator() - - tc.pre_adjust_funds() - tc.adjust_funds() - tc.post_adjust_funds() - }) - } -} - -func checkValidator(t *testing.T, app *junoapp.App, addr sdk.ValAddress, expFound bool) types.Validator { - ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{}) - validator, found := app.StakingKeeper.GetValidator(ctxCheck, addr) - - require.Equal(t, expFound, found) - return validator -} - -func checkDelegation( - t *testing.T, app *junoapp.App, delegatorAddr sdk.AccAddress, - validatorAddr sdk.ValAddress, expFound bool, expShares sdk.Dec, -) { - ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{}) - delegation, found := app.StakingKeeper.GetDelegation(ctxCheck, delegatorAddr, validatorAddr) - if expFound { - require.True(t, found) - require.True(sdk.DecEq(t, expShares, delegation.Shares)) - - return - } - - require.False(t, found) -} - -// CheckBalance checks the balance of an account. -func checkBalance(t *testing.T, app *junoapp.App, addr sdk.AccAddress, balances sdk.Coins) { - ctxCheck := app.BaseApp.NewContext(true, tmproto.Header{}) - require.True(t, balances.IsEqual(app.BankKeeper.GetAllBalances(ctxCheck, addr))) -} - -func (suite *UpgradeTestSuite) SetupTest() { - // acc1 is to create validator - acc1 := &authtypes.BaseAccount{Address: addr1.String()} - // acc2 is to delegate funds to acc1 validator - acc2 := &authtypes.BaseAccount{Address: addr2.String()} - - accs := authtypes.GenesisAccounts{acc1, acc2} - balances := []banktypes.Balance{ - { - Address: addr1.String(), - Coins: sdk.Coins{genCoin}, - }, - { - Address: addr2.String(), - Coins: sdk.Coins{genCoin}, - }, - } - - suite.app = app.SetupWithGenesisAccounts(accs, balances...) - suite.ctx = suite.app.BaseApp.NewContext(true, tmproto.Header{}) - checkBalance(suite.T(), suite.app, addr1, sdk.Coins{genCoin}) - checkBalance(suite.T(), suite.app, addr2, sdk.Coins{genCoin}) -} - -func (suite *UpgradeTestSuite) SetupValidatorDelegator() { - // create validator - description := types.NewDescription("acc1", "", "", "", "") - createValidatorMsg, err := types.NewMsgCreateValidator( - sdk.ValAddress(addr1), valKey.PubKey(), bondCoin, description, commissionRates, sdk.OneInt(), - ) - require.NoError(suite.T(), err) - - header := tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - txGen := cosmoscmd.MakeEncodingConfig(junoapp.ModuleBasics).TxConfig - _, _, err = cosmossimapp.SignCheckDeliver(suite.T(), txGen, suite.app.BaseApp, header, []sdk.Msg{createValidatorMsg}, "", []uint64{0}, []uint64{0}, true, true, priv1) - require.NoError(suite.T(), err) - checkBalance(suite.T(), suite.app, addr1, sdk.Coins{genCoin.Sub(bondCoin)}) - - header = tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - suite.app.BeginBlock(abci.RequestBeginBlock{Header: header}) - - validator := checkValidator(suite.T(), suite.app, sdk.ValAddress(addr1), true) - require.Equal(suite.T(), sdk.ValAddress(addr1).String(), validator.OperatorAddress) - require.Equal(suite.T(), types.Bonded, validator.Status) - require.True(sdk.IntEq(suite.T(), bondTokens, validator.BondedTokens())) - - header = tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - suite.app.BeginBlock(abci.RequestBeginBlock{Header: header}) - - // delegate - checkBalance(suite.T(), suite.app, addr2, sdk.Coins{genCoin}) - delegateMsg := types.NewMsgDelegate(addr2, sdk.ValAddress(addr1), bondCoin) - - header = tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - _, _, err = cosmossimapp.SignCheckDeliver(suite.T(), txGen, suite.app.BaseApp, header, []sdk.Msg{delegateMsg}, "", []uint64{1}, []uint64{0}, true, true, priv2) - require.NoError(suite.T(), err) - - checkBalance(suite.T(), suite.app, addr2, sdk.Coins{genCoin.Sub(bondCoin)}) - checkDelegation(suite.T(), suite.app, addr2, sdk.ValAddress(addr1), true, bondTokens.ToDec()) - - // begin unbonding half - beginUnbondingMsg := types.NewMsgUndelegate(addr2, sdk.ValAddress(addr1), escapeBondCoin) - header = tmproto.Header{Height: suite.app.LastBlockHeight() + 1} - _, _, err = cosmossimapp.SignCheckDeliver(suite.T(), txGen, suite.app.BaseApp, header, []sdk.Msg{beginUnbondingMsg}, "", []uint64{1}, []uint64{1}, true, true, priv2) - require.NoError(suite.T(), err) - - // delegation should be halved through unbonding cheat to avoid unity hunt - checkDelegation(suite.T(), suite.app, addr2, sdk.ValAddress(addr1), true, bondTokens.Sub(escapeBondTokens).ToDec()) - - // balance should be the same because bonding not yet complete - checkBalance(suite.T(), suite.app, addr2, sdk.Coins{genCoin.Sub(bondCoin)}) -} - -func TestSuite(t *testing.T) { - suite.Run(t, new(UpgradeTestSuite)) -}