2022-01-25 22:07:57 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain/store"
|
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_newSlot(t *testing.T) {
|
|
|
|
beaconDB := testDB.SetupDB(t)
|
|
|
|
fcs := protoarray.New(0, 0, [32]byte{})
|
|
|
|
opts := []Option{
|
|
|
|
WithDatabase(beaconDB),
|
|
|
|
WithStateGen(stategen.New(beaconDB)),
|
|
|
|
WithForkChoiceStore(fcs),
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-03-12 17:32:04 +00:00
|
|
|
require.NoError(t, fcs.InsertOptimisticBlock(ctx, 0, [32]byte{}, [32]byte{}, 0, 0)) // genesis
|
|
|
|
require.NoError(t, fcs.InsertOptimisticBlock(ctx, 32, [32]byte{'a'}, [32]byte{}, 0, 0)) // finalized
|
|
|
|
require.NoError(t, fcs.InsertOptimisticBlock(ctx, 64, [32]byte{'b'}, [32]byte{'a'}, 0, 0)) // justified
|
|
|
|
require.NoError(t, fcs.InsertOptimisticBlock(ctx, 96, [32]byte{'c'}, [32]byte{'a'}, 0, 0)) // best justified
|
|
|
|
require.NoError(t, fcs.InsertOptimisticBlock(ctx, 97, [32]byte{'d'}, [32]byte{}, 0, 0)) // bad
|
2022-01-25 22:07:57 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
slot types.Slot
|
|
|
|
finalized *ethpb.Checkpoint
|
|
|
|
justified *ethpb.Checkpoint
|
|
|
|
bestJustified *ethpb.Checkpoint
|
|
|
|
shouldEqual bool
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Not epoch boundary. No change",
|
|
|
|
args: args{
|
|
|
|
slot: params.BeaconConfig().SlotsPerEpoch + 1,
|
|
|
|
finalized: ðpb.Checkpoint{Epoch: 1, Root: bytesutil.PadTo([]byte{'a'}, 32)},
|
|
|
|
justified: ðpb.Checkpoint{Epoch: 2, Root: bytesutil.PadTo([]byte{'b'}, 32)},
|
|
|
|
bestJustified: ðpb.Checkpoint{Epoch: 3, Root: bytesutil.PadTo([]byte{'c'}, 32)},
|
|
|
|
shouldEqual: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Justified higher than best justified. No change",
|
|
|
|
args: args{
|
|
|
|
slot: params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
finalized: ðpb.Checkpoint{Epoch: 1, Root: bytesutil.PadTo([]byte{'a'}, 32)},
|
|
|
|
justified: ðpb.Checkpoint{Epoch: 3, Root: bytesutil.PadTo([]byte{'b'}, 32)},
|
|
|
|
bestJustified: ðpb.Checkpoint{Epoch: 2, Root: bytesutil.PadTo([]byte{'c'}, 32)},
|
|
|
|
shouldEqual: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Best justified not on the same chain as finalized. No change",
|
|
|
|
args: args{
|
|
|
|
slot: params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
finalized: ðpb.Checkpoint{Epoch: 1, Root: bytesutil.PadTo([]byte{'a'}, 32)},
|
|
|
|
justified: ðpb.Checkpoint{Epoch: 2, Root: bytesutil.PadTo([]byte{'b'}, 32)},
|
|
|
|
bestJustified: ðpb.Checkpoint{Epoch: 3, Root: bytesutil.PadTo([]byte{'d'}, 32)},
|
|
|
|
shouldEqual: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Best justified on the same chain as finalized. Yes change",
|
|
|
|
args: args{
|
|
|
|
slot: params.BeaconConfig().SlotsPerEpoch,
|
|
|
|
finalized: ðpb.Checkpoint{Epoch: 1, Root: bytesutil.PadTo([]byte{'a'}, 32)},
|
|
|
|
justified: ðpb.Checkpoint{Epoch: 2, Root: bytesutil.PadTo([]byte{'b'}, 32)},
|
|
|
|
bestJustified: ðpb.Checkpoint{Epoch: 3, Root: bytesutil.PadTo([]byte{'c'}, 32)},
|
|
|
|
shouldEqual: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
service, err := NewService(ctx, opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
store := store.New(test.args.justified, test.args.finalized)
|
|
|
|
store.SetBestJustifiedCheckpt(test.args.bestJustified)
|
|
|
|
service.store = store
|
|
|
|
|
2022-01-31 19:03:48 +00:00
|
|
|
require.NoError(t, service.NewSlot(ctx, test.args.slot))
|
2022-01-25 22:07:57 +00:00
|
|
|
if test.args.shouldEqual {
|
|
|
|
require.DeepSSZEqual(t, service.store.BestJustifiedCheckpt(), service.store.JustifiedCheckpt())
|
|
|
|
} else {
|
|
|
|
require.DeepNotSSZEqual(t, service.store.BestJustifiedCheckpt(), service.store.JustifiedCheckpt())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|