mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 07:58:22 -05:00
Change Default Validator Balance to Big Int (#559)
This commit is contained in:
@@ -78,7 +78,7 @@ func (c *ChainService) Start() {
|
||||
// TODO(#474): Fetch the slot: (block, state) DAGs from persistent storage
|
||||
// to truly continue across sessions.
|
||||
log.Infof("Starting service")
|
||||
go c.updateHead(time.NewTicker(time.Second * params.SlotDuration).C)
|
||||
go c.updateHead(time.NewTicker(time.Second * time.Duration(params.SlotDuration)).C)
|
||||
go c.blockProcessing()
|
||||
}
|
||||
|
||||
|
||||
@@ -383,10 +383,10 @@ func TestRunningChainService(t *testing.T) {
|
||||
}
|
||||
|
||||
secondsSinceGenesis := time.Since(types.GenesisTime).Seconds()
|
||||
currentSlot := uint64(math.Floor(secondsSinceGenesis / params.SlotDuration))
|
||||
currentSlot := uint64(math.Floor(secondsSinceGenesis / float64(params.SlotDuration)))
|
||||
|
||||
slotsStart := int64(crystallized.LastStateRecalc()) - params.CycleLength
|
||||
slotIndex := (int64(currentSlot) - slotsStart) % params.CycleLength
|
||||
slotsStart := crystallized.LastStateRecalc() - params.CycleLength
|
||||
slotIndex := (currentSlot - slotsStart) % params.CycleLength
|
||||
shardID := crystallized.ShardAndCommitteesForSlots()[slotIndex].ArrayShardAndCommittee[0].ShardId
|
||||
|
||||
block := types.NewBlock(&pb.BeaconBlock{
|
||||
@@ -749,7 +749,7 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) {
|
||||
}
|
||||
|
||||
secondsSinceGenesis := time.Since(types.GenesisTime).Seconds()
|
||||
currentSlot := uint64(math.Floor(secondsSinceGenesis / params.SlotDuration))
|
||||
currentSlot := uint64(math.Floor(secondsSinceGenesis / float64(params.SlotDuration)))
|
||||
|
||||
block1 := types.NewBlock(&pb.BeaconBlock{
|
||||
ParentHash: block0Hash[:],
|
||||
|
||||
@@ -28,7 +28,7 @@ func CalculateRewards(
|
||||
depositFactor := (totalParticipatedDeposit - totalDeposit) / totalDeposit
|
||||
|
||||
log.Debugf("Applying rewards and penalties for the validators for slot %d", slot)
|
||||
if timeSinceFinality <= 2*(params.CycleLength) {
|
||||
if timeSinceFinality <= 2*params.CycleLength {
|
||||
for _, validatorIndex := range activeValidators {
|
||||
var voted bool
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ func splitBySlotShard(shuffledValidators []uint32, crosslinkStartShard uint64) [
|
||||
committeBySlotAndShard := []*pb.ShardAndCommitteeArray{}
|
||||
|
||||
// split the validator indices by slot.
|
||||
validatorsBySlot := utils.SplitIndices(shuffledValidators, params.CycleLength)
|
||||
validatorsBySlot := utils.SplitIndices(shuffledValidators, int(params.CycleLength))
|
||||
for i, validatorsForSlot := range validatorsBySlot {
|
||||
shardCommittees := []*pb.ShardAndCommittee{}
|
||||
validatorsByShard := utils.SplitIndices(validatorsForSlot, committeesPerSlot)
|
||||
@@ -58,14 +58,14 @@ func splitBySlotShard(shuffledValidators []uint32, crosslinkStartShard uint64) [
|
||||
// If numActiveValidators < CycleLength * MinCommitteeSize, committees span across multiple slots
|
||||
// to attest the same shard.
|
||||
func getCommitteeParams(numValidators int) (committeesPerSlot, slotsPerCommittee int) {
|
||||
if numValidators >= params.CycleLength*params.MinCommiteeSize {
|
||||
committeesPerSlot := numValidators/(params.CycleLength*params.MinCommiteeSize*2) + 1
|
||||
if numValidators >= int(params.CycleLength*params.MinCommiteeSize) {
|
||||
committeesPerSlot := numValidators/int(params.CycleLength*params.MinCommiteeSize*2) + 1
|
||||
return committeesPerSlot, 1
|
||||
}
|
||||
|
||||
slotsPerCommittee = 1
|
||||
for numValidators*slotsPerCommittee < params.MinCommiteeSize*params.CycleLength &&
|
||||
slotsPerCommittee < params.CycleLength {
|
||||
for numValidators*slotsPerCommittee < int(params.MinCommiteeSize*params.CycleLength) &&
|
||||
slotsPerCommittee < int(params.CycleLength) {
|
||||
slotsPerCommittee *= 2
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ func Test1000ActiveValidators(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("validatorsBySlotShard failed with %v:", err)
|
||||
}
|
||||
if len(indices) != params.CycleLength {
|
||||
if len(indices) != int(params.CycleLength) {
|
||||
t.Errorf("incorret length for validator indices. Want: %d. Got: %v", params.CycleLength, len(indices))
|
||||
}
|
||||
}
|
||||
@@ -124,13 +124,13 @@ func TestSmallSampleValidators(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("validatorsBySlotShard failed with %v:", err)
|
||||
}
|
||||
if len(indices) != params.CycleLength {
|
||||
if len(indices) != int(params.CycleLength) {
|
||||
t.Errorf("incorret length for validator indices. Want: %d. Got: %d", params.CycleLength, len(indices))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCommitteeParamsSmallValidatorSet(t *testing.T) {
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize / 4
|
||||
numValidators := int(params.CycleLength * params.MinCommiteeSize / 4)
|
||||
|
||||
committesPerSlot, slotsPerCommittee := getCommitteeParams(numValidators)
|
||||
if committesPerSlot != 1 {
|
||||
@@ -143,7 +143,7 @@ func TestGetCommitteeParamsSmallValidatorSet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetCommitteeParamsRegularValidatorSet(t *testing.T) {
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize
|
||||
numValidators := int(params.CycleLength * params.MinCommiteeSize)
|
||||
|
||||
committesPerSlot, slotsPerCommittee := getCommitteeParams(numValidators)
|
||||
if committesPerSlot != 1 {
|
||||
@@ -156,7 +156,7 @@ func TestGetCommitteeParamsRegularValidatorSet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetCommitteeParamsLargeValidatorSet(t *testing.T) {
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize * 8
|
||||
numValidators := int(params.CycleLength*params.MinCommiteeSize) * 8
|
||||
|
||||
committesPerSlot, slotsPerCommittee := getCommitteeParams(numValidators)
|
||||
if committesPerSlot != 5 {
|
||||
@@ -170,14 +170,14 @@ func TestGetCommitteeParamsLargeValidatorSet(t *testing.T) {
|
||||
|
||||
func TestValidatorsBySlotShardRegularValidatorSet(t *testing.T) {
|
||||
validatorIndices := []uint32{}
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize
|
||||
numValidators := int(params.CycleLength * params.MinCommiteeSize)
|
||||
for i := 0; i < numValidators; i++ {
|
||||
validatorIndices = append(validatorIndices, uint32(i))
|
||||
}
|
||||
|
||||
shardAndCommitteeArray := splitBySlotShard(validatorIndices, 0)
|
||||
|
||||
if len(shardAndCommitteeArray) != params.CycleLength {
|
||||
if len(shardAndCommitteeArray) != int(params.CycleLength) {
|
||||
t.Fatalf("Expected length %d: got %d", params.CycleLength, len(shardAndCommitteeArray))
|
||||
}
|
||||
|
||||
@@ -188,7 +188,7 @@ func TestValidatorsBySlotShardRegularValidatorSet(t *testing.T) {
|
||||
}
|
||||
|
||||
committeeSize := len(shardAndCommittees[0].Committee)
|
||||
if committeeSize != params.MinCommiteeSize {
|
||||
if committeeSize != int(params.MinCommiteeSize) {
|
||||
t.Fatalf("Expected committee size %d: got %d", params.MinCommiteeSize, committeeSize)
|
||||
}
|
||||
}
|
||||
@@ -196,14 +196,14 @@ func TestValidatorsBySlotShardRegularValidatorSet(t *testing.T) {
|
||||
|
||||
func TestValidatorsBySlotShardLargeValidatorSet(t *testing.T) {
|
||||
validatorIndices := []uint32{}
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize * 2
|
||||
numValidators := int(params.CycleLength*params.MinCommiteeSize) * 2
|
||||
for i := 0; i < numValidators; i++ {
|
||||
validatorIndices = append(validatorIndices, uint32(i))
|
||||
}
|
||||
|
||||
shardAndCommitteeArray := splitBySlotShard(validatorIndices, 0)
|
||||
|
||||
if len(shardAndCommitteeArray) != params.CycleLength {
|
||||
if len(shardAndCommitteeArray) != int(params.CycleLength) {
|
||||
t.Fatalf("Expected length %d: got %d", params.CycleLength, len(shardAndCommitteeArray))
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ func TestValidatorsBySlotShardLargeValidatorSet(t *testing.T) {
|
||||
shardCommittee := shardAndCommittees[j]
|
||||
t.Logf("shard %d", shardCommittee.ShardId)
|
||||
t.Logf("committee: %v", shardCommittee.Committee)
|
||||
if len(shardCommittee.Committee) != params.MinCommiteeSize {
|
||||
if len(shardCommittee.Committee) != int(params.MinCommiteeSize) {
|
||||
t.Fatalf("Expected committee size %d: got %d", params.MinCommiteeSize, len(shardCommittee.Committee))
|
||||
}
|
||||
}
|
||||
@@ -228,14 +228,14 @@ func TestValidatorsBySlotShardLargeValidatorSet(t *testing.T) {
|
||||
|
||||
func TestValidatorsBySlotShardSmallValidatorSet(t *testing.T) {
|
||||
validatorIndices := []uint32{}
|
||||
numValidators := params.CycleLength * params.MinCommiteeSize / 2
|
||||
numValidators := int(params.CycleLength*params.MinCommiteeSize) / 2
|
||||
for i := 0; i < numValidators; i++ {
|
||||
validatorIndices = append(validatorIndices, uint32(i))
|
||||
}
|
||||
|
||||
shardAndCommitteeArray := splitBySlotShard(validatorIndices, 0)
|
||||
|
||||
if len(shardAndCommitteeArray) != params.CycleLength {
|
||||
if len(shardAndCommitteeArray) != int(params.CycleLength) {
|
||||
t.Fatalf("Expected length %d: got %d", params.CycleLength, len(shardAndCommitteeArray))
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ func TestValidatorsBySlotShardSmallValidatorSet(t *testing.T) {
|
||||
|
||||
for j := 0; j < len(shardAndCommittees); j++ {
|
||||
shardCommittee := shardAndCommittees[j]
|
||||
if len(shardCommittee.Committee) != params.MinCommiteeSize/2 {
|
||||
if len(shardCommittee.Committee) != int(params.MinCommiteeSize/2) {
|
||||
t.Fatalf("Expected committee size %d: got %d", params.MinCommiteeSize/2, len(shardCommittee.Committee))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,37 +12,6 @@ import (
|
||||
|
||||
const bitsInByte = 8
|
||||
|
||||
// RotateValidatorSet is called every dynasty transition. The primary functions are:
|
||||
// 1.) Go through queued validator indices and induct them to be active by setting start
|
||||
// dynasty to current cycle.
|
||||
// 2.) Remove bad active validator whose balance is below threshold to the exit set by
|
||||
// setting end dynasty to current cycle.
|
||||
func RotateValidatorSet(validators []*pb.ValidatorRecord, dynasty uint64) []*pb.ValidatorRecord {
|
||||
upperbound := len(ActiveValidatorIndices(validators, dynasty))/30 + 1
|
||||
|
||||
// Loop through active validator set, remove validator whose balance is below 50%.
|
||||
for _, index := range ActiveValidatorIndices(validators, dynasty) {
|
||||
if validators[index].Balance < params.DefaultBalance/2 {
|
||||
validators[index].EndDynasty = dynasty
|
||||
}
|
||||
}
|
||||
// Get the total number of validator we can induct.
|
||||
inductNum := upperbound
|
||||
if len(QueuedValidatorIndices(validators, dynasty)) < inductNum {
|
||||
inductNum = len(QueuedValidatorIndices(validators, dynasty))
|
||||
}
|
||||
|
||||
// Induct queued validator to active validator set until the switch dynasty is greater than current number.
|
||||
for _, index := range QueuedValidatorIndices(validators, dynasty) {
|
||||
validators[index].StartDynasty = dynasty
|
||||
inductNum--
|
||||
if inductNum == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return validators
|
||||
}
|
||||
|
||||
// ActiveValidatorIndices filters out active validators based on start and end dynasty
|
||||
// and returns their indices in a list.
|
||||
func ActiveValidatorIndices(validators []*pb.ValidatorRecord, dynasty uint64) []uint32 {
|
||||
@@ -82,8 +51,8 @@ func QueuedValidatorIndices(validators []*pb.ValidatorRecord, dynasty uint64) []
|
||||
// SampleAttestersAndProposers returns lists of random sampled attesters and proposer indices.
|
||||
func SampleAttestersAndProposers(seed common.Hash, validators []*pb.ValidatorRecord, dynasty uint64) ([]uint32, uint32, error) {
|
||||
attesterCount := params.MinCommiteeSize
|
||||
if len(validators) < params.MinCommiteeSize {
|
||||
attesterCount = len(validators)
|
||||
if len(validators) < int(params.MinCommiteeSize) {
|
||||
attesterCount = uint64(len(validators))
|
||||
}
|
||||
indices, err := utils.ShuffleIndices(seed, ActiveValidatorIndices(validators, dynasty))
|
||||
if err != nil {
|
||||
@@ -222,7 +191,7 @@ func TotalActiveValidatorDeposit(dynasty uint64, validators []*pb.ValidatorRecor
|
||||
// TotalActiveValidatorDepositInEth returns the total deposited amount in ETH for all active validators.
|
||||
func TotalActiveValidatorDepositInEth(dynasty uint64, validators []*pb.ValidatorRecord) uint64 {
|
||||
totalDeposit := TotalActiveValidatorDeposit(dynasty, validators)
|
||||
depositInEth := totalDeposit / params.EtherDenomination
|
||||
depositInEth := totalDeposit / uint64(params.EtherDenomination)
|
||||
|
||||
return depositInEth
|
||||
}
|
||||
|
||||
@@ -2,69 +2,14 @@ package casper
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
||||
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
||||
"github.com/prysmaticlabs/prysm/shared"
|
||||
)
|
||||
|
||||
func TestRotateValidatorSet(t *testing.T) {
|
||||
validators := []*pb.ValidatorRecord{
|
||||
{Balance: 10, StartDynasty: 0, EndDynasty: params.DefaultEndDynasty}, // half below default balance, should be moved to exit.
|
||||
{Balance: 15, StartDynasty: 1, EndDynasty: params.DefaultEndDynasty}, // half below default balance, should be moved to exit.
|
||||
{Balance: 20, StartDynasty: 2, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
{Balance: 25, StartDynasty: 3, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
{Balance: 30, StartDynasty: 4, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
{Balance: 30, StartDynasty: 15, EndDynasty: params.DefaultEndDynasty}, // will trigger for loop in rotate val indices.
|
||||
}
|
||||
|
||||
data := &pb.CrystallizedState{
|
||||
Validators: validators,
|
||||
CurrentDynasty: 10,
|
||||
}
|
||||
|
||||
// Rotate validator set and increment dynasty count by 1.
|
||||
rotatedValidators := RotateValidatorSet(data.Validators, data.CurrentDynasty)
|
||||
if !reflect.DeepEqual(ActiveValidatorIndices(rotatedValidators, data.CurrentDynasty), []uint32{2, 3, 4, 5}) {
|
||||
t.Errorf("active validator indices should be [2,3,4,5], got: %v", ActiveValidatorIndices(rotatedValidators, data.CurrentDynasty))
|
||||
}
|
||||
if len(QueuedValidatorIndices(rotatedValidators, data.CurrentDynasty)) != 0 {
|
||||
t.Errorf("queued validator indices should be [], got: %v", QueuedValidatorIndices(rotatedValidators, data.CurrentDynasty))
|
||||
}
|
||||
if !reflect.DeepEqual(ExitedValidatorIndices(rotatedValidators, data.CurrentDynasty), []uint32{0, 1}) {
|
||||
t.Errorf("exited validator indices should be [0,1], got: %v", ExitedValidatorIndices(rotatedValidators, data.CurrentDynasty))
|
||||
}
|
||||
|
||||
// Another run without queuing validators.
|
||||
validators = []*pb.ValidatorRecord{
|
||||
{Balance: 10, StartDynasty: 0, EndDynasty: params.DefaultEndDynasty}, // half below default balance, should be moved to exit.
|
||||
{Balance: 15, StartDynasty: 1, EndDynasty: params.DefaultEndDynasty}, // half below default balance, should be moved to exit.
|
||||
{Balance: 20, StartDynasty: 2, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
{Balance: 25, StartDynasty: 3, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
{Balance: 30, StartDynasty: 4, EndDynasty: params.DefaultEndDynasty}, // stays in active.
|
||||
}
|
||||
|
||||
data = &pb.CrystallizedState{
|
||||
Validators: validators,
|
||||
CurrentDynasty: 10,
|
||||
}
|
||||
|
||||
// rotate validator set and increment dynasty count by 1.
|
||||
RotateValidatorSet(data.Validators, data.CurrentDynasty)
|
||||
|
||||
if !reflect.DeepEqual(ActiveValidatorIndices(data.Validators, data.CurrentDynasty), []uint32{2, 3, 4}) {
|
||||
t.Errorf("active validator indices should be [2,3,4], got: %v", ActiveValidatorIndices(data.Validators, data.CurrentDynasty))
|
||||
}
|
||||
if len(QueuedValidatorIndices(data.Validators, data.CurrentDynasty)) != 0 {
|
||||
t.Errorf("queued validator indices should be [], got: %v", QueuedValidatorIndices(data.Validators, data.CurrentDynasty))
|
||||
}
|
||||
if !reflect.DeepEqual(ExitedValidatorIndices(data.Validators, data.CurrentDynasty), []uint32{0, 1}) {
|
||||
t.Errorf("exited validator indices should be [0,1], got: %v", ExitedValidatorIndices(data.Validators, data.CurrentDynasty))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasVoted(t *testing.T) {
|
||||
// Setting bit field to 11111111.
|
||||
pendingAttestation := &pb.AggregatedAttestation{
|
||||
@@ -319,8 +264,11 @@ func TestTotalActiveValidatorDeposit(t *testing.T) {
|
||||
validators = append(validators, &pb.ValidatorRecord{StartDynasty: 0, EndDynasty: 10, Balance: 1e18})
|
||||
}
|
||||
|
||||
expectedTotalDeposit := new(big.Int)
|
||||
expectedTotalDeposit.SetString("10000000000000000000", 10)
|
||||
|
||||
totalDeposit := TotalActiveValidatorDeposit(0, validators)
|
||||
if totalDeposit != 10e18 {
|
||||
if expectedTotalDeposit.Cmp(new(big.Int).SetUint64(totalDeposit)) != 0 {
|
||||
t.Fatalf("incorrect total deposit calculated %d", totalDeposit)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,36 +1,38 @@
|
||||
// Package params defines important constants that are essential to the beacon chain.
|
||||
package params
|
||||
|
||||
const (
|
||||
// AttesterReward determines how much ETH attesters get for performing their duty.
|
||||
AttesterReward = 1
|
||||
import (
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var (
|
||||
// CycleLength is the beacon chain cycle length in slots.
|
||||
CycleLength = 64
|
||||
CycleLength = uint64(64)
|
||||
// ShardCount is a fixed number.
|
||||
ShardCount = 1024
|
||||
// DefaultBalance of a validator in ETH.
|
||||
DefaultBalance = 32
|
||||
// DefaultBalance of a validator in wei.
|
||||
DefaultBalance = new(big.Int).Div(big.NewInt(32), big.NewInt(int64(EtherDenomination)))
|
||||
// MaxValidators in the protocol.
|
||||
MaxValidators = 4194304
|
||||
// SlotDuration in seconds.
|
||||
SlotDuration = 8
|
||||
SlotDuration = uint64(8)
|
||||
// Cofactor is used cutoff algorithm to select slot and shard cutoffs.
|
||||
Cofactor = 19
|
||||
// MinCommiteeSize is the minimal number of validator needs to be in a committee.
|
||||
MinCommiteeSize = 128
|
||||
MinCommiteeSize = uint64(128)
|
||||
// DefaultEndDynasty is the upper bound of dynasty. We use it to track queued and exited validators.
|
||||
DefaultEndDynasty = 9999999999999999999
|
||||
DefaultEndDynasty = uint64(999999999999999999)
|
||||
// BootstrappedValidatorsCount is the number of validators we seed the first crystallized
|
||||
// state with. This number has yet to be decided by research and is arbitrary for now.
|
||||
BootstrappedValidatorsCount = 1000
|
||||
// MinDynastyLength is the slots needed before dynasty transition happens.
|
||||
MinDynastyLength = 256
|
||||
MinDynastyLength = uint64(256)
|
||||
// EtherDenomination is the denomination of ether in wei.
|
||||
EtherDenomination = 1e18
|
||||
// BaseRewardQuotient is where 1/BaseRewardQuotient is the per-slot interest rate which will,
|
||||
// compound to an annual rate of 3.88% for 10 million eth staked.
|
||||
BaseRewardQuotient = 32768
|
||||
BaseRewardQuotient = uint64(32768)
|
||||
// SqrtDropTime is a constant set to reflect the amount of time it will take for the quadratic leak to
|
||||
// cut nonparticipating validators’ deposits by 39.4%.
|
||||
SqrtDropTime = 1048576
|
||||
SqrtDropTime = uint64(1048576)
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ type ActiveState struct {
|
||||
func NewGenesisActiveState() *ActiveState {
|
||||
// Bootstrap recent block hashes to all 0s for first 2 cycles.
|
||||
var recentBlockHashes [][]byte
|
||||
for i := 0; i < 2*params.CycleLength; i++ {
|
||||
for i := 0; i < 2*int(params.CycleLength); i++ {
|
||||
recentBlockHashes = append(recentBlockHashes, make([]byte, 0, 32))
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ func (a *ActiveState) calculateNewBlockHashes(block *Block, parentSlot uint64) (
|
||||
distance := block.SlotNumber() - parentSlot
|
||||
existing := a.data.RecentBlockHashes
|
||||
update := existing[distance:]
|
||||
for len(update) < 2*params.CycleLength {
|
||||
for len(update) < 2*int(params.CycleLength) {
|
||||
update = append(update, hash[:])
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestGenesisActiveState_HashEquality(t *testing.T) {
|
||||
|
||||
func TestGenesisActiveState_InitializesRecentBlockHashes(t *testing.T) {
|
||||
as := NewGenesisActiveState()
|
||||
want, got := len(as.data.RecentBlockHashes), 2*params.CycleLength
|
||||
want, got := len(as.data.RecentBlockHashes), 2*int(params.CycleLength)
|
||||
if want != got {
|
||||
t.Errorf("Wrong number of recent block hashes. Got: %d Want: %d", got, want)
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func TestUpdateRecentBlockHashes(t *testing.T) {
|
||||
})
|
||||
|
||||
recentBlockHashes := [][]byte{}
|
||||
for i := 0; i < 2*params.CycleLength; i++ {
|
||||
for i := 0; i < 2*int(params.CycleLength); i++ {
|
||||
recentBlockHashes = append(recentBlockHashes, []byte{0})
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ func TestUpdateRecentBlockHashes(t *testing.T) {
|
||||
t.Fatalf("failed to update recent blockhashes: %v", err)
|
||||
}
|
||||
|
||||
if len(updated) != 2*params.CycleLength {
|
||||
if len(updated) != 2*int(params.CycleLength) {
|
||||
t.Fatalf("length of updated recent blockhashes should be %d: found %d", params.CycleLength, len(updated))
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ func TestCalculateNewActiveState(t *testing.T) {
|
||||
}
|
||||
|
||||
recentBlockHashes := [][]byte{}
|
||||
for i := 0; i < 2*params.CycleLength; i++ {
|
||||
for i := 0; i < 2*int(params.CycleLength); i++ {
|
||||
recentBlockHashes = append(recentBlockHashes, []byte{0})
|
||||
}
|
||||
|
||||
@@ -263,7 +263,7 @@ func TestCalculateNewActiveState(t *testing.T) {
|
||||
t.Fatalf("expected 2 pending attestations, got %d", len(aState.PendingAttestations()))
|
||||
}
|
||||
|
||||
if len(aState.RecentBlockHashes()) != 2*params.CycleLength {
|
||||
if len(aState.RecentBlockHashes()) != 2*int(params.CycleLength) {
|
||||
t.Fatalf("incorrect number of items in RecentBlockHashes: %d", len(aState.RecentBlockHashes()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func TestBlockValidity(t *testing.T) {
|
||||
}
|
||||
|
||||
recentBlockHashes := make([][]byte, 2*params.CycleLength)
|
||||
for i := 0; i < 2*params.CycleLength; i++ {
|
||||
for i := 0; i < 2*int(params.CycleLength); i++ {
|
||||
recentBlockHashes = append(recentBlockHashes, make([]byte, 32))
|
||||
}
|
||||
aState := NewActiveState(&pb.ActiveState{
|
||||
|
||||
@@ -31,7 +31,7 @@ func initialValidators() []*pb.ValidatorRecord {
|
||||
validator := &pb.ValidatorRecord{
|
||||
StartDynasty: 0,
|
||||
EndDynasty: params.DefaultEndDynasty,
|
||||
Balance: params.DefaultBalance,
|
||||
Balance: params.DefaultBalance.Uint64(),
|
||||
WithdrawalAddress: []byte{},
|
||||
PublicKey: 0,
|
||||
}
|
||||
@@ -214,8 +214,8 @@ func (c *CrystallizedState) isDynastyTransition(slotNumber uint64) bool {
|
||||
|
||||
// getAttesterIndices fetches the attesters for a given attestation record.
|
||||
func (c *CrystallizedState) getAttesterIndices(attestation *pb.AggregatedAttestation) ([]uint32, error) {
|
||||
slotsStart := int64(c.LastStateRecalc()) - params.CycleLength
|
||||
slotIndex := (int64(attestation.Slot) - slotsStart) % params.CycleLength
|
||||
slotsStart := c.LastStateRecalc() - params.CycleLength
|
||||
slotIndex := (attestation.Slot - slotsStart) % params.CycleLength
|
||||
// TODO(#267): ShardAndCommitteesForSlots will return default value because the spec for dynasty transition is not finalized.
|
||||
shardCommitteeArray := c.data.ShardAndCommitteesForSlots
|
||||
shardCommittee := shardCommitteeArray[slotIndex].ArrayShardAndCommittee
|
||||
@@ -233,6 +233,7 @@ func (c *CrystallizedState) getAttesterIndices(attestation *pb.AggregatedAttesta
|
||||
func (c *CrystallizedState) NewStateRecalculations(aState *ActiveState, block *Block) (*CrystallizedState, error) {
|
||||
var blockVoteBalance uint64
|
||||
var rewardedValidators []*pb.ValidatorRecord
|
||||
var lastStateRecalcCycleBack uint64
|
||||
|
||||
justifiedStreak := c.JustifiedStreak()
|
||||
justifiedSlot := c.LastJustifiedSlot()
|
||||
@@ -245,11 +246,16 @@ func (c *CrystallizedState) NewStateRecalculations(aState *ActiveState, block *B
|
||||
timeSinceFinality := block.SlotNumber() - c.LastFinalizedSlot()
|
||||
recentBlockHashes := aState.RecentBlockHashes()
|
||||
|
||||
if lastStateRecalc < params.CycleLength {
|
||||
lastStateRecalcCycleBack = 0
|
||||
} else {
|
||||
lastStateRecalcCycleBack = lastStateRecalc - params.CycleLength
|
||||
}
|
||||
|
||||
// walk through all the slots from LastStateRecalc - cycleLength to LastStateRecalc - 1.
|
||||
for i := uint64(0); i < params.CycleLength; i++ {
|
||||
var voterIndices []uint32
|
||||
|
||||
slot := lastStateRecalc - params.CycleLength + i
|
||||
slot := lastStateRecalcCycleBack + i
|
||||
blockHash := recentBlockHashes[i]
|
||||
if _, ok := blockVoteCache[blockHash]; ok {
|
||||
blockVoteBalance = blockVoteCache[blockHash].VoteTotalDeposit
|
||||
@@ -278,7 +284,7 @@ func (c *CrystallizedState) NewStateRecalculations(aState *ActiveState, block *B
|
||||
justifiedStreak = 0
|
||||
}
|
||||
|
||||
if justifiedStreak >= params.CycleLength+1 && slot-params.CycleLength > finalizedSlot {
|
||||
if slot >= params.CycleLength && justifiedStreak >= params.CycleLength+1 && slot-params.CycleLength > finalizedSlot {
|
||||
finalizedSlot = slot - params.CycleLength
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,11 +58,11 @@ func TestInitialDeriveCrystallizedState(t *testing.T) {
|
||||
t.Fatalf("failed to derive new crystallized state: %v", err)
|
||||
}
|
||||
|
||||
if newCState.LastJustifiedSlot() != 0 {
|
||||
t.Fatalf("expected justified slot to equal %d: got %d", 0, newCState.LastFinalizedSlot())
|
||||
if newCState.LastJustifiedSlot() != 63 {
|
||||
t.Fatalf("expected justified slot to equal %d: got %d", 0, newCState.LastJustifiedSlot())
|
||||
}
|
||||
|
||||
if newCState.JustifiedStreak() != 0 {
|
||||
if newCState.JustifiedStreak() != 64 {
|
||||
t.Fatalf("expected justified streak to equal %d: got %d", 0, newCState.JustifiedStreak())
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func TestNextDeriveCrystallizedSlot(t *testing.T) {
|
||||
totalDeposits := cState.TotalDeposits()
|
||||
recentBlockHashes := make([][]byte, 3*params.CycleLength)
|
||||
voteCache := make(map[[32]byte]*VoteCache)
|
||||
for i := 0; i < 3*params.CycleLength; i++ {
|
||||
for i := 0; i < 3*int(params.CycleLength); i++ {
|
||||
blockHash := [32]byte{}
|
||||
counter := []byte(strconv.Itoa(i))
|
||||
copy(blockHash[:], counter)
|
||||
@@ -122,7 +122,7 @@ func TestNextDeriveCrystallizedSlot(t *testing.T) {
|
||||
if cState.LastJustifiedSlot() != params.CycleLength-1 {
|
||||
t.Fatalf("expected justified slot to equal %d: got %d", params.CycleLength-1, cState.LastJustifiedSlot())
|
||||
}
|
||||
if cState.JustifiedStreak() != params.CycleLength {
|
||||
if cState.JustifiedStreak() != 2*params.CycleLength {
|
||||
t.Fatalf("expected justified streak to equal %d: got %d", params.CycleLength, cState.JustifiedStreak())
|
||||
}
|
||||
if cState.LastFinalizedSlot() != 0 {
|
||||
@@ -139,7 +139,7 @@ func TestNextDeriveCrystallizedSlot(t *testing.T) {
|
||||
if cState.LastJustifiedSlot() != 2*params.CycleLength-1 {
|
||||
t.Fatalf("expected justified slot to equal %d: got %d", 2*params.CycleLength-1, cState.LastJustifiedSlot())
|
||||
}
|
||||
if cState.JustifiedStreak() != 2*params.CycleLength {
|
||||
if cState.JustifiedStreak() != 3*params.CycleLength {
|
||||
t.Fatalf("expected justified streak to equal %d: got %d", 2*params.CycleLength, cState.JustifiedStreak())
|
||||
}
|
||||
if cState.LastFinalizedSlot() != params.CycleLength-1 {
|
||||
@@ -273,7 +273,7 @@ func TestNewDynastyRecalculations(t *testing.T) {
|
||||
|
||||
// Create shard committee for every slot.
|
||||
var shardCommitteesForSlot []*pb.ShardAndCommitteeArray
|
||||
for i := 0; i < params.CycleLength; i++ {
|
||||
for i := 0; i < int(params.CycleLength); i++ {
|
||||
// Only 10 shards gets crosslinked by validators this dynasty.
|
||||
var shardCommittees []*pb.ShardAndCommittee
|
||||
for i := 0; i < 10; i++ {
|
||||
|
||||
@@ -53,14 +53,14 @@ func TestSplitIndices(t *testing.T) {
|
||||
for i := 0; i < validators; i++ {
|
||||
l = append(l, uint32(i))
|
||||
}
|
||||
split := SplitIndices(l, params.CycleLength)
|
||||
if len(split) != params.CycleLength {
|
||||
split := SplitIndices(l, int(params.CycleLength))
|
||||
if len(split) != int(params.CycleLength) {
|
||||
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", params.CycleLength, len(split))
|
||||
}
|
||||
|
||||
for _, s := range split {
|
||||
if len(s) != validators/params.CycleLength {
|
||||
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", validators/params.CycleLength, len(s))
|
||||
if len(s) != validators/int(params.CycleLength) {
|
||||
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", validators/int(params.CycleLength), len(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user