mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
59fc2e13e7
Former-commit-id: 5f673dffed4267c1aca8ebfb4fe182798b9b2d2a [formerly 3fd494231ba5a350ab59a86fcf60ae9f56f1da00] Former-commit-id: 1506dd418121884a8c7a727f8cf03c12e4c93cc3
147 lines
5.5 KiB
Go
147 lines
5.5 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/database"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/params"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/types"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestNewBeaconChain(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
tmp := fmt.Sprintf("%s/beacontest", os.TempDir())
|
|
config := &database.BeaconDBConfig{DataDir: tmp, Name: "beacontest", InMemory: false}
|
|
db, err := database.NewBeaconDB(config)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
db.Start()
|
|
beaconChain, err := NewBeaconChain(db.DB())
|
|
if err != nil {
|
|
t.Fatalf("unable to setup beacon chain: %v", err)
|
|
}
|
|
|
|
msg := hook.LastEntry().Message
|
|
want := "No chainstate found on disk, initializing beacon from genesis"
|
|
if msg != want {
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
}
|
|
|
|
hook.Reset()
|
|
active, crystallized := types.NewGenesisStates()
|
|
if !reflect.DeepEqual(beaconChain.ActiveState(), active) {
|
|
t.Errorf("active states not equal. received: %v, wanted: %v", beaconChain.ActiveState(), active)
|
|
}
|
|
if !reflect.DeepEqual(beaconChain.CrystallizedState(), crystallized) {
|
|
t.Errorf("crystallized states not equal. received: %v, wanted: %v", beaconChain.CrystallizedState(), crystallized)
|
|
}
|
|
}
|
|
|
|
func TestMutateActiveState(t *testing.T) {
|
|
tmp := fmt.Sprintf("%s/beacontest", os.TempDir())
|
|
config := &database.BeaconDBConfig{DataDir: tmp, Name: "beacontest2", InMemory: false}
|
|
db, err := database.NewBeaconDB(config)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
db.Start()
|
|
beaconChain, err := NewBeaconChain(db.DB())
|
|
if err != nil {
|
|
t.Fatalf("unable to setup beacon chain: %v", err)
|
|
}
|
|
|
|
active := &types.ActiveState{
|
|
AttestationCount: 4096,
|
|
AttesterBitfields: []byte{'A', 'B', 'C'},
|
|
}
|
|
if err := beaconChain.MutateActiveState(active); err != nil {
|
|
t.Fatalf("unable to mutate active state: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(beaconChain.state.ActiveState, active) {
|
|
t.Errorf("active state was not updated. wanted %v, got %v", active, beaconChain.state.ActiveState)
|
|
}
|
|
|
|
// Initializing a new beacon chain should deserialize persisted state from disk.
|
|
newBeaconChain, err := NewBeaconChain(db.DB())
|
|
if err != nil {
|
|
t.Fatalf("unable to setup second beacon chain: %v", err)
|
|
}
|
|
// The active state should still be the one we mutated and persited earlier.
|
|
if active.AttestationCount != newBeaconChain.state.ActiveState.AttestationCount {
|
|
t.Errorf("active state height incorrect. wanted %v, got %v", active.AttestationCount, newBeaconChain.state.ActiveState.AttestationCount)
|
|
}
|
|
if !bytes.Equal(active.AttesterBitfields, newBeaconChain.state.ActiveState.AttesterBitfields) {
|
|
t.Errorf("active state randao incorrect. wanted %v, got %v", active.AttesterBitfields, newBeaconChain.state.ActiveState.AttesterBitfields)
|
|
}
|
|
}
|
|
|
|
func TestMutateCrystallizedState(t *testing.T) {
|
|
tmp := fmt.Sprintf("%s/beacontest", os.TempDir())
|
|
config := &database.BeaconDBConfig{DataDir: tmp, Name: "beacontest3", InMemory: false}
|
|
db, err := database.NewBeaconDB(config)
|
|
if err != nil {
|
|
t.Fatalf("unable to setup db: %v", err)
|
|
}
|
|
db.Start()
|
|
beaconChain, err := NewBeaconChain(db.DB())
|
|
if err != nil {
|
|
t.Fatalf("unable to setup beacon chain: %v", err)
|
|
}
|
|
|
|
currentCheckpoint := common.BytesToHash([]byte("checkpoint"))
|
|
crystallized := &types.CrystallizedState{
|
|
Dynasty: 3,
|
|
CurrentCheckpoint: currentCheckpoint,
|
|
}
|
|
if err := beaconChain.MutateCrystallizedState(crystallized); err != nil {
|
|
t.Fatalf("unable to mutate crystallized state: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(beaconChain.state.CrystallizedState, crystallized) {
|
|
t.Errorf("crystallized state was not updated. wanted %v, got %v", crystallized, beaconChain.state.CrystallizedState)
|
|
}
|
|
|
|
// Initializing a new beacon chain should deserialize persisted state from disk.
|
|
newBeaconChain, err := NewBeaconChain(db.DB())
|
|
if err != nil {
|
|
t.Fatalf("unable to setup second beacon chain: %v", err)
|
|
}
|
|
// The crystallized state should still be the one we mutated and persited earlier.
|
|
if crystallized.Dynasty != newBeaconChain.state.CrystallizedState.Dynasty {
|
|
t.Errorf("crystallized state dynasty incorrect. wanted %v, got %v", crystallized.Dynasty, newBeaconChain.state.CrystallizedState.Dynasty)
|
|
}
|
|
if crystallized.CurrentCheckpoint.Hex() != newBeaconChain.state.CrystallizedState.CurrentCheckpoint.Hex() {
|
|
t.Errorf("crystallized state current checkpoint incorrect. wanted %v, got %v", crystallized.CurrentCheckpoint.Hex(), newBeaconChain.state.CrystallizedState.CurrentCheckpoint.Hex())
|
|
}
|
|
}
|
|
|
|
func TestFaultyShuffle(t *testing.T) {
|
|
if _, err := Shuffle(common.Hash{'a'}, params.MaxValidators+1); err == nil {
|
|
t.Error("Shuffle should have failed when validator count exceeds MaxValidators")
|
|
}
|
|
}
|
|
|
|
func TestShuffle(t *testing.T) {
|
|
hash1 := common.BytesToHash([]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'c', 'd', 'e', 'f', 'g'})
|
|
hash2 := common.BytesToHash([]byte{'1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7', '1', '2', '3', '4', '5', '6', '7'})
|
|
|
|
list1, err := Shuffle(hash1, 100)
|
|
if err != nil {
|
|
t.Errorf("Shuffle failed with: %v", err)
|
|
}
|
|
|
|
list2, err := Shuffle(hash2, 100)
|
|
if err != nil {
|
|
t.Errorf("Shuffle failed with: %v", err)
|
|
}
|
|
if reflect.DeepEqual(list1, list2) {
|
|
t.Errorf("2 shuffled lists shouldn't be equal")
|
|
}
|
|
}
|