2019-02-06 02:18:55 +00:00
|
|
|
package backend
|
|
|
|
|
2019-02-09 20:35:51 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-23 06:06:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-03-26 15:40:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
2019-02-10 22:09:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-02-09 20:35:51 +00:00
|
|
|
)
|
|
|
|
|
2019-03-26 15:40:55 +00:00
|
|
|
func init() {
|
|
|
|
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
|
|
|
|
EnableCrosslinks: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestSimulatedBackendStop_ShutsDown(t *testing.T) {
|
2019-02-06 02:18:55 +00:00
|
|
|
|
|
|
|
backend, err := NewSimulatedBackend()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create a new simulated backedn %v", err)
|
|
|
|
}
|
|
|
|
if err := backend.Shutdown(); err != nil {
|
|
|
|
t.Errorf("Could not successfully shutdown simulated backend %v", err)
|
|
|
|
}
|
2019-02-23 06:06:20 +00:00
|
|
|
|
|
|
|
db.TeardownDB(backend.beaconDB)
|
2019-02-06 02:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestGenerateBlockAndAdvanceChain_IncreasesSlot(t *testing.T) {
|
2019-02-06 02:18:55 +00:00
|
|
|
backend, err := NewSimulatedBackend()
|
|
|
|
if err != nil {
|
2019-02-23 06:06:20 +00:00
|
|
|
t.Fatalf("Could not create a new simulated backend %v", err)
|
2019-02-06 02:18:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:06:20 +00:00
|
|
|
privKeys, err := backend.SetupBackend(100)
|
2019-02-09 20:35:51 +00:00
|
|
|
if err != nil {
|
2019-02-23 06:06:20 +00:00
|
|
|
t.Fatalf("Could not set up backend %v", err)
|
2019-02-09 20:35:51 +00:00
|
|
|
}
|
2019-02-23 06:06:20 +00:00
|
|
|
defer backend.Shutdown()
|
|
|
|
defer db.TeardownDB(backend.beaconDB)
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
slotLimit := params.BeaconConfig().SlotsPerEpoch + uint64(1)
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
for i := uint64(0); i < slotLimit; i++ {
|
|
|
|
if err := backend.GenerateBlockAndAdvanceChain(&SimulatedObjects{}, privKeys); err != nil {
|
2019-02-06 02:18:55 +00:00
|
|
|
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, backend.state.Slot+1)
|
|
|
|
}
|
|
|
|
if backend.inMemoryBlocks[len(backend.inMemoryBlocks)-1].Slot != backend.state.Slot {
|
2019-04-21 14:14:34 +00:00
|
|
|
t.Errorf("In memory Blocks do not have the same last slot as the state, expected %d but got %v",
|
2019-02-06 02:18:55 +00:00
|
|
|
backend.state.Slot, backend.inMemoryBlocks[len(backend.inMemoryBlocks)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:09:35 +00:00
|
|
|
if backend.state.Slot != params.BeaconConfig().GenesisSlot+uint64(slotLimit) {
|
2019-02-06 02:18:55 +00:00
|
|
|
t.Errorf("Unequal state slot and expected slot %d %d", backend.state.Slot, slotLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestGenerateNilBlockAndAdvanceChain_IncreasesSlot(t *testing.T) {
|
2019-02-06 02:18:55 +00:00
|
|
|
backend, err := NewSimulatedBackend()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create a new simulated backedn %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:06:20 +00:00
|
|
|
if _, err := backend.SetupBackend(100); err != nil {
|
|
|
|
t.Fatalf("Could not set up backend %v", err)
|
2019-02-06 02:18:55 +00:00
|
|
|
}
|
2019-02-23 06:06:20 +00:00
|
|
|
defer backend.Shutdown()
|
|
|
|
defer db.TeardownDB(backend.beaconDB)
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
slotLimit := params.BeaconConfig().SlotsPerEpoch + uint64(1)
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
for i := uint64(0); i < slotLimit; i++ {
|
2019-02-06 02:18:55 +00:00
|
|
|
if err := backend.GenerateNilBlockAndAdvanceChain(); err != nil {
|
|
|
|
t.Fatalf("Could not generate block and transition state successfully %v for slot %d", err, backend.state.Slot+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 22:09:35 +00:00
|
|
|
if backend.state.Slot != params.BeaconConfig().GenesisSlot+uint64(slotLimit) {
|
2019-02-06 02:18:55 +00:00
|
|
|
t.Errorf("Unequal state slot and expected slot %d %d", backend.state.Slot, slotLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|