2020-02-08 02:05:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveHead_Same(t *testing.T) {
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := setupBeaconChain(t, db)
|
|
|
|
|
|
|
|
r := [32]byte{'A'}
|
2020-02-15 18:57:49 +00:00
|
|
|
service.head = &head{slot: 0, root: r}
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
if err := service.saveHead(context.Background(), r); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
if service.headSlot() != 0 {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not stay the same")
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
if service.headRoot() != r {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not stay the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveHead_Different(t *testing.T) {
|
|
|
|
db := testDB.SetupDB(t)
|
|
|
|
defer testDB.TeardownDB(t, db)
|
|
|
|
service := setupBeaconChain(t, db)
|
|
|
|
|
|
|
|
oldRoot := [32]byte{'A'}
|
2020-02-15 18:57:49 +00:00
|
|
|
service.head = &head{slot: 0, root: oldRoot}
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
newHeadBlock := ðpb.BeaconBlock{Slot: 1}
|
|
|
|
newHeadSignedBlock := ðpb.SignedBeaconBlock{Block: newHeadBlock}
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
newRoot, err := ssz.HashTreeRoot(newHeadBlock)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
headState, err := state.InitializeFromProto(&pb.BeaconState{Slot: 1})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveState(context.Background(), headState, newRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-08 02:05:43 +00:00
|
|
|
if err := service.saveHead(context.Background(), newRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:57:49 +00:00
|
|
|
if service.HeadSlot() != 1 {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedRoot, err := service.HeadRoot(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(cachedRoot, newRoot[:]) {
|
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
if !reflect.DeepEqual(service.headBlock(), newHeadSignedBlock) {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
2020-02-15 18:57:49 +00:00
|
|
|
if !reflect.DeepEqual(service.headState().CloneInnerState(), headState.CloneInnerState()) {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
|
|
|
}
|