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"
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-09-26 08:04:07 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2021-12-14 18:42:05 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
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()
|
2020-09-26 08:04:07 +00:00
|
|
|
b.Block.Slot = 32
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, beaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
2020-09-26 08:04:07 +00:00
|
|
|
r, err := b.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
|
|
wsVerified bool
|
2021-11-03 15:07:46 +00:00
|
|
|
wantErr error
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt *ethpb.Checkpoint
|
2021-02-09 10:05:22 +00:00
|
|
|
finalizedEpoch types.Epoch
|
2020-09-26 08:04:07 +00:00
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{
|
2021-11-03 15:07:46 +00:00
|
|
|
name: "nil root and epoch",
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "already verified",
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Epoch: 2},
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedEpoch: 2,
|
|
|
|
wsVerified: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not yet to verify, ws epoch higher than finalized epoch",
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Epoch: 2},
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedEpoch: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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},
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedEpoch: 3,
|
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",
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: r[:], Epoch: 2}, // Root belongs in epoch 1.
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedEpoch: 3,
|
2021-11-03 15:07:46 +00:00
|
|
|
wantErr: errWSBlockNotFoundInEpoch,
|
2020-09-26 08:04:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "can verify and pass",
|
2021-04-06 14:32:49 +00:00
|
|
|
checkpt: ðpb.Checkpoint{Root: r[:], Epoch: 1},
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedEpoch: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
require.NoError(t, err)
|
2020-09-26 08:04:07 +00:00
|
|
|
s := &Service{
|
2021-10-18 17:48:05 +00:00
|
|
|
cfg: &config{BeaconDB: beaconDB, WeakSubjectivityCheckpt: tt.checkpt},
|
2020-09-26 08:04:07 +00:00
|
|
|
finalizedCheckpt: ðpb.Checkpoint{Epoch: tt.finalizedEpoch},
|
2021-11-03 15:07:46 +00:00
|
|
|
wsVerifier: wv,
|
2020-09-26 08:04:07 +00:00
|
|
|
}
|
2021-11-03 15:07:46 +00:00
|
|
|
err = s.wsVerifier.VerifyWeakSubjectivity(context.Background(), s.finalizedCheckpt.Epoch)
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|