mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
Use justified state as start state for running fork choice (#2093)
* use justified state as start state * improts
This commit is contained in:
parent
79a04cac83
commit
324a186786
@ -120,7 +120,11 @@ func (c *ChainService) ApplyForkChoiceRule(ctx context.Context, block *pb.Beacon
|
||||
defer span.End()
|
||||
log.Info("Applying LMD-GHOST Fork Choice Rule")
|
||||
|
||||
attestationTargets, err := c.attestationTargets(postState)
|
||||
justifiedState, err := c.beaconDB.JustifiedState()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not retrieve justified state: %v", err)
|
||||
}
|
||||
attestationTargets, err := c.attestationTargets(justifiedState)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not retrieve attestation target: %v", err)
|
||||
}
|
||||
@ -128,7 +132,7 @@ func (c *ChainService) ApplyForkChoiceRule(ctx context.Context, block *pb.Beacon
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
head, err := c.lmdGhost(justifiedHead, postState, attestationTargets)
|
||||
head, err := c.lmdGhost(justifiedHead, justifiedState, postState, attestationTargets)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not run fork choice: %v", err)
|
||||
}
|
||||
@ -191,13 +195,14 @@ func (c *ChainService) ApplyForkChoiceRule(ctx context.Context, block *pb.Beacon
|
||||
// return head
|
||||
// head = max(children, key=get_vote_count)
|
||||
func (c *ChainService) lmdGhost(
|
||||
block *pb.BeaconBlock,
|
||||
state *pb.BeaconState,
|
||||
startBlock *pb.BeaconBlock,
|
||||
startState *pb.BeaconState,
|
||||
currState *pb.BeaconState,
|
||||
voteTargets map[uint64]*pb.BeaconBlock,
|
||||
) (*pb.BeaconBlock, error) {
|
||||
head := block
|
||||
head := startBlock
|
||||
for {
|
||||
children, err := c.blockChildren(head, state.Slot)
|
||||
children, err := c.blockChildren(head, currState.Slot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not fetch block children: %v", err)
|
||||
}
|
||||
@ -206,12 +211,12 @@ func (c *ChainService) lmdGhost(
|
||||
}
|
||||
maxChild := children[0]
|
||||
|
||||
maxChildVotes, err := VoteCount(maxChild, state, voteTargets, c.beaconDB)
|
||||
maxChildVotes, err := VoteCount(maxChild, startState, voteTargets, c.beaconDB)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to determine vote count for block: %v", err)
|
||||
}
|
||||
for i := 0; i < len(children); i++ {
|
||||
candidateChildVotes, err := VoteCount(children[i], state, voteTargets, c.beaconDB)
|
||||
candidateChildVotes, err := VoteCount(children[i], startState, voteTargets, c.beaconDB)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to determine vote count for block: %v", err)
|
||||
}
|
||||
@ -264,7 +269,7 @@ func (c *ChainService) blockChildren(block *pb.BeaconBlock, stateSlot uint64) ([
|
||||
// each attestation target consists of validator index and its attestation target (i.e. the block
|
||||
// which the validator attested to)
|
||||
func (c *ChainService) attestationTargets(state *pb.BeaconState) (map[uint64]*pb.BeaconBlock, error) {
|
||||
indices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, state.FinalizedEpoch)
|
||||
indices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, helpers.CurrentEpoch(state))
|
||||
attestationTargets := make(map[uint64]*pb.BeaconBlock)
|
||||
for i, index := range indices {
|
||||
block, err := c.attsService.LatestAttestationTarget(c.ctx, index)
|
||||
|
@ -529,7 +529,7 @@ func TestLMDGhost_TrivialHeadUpdate(t *testing.T) {
|
||||
voteTargets[0] = block2
|
||||
|
||||
// LMDGhost should pick block 2.
|
||||
head, err := chainService.lmdGhost(block1, state, voteTargets)
|
||||
head, err := chainService.lmdGhost(block1, state, state, voteTargets)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -613,7 +613,7 @@ func TestLMDGhost_3WayChainSplitsSameHeight(t *testing.T) {
|
||||
voteTargets[2] = block4
|
||||
voteTargets[3] = block4
|
||||
// LMDGhost should pick block 4.
|
||||
head, err := chainService.lmdGhost(block1, state, voteTargets)
|
||||
head, err := chainService.lmdGhost(block1, state, state, voteTargets)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -729,7 +729,7 @@ func TestLMDGhost_2WayChainSplitsDiffHeight(t *testing.T) {
|
||||
voteTargets[1] = block5
|
||||
voteTargets[2] = block5
|
||||
// LMDGhost should pick block 5.
|
||||
head, err := chainService.lmdGhost(block1, state, voteTargets)
|
||||
head, err := chainService.lmdGhost(block1, state, state, voteTargets)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -799,7 +799,7 @@ func BenchmarkLMDGhost_8Slots_8Validators(b *testing.B) {
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := chainService.lmdGhost(genesis, state, voteTargets)
|
||||
_, err := chainService.lmdGhost(genesis, state, state, voteTargets)
|
||||
if err != nil {
|
||||
b.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -869,7 +869,7 @@ func BenchmarkLMDGhost_32Slots_8Validators(b *testing.B) {
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := chainService.lmdGhost(genesis, state, voteTargets)
|
||||
_, err := chainService.lmdGhost(genesis, state, state, voteTargets)
|
||||
if err != nil {
|
||||
b.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -937,7 +937,7 @@ func BenchmarkLMDGhost_32Slots_64Validators(b *testing.B) {
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := chainService.lmdGhost(genesis, state, voteTargets)
|
||||
_, err := chainService.lmdGhost(genesis, state, state, voteTargets)
|
||||
if err != nil {
|
||||
b.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
@ -1005,7 +1005,7 @@ func BenchmarkLMDGhost_64Slots_16384Validators(b *testing.B) {
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := chainService.lmdGhost(genesis, state, voteTargets)
|
||||
_, err := chainService.lmdGhost(genesis, state, state, voteTargets)
|
||||
if err != nil {
|
||||
b.Fatalf("Could not run LMD GHOST: %v", err)
|
||||
}
|
||||
|
@ -162,12 +162,12 @@ func TestProcessBlock_OK(t *testing.T) {
|
||||
cfg := &RegularSyncConfig{
|
||||
BlockAnnounceBufferSize: 0,
|
||||
BlockBufferSize: 0,
|
||||
ChainService: &mockChainService{
|
||||
ChainService: &mockChainService{
|
||||
db: db,
|
||||
},
|
||||
P2P: &mockP2P{},
|
||||
BeaconDB: db,
|
||||
OperationService: &mockOperationService{},
|
||||
P2P: &mockP2P{},
|
||||
BeaconDB: db,
|
||||
OperationService: &mockOperationService{},
|
||||
}
|
||||
ss := NewRegularSyncService(context.Background(), cfg)
|
||||
|
||||
@ -237,12 +237,12 @@ func TestProcessBlock_MultipleBlocksProcessedOK(t *testing.T) {
|
||||
cfg := &RegularSyncConfig{
|
||||
BlockAnnounceBufferSize: 0,
|
||||
BlockBufferSize: 0,
|
||||
ChainService: &mockChainService{
|
||||
ChainService: &mockChainService{
|
||||
db: db,
|
||||
},
|
||||
P2P: &mockP2P{},
|
||||
BeaconDB: db,
|
||||
OperationService: &mockOperationService{},
|
||||
P2P: &mockP2P{},
|
||||
BeaconDB: db,
|
||||
OperationService: &mockOperationService{},
|
||||
}
|
||||
ss := NewRegularSyncService(context.Background(), cfg)
|
||||
|
||||
|
@ -72,7 +72,7 @@ func setupTestSyncService(t *testing.T, synced bool) (*Service, *db.BeaconDB) {
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
ChainService: &mockChainService{
|
||||
ChainService: &mockChainService{
|
||||
db: db,
|
||||
},
|
||||
P2P: &mockP2P{},
|
||||
|
@ -114,7 +114,7 @@ func setUpSyncedService(numOfBlocks int, simP2P *simulatedP2P, t *testing.T) (*S
|
||||
bFeed: new(event.Feed),
|
||||
sFeed: new(event.Feed),
|
||||
cFeed: new(event.Feed),
|
||||
db: bd.DB(),
|
||||
db: bd.DB(),
|
||||
}
|
||||
|
||||
cfg := &Config{
|
||||
@ -161,7 +161,7 @@ func setUpUnSyncedService(simP2P *simulatedP2P, stateRoot [32]byte, t *testing.T
|
||||
bFeed: new(event.Feed),
|
||||
sFeed: new(event.Feed),
|
||||
cFeed: new(event.Feed),
|
||||
db: bd.DB(),
|
||||
db: bd.DB(),
|
||||
}
|
||||
|
||||
// we add in 2 blocks to the unsynced node so that, we dont request the beacon state from the
|
||||
|
Loading…
Reference in New Issue
Block a user