Skip to content

Commit

Permalink
Get ValidationBlockPerSlot from ProtocolParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Oct 17, 2023
1 parent 621df66 commit bfd2786
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
),
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bfd2786

Please sign in to comment.