mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-11 12:10:05 +00:00
Add LatestWeakSubjectivityEpoch() method (#8707)
* Add IsWithinWeakSubjectivityPeriod helper method * Add LatestWeakSubjectivityEpoch method Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
parent
7998348bcb
commit
91df0112c7
@ -141,3 +141,18 @@ func IsWithinWeakSubjectivityPeriod(
|
||||
|
||||
return currentEpoch <= wsStateEpoch+wsPeriod, nil
|
||||
}
|
||||
|
||||
// LatestWeakSubjectivityEpoch returns epoch of the most recent weak subjectivity checkpoint known to a node.
|
||||
//
|
||||
// Within the weak subjectivity period, if two conflicting blocks are finalized, 1/3 - D (D := safety decay)
|
||||
// of validators will get slashed. Therefore, it is safe to assume that any finalized checkpoint within that
|
||||
// period is protected by this safety margin.
|
||||
func LatestWeakSubjectivityEpoch(st iface.ReadOnlyBeaconState) (types.Epoch, error) {
|
||||
wsPeriod, err := ComputeWeakSubjectivityPeriod(st)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
finalizedEpoch := st.FinalizedCheckpointEpoch()
|
||||
return finalizedEpoch - (finalizedEpoch % wsPeriod), nil
|
||||
}
|
||||
|
@ -368,7 +368,7 @@ func (bs *Server) GetWeakSubjectivityCheckpoint(ctx context.Context, _ *ptypes.E
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "Could not get head state")
|
||||
}
|
||||
wsEpoch, err := helpers.ComputeWeakSubjectivityPeriod(hs)
|
||||
wsEpoch, err := helpers.LatestWeakSubjectivityEpoch(hs)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "Could not get weak subjectivity epoch")
|
||||
}
|
||||
|
@ -711,6 +711,18 @@ func TestServer_GetWeakSubjectivityCheckpoint(t *testing.T) {
|
||||
require.NoError(t, db.SaveState(ctx, beaconState, genesisBlockRoot))
|
||||
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesisBlockRoot))
|
||||
|
||||
// Finalized checkpoint.
|
||||
finalizedEpoch := types.Epoch(1020)
|
||||
require.NoError(t, beaconState.SetSlot(types.Slot(finalizedEpoch.Mul(uint64(params.BeaconConfig().SlotsPerEpoch)))))
|
||||
require.NoError(t, beaconState.SetCurrentJustifiedCheckpoint(ðpb.Checkpoint{
|
||||
Epoch: finalizedEpoch - 1,
|
||||
Root: bytesutil.PadTo([]byte{'A'}, 32),
|
||||
}))
|
||||
require.NoError(t, beaconState.SetFinalizedCheckpoint(ðpb.Checkpoint{
|
||||
Epoch: finalizedEpoch,
|
||||
Root: bytesutil.PadTo([]byte{'B'}, 32),
|
||||
}))
|
||||
|
||||
chainService := &chainMock.ChainService{State: beaconState}
|
||||
server := &Server{
|
||||
Ctx: ctx,
|
||||
@ -720,9 +732,12 @@ func TestServer_GetWeakSubjectivityCheckpoint(t *testing.T) {
|
||||
StateGen: stategen.New(db),
|
||||
}
|
||||
|
||||
wsEpoch, err := helpers.ComputeWeakSubjectivityPeriod(beaconState)
|
||||
require.NoError(t, err)
|
||||
|
||||
c, err := server.GetWeakSubjectivityCheckpoint(ctx, &ptypes.Empty{})
|
||||
require.NoError(t, err)
|
||||
e := types.Epoch(257)
|
||||
e := finalizedEpoch - (finalizedEpoch % wsEpoch)
|
||||
require.Equal(t, e, c.Epoch)
|
||||
wsState, err := server.StateGen.StateBySlot(ctx, params.BeaconConfig().SlotsPerEpoch.Mul(uint64(e)))
|
||||
require.NoError(t, err)
|
||||
|
Loading…
Reference in New Issue
Block a user