2019-10-04 07:41:09 +00:00
|
|
|
package epoch_test
|
2019-09-19 20:59:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-09-19 20:59:23 +00:00
|
|
|
"github.com/prysmaticlabs/go-bitfield"
|
2019-10-04 07:41:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch"
|
2019-09-19 20:59:23 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-11-13 05:17:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-09-19 20:59:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
2019-11-13 05:17:34 +00:00
|
|
|
func TestComputeValidatorParticipation_PreviousEpoch(t *testing.T) {
|
2019-09-19 20:59:23 +00:00
|
|
|
params.OverrideBeaconConfig(params.MinimalSpecConfig())
|
2019-10-04 07:41:09 +00:00
|
|
|
e := uint64(1)
|
2019-11-13 05:17:34 +00:00
|
|
|
attestedBalance := uint64(20) * params.BeaconConfig().MaxEffectiveBalance
|
2019-09-19 20:59:23 +00:00
|
|
|
validatorCount := uint64(100)
|
|
|
|
|
|
|
|
validators := make([]*ethpb.Validator, validatorCount)
|
|
|
|
balances := make([]uint64, validatorCount)
|
|
|
|
for i := 0; i < len(validators); i++ {
|
|
|
|
validators[i] = ðpb.Validator{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
}
|
|
|
|
balances[i] = params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
}
|
|
|
|
|
2019-11-13 05:17:34 +00:00
|
|
|
blockRoots := make([][]byte, 256)
|
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
|
|
|
slot := bytesutil.Bytes32(uint64(i))
|
|
|
|
blockRoots[i] = slot
|
|
|
|
}
|
|
|
|
target := ðpb.Checkpoint{
|
|
|
|
Epoch: e,
|
|
|
|
Root: blockRoots[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
atts := []*pb.PendingAttestation{
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: 0},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: 1},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: 2},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: 3},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: 4},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &pb.BeaconState{
|
|
|
|
Slot: e*params.BeaconConfig().SlotsPerEpoch + 1,
|
|
|
|
Validators: validators,
|
|
|
|
Balances: balances,
|
|
|
|
BlockRoots: blockRoots,
|
|
|
|
Slashings: []uint64{0, 1e9, 1e9},
|
|
|
|
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
|
|
|
PreviousEpochAttestations: atts,
|
|
|
|
FinalizedCheckpoint: ðpb.Checkpoint{},
|
|
|
|
JustificationBits: bitfield.Bitvector4{0x00},
|
|
|
|
PreviousJustifiedCheckpoint: target,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := epoch.ComputeValidatorParticipation(s, e-1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted := ðpb.ValidatorParticipation{
|
|
|
|
VotedEther: attestedBalance,
|
|
|
|
EligibleEther: validatorCount * params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
GlobalParticipationRate: float32(attestedBalance) / float32(validatorCount*params.BeaconConfig().MaxEffectiveBalance),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, wanted) {
|
|
|
|
t.Errorf("Incorrect validator participation, wanted %v received %v", wanted, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComputeValidatorParticipation_CurrentEpoch(t *testing.T) {
|
|
|
|
params.OverrideBeaconConfig(params.MinimalSpecConfig())
|
|
|
|
e := uint64(1)
|
|
|
|
attestedBalance := uint64(16) * params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
validatorCount := uint64(100)
|
|
|
|
|
|
|
|
validators := make([]*ethpb.Validator, validatorCount)
|
|
|
|
balances := make([]uint64, validatorCount)
|
|
|
|
for i := 0; i < len(validators); i++ {
|
|
|
|
validators[i] = ðpb.Validator{
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
}
|
|
|
|
balances[i] = params.BeaconConfig().MaxEffectiveBalance
|
|
|
|
}
|
|
|
|
|
|
|
|
slot := e*params.BeaconConfig().SlotsPerEpoch + 4
|
|
|
|
blockRoots := make([][]byte, 256)
|
|
|
|
for i := 0; i < len(blockRoots); i++ {
|
|
|
|
slot := bytesutil.Bytes32(uint64(i))
|
|
|
|
blockRoots[i] = slot
|
|
|
|
}
|
|
|
|
target := ðpb.Checkpoint{
|
|
|
|
Epoch: e,
|
|
|
|
Root: blockRoots[params.BeaconConfig().SlotsPerEpoch],
|
|
|
|
}
|
|
|
|
|
|
|
|
atts := []*pb.PendingAttestation{
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: slot - 4},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: slot - 3},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: slot - 2},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Data: ðpb.AttestationData{Target: target, Slot: slot - 1},
|
|
|
|
AggregationBits: []byte{0xFF, 0xFF, 0xFF, 0xFF},
|
|
|
|
},
|
|
|
|
}
|
2019-09-19 20:59:23 +00:00
|
|
|
|
|
|
|
s := &pb.BeaconState{
|
2019-11-13 05:17:34 +00:00
|
|
|
Slot: slot,
|
2019-09-19 20:59:23 +00:00
|
|
|
Validators: validators,
|
|
|
|
Balances: balances,
|
2019-11-13 05:17:34 +00:00
|
|
|
BlockRoots: blockRoots,
|
2019-09-19 20:59:23 +00:00
|
|
|
Slashings: []uint64{0, 1e9, 1e9},
|
|
|
|
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
|
|
|
|
CurrentEpochAttestations: atts,
|
|
|
|
FinalizedCheckpoint: ðpb.Checkpoint{},
|
|
|
|
JustificationBits: bitfield.Bitvector4{0x00},
|
2019-11-13 05:17:34 +00:00
|
|
|
CurrentJustifiedCheckpoint: target,
|
2019-09-19 20:59:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 05:17:34 +00:00
|
|
|
res, err := epoch.ComputeValidatorParticipation(s, e)
|
2019-09-19 20:59:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted := ðpb.ValidatorParticipation{
|
|
|
|
VotedEther: attestedBalance,
|
|
|
|
EligibleEther: validatorCount * params.BeaconConfig().MaxEffectiveBalance,
|
|
|
|
GlobalParticipationRate: float32(attestedBalance) / float32(validatorCount*params.BeaconConfig().MaxEffectiveBalance),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(res, wanted) {
|
|
|
|
t.Errorf("Incorrect validator participation, wanted %v received %v", wanted, res)
|
|
|
|
}
|
|
|
|
}
|