mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-07 14:46:39 +00:00
test: add additonal validatorPopReward reallocation test (#436)
* test: add additonal validatorPopReward reallocation test * fix: linter & staticcheck errors --------- Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
5d84ae6806
commit
975a1005b0
@ -51,21 +51,21 @@ func (cm CheckMachineDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
|
|||||||
func (cm CheckMachineDecorator) handleNotarizeAsset(ctx sdk.Context, notarizeMsg *assettypes.MsgNotarizeAsset) (sdk.Context, error) {
|
func (cm CheckMachineDecorator) handleNotarizeAsset(ctx sdk.Context, notarizeMsg *assettypes.MsgNotarizeAsset) (sdk.Context, error) {
|
||||||
_, found := cm.mk.GetMachineIndexByAddress(ctx, notarizeMsg.GetCreator())
|
_, found := cm.mk.GetMachineIndexByAddress(ctx, notarizeMsg.GetCreator())
|
||||||
if !found {
|
if !found {
|
||||||
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(machinetypes.ErrMachineNotFound, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm CheckMachineDecorator) handleAttestMachine(ctx sdk.Context, attestMsg *machinetypes.MsgAttestMachine) (sdk.Context, error) {
|
func (cm CheckMachineDecorator) handleAttestMachine(ctx sdk.Context, attestMsg *machinetypes.MsgAttestMachine) (sdk.Context, error) {
|
||||||
if attestMsg.GetCreator() != attestMsg.Machine.GetAddress() {
|
if attestMsg.GetCreator() != attestMsg.Machine.GetAddress() {
|
||||||
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineIsNotCreator, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(machinetypes.ErrMachineIsNotCreator, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
_, activated, found := cm.mk.GetTrustAnchor(ctx, attestMsg.Machine.MachineId)
|
_, activated, found := cm.mk.GetTrustAnchor(ctx, attestMsg.Machine.MachineId)
|
||||||
if !found {
|
if !found {
|
||||||
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorNotFound, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(machinetypes.ErrTrustAnchorNotFound, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
if activated {
|
if activated {
|
||||||
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorAlreadyInUse, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(machinetypes.ErrTrustAnchorAlreadyInUse, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ func (cm CheckMachineDecorator) handleAttestMachine(ctx sdk.Context, attestMsg *
|
|||||||
func (cm CheckMachineDecorator) handlePopResult(ctx sdk.Context, popMsg *daotypes.MsgReportPopResult) (sdk.Context, error) {
|
func (cm CheckMachineDecorator) handlePopResult(ctx sdk.Context, popMsg *daotypes.MsgReportPopResult) (sdk.Context, error) {
|
||||||
_, found := cm.mk.GetMachineIndexByAddress(ctx, popMsg.GetCreator())
|
_, found := cm.mk.GetMachineIndexByAddress(ctx, popMsg.GetCreator())
|
||||||
if !found {
|
if !found {
|
||||||
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(machinetypes.ErrMachineNotFound, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ func (cv CheckValidatorDecorator) handleMsg(ctx sdk.Context, msg sdk.Msg) (_ sdk
|
|||||||
signer := msg.GetSigners()[0]
|
signer := msg.GetSigners()[0]
|
||||||
_, found := cv.sk.GetValidator(ctx, sdk.ValAddress(signer))
|
_, found := cv.sk.GetValidator(ctx, sdk.ValAddress(signer))
|
||||||
if !found {
|
if !found {
|
||||||
return ctx, errorsmod.Wrapf(types.ErrRestrictedMsg, ErrorAnteContext)
|
return ctx, errorsmod.Wrap(types.ErrRestrictedMsg, ErrorAnteContext)
|
||||||
}
|
}
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ func (dfd DeductFeeDecorator) deductFees(bankKeeper authtypes.BankKeeper, ctx sd
|
|||||||
|
|
||||||
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authtypes.FeeCollectorName, fees)
|
err := bankKeeper.SendCoinsFromAccountToModule(ctx, acc.GetAddress(), authtypes.FeeCollectorName, fees)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
|
return errorsmod.Wrap(sdkerrors.ErrInsufficientFunds, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -39,7 +39,7 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
|
|||||||
// Set a gas meter with limit 0 as to prevent an infinite gas meter attack
|
// Set a gas meter with limit 0 as to prevent an infinite gas meter attack
|
||||||
// during runTx.
|
// during runTx.
|
||||||
newCtx = SetGasMeter(simulate, ctx, 0)
|
newCtx = SetGasMeter(simulate, ctx, 0)
|
||||||
return newCtx, errorsmod.Wrapf(sdkerrors.ErrTxDecode, "Tx must be GasTx")
|
return newCtx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be GasTx")
|
||||||
}
|
}
|
||||||
|
|
||||||
gasLimit := sud.dk.GetTxGasLimit(ctx)
|
gasLimit := sud.dk.GetTxGasLimit(ctx)
|
||||||
@ -70,7 +70,7 @@ func (sud SetUpContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
|
|||||||
"out of gas in location: %v; gasWanted: %d, gasUsed: %d",
|
"out of gas in location: %v; gasWanted: %d, gasUsed: %d",
|
||||||
rType.Descriptor, gasTx.GetGas(), newCtx.GasMeter().GasConsumed())
|
rType.Descriptor, gasTx.GetGas(), newCtx.GasMeter().GasConsumed())
|
||||||
|
|
||||||
err = errorsmod.Wrapf(sdkerrors.ErrOutOfGas, log)
|
err = errorsmod.Wrap(sdkerrors.ErrOutOfGas, log)
|
||||||
default:
|
default:
|
||||||
panic(r)
|
panic(r)
|
||||||
}
|
}
|
||||||
|
@ -256,7 +256,7 @@ func IsLegitMachineAddress(address string) (active bool, err error) {
|
|||||||
|
|
||||||
// Check the status code
|
// Check the status code
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
log.Printf("[app] [Monitor] Error: unexpected status code: " + string(body))
|
log.Print("[app] [Monitor] Error: unexpected status code: " + string(body))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ func (logger *AppLogger) testingLog(msg string, keyvals ...interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg = format(msg, keyvals...)
|
msg = format(msg, keyvals...)
|
||||||
logger.testingLogger.Logf(msg)
|
logger.testingLogger.Log(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) {
|
func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) {
|
||||||
|
@ -70,7 +70,7 @@ func (k Keeper) GetChallenges(ctx sdk.Context) (challenges []types.Challenge, er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Keeper) storeChallangeInitiatorReward(ctx sdk.Context, height int64, amount uint64) {
|
func (k Keeper) StoreChallangeInitiatorReward(ctx sdk.Context, height int64, amount uint64) {
|
||||||
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoPInitiatorReward))
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoPInitiatorReward))
|
||||||
appendValue := util.SerializeUint64(amount)
|
appendValue := util.SerializeUint64(amount)
|
||||||
store.Set(util.SerializeInt64(height), appendValue)
|
store.Set(util.SerializeInt64(height), appendValue)
|
||||||
|
@ -25,6 +25,7 @@ func createNChallenge(keeper *keeper.Keeper, ctx sdk.Context, n int, popEpochs i
|
|||||||
items[i].Success = false
|
items[i].Success = false
|
||||||
items[i].Finished = false
|
items[i].Finished = false
|
||||||
keeper.StoreChallenge(ctx, items[i])
|
keeper.StoreChallenge(ctx, items[i])
|
||||||
|
keeper.StoreChallangeInitiatorReward(ctx, blockHeight, types.DefaultParams().ValidatorPopReward)
|
||||||
}
|
}
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
|
keepertest "github.com/planetmint/planetmint-go/testutil/keeper"
|
||||||
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
@ -91,3 +92,40 @@ func TestTokenDistribution(t *testing.T) {
|
|||||||
assert.True(t, expSum-sum < 0.000001)
|
assert.True(t, expSum-sum < 0.000001)
|
||||||
assert.Equal(t, float64(reissuances), Amount1stBatch+Amount2ndBatch)
|
assert.Equal(t, float64(reissuances), Amount1stBatch+Amount2ndBatch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidatorRewardReallocation(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
k, ctx := keepertest.DaoKeeper(t)
|
||||||
|
|
||||||
|
// create reissuances
|
||||||
|
_ = createNReissuances(k, ctx, 1000, types.DefaultParams().PopEpochs)
|
||||||
|
distribution, err := k.GetDistributionForReissuedTokens(ctx, 780)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// create challenges
|
||||||
|
_ = createNChallenge(k, ctx, 250, types.DefaultParams().PopEpochs)
|
||||||
|
distributionWithPopReallocation, err := k.GetDistributionForReissuedTokens(ctx, 780)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// compare
|
||||||
|
pops := 780 / types.DefaultParams().PopEpochs
|
||||||
|
distributionDaoAmount, err := util.RDDLTokenStringToFloat(distribution.DaoAmount)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
distributionPopAmount, err := util.RDDLTokenStringToFloat(distribution.PopAmount)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
distributionWithValidatorPopRewardDao, err := util.RDDLTokenStringToFloat(distributionWithPopReallocation.DaoAmount)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
distributionWithValidatorPopRewardPop, err := util.RDDLTokenStringToFloat(distributionWithPopReallocation.PopAmount)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Some of DaoAmount is transferred to PopAmount
|
||||||
|
assert.Greater(t, distributionDaoAmount, distributionWithValidatorPopRewardDao)
|
||||||
|
assert.Greater(t, distributionWithValidatorPopRewardPop, distributionPopAmount)
|
||||||
|
|
||||||
|
diffPopAmount := distributionWithValidatorPopRewardPop - distributionPopAmount
|
||||||
|
diffDaoAmount := distributionDaoAmount - distributionWithValidatorPopRewardDao
|
||||||
|
assert.Equal(t, diffPopAmount, diffDaoAmount)
|
||||||
|
assert.Equal(t, pops, int64(diffPopAmount))
|
||||||
|
assert.Equal(t, pops, int64(diffDaoAmount))
|
||||||
|
}
|
||||||
|
@ -25,7 +25,7 @@ func (k msgServer) InitPop(goCtx context.Context, msg *types.MsgInitPop) (*types
|
|||||||
k.StoreChallenge(ctx, challenge)
|
k.StoreChallenge(ctx, challenge)
|
||||||
|
|
||||||
amount := k.GetValidatorPoPReward(ctx)
|
amount := k.GetValidatorPoPReward(ctx)
|
||||||
k.storeChallangeInitiatorReward(ctx, msg.GetHeight(), amount)
|
k.StoreChallangeInitiatorReward(ctx, msg.GetHeight(), amount)
|
||||||
|
|
||||||
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -58,7 +58,7 @@ func (k msgServer) validateReissuanceProposal(ctx sdk.Context, msg *types.MsgRei
|
|||||||
isValid := k.IsValidReissuanceProposal(ctx, msg)
|
isValid := k.IsValidReissuanceProposal(ctx, msg)
|
||||||
if !isValid {
|
if !isValid {
|
||||||
util.GetAppLogger().Info(ctx, reissueTag+"rejected reissuance proposal")
|
util.GetAppLogger().Info(ctx, reissueTag+"rejected reissuance proposal")
|
||||||
return errorsmod.Wrapf(types.ErrReissuanceProposal, reissueTag)
|
return errorsmod.Wrap(types.ErrReissuanceProposal, reissueTag)
|
||||||
}
|
}
|
||||||
util.GetAppLogger().Debug(ctx, reissueTag+"accepted reissuance proposal: "+msg.String())
|
util.GetAppLogger().Debug(ctx, reissueTag+"accepted reissuance proposal: "+msg.String())
|
||||||
return
|
return
|
||||||
|
@ -35,7 +35,7 @@ func (k msgServer) ReportPopResult(goCtx context.Context, msg *types.MsgReportPo
|
|||||||
|
|
||||||
err := util.ValidateStruct(*msg.Challenge)
|
err := util.ValidateStruct(*msg.Challenge)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorsmod.Wrapf(types.ErrInvalidChallenge, err.Error())
|
return nil, errorsmod.Wrap(types.ErrInvalidChallenge, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify that the report origin is the challenger
|
// verify that the report origin is the challenger
|
||||||
@ -64,7 +64,7 @@ func (k msgServer) ReportPopResult(goCtx context.Context, msg *types.MsgReportPo
|
|||||||
|
|
||||||
err = k.issuePoPRewards(ctx, *msg.Challenge)
|
err = k.issuePoPRewards(ctx, *msg.Challenge)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errorsmod.Wrapf(types.ErrFailedPoPRewardsIssuance, err.Error())
|
return nil, errorsmod.Wrap(types.ErrFailedPoPRewardsIssuance, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &types.MsgReportPopResultResponse{}, nil
|
return &types.MsgReportPopResultResponse{}, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user