Resume new state mgmt (#5102)

This commit is contained in:
terence tsao 2020-03-15 09:47:49 -07:00 committed by GitHub
parent 8e6c16d416
commit 3660732f44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 2 deletions

View File

@ -1,11 +1,16 @@
package stategen
import (
"context"
"sync"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"go.opencensus.io/trace"
)
// State represents a management object that handles the internal
@ -37,6 +42,31 @@ func New(db db.NoHeadAccessDatabase) *State {
}
}
// Resume resumes a new state management object from previously saved finalized check point in DB.
func (s *State) Resume(ctx context.Context, finalizedRoot [32]byte) (*state.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "stateGen.Resume")
defer span.End()
finalizedState, err := s.beaconDB.State(ctx, finalizedRoot)
if err != nil {
return nil, err
}
s.splitInfo = &splitSlotAndRoot{slot: finalizedState.Slot(), root: finalizedRoot}
if err := s.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{Slot: finalizedState.Slot(), Root: finalizedRoot[:], BoundaryRoot: finalizedRoot[:]}); err != nil {
return nil, err
}
// In case the finalized state slot was skipped.
slot := finalizedState.Slot()
if !helpers.IsEpochStart(slot) {
slot = helpers.StartSlot(helpers.SlotToEpoch(slot) + 1)
}
s.setEpochBoundaryRoot(slot, finalizedRoot)
return finalizedState, nil
}
// This verifies the archive point frequency is valid. It checks the interval
// is a divisor of the number of slots per epoch. This ensures we have at least one
// archive point within range of our state root history when iterating

View File

@ -1,12 +1,44 @@
package stategen
import (
"context"
"testing"
"github.com/gogo/protobuf/proto"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func Test_verifySlotsPerArchivePoint(t *testing.T) {
func TestResume(t *testing.T) {
ctx := context.Background()
db := testDB.SetupDB(t)
defer testDB.TeardownDB(t, db)
service := New(db)
root := [32]byte{'A'}
beaconState, _ := testutil.DeterministicGenesisState(t, 32)
beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch - 2)
service.beaconDB.SaveState(ctx, beaconState, root)
resumeState, err := service.Resume(ctx, root)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(beaconState.InnerStateUnsafe(), resumeState.InnerStateUnsafe()) {
t.Error("Diff saved state")
}
if !service.beaconDB.HasStateSummary(ctx, root) {
t.Error("Did not save state summary")
}
if cachedRoot, _ := service.epochBoundaryRoot(params.BeaconConfig().SlotsPerEpoch); cachedRoot != root {
t.Error("Did not save boundary root")
}
}
func TestVerifySlotsPerArchivePoint(t *testing.T) {
type tc struct {
input uint64
result bool

View File

@ -74,7 +74,7 @@ func newBlocksFetcher(ctx context.Context, cfg *blocksFetcherConfig) *blocksFetc
rateLimiter := leakybucket.NewCollector(
allowedBlocksPerSecond, /* rate */
allowedBlocksPerSecond, /* capacity */
false /* deleteEmptyBuckets */)
false /* deleteEmptyBuckets */)
return &blocksFetcher{
ctx: ctx,