2020-02-08 02:05:43 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-13 18:26:43 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-02-08 02:05:43 +00:00
|
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2020-04-14 20:27:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-07-16 12:11:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
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
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.saveHead(context.Background(), r))
|
|
|
|
assert.Equal(t, uint64(0), service.headSlot(), "Head did not stay the same")
|
|
|
|
assert.Equal(t, r, service.headRoot(), "Head did not stay the same")
|
2020-02-08 02:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-08-27 18:13:32 +00:00
|
|
|
newHeadSignedBlock := testutil.NewBeaconBlock()
|
|
|
|
newHeadSignedBlock.Block.Slot = 1
|
|
|
|
newHeadBlock := newHeadSignedBlock.Block
|
2020-04-14 20:27:03 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock))
|
2020-08-27 18:13:32 +00:00
|
|
|
newRoot, err := newHeadBlock.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-14 20:27:03 +00:00
|
|
|
headState := testutil.NewBeaconState()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, headState.SetSlot(1))
|
|
|
|
require.NoError(t, service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1, Root: newRoot[:]}))
|
|
|
|
require.NoError(t, service.beaconDB.SaveState(context.Background(), headState, newRoot))
|
|
|
|
require.NoError(t, service.saveHead(context.Background(), newRoot))
|
2020-02-08 02:05:43 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.Equal(t, uint64(1), service.HeadSlot(), "Head did not change")
|
2020-02-08 02:05:43 +00:00
|
|
|
|
|
|
|
cachedRoot, err := service.HeadRoot(context.Background())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-08 02:05:43 +00:00
|
|
|
if !bytes.Equal(cachedRoot, newRoot[:]) {
|
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.DeepEqual(t, newHeadSignedBlock, service.headBlock(), "Head did not change")
|
|
|
|
assert.DeepEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change")
|
2020-02-08 02:05:43 +00:00
|
|
|
}
|
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'}
|
2020-08-27 18:13:32 +00:00
|
|
|
newHeadSignedBlock := testutil.NewBeaconBlock()
|
|
|
|
newHeadSignedBlock.Block.Slot = 1
|
|
|
|
newHeadSignedBlock.Block.ParentRoot = reorgChainParent[:]
|
|
|
|
newHeadBlock := newHeadSignedBlock.Block
|
2020-05-06 19:15:31 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(context.Background(), newHeadSignedBlock))
|
2020-08-27 18:13:32 +00:00
|
|
|
newRoot, err := newHeadBlock.HashTreeRoot()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-06 19:15:31 +00:00
|
|
|
headState := testutil.NewBeaconState()
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, headState.SetSlot(1))
|
|
|
|
require.NoError(t, service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1, Root: newRoot[:]}))
|
|
|
|
require.NoError(t, service.beaconDB.SaveState(context.Background(), headState, newRoot))
|
|
|
|
require.NoError(t, service.saveHead(context.Background(), newRoot))
|
2020-05-06 19:15:31 +00:00
|
|
|
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.Equal(t, uint64(1), service.HeadSlot(), "Head did not change")
|
2020-05-06 19:15:31 +00:00
|
|
|
|
|
|
|
cachedRoot, err := service.HeadRoot(context.Background())
|
2020-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-05-06 19:15:31 +00:00
|
|
|
if !bytes.Equal(cachedRoot, newRoot[:]) {
|
|
|
|
t.Error("Head did not change")
|
|
|
|
}
|
2020-07-16 12:11:39 +00:00
|
|
|
assert.DeepEqual(t, newHeadSignedBlock, service.headBlock(), "Head did not change")
|
|
|
|
assert.DeepEqual(t, headState.CloneInnerState(), service.headState(ctx).CloneInnerState(), "Head did not change")
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Chain reorg occurred")
|
2020-05-06 19:15:31 +00:00
|
|
|
}
|
2020-05-20 19:16:56 +00:00
|
|
|
|
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-07-16 12:11:39 +00:00
|
|
|
require.NoError(t, service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Root: r[:]}))
|
|
|
|
require.NoError(t, service.beaconDB.SaveState(context.Background(), state, r))
|
|
|
|
require.NoError(t, service.cacheJustifiedStateBalances(context.Background(), r))
|
|
|
|
require.DeepEqual(t, service.getJustifiedBalances(), state.Balances(), "Incorrect justified balances")
|
2020-06-16 15:10:34 +00:00
|
|
|
}
|
2020-10-13 18:26:43 +00:00
|
|
|
|
|
|
|
func TestUpdateHead_MissingJustifiedRoot(t *testing.T) {
|
|
|
|
db, sc := testDB.SetupDB(t)
|
|
|
|
service := setupBeaconChain(t, db, sc)
|
|
|
|
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
|
|
require.NoError(t, service.beaconDB.SaveBlock(context.Background(), b))
|
|
|
|
r, err := b.Block.HashTreeRoot()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
service.justifiedCheckpt = ðpb.Checkpoint{Root: r[:]}
|
|
|
|
service.finalizedCheckpt = ðpb.Checkpoint{}
|
|
|
|
service.bestJustifiedCheckpt = ðpb.Checkpoint{}
|
|
|
|
|
|
|
|
require.NoError(t, service.updateHead(context.Background(), []uint64{}))
|
|
|
|
}
|