Use DB to Fetch Justified Block (#2172)

* builds

* db tests in core

* spacing

* Gazelle

* fix lint

* iterative block root loop

* Update beacon-chain/core/blocks/block_operations_test.go

Co-Authored-By: rauljordan <raul@prysmaticlabs.com>

* imports
This commit is contained in:
Raul Jordan
2019-04-05 14:48:49 -05:00
committed by GitHub
parent 55ca5cc2c3
commit 60c254d818
31 changed files with 301 additions and 213 deletions

View File

@@ -12,6 +12,7 @@ go_library(
deps = [
"//beacon-chain/attestation:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/core/validators:go_default_library",
@@ -41,9 +42,8 @@ go_test(
embed = [":go_default_library"],
deps = [
"//beacon-chain/attestation:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/internal:go_default_library",

View File

@@ -210,6 +210,7 @@ func (c *ChainService) runStateTransition(
beaconState,
block,
headRoot,
c.beaconDB,
&state.TransitionConfig{
VerifySignatures: false, // We disable signature verification for now.
Logging: true, // We enable logging in this state transition call.

View File

@@ -8,8 +8,7 @@ import (
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@@ -80,7 +79,7 @@ func TestReceiveBlock_ProcessCorrectly(t *testing.T) {
DepositRootHash32: []byte{},
BlockHash32: []byte{},
}
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
beaconState, err := genesis.BeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatalf("Can't generate genesis state: %v", err)
}
@@ -145,7 +144,7 @@ func TestReceiveBlock_RemovesPendingDeposits(t *testing.T) {
DepositRootHash32: []byte{},
BlockHash32: []byte{},
}
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
beaconState, err := genesis.BeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatalf("Can't generate genesis state: %v", err)
}
@@ -300,7 +299,7 @@ func TestReceiveBlock_OnChainSplit(t *testing.T) {
DepositRootHash32: []byte{},
BlockHash32: []byte{},
}
beaconState, err := state.GenesisBeaconState(deposits, 0, eth1Data)
beaconState, err := genesis.BeaconState(deposits, 0, eth1Data)
if err != nil {
t.Fatalf("Can't generate genesis state: %v", err)
}
@@ -442,7 +441,7 @@ func TestIsBlockReadyForProcessing_ValidBlock(t *testing.T) {
if err != nil {
t.Fatalf("Could not tree hash state: %v", err)
}
genesis := b.NewGenesisBlock([]byte{})
genesis := genesis.NewGenesisBlock([]byte{})
if err := chainService.beaconDB.SaveBlock(genesis); err != nil {
t.Fatalf("cannot save block: %v", err)
}

View File

@@ -10,9 +10,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
@@ -31,7 +30,7 @@ var _ = ForkChoice(&ChainService{})
func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) {
deposits, _ := setupInitialDeposits(t, 5)
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
beaconState, err := genesis.BeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Cannot create genesis beacon state: %v", err)
}
@@ -39,7 +38,7 @@ func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) {
if err != nil {
t.Fatalf("Could not tree hash state: %v", err)
}
genesis := b.NewGenesisBlock(stateRoot[:])
genesis := genesis.NewGenesisBlock(stateRoot[:])
genesisRoot, err := hashutil.HashProto(genesis)
if err != nil {
t.Fatalf("Could not get genesis block root: %v", err)
@@ -128,7 +127,7 @@ func TestApplyForkChoice_SetsCanonicalHead(t *testing.T) {
func TestVoteCount_ParentDoesNotExistNoVoteCount(t *testing.T) {
beaconDB := internal.SetupDB(t)
defer internal.TeardownDB(t, beaconDB)
genesisBlock := b.NewGenesisBlock([]byte("stateroot"))
genesisBlock := genesis.NewGenesisBlock([]byte("stateroot"))
if err := beaconDB.SaveBlock(genesisBlock); err != nil {
t.Fatal(err)
}
@@ -153,7 +152,7 @@ func TestVoteCount_ParentDoesNotExistNoVoteCount(t *testing.T) {
func TestVoteCount_IncreaseCountCorrectly(t *testing.T) {
beaconDB := internal.SetupDB(t)
defer internal.TeardownDB(t, beaconDB)
genesisBlock := b.NewGenesisBlock([]byte("stateroot"))
genesisBlock := genesis.NewGenesisBlock([]byte("stateroot"))
genesisRoot, err := hashutil.HashBeaconBlock(genesisBlock)
if err != nil {
t.Fatal(err)

View File

@@ -9,7 +9,7 @@ import (
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/operations"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
@@ -147,7 +147,7 @@ func (c *ChainService) initializeBeaconChain(genesisTime time.Time, deposits []*
if err != nil {
return nil, fmt.Errorf("could not hash beacon state: %v", err)
}
genBlock := b.NewGenesisBlock(stateRoot[:])
genBlock := genesis.NewGenesisBlock(stateRoot[:])
// TODO(#2011): Remove this in state caching.
beaconState.LatestBlock = genBlock

View File

@@ -15,7 +15,7 @@ import (
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/attestation"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
@@ -203,7 +203,7 @@ func createRandaoReveal(t *testing.T, beaconState *pb.BeaconState, privKeys []*b
}
func setupGenesisBlock(t *testing.T, cs *ChainService, beaconState *pb.BeaconState) ([32]byte, *pb.BeaconBlock) {
genesis := b.NewGenesisBlock([]byte{})
genesis := genesis.NewGenesisBlock([]byte{})
if err := cs.beaconDB.SaveBlock(genesis); err != nil {
t.Fatalf("could not save block to db: %v", err)
}

View File

@@ -97,6 +97,7 @@ func GenerateStateFromBlock(ctx context.Context, db *db.BeaconDB, slot uint64) (
postState,
nil,
root,
db,
&state.TransitionConfig{
VerifySignatures: false,
Logging: false,
@@ -111,6 +112,7 @@ func GenerateStateFromBlock(ctx context.Context, db *db.BeaconDB, slot uint64) (
postState,
block,
root,
db,
&state.TransitionConfig{
VerifySignatures: false,
Logging: false,
@@ -135,6 +137,7 @@ func GenerateStateFromBlock(ctx context.Context, db *db.BeaconDB, slot uint64) (
postState,
nil,
root,
db,
&state.TransitionConfig{
VerifySignatures: false,
Logging: false,

View File

@@ -13,7 +13,7 @@ go_library(
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/blockchain:go_default_library",
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/db:go_default_library",

View File

@@ -11,7 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
@@ -108,6 +108,7 @@ func (sb *SimulatedBackend) GenerateBlockAndAdvanceChain(objects *SimulatedObjec
sb.state,
newBlock,
prevBlockRoot,
sb.beaconDB,
state.DefaultConfig(),
)
if err != nil {
@@ -132,6 +133,7 @@ func (sb *SimulatedBackend) GenerateNilBlockAndAdvanceChain() error {
sb.state,
nil,
prevBlockRoot,
sb.beaconDB,
state.DefaultConfig(),
)
if err != nil {
@@ -271,7 +273,7 @@ func (sb *SimulatedBackend) initializeStateTest(testCase *StateTestCase) ([]*bls
func (sb *SimulatedBackend) setupBeaconStateAndGenesisBlock(initialDeposits []*pb.Deposit) error {
var err error
genesisTime := time.Date(2018, 9, 0, 0, 0, 0, 0, time.UTC).Unix()
sb.state, err = state.GenesisBeaconState(initialDeposits, uint64(genesisTime), nil)
sb.state, err = genesis.BeaconState(initialDeposits, uint64(genesisTime), nil)
if err != nil {
return fmt.Errorf("could not initialize simulated beacon state: %v", err)
}
@@ -283,7 +285,7 @@ func (sb *SimulatedBackend) setupBeaconStateAndGenesisBlock(initialDeposits []*p
if err != nil {
return fmt.Errorf("could not tree hash state: %v", err)
}
genesisBlock := b.NewGenesisBlock(stateRoot[:])
genesisBlock := genesis.NewGenesisBlock(stateRoot[:])
genesisBlockRoot, err := hashutil.HashBeaconBlock(genesisBlock)
if err != nil {
return fmt.Errorf("could not tree hash genesis block: %v", err)

View File

@@ -13,6 +13,7 @@ go_library(
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state/stateutils:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/utils:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
@@ -38,8 +39,9 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
"//shared/forkutil:go_default_library",
@@ -49,7 +51,6 @@ go_test(
"//shared/trieutil:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_ethereum_go_ethereum//core/types:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

View File

@@ -17,29 +17,6 @@ import (
var clock utils.Clock = &utils.RealClock{}
// NewGenesisBlock returns the canonical, genesis block for the beacon chain protocol.
func NewGenesisBlock(stateRoot []byte) *pb.BeaconBlock {
block := &pb.BeaconBlock{
Slot: params.BeaconConfig().GenesisSlot,
ParentRootHash32: params.BeaconConfig().ZeroHash[:],
StateRootHash32: stateRoot,
RandaoReveal: params.BeaconConfig().ZeroHash[:],
Signature: params.BeaconConfig().EmptySignature[:],
Eth1Data: &pb.Eth1Data{
DepositRootHash32: params.BeaconConfig().ZeroHash[:],
BlockHash32: params.BeaconConfig().ZeroHash[:],
},
Body: &pb.BeaconBlockBody{
ProposerSlashings: []*pb.ProposerSlashing{},
AttesterSlashings: []*pb.AttesterSlashing{},
Attestations: []*pb.Attestation{},
Deposits: []*pb.Deposit{},
VoluntaryExits: []*pb.VoluntaryExit{},
},
}
return block
}
// BlockRoot returns the block root stored in the BeaconState for a given slot.
// It returns an error if the requested block root is not within the BeaconState.
// Spec pseudocode definition:

View File

@@ -15,6 +15,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/forkutil"
@@ -417,6 +418,7 @@ func ProcessBlockAttestations(
beaconState *pb.BeaconState,
block *pb.BeaconBlock,
verifySignatures bool,
beaconDB *db.BeaconDB,
) (*pb.BeaconState, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.ChainService.state.ProcessBlock.ProcessBlockAttestations")
defer span.End()
@@ -431,7 +433,7 @@ func ProcessBlockAttestations(
}
for idx, attestation := range atts {
if err := verifyAttestation(beaconState, attestation, verifySignatures); err != nil {
if err := verifyAttestation(beaconState, attestation, verifySignatures, beaconDB); err != nil {
return nil, fmt.Errorf("could not verify attestation at index %d in block: %v", idx, err)
}
beaconState.LatestAttestations = append(beaconState.LatestAttestations, &pb.PendingAttestation{
@@ -445,7 +447,7 @@ func ProcessBlockAttestations(
return beaconState, nil
}
func verifyAttestation(beaconState *pb.BeaconState, att *pb.Attestation, verifySignatures bool) error {
func verifyAttestation(beaconState *pb.BeaconState, att *pb.Attestation, verifySignatures bool, beaconDB *db.BeaconDB) error {
if att.Data.Slot < params.BeaconConfig().GenesisSlot {
return fmt.Errorf(
"attestation slot (slot %d) less than genesis slot (%d)",
@@ -493,13 +495,22 @@ func verifyAttestation(beaconState *pb.BeaconState, att *pb.Attestation, verifyS
// Verify that attestation.data.justified_block_root is equal to
// get_block_root(state, get_epoch_start_slot(attestation.data.justified_epoch)).
blockRoot, err := BlockRoot(beaconState, helpers.StartSlot(att.Data.JustifiedEpoch))
justifiedSlot := helpers.StartSlot(att.Data.JustifiedEpoch)
var justifiedBlock *pb.BeaconBlock
var err error
for i := uint64(0); justifiedBlock == nil && i < params.BeaconConfig().SlotsPerEpoch; i++ {
justifiedBlock, err = beaconDB.BlockBySlot(justifiedSlot - i)
if err != nil {
return fmt.Errorf("could not get justified block: %v", err)
}
}
blockRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
return fmt.Errorf("could not get block root for justified epoch: %v", err)
return fmt.Errorf("could not get justified block: %v", err)
}
justifiedBlockRoot := att.Data.JustifiedBlockRootHash32
if !bytes.Equal(justifiedBlockRoot, blockRoot) {
if !bytes.Equal(justifiedBlockRoot, blockRoot[:]) {
return fmt.Errorf(
"expected JustifiedBlockRoot == getBlockRoot(state, JustifiedEpoch): got %#x = %#x",
justifiedBlockRoot,

View File

@@ -12,11 +12,13 @@ import (
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/forkutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/trieutil"
@@ -46,7 +48,7 @@ func setupInitialDeposits(t *testing.T, numDeposits int) ([]*pb.Deposit, []*bls.
func TestProcessBlockRandao_IncorrectProposerFailsVerification(t *testing.T) {
deposits, privKeys := setupInitialDeposits(t, 100)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -80,7 +82,7 @@ func TestProcessBlockRandao_IncorrectProposerFailsVerification(t *testing.T) {
func TestProcessBlockRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) {
deposits, privKeys := setupInitialDeposits(t, 100)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -684,6 +686,8 @@ func TestProcessAttesterSlashings_AppliesCorrectStatus(t *testing.T) {
}
func TestProcessBlockAttestations_ThresholdReached(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attestations := make([]*pb.Attestation, params.BeaconConfig().MaxAttestations+1)
block := &pb.BeaconBlock{
Body: &pb.BeaconBlockBody{
@@ -703,12 +707,15 @@ func TestProcessBlockAttestations_ThresholdReached(t *testing.T) {
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_InclusionDelayFailure(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
@@ -736,12 +743,15 @@ func TestProcessBlockAttestations_InclusionDelayFailure(t *testing.T) {
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_EpochDistanceFailure(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
@@ -769,12 +779,15 @@ func TestProcessBlockAttestations_EpochDistanceFailure(t *testing.T) {
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_JustifiedEpochVerificationFailure(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
@@ -803,12 +816,15 @@ func TestProcessBlockAttestations_JustifiedEpochVerificationFailure(t *testing.T
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_PreviousJustifiedEpochVerificationFailure(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
@@ -837,58 +853,18 @@ func TestProcessBlockAttestations_PreviousJustifiedEpochVerificationFailure(t *t
state,
block,
false,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_BlockRootOutOfBounds(t *testing.T) {
var blockRoots [][]byte
for i := uint64(0); i < 2*params.BeaconConfig().SlotsPerEpoch; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
state := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + 64,
PreviousJustifiedEpoch: 1,
LatestBlockRootHash32S: blockRoots,
}
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
Slot: params.BeaconConfig().GenesisSlot + 60,
JustifiedBlockRootHash32: []byte{},
JustifiedEpoch: 1,
},
},
}
block := &pb.BeaconBlock{
Body: &pb.BeaconBlockBody{
Attestations: attestations,
},
}
want := "could not get block root for justified epoch"
if _, err := blocks.ProcessBlockAttestations(
context.Background(),
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_BlockRootFailure(t *testing.T) {
var blockRoots [][]byte
for i := uint64(0); i < 2*params.BeaconConfig().SlotsPerEpoch; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
state := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + 129,
PreviousJustifiedEpoch: params.BeaconConfig().GenesisEpoch + 1,
LatestBlockRootHash32S: blockRoots,
}
attestations := []*pb.Attestation{
{
@@ -899,6 +875,16 @@ func TestProcessBlockAttestations_BlockRootFailure(t *testing.T) {
},
},
}
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch+1) - 2, // Imagine 2 skip blocks
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
block := &pb.BeaconBlock{
Body: &pb.BeaconBlockBody{
Attestations: attestations,
@@ -908,23 +894,22 @@ func TestProcessBlockAttestations_BlockRootFailure(t *testing.T) {
want := fmt.Sprintf(
"expected JustifiedBlockRoot == getBlockRoot(state, JustifiedEpoch): got %#x = %#x",
[]byte{},
blockRoots[64],
justifiedRoot,
)
if _, err := blocks.ProcessBlockAttestations(
context.Background(),
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_CrosslinkRootFailure(t *testing.T) {
var blockRoots [][]byte
for i := uint64(0); i < 2*params.BeaconConfig().SlotsPerEpoch; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
// If attestation.latest_cross_link_root != state.latest_crosslinks[shard].shard_block_root
// AND
@@ -938,15 +923,24 @@ func TestProcessBlockAttestations_CrosslinkRootFailure(t *testing.T) {
state := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + 70,
PreviousJustifiedEpoch: params.BeaconConfig().GenesisEpoch,
LatestBlockRootHash32S: blockRoots,
LatestCrosslinks: stateLatestCrosslinks,
}
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch),
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
Shard: 0,
Slot: params.BeaconConfig().GenesisSlot + 20,
JustifiedBlockRootHash32: blockRoots[0],
JustifiedBlockRootHash32: justifiedRoot[:],
LatestCrosslink: &pb.Crosslink{CrosslinkDataRootHash32: []byte{2}},
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
@@ -967,16 +961,15 @@ func TestProcessBlockAttestations_CrosslinkRootFailure(t *testing.T) {
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_ShardBlockRootEqualZeroHashFailure(t *testing.T) {
var blockRoots [][]byte
for i := uint64(0); i < 2*params.BeaconConfig().SlotsPerEpoch; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
stateLatestCrosslinks := []*pb.Crosslink{
{
CrosslinkDataRootHash32: []byte{1},
@@ -985,15 +978,24 @@ func TestProcessBlockAttestations_ShardBlockRootEqualZeroHashFailure(t *testing.
state := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + 70,
PreviousJustifiedEpoch: params.BeaconConfig().GenesisEpoch,
LatestBlockRootHash32S: blockRoots,
LatestCrosslinks: stateLatestCrosslinks,
}
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch),
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
attestations := []*pb.Attestation{
{
Data: &pb.AttestationData{
Shard: 0,
Slot: params.BeaconConfig().GenesisSlot + 20,
JustifiedBlockRootHash32: blockRoots[0],
JustifiedBlockRootHash32: justifiedRoot[:],
LatestCrosslink: &pb.Crosslink{CrosslinkDataRootHash32: []byte{1}},
CrosslinkDataRootHash32: []byte{1},
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
@@ -1015,12 +1017,15 @@ func TestProcessBlockAttestations_ShardBlockRootEqualZeroHashFailure(t *testing.
state,
block,
false,
db,
); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlockAttestations_CreatePendingAttestations(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
var blockRoots [][]byte
for i := uint64(0); i < params.BeaconConfig().LatestBlockRootsLength; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
@@ -1036,11 +1041,21 @@ func TestProcessBlockAttestations_CreatePendingAttestations(t *testing.T) {
LatestBlockRootHash32S: blockRoots,
LatestCrosslinks: stateLatestCrosslinks,
}
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch),
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
att1 := &pb.Attestation{
Data: &pb.AttestationData{
Shard: 0,
Slot: params.BeaconConfig().GenesisSlot + 20,
JustifiedBlockRootHash32: blockRoots[0],
JustifiedBlockRootHash32: justifiedRoot[:],
LatestCrosslink: &pb.Crosslink{CrosslinkDataRootHash32: []byte{1}},
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
@@ -1059,6 +1074,7 @@ func TestProcessBlockAttestations_CreatePendingAttestations(t *testing.T) {
state,
block,
false,
db,
)
pendingAttestations := newState.LatestAttestations
if err != nil {

View File

@@ -4,43 +4,13 @@ import (
"bytes"
"context"
"fmt"
"reflect"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestGenesisBlock_InitializedCorrectly(t *testing.T) {
stateHash := []byte{0}
b1 := NewGenesisBlock(stateHash)
if b1.ParentRootHash32 == nil {
t.Error("genesis block missing ParentHash field")
}
if !reflect.DeepEqual(b1.Body.Attestations, []*pb.Attestation{}) {
t.Errorf("genesis block should have 0 attestations")
}
if !bytes.Equal(b1.RandaoReveal, params.BeaconConfig().ZeroHash[:]) {
t.Error("genesis block missing RandaoReveal field")
}
if !bytes.Equal(b1.StateRootHash32, stateHash) {
t.Error("genesis block StateRootHash32 isn't initialized correctly")
}
expectedEth1 := &pb.Eth1Data{
DepositRootHash32: params.BeaconConfig().ZeroHash[:],
BlockHash32: params.BeaconConfig().ZeroHash[:],
}
if !proto.Equal(b1.Eth1Data, expectedEth1) {
t.Error("genesis block Eth1Data isn't initialized correctly")
}
}
func TestBlockRootAtSlot_AccurateBlockRoot(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("slotsPerEpoch should be 64 for these tests to pass")

View File

@@ -0,0 +1,29 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["genesis.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/genesis",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state/stateutils:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["genesis_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
],
)

View File

@@ -1,24 +1,45 @@
// Package state implements the whole state transition
// function which consists of per slot, per-epoch transitions.
// It also bootstraps the genesis beacon state for slot 0.
package state
// Package genesis defines the initial state and block for Ethereum 2.0's beacon chain.
package genesis
import (
"encoding/binary"
"fmt"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
// GenesisBeaconState gets called when DepositsForChainStart count of
// NewGenesisBlock initializes an initial block for the Ethereum 2.0 beacon chain that is
// fixed in all clients and embedded in the protocol.
func NewGenesisBlock(stateRoot []byte) *pb.BeaconBlock {
block := &pb.BeaconBlock{
Slot: params.BeaconConfig().GenesisSlot,
ParentRootHash32: params.BeaconConfig().ZeroHash[:],
StateRootHash32: stateRoot,
RandaoReveal: params.BeaconConfig().ZeroHash[:],
Signature: params.BeaconConfig().EmptySignature[:],
Eth1Data: &pb.Eth1Data{
DepositRootHash32: params.BeaconConfig().ZeroHash[:],
BlockHash32: params.BeaconConfig().ZeroHash[:],
},
Body: &pb.BeaconBlockBody{
ProposerSlashings: []*pb.ProposerSlashing{},
AttesterSlashings: []*pb.AttesterSlashing{},
Attestations: []*pb.Attestation{},
Deposits: []*pb.Deposit{},
VoluntaryExits: []*pb.VoluntaryExit{},
},
}
return block
}
// BeaconState initializes a genesis beacon state - it gets called when DepositsForChainStart count of
// full deposits were made to the deposit contract and the ChainStart log gets emitted.
func GenesisBeaconState(
func BeaconState(
genesisValidatorDeposits []*pb.Deposit,
genesisTime uint64,
eth1Data *pb.Eth1Data,

View File

@@ -1,4 +1,4 @@
package state_test
package genesis_test
import (
"bytes"
@@ -8,14 +8,42 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestGenesisBlock_InitializedCorrectly(t *testing.T) {
stateHash := []byte{0}
b1 := genesis.NewGenesisBlock(stateHash)
if b1.ParentRootHash32 == nil {
t.Error("genesis block missing ParentHash field")
}
if !reflect.DeepEqual(b1.Body.Attestations, []*pb.Attestation{}) {
t.Errorf("genesis block should have 0 attestations")
}
if !bytes.Equal(b1.RandaoReveal, params.BeaconConfig().ZeroHash[:]) {
t.Error("genesis block missing RandaoReveal field")
}
if !bytes.Equal(b1.StateRootHash32, stateHash) {
t.Error("genesis block StateRootHash32 isn't initialized correctly")
}
expectedEth1 := &pb.Eth1Data{
DepositRootHash32: params.BeaconConfig().ZeroHash[:],
BlockHash32: params.BeaconConfig().ZeroHash[:],
}
if !proto.Equal(b1.Eth1Data, expectedEth1) {
t.Error("genesis block Eth1Data isn't initialized correctly")
}
}
func TestGenesisBeaconState_OK(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("SlotsPerEpoch should be 64 for these tests to pass")
@@ -82,7 +110,7 @@ func TestGenesisBeaconState_OK(t *testing.T) {
})
}
newState, err := state.GenesisBeaconState(
newState, err := genesis.BeaconState(
deposits,
genesisTime,
&pb.Eth1Data{
@@ -183,8 +211,8 @@ func TestGenesisBeaconState_OK(t *testing.T) {
}
func TestGenesisState_HashEquality(t *testing.T) {
state1, _ := state.GenesisBeaconState(nil, 0, &pb.Eth1Data{})
state2, _ := state.GenesisBeaconState(nil, 0, &pb.Eth1Data{})
state1, _ := genesis.BeaconState(nil, 0, &pb.Eth1Data{})
state2, _ := genesis.BeaconState(nil, 0, &pb.Eth1Data{})
root1, err1 := hashutil.HashProto(state1)
root2, err2 := hashutil.HashProto(state2)
@@ -199,7 +227,7 @@ func TestGenesisState_HashEquality(t *testing.T) {
}
func TestGenesisState_InitializesLatestBlockHashes(t *testing.T) {
s, _ := state.GenesisBeaconState(nil, 0, nil)
s, _ := genesis.BeaconState(nil, 0, nil)
want, got := len(s.LatestBlockRootHash32S), int(params.BeaconConfig().LatestBlockRootsLength)
if want != got {
t.Errorf("Wrong number of recent block hashes. Got: %d Want: %d", got, want)

View File

@@ -2,10 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"state.go",
"transition.go",
],
srcs = ["transition.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/core/state",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
@@ -13,8 +10,8 @@ go_library(
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/epoch:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state/stateutils:go_default_library",
"//beacon-chain/core/validators:go_default_library",
"//beacon-chain/db:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
@@ -26,13 +23,12 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"state_test.go",
"transition_test.go",
],
srcs = ["transition_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
"//shared/featureconfig:go_default_library",

View File

@@ -12,6 +12,7 @@ import (
e "github.com/prysmaticlabs/prysm/beacon-chain/core/epoch"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
@@ -53,6 +54,7 @@ func ExecuteStateTransition(
state *pb.BeaconState,
block *pb.BeaconBlock,
headRoot [32]byte,
beaconDB *db.BeaconDB,
config *TransitionConfig,
) (*pb.BeaconState, error) {
var err error
@@ -62,7 +64,7 @@ func ExecuteStateTransition(
// Execute per block transition.
if block != nil {
state, err = ProcessBlock(ctx, state, block, config)
state, err = ProcessBlock(ctx, state, block, beaconDB, config)
if err != nil {
return nil, fmt.Errorf("could not process block: %v", err)
}
@@ -103,6 +105,7 @@ func ProcessBlock(
ctx context.Context,
state *pb.BeaconState,
block *pb.BeaconBlock,
beaconDB *db.BeaconDB,
config *TransitionConfig,
) (*pb.BeaconState, error) {
@@ -153,7 +156,7 @@ func ProcessBlock(
return nil, fmt.Errorf("could not verify block proposer slashings: %v", err)
}
state, err = b.ProcessBlockAttestations(ctx, state, block, config.VerifySignatures)
state, err = b.ProcessBlockAttestations(ctx, state, block, config.VerifySignatures, beaconDB)
if err != nil {
return nil, fmt.Errorf("could not process block attestations: %v", err)
}

View File

@@ -9,12 +9,15 @@ import (
"testing"
"time"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/forkutil"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
@@ -62,6 +65,8 @@ func createRandaoReveal(t *testing.T, beaconState *pb.BeaconState, privKeys []*b
}
func TestProcessBlock_IncorrectSlot(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
beaconState := &pb.BeaconState{
Slot: params.BeaconConfig().GenesisSlot + 5,
}
@@ -73,14 +78,16 @@ func TestProcessBlock_IncorrectSlot(t *testing.T) {
4,
5,
)
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlock_IncorrectProposerSlashing(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
deposits, privKeys := setupInitialDeposits(t, params.BeaconConfig().SlotsPerEpoch)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -101,14 +108,16 @@ func TestProcessBlock_IncorrectProposerSlashing(t *testing.T) {
},
}
want := "could not verify block proposer slashing"
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlock_IncorrectAttesterSlashing(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
deposits, privKeys := setupInitialDeposits(t, params.BeaconConfig().SlotsPerEpoch)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -145,14 +154,16 @@ func TestProcessBlock_IncorrectAttesterSlashing(t *testing.T) {
},
}
want := "could not verify block attester slashing"
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
deposits, privKeys := setupInitialDeposits(t, params.BeaconConfig().SlotsPerEpoch)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -214,14 +225,16 @@ func TestProcessBlock_IncorrectProcessBlockAttestations(t *testing.T) {
},
}
want := "could not process block attestations"
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
deposits, privKeys := setupInitialDeposits(t, params.BeaconConfig().SlotsPerEpoch)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -274,12 +287,22 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
},
}
beaconState.Slot = params.BeaconConfig().GenesisSlot + 10
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch),
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
blockAtt := &pb.Attestation{
Data: &pb.AttestationData{
Shard: 0,
Slot: params.BeaconConfig().GenesisSlot,
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
JustifiedBlockRootHash32: blockRoots[0],
JustifiedBlockRootHash32: justifiedRoot[:],
LatestCrosslink: &pb.Crosslink{CrosslinkDataRootHash32: []byte{1}},
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
},
@@ -307,14 +330,16 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
},
}
want := "could not process validator exits"
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
}
}
func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
deposits, privKeys := setupInitialDeposits(t, params.BeaconConfig().SlotsPerEpoch)
beaconState, err := state.GenesisBeaconState(deposits, uint64(0), &pb.Eth1Data{})
beaconState, err := genesis.BeaconState(deposits, uint64(0), &pb.Eth1Data{})
if err != nil {
t.Fatal(err)
}
@@ -367,12 +392,22 @@ func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
},
}
beaconState.Slot = params.BeaconConfig().GenesisSlot + 10
justifiedBlock := &pb.BeaconBlock{
Slot: helpers.StartSlot(params.BeaconConfig().GenesisEpoch),
}
if err := db.SaveBlock(justifiedBlock); err != nil {
t.Fatal(err)
}
justifiedRoot, err := hashutil.HashBeaconBlock(justifiedBlock)
if err != nil {
t.Fatal(err)
}
blockAtt := &pb.Attestation{
Data: &pb.AttestationData{
Shard: 0,
Slot: params.BeaconConfig().GenesisSlot,
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
JustifiedBlockRootHash32: blockRoots[0],
JustifiedBlockRootHash32: justifiedRoot[:],
LatestCrosslink: &pb.Crosslink{CrosslinkDataRootHash32: []byte{1}},
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
},
@@ -401,7 +436,7 @@ func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
VoluntaryExits: exits,
},
}
if _, err := state.ProcessBlock(context.Background(), beaconState, block, state.DefaultConfig()); err != nil {
if _, err := state.ProcessBlock(context.Background(), beaconState, block, db, state.DefaultConfig()); err != nil {
t.Errorf("Expected block to pass processing conditions: %v", err)
}
}

View File

@@ -19,8 +19,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db",
visibility = ["//beacon-chain:__subpackages__"],
deps = [
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/hashutil:go_default_library",

View File

@@ -11,8 +11,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
@@ -29,7 +28,7 @@ var (
// InitializeState creates an initial genesis state for the beacon
// node using a set of genesis validators.
func (db *BeaconDB) InitializeState(genesisTime uint64, deposits []*pb.Deposit, eth1Data *pb.Eth1Data) error {
beaconState, err := state.GenesisBeaconState(deposits, genesisTime, eth1Data)
beaconState, err := genesis.BeaconState(deposits, genesisTime, eth1Data)
if err != nil {
return err
}
@@ -37,7 +36,7 @@ func (db *BeaconDB) InitializeState(genesisTime uint64, deposits []*pb.Deposit,
// #nosec G104
stateEnc, _ := proto.Marshal(beaconState)
stateHash := hashutil.Hash(stateEnc)
genesisBlock := b.NewGenesisBlock(stateHash[:])
genesisBlock := genesis.NewGenesisBlock(stateHash[:])
// #nosec G104
blockRoot, _ := hashutil.HashBeaconBlock(genesisBlock)
// #nosec G104

View File

@@ -50,9 +50,8 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//proto/beacon/rpc/v1:go_default_library",

View File

@@ -59,7 +59,7 @@ func (as *AttesterServer) AttestationDataAtSlot(ctx context.Context, req *pb.Att
for headState.Slot < req.Slot {
headState, err = state.ExecuteStateTransition(
ctx, headState, nil /* block */, headRoot, state.DefaultConfig(),
ctx, headState, nil /* block */, headRoot, as.beaconDB, state.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("could not execute head transition: %v", err)

View File

@@ -6,7 +6,7 @@ import (
"testing"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@@ -42,7 +42,7 @@ func TestAttestationDataAtSlot_EpochBoundaryFailure(t *testing.T) {
LatestBlockRootHash32S: make([][]byte, 20),
JustifiedEpoch: params.BeaconConfig().GenesisEpoch + 1*params.BeaconConfig().GenesisEpoch,
}
block := blocks.NewGenesisBlock([]byte("stateroot"))
block := genesis.NewGenesisBlock([]byte("stateroot"))
block.Slot = params.BeaconConfig().GenesisSlot + 3*params.BeaconConfig().SlotsPerEpoch + 1
attesterServer := &AttesterServer{
beaconDB: db,

View File

@@ -96,7 +96,7 @@ func (ps *ProposerServer) PendingAttestations(ctx context.Context, req *pb.Pendi
}
for beaconState.Slot < req.ProposalBlockSlot {
beaconState, err = state.ExecuteStateTransition(
ctx, beaconState, nil /* block */, blockRoot, &state.TransitionConfig{},
ctx, beaconState, nil /* block */, blockRoot, ps.beaconDB, &state.TransitionConfig{},
)
if err != nil {
return nil, fmt.Errorf("could not execute head transition: %v", err)
@@ -168,6 +168,7 @@ func (ps *ProposerServer) ComputeStateRoot(ctx context.Context, req *pbp2p.Beaco
beaconState,
nil,
parentHash,
ps.beaconDB,
state.DefaultConfig(),
)
if err != nil {
@@ -179,6 +180,7 @@ func (ps *ProposerServer) ComputeStateRoot(ctx context.Context, req *pbp2p.Beaco
beaconState,
req,
parentHash,
ps.beaconDB,
state.DefaultConfig(),
)
if err != nil {

View File

@@ -6,9 +6,8 @@ import (
"testing"
"time"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
@@ -28,8 +27,8 @@ func TestProposeBlock_OK(t *testing.T) {
mockChain := &mockChainService{}
ctx := context.Background()
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
gBlock := genesis.NewGenesisBlock([]byte{})
if err := db.SaveBlock(gBlock); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
@@ -50,12 +49,12 @@ func TestProposeBlock_OK(t *testing.T) {
}
}
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
beaconState, err := genesis.BeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(ctx, genesis, beaconState); err != nil {
if err := db.UpdateChainHead(ctx, gBlock, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
@@ -83,8 +82,8 @@ func TestComputeStateRoot_OK(t *testing.T) {
mockChain := &mockChainService{}
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
gBlock := genesis.NewGenesisBlock([]byte{})
if err := db.SaveBlock(gBlock); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
@@ -105,14 +104,14 @@ func TestComputeStateRoot_OK(t *testing.T) {
}
}
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
beaconState, err := genesis.BeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
beaconState.Slot = 10
if err := db.UpdateChainHead(ctx, genesis, beaconState); err != nil {
if err := db.UpdateChainHead(ctx, gBlock, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}

View File

@@ -162,7 +162,7 @@ func (vs *ValidatorServer) assignment(
}
for beaconState.Slot < epochStart {
beaconState, err = state.ExecuteStateTransition(
ctx, beaconState, nil /* block */, headRoot, state.DefaultConfig(),
ctx, beaconState, nil /* block */, headRoot, vs.beaconDB, state.DefaultConfig(),
)
if err != nil {
return nil, fmt.Errorf("could not execute head transition: %v", err)

View File

@@ -11,9 +11,8 @@ import (
"time"
"github.com/golang/mock/gomock"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
@@ -39,7 +38,7 @@ func genesisState(validators uint64) (*pbp2p.BeaconState, error) {
}
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
}
return state.GenesisBeaconState(deposits, uint64(genesisTime), nil)
return genesis.BeaconState(deposits, uint64(genesisTime), nil)
}
func TestValidatorIndex_OK(t *testing.T) {
@@ -108,7 +107,7 @@ func TestCommitteeAssignment_OK(t *testing.T) {
defer internal.TeardownDB(t, db)
ctx := context.Background()
genesis := b.NewGenesisBlock([]byte{})
genesis := genesis.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
@@ -190,7 +189,7 @@ func TestCommitteeAssignment_multipleKeys_OK(t *testing.T) {
defer internal.TeardownDB(t, db)
ctx := context.Background()
genesis := b.NewGenesisBlock([]byte{})
genesis := genesis.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}

View File

@@ -34,7 +34,7 @@ go_test(
srcs = ["service_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/core/blocks:go_default_library",
"//beacon-chain/core/genesis:go_default_library",
"//beacon-chain/db:go_default_library",
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",

View File

@@ -9,7 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/gogo/protobuf/proto"
peer "github.com/libp2p/go-libp2p-peer"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/genesis"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
@@ -105,7 +105,7 @@ func setUpGenesisStateAndBlock(beaconDB *db.BeaconDB, t *testing.T) {
log.Errorf("unable to marshal the beacon state: %v", err)
return
}
genBlock := b.NewGenesisBlock(stateRoot[:])
genBlock := genesis.NewGenesisBlock(stateRoot[:])
if err := beaconDB.SaveBlock(genBlock); err != nil {
t.Fatalf("could not save genesis block to disk: %v", err)
}