prysm-pulse/beacon-chain/blockchain/receive_attestation_test.go

124 lines
4.9 KiB
Go
Raw Normal View History

Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
package blockchain
import (
"context"
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
"testing"
"time"
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/core"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/transition"
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/shared/timeutils"
logTest "github.com/sirupsen/logrus/hooks/test"
Part 6 of update fork choice - implement new ReceiveAttestation (#3246) * Implemented new fork choice service and helpers * Added rest of the tests * Lint * Add back helpers test * Reformatted to doc, helpers and metrics.go * include new getter for block * create block filters from indices * give every block index a unique bucket * construct block indices by bucket mmap * almost done save for the block filters * include block filters, need a few more small touches for fetching the proper indices by bucket * full functionality to filter by parent root * tests pass when using the same logic as attestations * todo * proper todo formatting * first minimum slot range filter * slot range filters pass * more filter criteria passing * tests passing * add todos * all block tests pass and work * rem fmt * range retrieval test * fixed test conditions * instantiate the other buckets * simplify bucket lookups * deprecate non map code * revamp to remove old index prefixes * create indices from data * create indices from data * fetch block roots by slot range * better abstractions * simpler abstractions * roots rename * comment * preston feedback * Fixed existing tests * allow blocks without parent root * Cleaned up a few things * Removed todo * Lint * Cleaned up a few things * A few functions don't need to be exported * Gaz * Fixed visibility * Review feedback * Review feedback part1 * Raul's feedback, refactored OnBlock and OnAttestation to its own file * Fixed grammar * Lint * Implemented ReceiveAttestation * Use time.Time * Implemented ReceiveAttestation * All tests pass * Lint * Oooops * Typo
2019-08-23 01:00:55 +00:00
)
func TestAttestationCheckPtState_FarFutureSlot(t *testing.T) {
helpers.ClearCache()
beaconDB := testDB.SetupDB(t)
chainService := setupBeaconChain(t, beaconDB)
chainService.genesisTime = time.Now()
e := types.Epoch(core.MaxSlotBuffer/uint64(params.BeaconConfig().SlotsPerEpoch) + 1)
_, err := chainService.AttestationPreState(context.Background(), &ethpb.Attestation{Data: &ethpb.AttestationData{Target: &ethpb.Checkpoint{Epoch: e}}})
require.ErrorContains(t, "exceeds max allowed value relative to the local clock", err)
}
func TestVerifyLMDFFGConsistent_NotOK(t *testing.T) {
ctx := context.Background()
beaconDB := testDB.SetupDB(t)
cfg := &Config{BeaconDB: beaconDB, ForkChoiceStore: protoarray.New(0, 0, [32]byte{})}
service, err := NewService(ctx, cfg)
require.NoError(t, err)
b32 := testutil.NewBeaconBlock()
b32.Block.Slot = 32
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b32)))
r32, err := b32.Block.HashTreeRoot()
require.NoError(t, err)
b33 := testutil.NewBeaconBlock()
b33.Block.Slot = 33
b33.Block.ParentRoot = r32[:]
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b33)))
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{})}
service, err := NewService(ctx, cfg)
require.NoError(t, err)
b32 := testutil.NewBeaconBlock()
b32.Block.Slot = 32
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b32)))
r32, err := b32.Block.HashTreeRoot()
require.NoError(t, err)
b33 := testutil.NewBeaconBlock()
b33.Block.Slot = 33
b33.Block.ParentRoot = r32[:]
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b33)))
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")
}
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(),
}
service, err := NewService(ctx, cfg)
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()
copied, err = transition.ProcessSlots(ctx, copied, 1)
require.NoError(t, err)
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))
service.processAttestations(ctx)
require.Equal(t, 0, len(service.cfg.AttPool.ForkchoiceAttestations()))
require.LogsDoNotContain(t, hook, "Could not process attestation for fork choice")
}