mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
committed by
Yutaro Mori
parent
f0abbf6f26
commit
d30d81a608
@@ -21,7 +21,7 @@ type Service struct {
|
||||
cancel context.CancelFunc
|
||||
rpcClient types.RPCClient
|
||||
validatorIndex int
|
||||
assignedHeight uint64
|
||||
assignedSlot uint64
|
||||
responsibility string
|
||||
attesterChan chan bool
|
||||
proposerChan chan bool
|
||||
@@ -96,13 +96,13 @@ func (s *Service) fetchBeaconBlocks(client pb.BeaconServiceClient) {
|
||||
}
|
||||
log.WithField("slotNumber", block.GetSlotNumber()).Info("Latest beacon block slot number")
|
||||
|
||||
// Based on the height determined from the latest crystallized state, check if
|
||||
// it matches the latest received beacon height.
|
||||
// Based on the slot determined from the latest crystallized state, check if
|
||||
// it matches the latest received beacon slot.
|
||||
if s.responsibility == "proposer" {
|
||||
log.WithField("slotNumber", block.GetSlotNumber()).Info("Assigned proposal slot number reached")
|
||||
s.responsibility = ""
|
||||
s.proposerChan <- true
|
||||
} else if s.responsibility == "attester" && block.GetSlotNumber() == s.assignedHeight {
|
||||
} else if s.responsibility == "attester" && block.GetSlotNumber() == s.assignedSlot {
|
||||
// TODO: Let the validator know a few slots in advance if its attestation slot is coming up
|
||||
log.Info("Assigned attestation slot number reached")
|
||||
s.responsibility = ""
|
||||
@@ -185,27 +185,27 @@ func (s *Service) fetchCrystallizedState(client pb.BeaconServiceClient) {
|
||||
// If the condition above did not pass, the validator is an attester.
|
||||
s.responsibility = "attester"
|
||||
|
||||
// Based on the cutoff and assigned heights, determine the beacon block
|
||||
// height at which attester has to perform its responsibility.
|
||||
currentAssignedHeights := res.GetAssignedAttestationHeights()
|
||||
// Based on the cutoff and assigned slots, determine the beacon block
|
||||
// slot at which attester has to perform its responsibility.
|
||||
currentAssignedSlots := res.GetAssignedAttestationSlots()
|
||||
currentCutoffs := res.GetCutoffIndices()
|
||||
|
||||
// The algorithm functions as follows:
|
||||
// Given a list of heights: [0 19 38 57 12 31 50] and
|
||||
// Given a list of slots: [0 19 38 57 12 31 50] and
|
||||
// A list of cutoff indices: [0 142 285 428 571 714 857 1000]
|
||||
// if the validator index is between 0-142, it can attest at height 0, if it is
|
||||
// between 142-285, that validator can attest at height 19, etc.
|
||||
heightIndex := 0
|
||||
// if the validator index is between 0-142, it can attest at slot 0, if it is
|
||||
// between 142-285, that validator can attest at slot 19, etc.
|
||||
slotIndex := 0
|
||||
for i := 0; i < len(currentCutoffs)-1; i++ {
|
||||
lowCutoff := currentCutoffs[i]
|
||||
highCutoff := currentCutoffs[i+1]
|
||||
if (uint64(s.validatorIndex) >= lowCutoff) && (uint64(s.validatorIndex) <= highCutoff) {
|
||||
break
|
||||
}
|
||||
heightIndex++
|
||||
slotIndex++
|
||||
}
|
||||
s.assignedHeight = currentAssignedHeights[heightIndex]
|
||||
log.Debug("Validator selected as attester at slot number: %d", s.assignedHeight)
|
||||
s.assignedSlot = currentAssignedSlots[slotIndex]
|
||||
log.Debug("Validator selected as attester at slot number: %d", s.assignedSlot)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,11 +93,11 @@ func TestFetchBeaconBlocks(t *testing.T) {
|
||||
// Create mock for the stream returned by LatestBeaconBlock.
|
||||
stream := internal.NewMockBeaconService_LatestBeaconBlockClient(ctrl)
|
||||
|
||||
// If the block's slot number from the stream matches the assigned attestation height,
|
||||
// If the block's slot number from the stream matches the assigned attestation slot,
|
||||
// trigger a log.
|
||||
stream.EXPECT().Recv().Return(&pbp2p.BeaconBlock{SlotNumber: 10}, nil)
|
||||
stream.EXPECT().Recv().Return(&pbp2p.BeaconBlock{}, io.EOF)
|
||||
b.assignedHeight = 10
|
||||
b.assignedSlot = 10
|
||||
b.responsibility = "attester"
|
||||
|
||||
mockServiceClient := internal.NewMockBeaconServiceClient(ctrl)
|
||||
@@ -243,7 +243,7 @@ func TestFetchCrystallizedState(t *testing.T) {
|
||||
|
||||
testutil.AssertLogsContain(t, hook, "Could not fetch shuffled validator indices: something went wrong")
|
||||
|
||||
// Height should be assigned based on the result of ShuffleValidators.
|
||||
// Slot should be assigned based on the result of ShuffleValidators.
|
||||
validator1 := &pbp2p.ValidatorRecord{WithdrawalAddress: []byte("0x0"), StartDynasty: 1, EndDynasty: 10}
|
||||
validator2 := &pbp2p.ValidatorRecord{WithdrawalAddress: []byte("0x1"), StartDynasty: 1, EndDynasty: 10}
|
||||
validator3 := &pbp2p.ValidatorRecord{WithdrawalAddress: []byte{}, StartDynasty: 1, EndDynasty: 10}
|
||||
@@ -260,9 +260,9 @@ func TestFetchCrystallizedState(t *testing.T) {
|
||||
gomock.Any(),
|
||||
gomock.Any(),
|
||||
).Return(&pb.ShuffleResponse{
|
||||
AssignedAttestationHeights: []uint64{0, 1, 2},
|
||||
CutoffIndices: []uint64{0, 1, 2},
|
||||
ShuffledValidatorIndices: []uint64{2, 1, 0},
|
||||
AssignedAttestationSlots: []uint64{0, 1, 2},
|
||||
CutoffIndices: []uint64{0, 1, 2},
|
||||
ShuffledValidatorIndices: []uint64{2, 1, 0},
|
||||
}, nil)
|
||||
|
||||
b.fetchCrystallizedState(mockServiceClient)
|
||||
|
||||
Reference in New Issue
Block a user