Skip to content

Commit

Permalink
wip: add upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
boodyvo committed Oct 23, 2024
1 parent 149ef99 commit fec9d4b
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 37 deletions.
4 changes: 4 additions & 0 deletions x/committee/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -22,14 +23,17 @@ var _ types.MsgServer = msgServer{}

// SubmitProposal handles MsgSubmitProposal messages
func (m msgServer) SubmitProposal(goCtx context.Context, msg *types.MsgSubmitProposal) (*types.MsgSubmitProposalResponse, error) {
fmt.Println("msgServer.SubmitProposal")
ctx := sdk.UnwrapSDKContext(goCtx)

proposer, err := sdk.AccAddressFromBech32(msg.Proposer)
fmt.Println("proposer", proposer, err)
if err != nil {
return nil, err
}

proposalID, err := m.keeper.SubmitProposal(ctx, proposer, msg.CommitteeID, msg.GetPubProposal())
fmt.Println("proposalID", proposalID, err)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions x/committee/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (

// SubmitProposal adds a proposal to a committee so that it can be voted on.
func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committeeID uint64, pubProposal types.PubProposal) (uint64, error) {
fmt.Println("keeper.SubmitProposal")
// Limit proposals to only be submitted by committee members
com, found := k.GetCommittee(ctx, committeeID)
fmt.Println("com", com, found)
if !found {
return 0, errorsmod.Wrapf(types.ErrUnknownCommittee, "%d", committeeID)
}
Expand All @@ -27,6 +29,8 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committ
return 0, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "committee does not have permissions to enact proposal")
}

fmt.Println("ValidatePubProposal", k.ValidatePubProposal(ctx, pubProposal))

// Check proposal is valid
if err := k.ValidatePubProposal(ctx, pubProposal); err != nil {
return 0, err
Expand All @@ -35,6 +39,7 @@ func (k Keeper) SubmitProposal(ctx sdk.Context, proposer sdk.AccAddress, committ
// Get a new ID and store the proposal
deadline := ctx.BlockTime().Add(com.GetProposalDuration())
proposalID, err := k.StoreNewProposal(ctx, pubProposal, committeeID, deadline)
fmt.Println("proposalID", proposalID, err)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -98,6 +103,9 @@ func (k Keeper) ValidatePubProposal(ctx sdk.Context, pubProposal types.PubPropos
return err
}

fmt.Println("proposal route", pubProposal.ProposalRoute())
fmt.Println("HasRoute", k.router.HasRoute(pubProposal.ProposalRoute()))

if !k.router.HasRoute(pubProposal.ProposalRoute()) {
return errorsmod.Wrapf(types.ErrNoProposalHandlerExists, "%T", pubProposal)
}
Expand Down
1 change: 1 addition & 0 deletions x/liquid/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (k Keeper) CollectStakingRewards(
))
}

