Add LatestWeakSubjectivityEpoch() method (#8707)

* Add IsWithinWeakSubjectivityPeriod helper method

* Add LatestWeakSubjectivityEpoch method

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Victor Farazdagi 2021-04-06 09:08:44 +03:00 committed by GitHub
parent 7998348bcb
commit 91df0112c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -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
}

View File

@ -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")
}

View File

@ -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(&ethpb.Checkpoint{
Epoch: finalizedEpoch - 1,
Root: bytesutil.PadTo([]byte{'A'}, 32),
}))
require.NoError(t, beaconState.SetFinalizedCheckpoint(&ethpb.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)