mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
4beb352e6f
* Move Slot and Epoch functions from helpers to core * limited viz * goimports * fix fuzz build * fix fuzz build * fix * fix Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
268 lines
9.8 KiB
Go
268 lines
9.8 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
ethpbv1 "github.com/prysmaticlabs/prysm/proto/eth/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/wrapper"
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestSaveHead_Same(t *testing.T) {
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
|
|
r := [32]byte{'A'}
|
|
service.head = &head{slot: 0, root: r}
|
|
|
|
require.NoError(t, service.saveHead(context.Background(), r))
|
|
assert.Equal(t, types.Slot(0), service.headSlot(), "Head did not stay the same")
|
|
assert.Equal(t, r, service.headRoot(), "Head did not stay the same")
|
|
}
|
|
|
|
func TestSaveHead_Different(t *testing.T) {
|
|
ctx := context.Background()
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
|
|
testutil.NewBeaconBlock()
|
|
oldBlock := wrapper.WrappedPhase0SignedBeaconBlock(
|
|
testutil.NewBeaconBlock(),
|
|
)
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), oldBlock))
|
|
oldRoot, err := oldBlock.Block().HashTreeRoot()
|
|
require.NoError(t, err)
|
|
service.head = &head{
|
|
slot: 0,
|
|
root: oldRoot,
|
|
block: oldBlock,
|
|
}
|
|
|
|
newHeadSignedBlock := testutil.NewBeaconBlock()
|
|
newHeadSignedBlock.Block.Slot = 1
|
|
newHeadBlock := newHeadSignedBlock.Block
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(newHeadSignedBlock)))
|
|
newRoot, err := newHeadBlock.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
headState, err := testutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
require.NoError(t, headState.SetSlot(1))
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(context.Background(), ðpb.StateSummary{Slot: 1, Root: newRoot[:]}))
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(context.Background(), headState, newRoot))
|
|
require.NoError(t, service.saveHead(context.Background(), newRoot))
|
|
|
|
assert.Equal(t, types.Slot(1), service.HeadSlot(), "Head did not change")
|
|
|
|
cachedRoot, err := service.HeadRoot(context.Background())
|
|
require.NoError(t, err)
|
|
assert.DeepEqual(t, cachedRoot, newRoot[:], "Head did not change")
|
|
assert.DeepEqual(t, newHeadSignedBlock, service.headBlock().Proto(), "Head did not change")
|
|
assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change")
|
|
}
|
|
|
|
func TestSaveHead_Different_Reorg(t *testing.T) {
|
|
ctx := context.Background()
|
|
hook := logTest.NewGlobal()
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
|
|
oldBlock := wrapper.WrappedPhase0SignedBeaconBlock(
|
|
testutil.NewBeaconBlock(),
|
|
)
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), oldBlock))
|
|
oldRoot, err := oldBlock.Block().HashTreeRoot()
|
|
require.NoError(t, err)
|
|
service.head = &head{
|
|
slot: 0,
|
|
root: oldRoot,
|
|
block: oldBlock,
|
|
}
|
|
|
|
reorgChainParent := [32]byte{'B'}
|
|
newHeadSignedBlock := testutil.NewBeaconBlock()
|
|
newHeadSignedBlock.Block.Slot = 1
|
|
newHeadSignedBlock.Block.ParentRoot = reorgChainParent[:]
|
|
newHeadBlock := newHeadSignedBlock.Block
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(newHeadSignedBlock)))
|
|
newRoot, err := newHeadBlock.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
headState, err := testutil.NewBeaconState()
|
|
require.NoError(t, err)
|
|
require.NoError(t, headState.SetSlot(1))
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(context.Background(), ðpb.StateSummary{Slot: 1, Root: newRoot[:]}))
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(context.Background(), headState, newRoot))
|
|
require.NoError(t, service.saveHead(context.Background(), newRoot))
|
|
|
|
assert.Equal(t, types.Slot(1), service.HeadSlot(), "Head did not change")
|
|
|
|
cachedRoot, err := service.HeadRoot(context.Background())
|
|
require.NoError(t, err)
|
|
if !bytes.Equal(cachedRoot, newRoot[:]) {
|
|
t.Error("Head did not change")
|
|
}
|
|
assert.DeepEqual(t, newHeadSignedBlock, service.headBlock().Proto(), "Head did not change")
|
|
assert.DeepSSZEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change")
|
|
require.LogsContain(t, hook, "Chain reorg occurred")
|
|
}
|
|
|
|
func TestCacheJustifiedStateBalances_CanCache(t *testing.T) {
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
|
|
state, _ := testutil.DeterministicGenesisState(t, 100)
|
|
r := [32]byte{'a'}
|
|
require.NoError(t, service.cfg.BeaconDB.SaveStateSummary(context.Background(), ðpb.StateSummary{Root: r[:]}))
|
|
require.NoError(t, service.cfg.BeaconDB.SaveState(context.Background(), state, r))
|
|
require.NoError(t, service.cacheJustifiedStateBalances(context.Background(), r))
|
|
require.DeepEqual(t, service.getJustifiedBalances(), state.Balances(), "Incorrect justified balances")
|
|
}
|
|
|
|
func TestUpdateHead_MissingJustifiedRoot(t *testing.T) {
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(context.Background(), wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: r[:]}
|
|
service.finalizedCheckpt = ðpb.Checkpoint{}
|
|
service.bestJustifiedCheckpt = ðpb.Checkpoint{}
|
|
|
|
require.NoError(t, service.updateHead(context.Background(), []uint64{}))
|
|
}
|
|
|
|
func Test_notifyNewHeadEvent(t *testing.T) {
|
|
t.Run("genesis_state_root", func(t *testing.T) {
|
|
bState, _ := testutil.DeterministicGenesisState(t, 10)
|
|
notifier := &mock.MockStateNotifier{RecordEvents: true}
|
|
srv := &Service{
|
|
cfg: &Config{
|
|
StateNotifier: notifier,
|
|
},
|
|
genesisRoot: [32]byte{1},
|
|
}
|
|
newHeadStateRoot := [32]byte{2}
|
|
newHeadRoot := [32]byte{3}
|
|
err := srv.notifyNewHeadEvent(1, bState, newHeadStateRoot[:], newHeadRoot[:])
|
|
require.NoError(t, err)
|
|
events := notifier.ReceivedEvents()
|
|
require.Equal(t, 1, len(events))
|
|
|
|
eventHead, ok := events[0].Data.(*ethpbv1.EventHead)
|
|
require.Equal(t, true, ok)
|
|
wanted := ðpbv1.EventHead{
|
|
Slot: 1,
|
|
Block: newHeadRoot[:],
|
|
State: newHeadStateRoot[:],
|
|
EpochTransition: false,
|
|
PreviousDutyDependentRoot: srv.genesisRoot[:],
|
|
CurrentDutyDependentRoot: srv.genesisRoot[:],
|
|
}
|
|
require.DeepSSZEqual(t, wanted, eventHead)
|
|
})
|
|
t.Run("non_genesis_values", func(t *testing.T) {
|
|
bState, _ := testutil.DeterministicGenesisState(t, 10)
|
|
notifier := &mock.MockStateNotifier{RecordEvents: true}
|
|
genesisRoot := [32]byte{1}
|
|
srv := &Service{
|
|
cfg: &Config{
|
|
StateNotifier: notifier,
|
|
},
|
|
genesisRoot: genesisRoot,
|
|
}
|
|
epoch1Start, err := core.StartSlot(1)
|
|
require.NoError(t, err)
|
|
epoch2Start, err := core.StartSlot(1)
|
|
require.NoError(t, err)
|
|
require.NoError(t, bState.SetSlot(epoch1Start))
|
|
|
|
newHeadStateRoot := [32]byte{2}
|
|
newHeadRoot := [32]byte{3}
|
|
err = srv.notifyNewHeadEvent(epoch2Start, bState, newHeadStateRoot[:], newHeadRoot[:])
|
|
require.NoError(t, err)
|
|
events := notifier.ReceivedEvents()
|
|
require.Equal(t, 1, len(events))
|
|
|
|
eventHead, ok := events[0].Data.(*ethpbv1.EventHead)
|
|
require.Equal(t, true, ok)
|
|
wanted := ðpbv1.EventHead{
|
|
Slot: epoch2Start,
|
|
Block: newHeadRoot[:],
|
|
State: newHeadStateRoot[:],
|
|
EpochTransition: false,
|
|
PreviousDutyDependentRoot: genesisRoot[:],
|
|
CurrentDutyDependentRoot: make([]byte, 32),
|
|
}
|
|
require.DeepSSZEqual(t, wanted, eventHead)
|
|
})
|
|
}
|
|
|
|
func TestSaveOrphanedAtts(t *testing.T) {
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
|
|
CorrectlyInsertOrphanedAtts: true,
|
|
})
|
|
defer resetCfg()
|
|
|
|
genesis, keys := testutil.DeterministicGenesisState(t, 64)
|
|
b, err := testutil.GenerateFullBlock(genesis, keys, testutil.DefaultBlockGenConfig(), 1)
|
|
assert.NoError(t, err)
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
ctx := context.Background()
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
service.genesisTime = time.Now()
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
|
require.NoError(t, service.saveOrphanedAtts(ctx, r))
|
|
|
|
require.Equal(t, len(b.Block.Body.Attestations), service.cfg.AttPool.AggregatedAttestationCount())
|
|
savedAtts := service.cfg.AttPool.AggregatedAttestations()
|
|
atts := b.Block.Body.Attestations
|
|
require.DeepSSZEqual(t, atts, savedAtts)
|
|
}
|
|
|
|
func TestSaveOrphanedAtts_CanFilter(t *testing.T) {
|
|
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
|
|
CorrectlyInsertOrphanedAtts: true,
|
|
})
|
|
defer resetCfg()
|
|
|
|
genesis, keys := testutil.DeterministicGenesisState(t, 64)
|
|
b, err := testutil.GenerateFullBlock(genesis, keys, testutil.DefaultBlockGenConfig(), 1)
|
|
assert.NoError(t, err)
|
|
r, err := b.Block.HashTreeRoot()
|
|
require.NoError(t, err)
|
|
|
|
ctx := context.Background()
|
|
beaconDB := testDB.SetupDB(t)
|
|
service := setupBeaconChain(t, beaconDB)
|
|
service.genesisTime = time.Now().Add(time.Duration(-1*int64(params.BeaconConfig().SlotsPerEpoch+1)*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second)
|
|
|
|
require.NoError(t, service.cfg.BeaconDB.SaveBlock(ctx, wrapper.WrappedPhase0SignedBeaconBlock(b)))
|
|
require.NoError(t, service.saveOrphanedAtts(ctx, r))
|
|
|
|
require.Equal(t, 0, service.cfg.AttPool.AggregatedAttestationCount())
|
|
savedAtts := service.cfg.AttPool.AggregatedAttestations()
|
|
atts := b.Block.Body.Attestations
|
|
require.DeepNotSSZEqual(t, atts, savedAtts)
|
|
}
|