2018-08-24 04:09:59 +00:00
|
|
|
package casper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
|
|
|
|
var validators []*pb.ValidatorRecord
|
|
|
|
for i := 0; i < 40; i++ {
|
|
|
|
validator := &pb.ValidatorRecord{Balance: 32, StartDynasty: 1, EndDynasty: 10}
|
|
|
|
validators = append(validators, validator)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &pb.CrystallizedState{
|
|
|
|
Validators: validators,
|
|
|
|
CurrentDynasty: 1,
|
|
|
|
TotalDeposits: 100,
|
|
|
|
LastJustifiedSlot: 4,
|
|
|
|
LastFinalizedSlot: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary representation of bitfield: 11001000 10010100 10010010 10110011 00110001
|
2018-09-15 14:51:17 +00:00
|
|
|
testAttesterBitfield := []*pb.AggregatedAttestation{{AttesterBitfield: []byte{200, 148, 146, 179, 49}}}
|
2018-08-25 18:59:46 +00:00
|
|
|
rewardedValidators, err := CalculateRewards(
|
|
|
|
testAttesterBitfield,
|
|
|
|
data.Validators,
|
|
|
|
data.CurrentDynasty,
|
|
|
|
data.TotalDeposits)
|
|
|
|
if err != nil {
|
2018-08-24 04:09:59 +00:00
|
|
|
t.Fatalf("could not compute validator rewards and penalties: %v", err)
|
|
|
|
}
|
2018-08-25 18:59:46 +00:00
|
|
|
if rewardedValidators[0].Balance != uint64(33) {
|
|
|
|
t.Fatalf("validator balance not updated: %d", rewardedValidators[0].Balance)
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
2018-08-25 18:59:46 +00:00
|
|
|
if rewardedValidators[7].Balance != uint64(31) {
|
|
|
|
t.Fatalf("validator balance not updated: %d", rewardedValidators[7].Balance)
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
2018-08-25 18:59:46 +00:00
|
|
|
if rewardedValidators[29].Balance != uint64(31) {
|
|
|
|
t.Fatalf("validator balance not updated: %d", rewardedValidators[29].Balance)
|
2018-08-24 04:09:59 +00:00
|
|
|
}
|
|
|
|
}
|