// ErrNoValidatorExists
rewards, err := k.distributionKeeper.WithdrawDelegationRewards(ctx, macc.GetAddress(), validator)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions x/liquid/keeper/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (suite *KeeperTestSuite) TestCollectStakingRewards() {

suite.CreateNewUnbondedValidator(valAddr1, initialBalance)
suite.CreateDelegation(valAddr1, delegator, delegateAmount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

// Transfers delegation to module account
Expand Down Expand Up @@ -79,7 +79,8 @@ func (suite *KeeperTestSuite) TestCollectStakingRewards() {
derivativeDenom := suite.Keeper.GetLiquidStakingTokenDenom(sdk.ValAddress(addrs[2]))
_, err := suite.Keeper.CollectStakingRewardsByDenom(suite.Ctx, derivativeDenom, types.ModuleName)
suite.Require().Error(err)
suite.Require().Equal("no validator distribution info", err.Error())
// NOTE(boodyvo): the error was changed in cosmos-sdk
suite.Require().Equal("validator does not exist", err.Error())
})

suite.Run("collect staking rewards with invalid denom", func() {
Expand Down
5 changes: 5 additions & 0 deletions x/liquid/keeper/derivative.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (k Keeper) IsDerivativeDenom(ctx sdk.Context, denom string) bool {
// GetStakedTokensForDerivatives returns the total value of the provided derivatives
// in staked tokens, accounting for the specific share prices.
func (k Keeper) GetStakedTokensForDerivatives(ctx sdk.Context, coins sdk.Coins) (sdk.Coin, error) {
fmt.Println("GetStakedTokensForDerivatives", coins)
total := sdkmath.ZeroInt()

for _, coin := range coins {
Expand All @@ -147,7 +148,11 @@ func (k Keeper) GetStakedTokensForDerivatives(ctx sdk.Context, coins sdk.Coins)
return sdk.Coin{}, err
}

fmt.Println("total", total)

totalCoin := sdk.NewCoin(bondDenom, total)

fmt.Println("totalCoin", totalCoin)
return totalCoin, nil
}

Expand Down
12 changes: 6 additions & 6 deletions x/liquid/keeper/derivative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (suite *KeeperTestSuite) TestBurnDerivative() {
moduleAccAddress := authtypes.NewModuleAddress(types.ModuleAccountName)
suite.CreateNewUnbondedValidator(valAddr, i(1e6))
suite.CreateDelegation(valAddr, moduleAccAddress, tc.moduleDelegation)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)
modBalance := suite.BankKeeper.GetAllBalances(suite.Ctx, moduleAccAddress)

Expand Down Expand Up @@ -288,7 +288,7 @@ func (suite *KeeperTestSuite) TestMintDerivative() {

suite.CreateNewUnbondedValidator(valAddr, initialBalance)
suite.CreateDelegation(valAddr, delegator, initialBalance)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

_, err = suite.Keeper.MintDerivative(suite.Ctx, delegator, valAddr, tc.amount)
Expand Down Expand Up @@ -333,7 +333,7 @@ func (suite *KeeperTestSuite) TestIsDerivativeDenom() {

suite.CreateNewUnbondedValidator(valAddr1, initialBalance)
suite.CreateDelegation(valAddr1, delegator, initialBalance)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

testCases := []struct {
Expand Down Expand Up @@ -406,7 +406,7 @@ func (suite *KeeperTestSuite) TestGetStakedTokensForDerivatives() {

suite.CreateNewUnbondedValidator(valAddr3, initialBalance)
suite.CreateDelegation(valAddr3, delegator, delegateAmount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

suite.SlashValidator(valAddr3, d("0.05"))
Expand Down Expand Up @@ -495,7 +495,7 @@ func (suite *KeeperTestSuite) TestGetDerivativeValue() {

suite.CreateNewUnbondedValidator(valAddr2, initialBalance)
suite.CreateDelegation(valAddr2, delegator, delegateAmount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

_, err = suite.Keeper.MintDerivative(suite.Ctx, delegator, valAddr1, suite.NewBondCoin(delegateAmount))
Expand Down Expand Up @@ -543,7 +543,7 @@ func (suite *KeeperTestSuite) TestDerivativeFromTokens() {

suite.CreateNewUnbondedValidator(valAddr, initialBalance)
suite.CreateDelegation(valAddr, moduleAccAddress, initialBalance)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err)

_, err = suite.Keeper.DerivativeFromTokens(suite.Ctx, valAddr, sdk.NewCoin("invalid", initialBalance))
Expand Down
8 changes: 7 additions & 1 deletion x/liquid/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ func (s queryServer) getDelegatedBalance(ctx sdk.Context, delegator sdk.AccAddre
balance := sdkmath.LegacyZeroDec()

s.keeper.stakingKeeper.IterateDelegatorDelegations(ctx, delegator, func(delegation stakingtypes.Delegation) bool {
validator, err := s.keeper.stakingKeeper.GetValidator(ctx, []byte(delegation.GetValidatorAddr()))
valAddr, err := s.keeper.stakingKeeper.ValidatorAddressCodec().StringToBytes(delegation.GetValidatorAddr())
if err != nil {
panic(err)
}

validator, err := s.keeper.stakingKeeper.GetValidator(ctx, valAddr)

if err != nil {
panic(fmt.Sprintf("validator %s for delegation not found", delegation.GetValidatorAddr()))
}
Expand Down
14 changes: 7 additions & 7 deletions x/liquid/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *grpcQueryTestSuite) TestQueryDelegatedBalance() {

suite.CreateNewUnbondedValidator(sdk.ValAddress(valAddr), initBalance.Amount)
suite.CreateDelegation(sdk.ValAddress(valAddr), delAddr, initBalance.Amount.QuoRaw(4))
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

return delAddr.String()
Expand All @@ -82,7 +82,7 @@ func (suite *grpcQueryTestSuite) TestQueryDelegatedBalance() {
suite.CreateNewUnbondedValidator(sdk.ValAddress(valAddr), initBalance.Amount)
threeQuarters := initBalance.Amount.QuoRaw(4).MulRaw(3)
suite.CreateDelegation(sdk.ValAddress(valAddr), delAddr, threeQuarters)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

return delAddr.String()
Expand Down Expand Up @@ -137,7 +137,7 @@ func (suite *grpcQueryTestSuite) TestQueryDelegatedBalance() {

suite.CreateNewUnbondedValidator(valAcc.GetAddress().Bytes(), initBalance.Amount)
suite.CreateDelegation(valAcc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

suite.CreateUnbondingDelegation(delAcc.GetAddress(), valAcc.GetAddress().Bytes(), initBalance.Amount.QuoRaw(2))
Expand Down Expand Up @@ -192,7 +192,7 @@ func (suite *grpcQueryTestSuite) TestQueryTotalSupply() {

suite.CreateNewUnbondedValidator(valAcc.GetAddress().Bytes(), initBalance.Amount)
suite.CreateDelegation(valAcc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

_, err = suite.Keeper.MintDerivative(
Expand All @@ -218,7 +218,7 @@ func (suite *grpcQueryTestSuite) TestQueryTotalSupply() {
suite.CreateDelegation(val1Acc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
suite.CreateNewUnbondedValidator(val2Acc.GetAddress().Bytes(), initBalance.Amount)
suite.CreateDelegation(val2Acc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

_, err = suite.Keeper.MintDerivative(suite.Ctx, delAcc.GetAddress(), val1Acc.GetAddress().Bytes(), initBalance)
Expand All @@ -240,7 +240,7 @@ func (suite *grpcQueryTestSuite) TestQueryTotalSupply() {
suite.CreateNewUnbondedValidator(valAcc.GetAddress().Bytes(), initBalance.Amount)
suite.CreateDelegation(valAcc.GetAddress().Bytes(), del1Acc.GetAddress(), initBalance.Amount)
suite.CreateDelegation(valAcc.GetAddress().Bytes(), del2Acc.GetAddress(), initBalance.Amount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

_, err = suite.Keeper.MintDerivative(suite.Ctx, del1Acc.GetAddress(), valAcc.GetAddress().Bytes(), initBalance)
Expand All @@ -263,7 +263,7 @@ func (suite *grpcQueryTestSuite) TestQueryTotalSupply() {
suite.CreateDelegation(val1Acc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
suite.CreateNewUnbondedValidator(val2Acc.GetAddress().Bytes(), initBalance.Amount)
suite.CreateDelegation(val2Acc.GetAddress().Bytes(), delAcc.GetAddress(), initBalance.Amount)
err := suite.StakingKeeper.BeginBlocker(suite.Ctx)
_, err := suite.StakingKeeper.EndBlocker(suite.Ctx)
suite.Require().NoError(err) // bond the validator

_, err = suite.Keeper.MintDerivative(suite.Ctx, delAcc.GetAddress(), val1Acc.GetAddress().Bytes(), initBalance)
Expand Down
Loading

0 comments on commit fec9d4b

Please sign in to comment.