2019-08-23 01:00:55 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2020-09-03 18:18:38 +00:00
|
|
|
"context"
|
2019-08-23 01:00:55 +00:00
|
|
|
"testing"
|
2020-01-10 23:51:49 +00:00
|
|
|
"time"
|
2019-08-23 01:00:55 +00:00
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-09-03 20:10:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
2020-06-14 22:24:49 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2021-09-08 10:41:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/transition"
|
2019-08-23 01:00:55 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2021-01-20 10:19:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
2021-01-22 18:08:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
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-01-22 18:08:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-09-03 18:18:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2021-01-20 10:19:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-09-03 18:18:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2021-01-22 18:08:16 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2019-08-23 01:00:55 +00:00
|
|
|
)
|
|
|
|
|
2020-10-26 19:09:19 +00:00
|
|
|
func TestAttestationCheckPtState_FarFutureSlot(t *testing.T) {
|
2020-09-03 18:18:38 +00:00
|
|
|
helpers.ClearCache()
|
2020-12-18 19:12:30 +00:00
|
|
|
beaconDB := testDB.SetupDB(t)
|
2020-09-03 18:18:38 +00:00
|
|
|
|
2020-12-18 19:12:30 +00:00
|
|
|
chainService := setupBeaconChain(t, beaconDB)
|
2020-09-03 18:18:38 +00:00
|
|
|
chainService.genesisTime = time.Now()
|
|
|
|
|
2021-09-03 20:10:31 +00:00
|
|
|
e := types.Epoch(core.MaxSlotBuffer/uint64(params.BeaconConfig().SlotsPerEpoch) + 1)
|
2020-09-03 18:18:38 +00:00
|
|
|
_, err := chainService.AttestationPreState(context.Background(), ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{Epoch: e}}})
|
|
|
|
require.ErrorContains(t, "exceeds max allowed value relative to the local clock", err)
|
|
|
|
}
|
2021-01-20 10:19:43 +00:00
|
|
|
|
|
|
|
func TestVerifyLMDFFGConsistent_NotOK(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2021-01-20 10:19:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b32 := testutil.NewBeaconBlock()
|
|
|
|
b32.Block.Slot = 32
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b32)))
|
2021-01-20 10:19:43 +00:00
|
|
|
r32, err := b32.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b33 := testutil.NewBeaconBlock()
|
|
|
|
b33.Block.Slot = 33
|
|
|
|
b33.Block.ParentRoot = r32[:]
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b33)))
|
2021-01-20 10:19:43 +00:00
|
|
|
r33, err := b33.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wanted := "FFG and LMD votes are not consistent"
|
|
|
|
a := testutil.NewAttestation()
|
|
|
|
a.Data.Target.Epoch = 1
|
|
|
|
a.Data.Target.Root = []byte{'a'}
|
|
|
|
a.Data.BeaconBlockRoot = r33[:]
|
|
|
|
require.ErrorContains(t, wanted, service.VerifyLmdFfgConsistency(context.Background(), a))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyLMDFFGConsistent_OK(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2021-01-20 10:19:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
b32 := testutil.NewBeaconBlock()
|
|
|
|
b32.Block.Slot = 32
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b32)))
|
2021-01-20 10:19:43 +00:00
|
|
|
r32, err := b32.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
b33 := testutil.NewBeaconBlock()
|
|
|
|
b33.Block.Slot = 33
|
|
|
|
b33.Block.ParentRoot = r32[:]
|
2021-07-06 15:34:05 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b33)))
|
2021-01-20 10:19:43 +00:00
|
|
|
r33, err := b33.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a := testutil.NewAttestation()
|
|
|
|
a.Data.Target.Epoch = 1
|
|
|
|
a.Data.Target.Root = r32[:]
|
|
|
|
a.Data.BeaconBlockRoot = r33[:]
|
|
|
|
err = service.VerifyLmdFfgConsistency(context.Background(), a)
|
|
|
|
require.NoError(t, err, "Could not verify LMD and FFG votes to be consistent")
|
|
|
|
}
|
2021-01-22 18:08:16 +00:00
|
|
|
|
|
|
|
func TestProcessAttestations_Ok(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
|
|
|
|
cfg := &Config{
|
|
|
|
BeaconDB: beaconDB,
|
|
|
|
ForkChoiceStore: protoarray.New(0, 0, [32]byte{}),
|
|
|
|
StateGen: stategen.New(beaconDB),
|
|
|
|
AttPool: attestations.NewPool(),
|
|
|
|
}
|
2021-02-12 17:45:22 +00:00
|
|
|
service, err := NewService(ctx, cfg)
|
2021-01-22 18:08:16 +00:00
|
|
|
service.genesisTime = timeutils.Now().Add(-1 * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second)
|
|
|
|
require.NoError(t, err)
|
|
|
|
genesisState, pks := testutil.DeterministicGenesisState(t, 64)
|
|
|
|
require.NoError(t, genesisState.SetGenesisTime(uint64(timeutils.Now().Unix())-params.BeaconConfig().SecondsPerSlot))
|
|
|
|
require.NoError(t, service.saveGenesisData(ctx, genesisState))
|
|
|
|
atts, err := testutil.GenerateAttestations(genesisState, pks, 1, 0, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tRoot := bytesutil.ToBytes32(atts[0].Data.Target.Root)
|
|
|
|
copied := genesisState.Copy()
|
2021-09-08 10:41:47 +00:00
|
|
|
copied, err = transition.ProcessSlots(ctx, copied, 1)
|
2021-01-22 18:08:16 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(ctx, copied, tRoot))
|
|
|
|
require.NoError(t, service.cfg.ForkChoiceStore.ProcessBlock(ctx, 0, tRoot, tRoot, tRoot, 1, 1))
|
|
|
|
require.NoError(t, service.cfg.AttPool.SaveForkchoiceAttestations(atts))
|
2021-01-22 18:08:16 +00:00
|
|
|
service.processAttestations(ctx)
|
2021-03-17 18:36:56 +00:00
|
|
|
require.Equal(t, 0, len(service.cfg.AttPool.ForkchoiceAttestations()))
|
2021-01-22 18:08:16 +00:00
|
|
|
require.LogsDoNotContain(t, hook, "Could not process attestation for fork choice")
|
|
|
|
}
|