2020-09-26 08:04:07 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-03 15:07:46 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/v4/beacon-chain/db/testing"
|
|
|
|
doublylinkedtree "github.com/prysmaticlabs/prysm/v4/beacon-chain/forkchoice/doubly-linked-tree"
|
|
|
|
forkchoicetypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/forkchoice/types"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/util"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
2020-09-26 08:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_VerifyWeakSubjectivityRoot(t *testing.T) {
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-09-26 08:04:07 +00:00
|
|
|
|
2021-09-23 18:53:46 +00:00
|
|
|
b := util.NewBeaconBlock()
|
2022-03-28 21:01:55 +00:00
|
|
|
b.Block.Slot = 1792480
|
2022-06-24 17:22:39 +00:00
|
|
|
util.SaveBlock(t, context.Background(), beaconDB, b)
|
2020-09-26 08:04:07 +00:00
|
|
|
r, err := b.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
2022-03-28 21:01:55 +00:00
|
|
|
|
|
|
|
blockEpoch := slots.ToEpoch(b.Block.Slot)
|
2020-09-26 08:04:07 +00:00
|
|
|
tests := []struct {
|
|
|
|
wsVerified bool
|
2022-03-28 21:01:55 +00:00
|
|
|
disabled bool
|
2021-11-03 15:07:46 +00:00
|
|
|
wantErr error
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt *ethpb.Checkpoint
|
2023-01-26 14:40:12 +00:00
|
|
|
finalizedEpoch primitives.Epoch
|
2020-09-26 08:04:07 +00:00
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2022-03-28 21:01:55 +00:00
|
|
|
name: "nil root and epoch",
|
|
|
|
disabled: true,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not yet to verify, ws epoch higher than finalized epoch",
|
2022-03-28 21:01:55 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'a'}, 32), Epoch: blockEpoch},
|
|
|
|
finalizedEpoch: blockEpoch - 1,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "can't find the block in DB",
|
2021-12-14 18:42:05 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: bytesutil.PadTo([]byte{'a'}, fieldparams.RootLength), Epoch: 1},
|
2022-03-28 21:01:55 +00:00
|
|
|
finalizedEpoch: blockEpoch + 1,
|
2021-11-03 15:07:46 +00:00
|
|
|
wantErr: errWSBlockNotFound,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "can't find the block corresponds to ws epoch in DB",
|
2022-03-28 21:01:55 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: r[:], Epoch: blockEpoch - 2}, // Root belongs in epoch 1.
|
|
|
|
finalizedEpoch: blockEpoch - 1,
|
2021-11-03 15:07:46 +00:00
|
|
|
wantErr: errWSBlockNotFoundInEpoch,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "can verify and pass",
|
2022-03-28 21:01:55 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: r[:], Epoch: blockEpoch},
|
|
|
|
finalizedEpoch: blockEpoch + 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "equal epoch",
|
|
|
|
checkpt: ðpb.Checkpoint{Root: r[:], Epoch: blockEpoch},
|
|
|
|
finalizedEpoch: blockEpoch,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-11-03 15:07:46 +00:00
|
|
|
wv, err := NewWeakSubjectivityVerifier(tt.checkpt, beaconDB)
|
2022-03-28 21:01:55 +00:00
|
|
|
require.Equal(t, !tt.disabled, wv.enabled)
|
2021-11-03 15:07:46 +00:00
|
|
|
require.NoError(t, err)
|
2022-09-26 14:45:21 +00:00
|
|
|
fcs := doublylinkedtree.New()
|
2020-09-26 08:04:07 +00:00
|
|
|
s := &Service{
|
2022-06-25 03:57:52 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB, WeakSubjectivityCheckpt: tt.checkpt, ForkChoiceStore: fcs},
|
2022-01-25 18:47:39 +00:00
|
|
|
wsVerifier: wv,
|
2020-09-26 08:04:07 +00:00
|
|
|
}
|
2022-06-25 03:57:52 +00:00
|
|
|
require.NoError(t, fcs.UpdateFinalizedCheckpoint(&forkchoicetypes.Checkpoint{Epoch: tt.finalizedEpoch}))
|
2023-03-22 01:12:54 +00:00
|
|
|
cp := s.cfg.ForkChoiceStore.FinalizedCheckpoint()
|
2022-05-20 18:41:33 +00:00
|
|
|
err = s.wsVerifier.VerifyWeakSubjectivity(context.Background(), cp.Epoch)
|
2021-11-03 15:07:46 +00:00
|
|
|
if tt.wantErr == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, true, errors.Is(err, tt.wantErr))
|
2020-09-26 08:04:07 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|