2020-02-08 02:05:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-05-05 04:30:24 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2020-02-08 02:05:43 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-05-06 19:15:31 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2020-02-08 02:05:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaveHead_Same(t *testing.T) {
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-26 22:52:11 +00:00
|
|
|
ctx := context.Background()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
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 20:27:03 +00:00
|
|
|
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-05 04:30:24 +00:00
|
|
|
newRoot, err := stateutil.BlockRoot(newHeadBlock)
|
2020-04-14 16:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
headState := testutil.NewBeaconState()
|
|
|
|
if err := headState.SetSlot(1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1, Root: newRoot[:]}); err != nil {
|
2020-04-14 16:41:09 +00:00
|
|
|
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-06-26 22:52:11 +00:00
|
|
|
if !reflect.DeepEqual(service.headState(ctx).CloneInnerState(), headState.CloneInnerState()) {
|
2020-02-08 02:05:43 +00:00
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 19:15:31 +00:00
|
|
|
|
|
|
|
func TestSaveHead_Different_Reorg(t *testing.T) {
|
2020-06-26 22:52:11 +00:00
|
|
|
ctx := context.Background()
|
2020-05-06 19:15:31 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-05-06 19:15:31 +00:00
|
|
|
|
|
|
|
oldRoot := [32]byte{'A'}
|
|
|
|
service.head = &head{slot: 0, root: oldRoot}
|
|
|
|
|
|
|
|
reorgChainParent := [32]byte{'B'}
|
|
|
|
newHeadBlock := ðpb.BeaconBlock{
|
|
|
|
Slot: 1,
|
|
|
|
ParentRoot: reorgChainParent[:],
|
|
|
|
}
|
|
|
|
newHeadSignedBlock := ðpb.SignedBeaconBlock{Block: newHeadBlock}
|
|
|
|
|
|
|
|
if err := service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
newRoot, err := stateutil.BlockRoot(newHeadBlock)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
headState := testutil.NewBeaconState()
|
|
|
|
if err := headState.SetSlot(1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1, Root: newRoot[:]}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.beaconDB.SaveState(context.Background(), headState, newRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.saveHead(context.Background(), newRoot); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.HeadSlot() != 1 {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(service.headBlock(), newHeadSignedBlock) {
|
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
2020-06-26 22:52:11 +00:00
|
|
|
if !reflect.DeepEqual(service.headState(ctx).CloneInnerState(), headState.CloneInnerState()) {
|
2020-05-06 19:15:31 +00:00
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
|
|
|
testutil.AssertLogsContain(t, hook, "Chain reorg occurred")
|
|
|
|
}
|
2020-05-20 19:16:56 +00:00
|
|
|
|
|
|
|
func TestUpdateRecentCanonicalBlocks_CanUpdateWithoutParent(t *testing.T) {
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-05-20 19:16:56 +00:00
|
|
|
|
|
|
|
r := [32]byte{'a'}
|
|
|
|
if err := service.updateRecentCanonicalBlocks(context.Background(), r); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
canonical, err := service.IsCanonical(context.Background(), r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !canonical {
|
|
|
|
t.Error("Block should be canonical")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateRecentCanonicalBlocks_CanUpdateWithParent(t *testing.T) {
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-05-20 19:16:56 +00:00
|
|
|
oldHead := [32]byte{'a'}
|
|
|
|
if err := service.forkChoiceStore.ProcessBlock(context.Background(), 1, oldHead, [32]byte{'g'}, [32]byte{}, 0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
currentHead := [32]byte{'b'}
|
|
|
|
if err := service.forkChoiceStore.ProcessBlock(context.Background(), 3, currentHead, oldHead, [32]byte{}, 0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
forkedRoot := [32]byte{'c'}
|
|
|
|
if err := service.forkChoiceStore.ProcessBlock(context.Background(), 2, forkedRoot, oldHead, [32]byte{}, 0, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := service.updateRecentCanonicalBlocks(context.Background(), currentHead); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
canonical, err := service.IsCanonical(context.Background(), currentHead)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !canonical {
|
|
|
|
t.Error("Block should be canonical")
|
|
|
|
}
|
|
|
|
canonical, err = service.IsCanonical(context.Background(), oldHead)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !canonical {
|
|
|
|
t.Error("Block should be canonical")
|
|
|
|
}
|
|
|
|
canonical, err = service.IsCanonical(context.Background(), forkedRoot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if canonical {
|
|
|
|
t.Error("Block should not be canonical")
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 15:10:34 +00:00
|
|
|
|
|
|
|
func TestCacheJustifiedStateBalances_CanCache(t *testing.T) {
|
2020-06-23 20:40:55 +00:00
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
2020-06-16 15:10:34 +00:00
|
|
|
|
|
|
|
state, _ := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
r := [32]byte{'a'}
|
2020-06-23 00:19:33 +00:00
|
|
|
if err := service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Root: r[:]}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-16 15:10:34 +00:00
|
|
|
if err := service.beaconDB.SaveState(context.Background(), state, r); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := service.cacheJustifiedStateBalances(context.Background(), r); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(state.Balances(), service.getJustifiedBalances()) {
|
|
|
|
t.Fatal("Incorrect justified balances")
|
|
|
|
}
|
|
|
|
}
|