-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database methods for optimistic sync (#10357)
* Add database methods for optimistic sync * Add epoch comparison * add extra epoch comparison * Summary instead of block * fix tests Co-authored-by: terence tsao <[email protected]>
- Loading branch information
1 parent
2744eba
commit 807b712
Showing
17 changed files
with
254 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kv | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prysmaticlabs/prysm/encoding/bytesutil" | ||
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" | ||
bolt "go.etcd.io/bbolt" | ||
"go.opencensus.io/trace" | ||
) | ||
|
||
// LastValidatedCheckpoint returns the latest fully validated checkpoint in beacon chain. | ||
func (s *Store) LastValidatedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) { | ||
ctx, span := trace.StartSpan(ctx, "BeaconDB.LastValidatedCheckpoint") | ||
defer span.End() | ||
var checkpoint *ethpb.Checkpoint | ||
err := s.db.View(func(tx *bolt.Tx) error { | ||
bkt := tx.Bucket(checkpointBucket) | ||
enc := bkt.Get(lastValidatedCheckpointKey) | ||
if enc == nil { | ||
var finErr error | ||
checkpoint, finErr = s.FinalizedCheckpoint(ctx) | ||
return finErr | ||
} | ||
checkpoint = ðpb.Checkpoint{} | ||
return decode(ctx, enc, checkpoint) | ||
}) | ||
return checkpoint, err | ||
} | ||
|
||
// SaveLastValidatedCheckpoint saves the last validated checkpoint in beacon chain. | ||
func (s *Store) SaveLastValidatedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error { | ||
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveLastValidatedCheckpoint") | ||
defer span.End() | ||
|
||
enc, err := encode(ctx, checkpoint) | ||
if err != nil { | ||
return err | ||
} | ||
return s.db.Update(func(tx *bolt.Tx) error { | ||
bucket := tx.Bucket(checkpointBucket) | ||
hasStateSummary := s.hasStateSummaryBytes(tx, bytesutil.ToBytes32(checkpoint.Root)) | ||
hasStateInDB := tx.Bucket(stateBucket).Get(checkpoint.Root) != nil | ||
if !(hasStateInDB || hasStateSummary) { | ||
return errMissingStateForCheckpoint | ||
} | ||
return bucket.Put(lastValidatedCheckpointKey, enc) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package kv | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/encoding/bytesutil" | ||
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper" | ||
"github.com/prysmaticlabs/prysm/testing/assert" | ||
"github.com/prysmaticlabs/prysm/testing/require" | ||
"github.com/prysmaticlabs/prysm/testing/util" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
func TestStore_LastValidatedCheckpoint_CanSaveRetrieve(t *testing.T) { | ||
db := setupDB(t) | ||
ctx := context.Background() | ||
root := bytesutil.ToBytes32([]byte{'A'}) | ||
cp := ðpb.Checkpoint{ | ||
Epoch: 10, | ||
Root: root[:], | ||
} | ||
st, err := util.NewBeaconState() | ||
require.NoError(t, err) | ||
require.NoError(t, st.SetSlot(1)) | ||
require.NoError(t, db.SaveState(ctx, st, root)) | ||
require.NoError(t, db.SaveLastValidatedCheckpoint(ctx, cp)) | ||
|
||
retrieved, err := db.LastValidatedCheckpoint(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved) | ||
} | ||
|
||
func TestStore_LastValidatedCheckpoint_DefaultIsFinalized(t *testing.T) { | ||
db := setupDB(t) | ||
ctx := context.Background() | ||
|
||
genesis := bytesutil.ToBytes32([]byte{'G', 'E', 'N', 'E', 'S', 'I', 'S'}) | ||
require.NoError(t, db.SaveGenesisBlockRoot(ctx, genesis)) | ||
|
||
blk := util.NewBeaconBlock() | ||
blk.Block.ParentRoot = genesis[:] | ||
blk.Block.Slot = 40 | ||
|
||
root, err := blk.Block.HashTreeRoot() | ||
require.NoError(t, err) | ||
|
||
cp := ðpb.Checkpoint{ | ||
Epoch: 5, | ||
Root: root[:], | ||
} | ||
|
||
// a valid chain is required to save finalized checkpoint. | ||
require.NoError(t, db.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(blk))) | ||
st, err := util.NewBeaconState() | ||
require.NoError(t, err) | ||
require.NoError(t, st.SetSlot(1)) | ||
// a state is required to save checkpoint | ||
require.NoError(t, db.SaveState(ctx, st, root)) | ||
|
||
require.NoError(t, db.SaveFinalizedCheckpoint(ctx, cp)) | ||
|
||
retrieved, err := db.LastValidatedCheckpoint(ctx) | ||
require.NoError(t, err) | ||
assert.Equal(t, true, proto.Equal(cp, retrieved), "Wanted %v, received %v", cp, retrieved) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.