-
Notifications
You must be signed in to change notification settings - Fork 969
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add, use and test
VerifyWeakSubjectivityRoot
(#7344)
* Add, use and test `VerifyWeakSubjectivityRoot` * Comments * Make bazel test happy * Merge branch 'master' into verify-ws * Add flag to help * Merge branch 'verify-ws' of github.com:prysmaticlabs/prysm into verify-ws * Merge refs/heads/master into verify-ws * Merge refs/heads/master into verify-ws * Merge refs/heads/master into verify-ws * Merge branch 'master' of github.com:prysmaticlabs/prysm into verify-ws * Update beacon-chain/blockchain/service.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/blockchain/weak_subjectivity_checks.go Co-authored-by: Raul Jordan <raul@prysmaticlabs.com> * Merge branch 'verify-ws' of github.com:prysmaticlabs/prysm into verify-ws * s/&&/|| for VerifyWeakSubjectivityRoot circuit breaker * Merge refs/heads/master into verify-ws * Merge refs/heads/master into verify-ws
- Loading branch information
1 parent
282f3ee
commit 68d0c09
Showing
9 changed files
with
175 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters" | ||
"github.com/prysmaticlabs/prysm/shared/bytesutil" | ||
"github.com/prysmaticlabs/prysm/shared/params" | ||
) | ||
|
||
// VerifyWeakSubjectivityRoot verifies the weak subjectivity root in the service struct. | ||
// Reference design: https://github.com/ethereum/eth2.0-specs/blob/master/specs/phase0/weak-subjectivity.md#weak-subjectivity-sync-procedure | ||
func (s *Service) VerifyWeakSubjectivityRoot(ctx context.Context) error { | ||
// TODO(7342): Remove the following to fully use weak subjectivity in production. | ||
if len(s.wsRoot) == 0 || s.wsEpoch == 0 { | ||
return nil | ||
} | ||
|
||
// Do nothing if the weak subjectivity has previously been verified, | ||
// or weak subjectivity epoch is higher than last finalized epoch. | ||
if s.wsVerified { | ||
return nil | ||
} | ||
if s.wsEpoch > s.finalizedCheckpt.Epoch { | ||
return nil | ||
} | ||
|
||
r := bytesutil.ToBytes32(s.wsRoot) | ||
log.Infof("Performing weak subjectivity check for root %#x in epoch %d", r, s.wsEpoch) | ||
// Save initial sync cached blocks to DB. | ||
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { | ||
return err | ||
} | ||
// A node should have the weak subjectivity block in the DB. | ||
if !s.beaconDB.HasBlock(ctx, r) { | ||
return fmt.Errorf("node does not have root in DB: %#x", r) | ||
} | ||
|
||
startSlot, err := helpers.StartSlot(s.wsEpoch) | ||
if err != nil { | ||
return err | ||
} | ||
// A node should have the weak subjectivity block corresponds to the correct epoch in the DB. | ||
filter := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(startSlot + params.BeaconConfig().SlotsPerEpoch) | ||
roots, err := s.beaconDB.BlockRoots(ctx, filter) | ||
if err != nil { | ||
return err | ||
} | ||
for _, root := range roots { | ||
if r == root { | ||
log.Info("Weak subjectivity check has passed") | ||
s.wsVerified = true | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("node does not have root in db corresponding to epoch: %#x %d", r, s.wsEpoch) | ||
} |
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,85 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" | ||
"github.com/prysmaticlabs/prysm/shared/testutil" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/require" | ||
) | ||
|
||
func TestService_VerifyWeakSubjectivityRoot(t *testing.T) { | ||
db, _ := testDB.SetupDB(t) | ||
|
||
b := testutil.NewBeaconBlock() | ||
b.Block.Slot = 32 | ||
require.NoError(t, db.SaveBlock(context.Background(), b)) | ||
r, err := b.Block.HashTreeRoot() | ||
require.NoError(t, err) | ||
tests := []struct { | ||
wsVerified bool | ||
wantErr bool | ||
wsRoot [32]byte | ||
wsEpoch uint64 | ||
finalizedEpoch uint64 | ||
errString string | ||
name string | ||
}{ | ||
{ | ||
name: "nil root and epoch", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "already verified", | ||
wsEpoch: 2, | ||
finalizedEpoch: 2, | ||
wsVerified: true, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "not yet to verify, ws epoch higher than finalized epoch", | ||
wsEpoch: 2, | ||
finalizedEpoch: 1, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "can't find the block in DB", | ||
wsEpoch: 1, | ||
wsRoot: [32]byte{'a'}, | ||
finalizedEpoch: 3, | ||
wantErr: true, | ||
errString: "node does not have root in DB", | ||
}, | ||
{ | ||
name: "can't find the block corresponds to ws epoch in DB", | ||
wsEpoch: 2, | ||
wsRoot: r, // Root belongs in epoch 1. | ||
finalizedEpoch: 3, | ||
wantErr: true, | ||
errString: "node does not have root in db corresponding to epoch", | ||
}, | ||
{ | ||
name: "can verify and pass", | ||
wsEpoch: 1, | ||
wsRoot: r, | ||
finalizedEpoch: 3, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &Service{ | ||
beaconDB: db, | ||
wsRoot: tt.wsRoot[:], | ||
wsEpoch: tt.wsEpoch, | ||
wsVerified: tt.wsVerified, | ||
finalizedCheckpt: ðpb.Checkpoint{Epoch: tt.finalizedEpoch}, | ||
} | ||
if err := s.VerifyWeakSubjectivityRoot(context.Background()); (err != nil) != tt.wantErr { | ||
require.ErrorContains(t, tt.errString, err) | ||
} | ||
}) | ||
} | ||
} |
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