From bfd27866dbe8ad66da2a703635acbf13c7e3b859 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Tue, 17 Oct 2023 21:52:50 +0800 Subject: [PATCH] Get ValidationBlockPerSlot from ProtocolParameters --- .../sybilprotectionv1/performance/performance.go | 6 +++--- .../sybilprotectionv1/performance/rewards.go | 2 +- .../sybilprotectionv1/performance/testsuite_test.go | 10 +++++----- .../sybilprotectionv1/performance/tracker_test.go | 2 +- pkg/testsuite/testsuite.go | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/performance.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/performance.go index 27c9f9c9e..7dafd3b62 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/performance.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/performance.go @@ -224,7 +224,7 @@ func (t *Tracker) aggregatePerformanceFactors(slotActivityVector []*model.Valida // we reward not only total number of blocks issued, but also regularity based on block timestamp slotPerformanceFactor := bits.OnesCount32(pf.SlotActivityVector) - if pf.BlockIssuedCount > t.apiProvider.APIForEpoch(epoch).ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot { + if pf.BlockIssuedCount > t.apiProvider.APIForEpoch(epoch).ProtocolParameters().ValidationBlocksPerSlot() { // we harshly punish validators that issue any blocks more than allowed return 0 @@ -269,7 +269,7 @@ func (t *Tracker) trackCommitteeMemberPerformance(validationBlock *iotago.Valida apiForSlot := t.apiProvider.APIForSlot(block.ID().Slot()) // we restrict the number up to ValidatorBlocksPerSlot + 1 to know later if the validator issued more blocks than allowed and be able to punish for it // also it can fint into uint8 - if validatorPerformance.BlockIssuedCount < apiForSlot.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot+1 { + if validatorPerformance.BlockIssuedCount < apiForSlot.ProtocolParameters().ValidationBlocksPerSlot()+1 { validatorPerformance.BlockIssuedCount++ } validatorPerformance.HighestSupportedVersionAndHash = model.VersionAndHash{ @@ -284,7 +284,7 @@ func (t *Tracker) trackCommitteeMemberPerformance(validationBlock *iotago.Valida // subslotIndex returns the index for timestamp corresponding to subslot created dividing slot on validatorBlocksPerSlot equal parts. func (t *Tracker) subslotIndex(slot iotago.SlotIndex, issuingTime time.Time) int { epochAPI := t.apiProvider.APIForEpoch(t.latestAppliedEpoch) - valBlocksNum := epochAPI.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot + valBlocksNum := epochAPI.ProtocolParameters().ValidationBlocksPerSlot() subslotDur := time.Duration(epochAPI.TimeProvider().SlotDurationSeconds()) * time.Second / time.Duration(valBlocksNum) slotStart := epochAPI.TimeProvider().SlotStartTime(slot) diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/rewards.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/rewards.go index 287fa5ec1..5dfa56236 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/rewards.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/rewards.go @@ -239,7 +239,7 @@ func (t *Tracker) poolReward(slot iotago.SlotIndex, totalValidatorsStake, totalS return 0, ierrors.Wrapf(err, "failed to calculate pool reward without fixed costs due to overflow for slot %d", slot) } - result, err = safemath.SafeDiv(scaledPoolReward, uint64(params.RewardsParameters().ValidatorBlocksPerSlot)) + result, err = safemath.SafeDiv(scaledPoolReward, uint64(params.ValidationBlocksPerSlot())) if err != nil { return 0, ierrors.Wrapf(err, "failed to calculate result reward due division by zero for slot %d", slot) } diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go index 16d5dfa63..9ce7a9f9a 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/testsuite_test.go @@ -42,7 +42,7 @@ func NewTestSuite(t *testing.T) *TestSuite { api: iotago.V3API( iotago.NewV3ProtocolParameters( iotago.WithTimeProviderOptions(time.Now().Unix(), 10, 3), - iotago.WithRewardsOptions(10, 8, 8, 11, 1154, 2, 1), + iotago.WithRewardsOptions(8, 8, 11, 1154, 2, 1), ), ), } @@ -192,7 +192,7 @@ func (t *TestSuite) AssertRewardForDelegatorsOnly(alias string, epoch iotago.Epo } func (t *TestSuite) validatorReward(alias string, epoch iotago.EpochIndex, profitMargin, poolRewards, stakeAmount, poolStake, fixedCost uint64, action *EpochActions) iotago.Mana { - if action.ValidationBlocksSentPerSlot > uint64(t.api.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot) { + if action.ValidationBlocksSentPerSlot > uint64(t.api.ProtocolParameters().ValidationBlocksPerSlot()) { return iotago.Mana(0) } if action.FixedCost > t.poolRewards[epoch][alias].PoolRewards { @@ -218,7 +218,7 @@ func (t *TestSuite) validatorReward(alias string, epoch iotago.EpochIndex, profi } func (t *TestSuite) delegatorReward(epoch iotago.EpochIndex, profitMargin, poolRewardWithFixedCost, delegatedAmount, poolStake, fixedCost uint64, action *EpochActions) iotago.Mana { - if action.ValidationBlocksSentPerSlot > uint64(t.api.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot) { + if action.ValidationBlocksSentPerSlot > uint64(t.api.ProtocolParameters().ValidationBlocksPerSlot()) { return iotago.Mana(0) } @@ -245,7 +245,7 @@ func (t *TestSuite) calculatePoolReward(epoch iotago.EpochIndex, totalValidators poolCoefficient := t.calculatePoolCoefficient(poolStake, totalStake, validatorStake, totalValidatorsStake) scaledPoolReward := poolCoefficient * uint64(targetReward) * performanceFactor - poolRewardNoFixedCost := scaledPoolReward / uint64(params.RewardsParameters().ValidatorBlocksPerSlot) >> (params.RewardsParameters().PoolCoefficientExponent + 1) + poolRewardNoFixedCost := scaledPoolReward / uint64(params.ValidationBlocksPerSlot()) >> (params.RewardsParameters().PoolCoefficientExponent + 1) return poolRewardNoFixedCost } @@ -266,7 +266,7 @@ func (t *TestSuite) calculateProfitMargin(totalValidatorsStake, totalPoolStake i func (t *TestSuite) applyPerformanceFactor(accountID iotago.AccountID, epoch iotago.EpochIndex, activeSlotsCount, validationBlocksSentPerSlot, slotPerformanceFactor uint64) { startSlot := t.api.TimeProvider().EpochStart(epoch) endSlot := t.api.TimeProvider().EpochEnd(epoch) - valBlocksNum := t.api.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot + valBlocksNum := t.api.ProtocolParameters().ValidationBlocksPerSlot() subslotDur := time.Duration(t.api.TimeProvider().SlotDurationSeconds()) * time.Second / time.Duration(valBlocksNum) slotCount := uint64(0) diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go index dc681463c..46033f793 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/performance/tracker_test.go @@ -61,7 +61,7 @@ func TestManager_Rewards(t *testing.T) { Delegators: []iotago.BaseToken{3, 2}, FixedCost: 100, ActiveSlotsCount: 8, - ValidationBlocksSentPerSlot: uint64(ts.api.ProtocolParameters().RewardsParameters().ValidatorBlocksPerSlot + 2), // no reward for validator issuing more blocks than allowed + ValidationBlocksSentPerSlot: uint64(ts.api.ProtocolParameters().ValidationBlocksPerSlot() + 2), // no reward for validator issuing more blocks than allowed SlotPerformance: 10, }, "D": { diff --git a/pkg/testsuite/testsuite.go b/pkg/testsuite/testsuite.go index febd28625..dece896de 100644 --- a/pkg/testsuite/testsuite.go +++ b/pkg/testsuite/testsuite.go @@ -89,7 +89,7 @@ func NewTestSuite(testingT *testing.T, opts ...options.Option[TestSuite]) *TestS 100, 100, ), - iotago.WithRewardsOptions(10, 8, 8, 31, 1154, 2, 1), + iotago.WithRewardsOptions(8, 8, 31, 1154, 2, 1), iotago.WithStakingOptions(1, 100, 1), iotago.WithTimeProviderOptions(