Skip to content

Commit

Permalink
fix: sp staking ledger error when slash (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnss authored May 6, 2023
1 parent c119c32 commit cfdd896
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
14 changes: 8 additions & 6 deletions e2e/tests/gensp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/suite"

"github.com/bnb-chain/greenfield/e2e/core"
"github.com/bnb-chain/greenfield/sdk/types"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
)

Expand All @@ -25,6 +24,12 @@ func (s *GenStorageProviderTestSuite) TestGenStorageProvider() {

sp := s.StorageProviders[0]

querySPReq := sptypes.QueryStorageProviderRequest{
SpAddress: sp.OperatorKey.GetAddr().String(),
}

querySPResp, err := s.Client.StorageProvider(ctx, &querySPReq)

genSP := &sptypes.StorageProvider{
OperatorAddress: sp.OperatorKey.GetAddr().String(),
FundingAddress: sp.FundingKey.GetAddr().String(),
Expand All @@ -38,12 +43,9 @@ func (s *GenStorageProviderTestSuite) TestGenStorageProvider() {
Website: "http://website",
},
Endpoint: "http://127.0.0.1:9033",
TotalDeposit: types.NewIntFromInt64WithDecimal(10000000, types.DecimalBNB),
TotalDeposit: querySPResp.StorageProvider.TotalDeposit,
}
querySPReq := sptypes.QueryStorageProviderRequest{
SpAddress: sp.OperatorKey.GetAddr().String(),
}
querySPResp, err := s.Client.StorageProvider(ctx, &querySPReq)

s.Require().NoError(err)
s.Require().Equal(querySPResp.StorageProvider, genSP)
}
Expand Down
6 changes: 4 additions & 2 deletions x/sp/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (k Keeper) Slash(ctx sdk.Context, spAcc sdk.AccAddress, rewardInfos []types

totalAmount := sdkmath.NewInt(0)
for _, rewardInfo := range rewardInfos {
totalAmount.Add(rewardInfo.Amount.Amount)
totalAmount = totalAmount.Add(rewardInfo.Amount.Amount)
if k.DepositDenomForSP(ctx) != rewardInfo.GetAmount().Denom {
return types.ErrInvalidDenom.Wrapf("Expect: %s, actual: %s", k.DepositDenomForSP(ctx), rewardInfo.GetAmount().Denom)
}
Expand All @@ -37,6 +37,8 @@ func (k Keeper) Slash(ctx sdk.Context, spAcc sdk.AccAddress, rewardInfos []types
}
}

// TODO: if the total deposit of SP is less than the MinDeposit, we will jail it.
sp.TotalDeposit = sp.TotalDeposit.Sub(totalAmount)
k.SetStorageProvider(ctx, sp)

return nil
}
50 changes: 50 additions & 0 deletions x/sp/keeper/sp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package keeper_test
import (
"fmt"

"cosmossdk.io/math"
types2 "github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/bnb-chain/greenfield/testutil/sample"
Expand Down Expand Up @@ -79,3 +82,50 @@ func (s *KeeperTestSuite) TestStorageProviderBasics() {
}
require.EqualValues(s.T(), found, true)
}

func (s *KeeperTestSuite) TestSlashBasic() {
// mock
s.bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)

k := s.spKeeper
ctx := s.ctx
spAccStr := sample.AccAddress()
spAcc := sdk.MustAccAddressFromHex(spAccStr)

fundingAccStr := sample.AccAddress()
fundingAcc := sdk.MustAccAddressFromHex(fundingAccStr)

sealAccStr := sample.AccAddress()
sealAcc := sdk.MustAccAddressFromHex(sealAccStr)

approvalAccStr := sample.AccAddress()
approvalAcc := sdk.MustAccAddressFromHex(approvalAccStr)

sp := &types.StorageProvider{
OperatorAddress: spAcc.String(),
FundingAddress: fundingAcc.String(),
SealAddress: sealAcc.String(),
ApprovalAddress: approvalAcc.String(),
TotalDeposit: math.NewIntWithDecimal(2010, types2.DecimalBNB),
}

k.SetStorageProvider(ctx, sp)
_, found := k.GetStorageProvider(ctx, spAcc)
if !found {
fmt.Printf("no such sp: %s", spAcc)
}
require.EqualValues(s.T(), found, true)

rewardInfo := types.RewardInfo{
Address: sample.AccAddress(),
Amount: sdk.NewCoin(types2.Denom, math.NewIntWithDecimal(10, types2.DecimalBNB)),
}

err := k.Slash(ctx, spAcc, []types.RewardInfo{rewardInfo})
require.NoError(s.T(), err)

spAfterSlash, found := k.GetStorageProvider(ctx, spAcc)
require.True(s.T(), found)
s.T().Logf("%s", spAfterSlash.TotalDeposit.String())
require.True(s.T(), spAfterSlash.TotalDeposit.Equal(math.NewIntWithDecimal(2000, types2.DecimalBNB)))
}

0 comments on commit cfdd896

Please sign in to comment.