Skip to content

Commit

Permalink
Cleanup RewardValidatorTx logic (#1891)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Sep 19, 2023
1 parent 27b6a4c commit 56816d7
Show file tree
Hide file tree
Showing 6 changed files with 592 additions and 481 deletions.
2 changes: 2 additions & 0 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
NodeID: utx.NodeID(),
SubnetID: utx.SubnetID(),
StartTime: utx.StartTime(),
NextTime: chainTime,
EndTime: chainTime,
}).Times(2)
currentStakersIt.EXPECT().Release()
Expand All @@ -104,6 +105,7 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) {
NodeID: utx.NodeID(),
SubnetID: utx.SubnetID(),
StartTime: utx.StartTime(),
NextTime: chainTime,
EndTime: chainTime,
}, nil)
onParentAccept.EXPECT().GetTx(addValTx.ID()).Return(addValTx, status.Committed, nil)
Expand Down
18 changes: 18 additions & 0 deletions vms/platformvm/reward/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package reward
import (
"math/big"
"time"

"github.com/ava-labs/avalanchego/utils/math"
)

var _ Calculator = (*calculator)(nil)
Expand Down Expand Up @@ -67,3 +69,19 @@ func (c *calculator) Calculate(stakedDuration time.Duration, stakedAmount, curre

return finalReward
}

// Split [totalAmount] into [totalAmount * shares percentage] and the remainder.
//
// Invariant: [shares] <= [PercentDenominator]
func Split(totalAmount uint64, shares uint32) (uint64, uint64) {
remainderShares := PercentDenominator - uint64(shares)
remainderAmount := remainderShares * (totalAmount / PercentDenominator)

// Delay rounding as long as possible for small numbers
if optimisticReward, err := math.Mul64(remainderShares, totalAmount); err == nil {
remainderAmount = optimisticReward / PercentDenominator
}

amountFromShares := totalAmount - remainderAmount
return amountFromShares, remainderAmount
}
63 changes: 63 additions & 0 deletions vms/platformvm/reward/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,66 @@ func TestRewardsMint(t *testing.T) {
)
require.Equal(t, maxSupply-initialSupply, rewards)
}

func TestSplit(t *testing.T) {
tests := []struct {
amount uint64
shares uint32
expectedSplit uint64
}{
{
amount: 1000,
shares: PercentDenominator / 2,
expectedSplit: 500,
},
{
amount: 1,
shares: PercentDenominator,
expectedSplit: 1,
},
{
amount: 1,
shares: PercentDenominator - 1,
expectedSplit: 1,
},
{
amount: 1,
shares: 1,
expectedSplit: 1,
},
{
amount: 1,
shares: 0,
expectedSplit: 0,
},
{
amount: 9223374036974675809,
shares: 2,
expectedSplit: 18446748749757,
},
{
amount: 9223374036974675809,
shares: PercentDenominator,
expectedSplit: 9223374036974675809,
},
{
amount: 9223372036855275808,
shares: PercentDenominator - 2,
expectedSplit: 9223353590111202098,
},
{
amount: 9223372036855275808,
shares: 2,
expectedSplit: 18446744349518,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%d_%d", test.amount, test.shares), func(t *testing.T) {
require := require.New(t)

split, remainder := Split(test.amount, test.shares)
require.Equal(test.expectedSplit, split)
require.Equal(test.amount-test.expectedSplit, remainder)
})
}
}

0 comments on commit 56816d7

Please sign in to comment.