prysm-pulse/beacon-chain/casper/sharding.go
Raul Jordan 4a1b5db07b
beacon: Store Incoming Blocks and Calculated States in DAG (#422)
* begin with a block/state dag approach in prep for fork choice

* add TODOs for fork choice items, add data structure for forks

* remove syncing and processing states for incoming block sync

* simulator only broadcasts blocks now, no more state simulation

* fix sim tests, no more state sim

* bazel rerun

* naive fork choice

* split update head routine

* pesky race conditions

* fork choice rule works

* dag + fork choice working

* canonical head storage across sessions

* todo: save dag

* no more stalling after 10 blocks, using event feeds

* address review

* sync instead uses event feed

* refactored pure funcs into casper package

* tests pass

* fix lint

* refactor get blockhash

* refactor blockhashforslot

* event feed for incoming blocks in chainservice

* use config

* addressed all comments

* fix typo

* address yutaro comment

* using db interface

* check if parent hash in previous slot DAG

* works

* tests

* drop ffg suffix

* bazel gazelle

* full cov validators by height shard

* gazelle

* state tests

* all state tests

* 100% cov types and 99% cover casper

* cov up

* 80% blockchain cov

* fix lint
2018-08-23 22:09:59 -06:00

57 lines
1.8 KiB
Go

package casper
import (
"github.com/prysmaticlabs/prysm/beacon-chain/params"
"github.com/prysmaticlabs/prysm/beacon-chain/types"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
)
// BeaconCommittee structure encompassing a specific shard and validator indices
// within that shard's committee.
type BeaconCommittee struct {
shardID int
committee []int
}
// ValidatorsByHeightShard splits a shuffled validator list by height and by shard,
// it ensures there's enough validators per height and per shard, if not, it'll skip
// some heights and shards.
func ValidatorsByHeightShard(crystallized *types.CrystallizedState) ([]*BeaconCommittee, error) {
indices := ActiveValidatorIndices(crystallized)
var committeesPerSlot int
var slotsPerCommittee int
var committees []*BeaconCommittee
if len(indices) >= params.CycleLength*params.MinCommiteeSize {
committeesPerSlot = len(indices)/params.CycleLength/(params.MinCommiteeSize*2) + 1
slotsPerCommittee = 1
} else {
committeesPerSlot = 1
slotsPerCommittee = 1
for len(indices)*slotsPerCommittee < params.MinCommiteeSize && slotsPerCommittee < params.CycleLength {
slotsPerCommittee *= 2
}
}
// split the shuffled list for heights.
shuffledList, err := utils.ShuffleIndices(crystallized.DynastySeed(), indices)
if err != nil {
return nil, err
}
heightList := utils.SplitIndices(shuffledList, params.CycleLength)
// split the shuffled height list for shards
for i, subList := range heightList {
shardList := utils.SplitIndices(subList, params.MinCommiteeSize)
for _, shardIndex := range shardList {
shardID := int(crystallized.CrosslinkingStartShard()) + i*committeesPerSlot/slotsPerCommittee
committees = append(committees, &BeaconCommittee{
shardID: shardID,
committee: shardIndex,
})
}
}
return committees, nil
}