Skip to content

Commit

Permalink
Merge pull request #472 from bnb-chain/develop
Browse files Browse the repository at this point in the history
release: prepare for release v0.2.5-alpha.3
  • Loading branch information
unclezoro committed Sep 11, 2023
2 parents 60cebfa + 1286dc9 commit af2eb4f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.2.5-alpha.3
This release is a bugfix release.

Bugfixes:
* [#470](https://github.com/bnb-chain/greenfield/pull/470) fix: testnet block sync issue
* [#471](https://github.com/bnb-chain/greenfield/pull/471) fix: some issues in challenge module

## v0.2.5-alpha.2
This release contains 1 bugfix.
* [#465](https://github.com/bnb-chain/greenfield/pull/465) fix: remaining policies need re-persistence
Expand Down
75 changes: 43 additions & 32 deletions x/challenge/keeper/msg_server_attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"math/big"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,6 +18,9 @@ import (
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)

// one_gb_bytes stands for the total bytes in 1gb
const one_gb_bytes = 1024 * 1024 * 1024

// Attest handles user's request for attesting a challenge.
// The attestation can include a valid challenge or is only for heartbeat purpose.
// If the challenge is valid, the related storage provider will be slashed.
Expand Down Expand Up @@ -145,8 +150,8 @@ func (k msgServer) Attest(goCtx context.Context, msg *types.MsgAttest) (*types.M
func (k msgServer) calculateSlashAmount(ctx sdk.Context, objectSize uint64) sdkmath.Int {
params := k.GetParams(ctx)
sizeRate := params.SlashAmountSizeRate
objectSizeInGB := sdk.NewDecFromBigInt(new(big.Int).SetUint64(objectSize)).QuoRoundUp(sdk.NewDec(1073741824))
slashAmount := objectSizeInGB.MulMut(sizeRate).MulMut(sdk.NewDec(1e18)).TruncateInt()
objectSizeInGB := sdk.NewDecFromBigInt(new(big.Int).SetUint64(objectSize)).QuoRoundUp(sdk.NewDec(one_gb_bytes))
slashAmount := objectSizeInGB.Mul(sizeRate).Mul(sdk.NewDec(1e18)).TruncateInt()

min := params.SlashAmountMin
if slashAmount.LT(min) {
Expand Down Expand Up @@ -195,40 +200,46 @@ func (k msgServer) calculateSlashRewards(ctx sdk.Context, total sdkmath.Int, cha
func (k msgServer) doSlashAndRewards(ctx sdk.Context, challengeId uint64, voteResult types.VoteResult, slashAmount sdkmath.Int,
spID uint32, submitter, challenger sdk.AccAddress, validators []string) error {

challengerReward, eachValidatorReward, submitterReward := k.calculateSlashRewards(ctx, slashAmount,
challenger, int64(len(validators)))
challengerReward, eachValidatorReward, submitterReward := sdkmath.ZeroInt(), sdkmath.ZeroInt(), sdkmath.ZeroInt()

denom := k.SpKeeper.DepositDenomForSP(ctx)
rewards := make([]sptypes.RewardInfo, 0)
if !challenger.Equals(sdk.AccAddress{}) {
rewards = append(rewards, sptypes.RewardInfo{
Address: challenger.String(),
Amount: sdk.Coin{
Denom: denom,
Amount: challengerReward,
},
})
}
for _, val := range validators {
noNeedCalculation := ctx.IsUpgraded(upgradetypes.Nagqu) && slashAmount.IsZero()

if !noNeedCalculation {
challengerReward, eachValidatorReward, submitterReward = k.calculateSlashRewards(ctx, slashAmount,
challenger, int64(len(validators)))

denom := k.SpKeeper.DepositDenomForSP(ctx)
rewards := make([]sptypes.RewardInfo, 0)
if !challenger.Equals(sdk.AccAddress{}) {
rewards = append(rewards, sptypes.RewardInfo{
Address: challenger.String(),
Amount: sdk.Coin{
Denom: denom,
Amount: challengerReward,
},
})
}
for _, val := range validators {
rewards = append(rewards, sptypes.RewardInfo{
Address: val,
Amount: sdk.Coin{
Denom: denom,
Amount: eachValidatorReward,
},
})
}
rewards = append(rewards, sptypes.RewardInfo{
Address: val,
Address: submitter.String(),
Amount: sdk.Coin{
Denom: denom,
Amount: eachValidatorReward,
},
})
}
rewards = append(rewards, sptypes.RewardInfo{
Address: submitter.String(),
Amount: sdk.Coin{
Denom: denom,
Amount: submitterReward,
}})

if challengerReward.IsPositive() || eachValidatorReward.IsPositive() || submitterReward.IsPositive() {
err := k.SpKeeper.Slash(ctx, spID, rewards)
if err != nil {
return err
Amount: submitterReward,
}})

if challengerReward.IsPositive() || eachValidatorReward.IsPositive() || submitterReward.IsPositive() {
err := k.SpKeeper.Slash(ctx, spID, rewards)
if err != nil {
return err
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions x/challenge/keeper/msg_server_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (k msgServer) Submit(goCtx context.Context, msg *types.MsgSubmit) (*types.M
if objectInfo.ObjectStatus != storagetypes.OBJECT_STATUS_SEALED {
return nil, types.ErrInvalidObjectStatus
}
if objectInfo.PayloadSize == 0 {
return nil, errors.Wrap(types.ErrInvalidSegmentIndex, "the object is empty, no segment")
}

// check whether the sp stores the object info, generate redundancy index
stored := false
Expand Down
2 changes: 1 addition & 1 deletion x/payment/keeper/msg_server_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func (k msgServer) Withdraw(goCtx context.Context, msg *types.MsgWithdraw) (*typ
ctx := sdk.UnwrapSDKContext(goCtx)
creator := sdk.MustAccAddressFromHex(msg.Creator)

params := k.GetParams(ctx)
if ctx.IsUpgraded(upgradetypes.Nagqu) {
if msg.From == "" { // withdraw from the locked one
delayedWithdrawal, found := k.GetDelayedWithdrawalRecord(ctx, creator)
Expand Down Expand Up @@ -79,6 +78,7 @@ func (k msgServer) Withdraw(goCtx context.Context, msg *types.MsgWithdraw) (*typ
}

if ctx.IsUpgraded(upgradetypes.Nagqu) {
params := k.GetParams(ctx)
if msg.Amount.GTE(*params.WithdrawTimeLockThreshold) {
// check whether there is delayed withdrawal, if there is delayed withdrawal, must withdraw it firstly
if _, found := k.GetDelayedWithdrawalRecord(ctx, creator); found {
Expand Down

0 comments on commit af2eb4f

Please sign in to comment.