prysm-pulse/beacon-chain/forkchoice/doubly-linked-tree/on_tick_test.go
terence 5a66807989
Update to V5 (#13622)
* First take at updating everything to v5

* Patch gRPC gateway to use prysm v5

Fix patch

* Update go ssz

---------

Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
2024-02-15 05:46:47 +00:00

96 lines
3.5 KiB
Go

package doublylinkedtree
import (
"context"
"testing"
forkchoicetypes "github.com/prysmaticlabs/prysm/v5/beacon-chain/forkchoice/types"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/testing/require"
)
func TestStore_NewSlot(t *testing.T) {
ctx := context.Background()
bj := [32]byte{'z'}
type args struct {
slot primitives.Slot
finalized *forkchoicetypes.Checkpoint
justified *forkchoicetypes.Checkpoint
bestJustified *forkchoicetypes.Checkpoint
shouldEqual bool
}
tests := []struct {
name string
args args
}{
{
name: "Not epoch boundary. No change",
args: args{
slot: params.BeaconConfig().SlotsPerEpoch + 1,
finalized: &forkchoicetypes.Checkpoint{Epoch: 1, Root: [32]byte{'a'}},
justified: &forkchoicetypes.Checkpoint{Epoch: 2, Root: [32]byte{'b'}},
bestJustified: &forkchoicetypes.Checkpoint{Epoch: 3, Root: bj},
shouldEqual: false,
},
},
{
name: "Justified higher than best justified. No change",
args: args{
slot: params.BeaconConfig().SlotsPerEpoch,
finalized: &forkchoicetypes.Checkpoint{Epoch: 1, Root: [32]byte{'a'}},
justified: &forkchoicetypes.Checkpoint{Epoch: 3, Root: [32]byte{'b'}},
bestJustified: &forkchoicetypes.Checkpoint{Epoch: 2, Root: bj},
shouldEqual: false,
},
},
{
name: "Best justified not on the same chain as finalized. No change",
args: args{
slot: params.BeaconConfig().SlotsPerEpoch,
finalized: &forkchoicetypes.Checkpoint{Epoch: 1, Root: [32]byte{'a'}},
justified: &forkchoicetypes.Checkpoint{Epoch: 2, Root: [32]byte{'b'}},
bestJustified: &forkchoicetypes.Checkpoint{Epoch: 3, Root: [32]byte{'d'}},
shouldEqual: false,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
f := setup(test.args.justified.Epoch, test.args.finalized.Epoch)
state, blkRoot, err := prepareForkchoiceState(ctx, 0, [32]byte{}, [32]byte{}, [32]byte{}, 0, 0)
require.NoError(t, err)
require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // genesis
state, blkRoot, err = prepareForkchoiceState(ctx, 32, [32]byte{'a'}, [32]byte{}, [32]byte{}, 0, 0)
require.NoError(t, err)
require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // finalized
state, blkRoot, err = prepareForkchoiceState(ctx, 64, [32]byte{'b'}, [32]byte{'a'}, [32]byte{}, 0, 0)
require.NoError(t, err)
require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // justified
state, blkRoot, err = prepareForkchoiceState(ctx, 96, bj, [32]byte{'a'}, [32]byte{}, 0, 0)
require.NoError(t, err)
require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // best justified
state, blkRoot, err = prepareForkchoiceState(ctx, 97, [32]byte{'d'}, [32]byte{}, [32]byte{}, 0, 0)
require.NoError(t, err)
require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // bad
require.NoError(t, f.UpdateFinalizedCheckpoint(test.args.finalized))
require.NoError(t, f.UpdateJustifiedCheckpoint(ctx, test.args.justified))
require.NoError(t, f.NewSlot(ctx, test.args.slot))
bcp := test.args.bestJustified
if test.args.shouldEqual {
cp := f.JustifiedCheckpoint()
require.Equal(t, bcp.Epoch, cp.Epoch)
require.Equal(t, bcp.Root, cp.Root)
} else {
cp := f.JustifiedCheckpoint()
epochsEqual := bcp.Epoch == cp.Epoch
rootsEqual := bcp.Root == cp.Root
require.Equal(t, false, epochsEqual && rootsEqual)
}
})
}
}