Skip to content

Commit

Permalink
Removed Deprecated BeaconBlock Fields (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain authored Dec 3, 2018
1 parent 84259fc commit 121654f
Show file tree
Hide file tree
Showing 21 changed files with 443 additions and 464 deletions.
4 changes: 2 additions & 2 deletions beacon-chain/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *ChainService) CanonicalStateFeed() *event.Feed {

// doesPoWBlockExist checks if the referenced PoW block exists.
func (c *ChainService) doesPoWBlockExist(block *types.Block) bool {
powBlock, err := c.web3Service.Client().BlockByHash(context.Background(), block.PowChainRef())
powBlock, err := c.web3Service.Client().BlockByHash(context.Background(), block.CandidatePowReceiptRootHash32())
if err != nil {
log.Debugf("fetching PoW block corresponding to mainchain reference failed: %v", err)
return false
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *ChainService) updateHead(processedBlock <-chan *types.Block) {
newHead := currentHead
// If both blocks have the same crystallized state root, we favor one which has
// the higher slot.
if currentHead.StateRoot() == block.StateRoot() {
if currentHead.StateRootHash32() == block.StateRootHash32() {
if block.SlotNumber() > currentHead.SlotNumber() {
newHead = block
headUpdated = true
Expand Down
39 changes: 19 additions & 20 deletions beacon-chain/blockchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"

"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
)
Expand Down Expand Up @@ -142,8 +141,8 @@ func TestRunningChainServiceFaultyPOWChain(t *testing.T) {
chainService := setupBeaconChain(t, true, db)

block := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
PowChainRef: []byte("a"),
Slot: 1,
CandidatePowReceiptRootHash32: []byte("a"),
})

blockChan := make(chan *types.Block)
Expand Down Expand Up @@ -190,10 +189,10 @@ func TestRunningChainService(t *testing.T) {
shard := beaconState.ShardAndCommitteesForSlots()[attestationSlot].ArrayShardAndCommittee[0].Shard

block := types.NewBlock(&pb.BeaconBlock{
Slot: currentSlot,
StateRoot: stateRoot[:],
AncestorHashes: [][]byte{parentHash[:]},
PowChainRef: []byte("a"),
Slot: currentSlot,
StateRootHash32: stateRoot[:],
AncestorHash32S: [][]byte{parentHash[:]},
CandidatePowReceiptRootHash32: []byte("a"),
Attestations: []*pb.AggregatedAttestation{{
Slot: attestationSlot,
AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -278,9 +277,9 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) {
attestationSlot := currentSlot - 1

block1 := types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{block0Hash[:]},
Slot: currentSlot,
StateRoot: stateRoot[:],
AncestorHash32S: [][]byte{block0Hash[:]},
Slot: currentSlot,
StateRootHash32: stateRoot[:],
Attestations: []*pb.AggregatedAttestation{{
Slot: attestationSlot,
AttesterBitfield: []byte{128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Expand Down Expand Up @@ -313,8 +312,8 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) {

// Add 1 more attestation field for slot2
block2 := types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{block1Hash[:]},
Slot: currentSlot,
AncestorHash32S: [][]byte{block1Hash[:]},
Slot: currentSlot,
Attestations: []*pb.AggregatedAttestation{
{
Slot: currentSlot - 1,
Expand All @@ -338,8 +337,8 @@ func TestProcessBlocksWithCorrectAttestations(t *testing.T) {

// Add 1 more attestation field for slot3
block3 := types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{block2Hash[:]},
Slot: currentSlot,
AncestorHash32S: [][]byte{block2Hash[:]},
Slot: currentSlot,
Attestations: []*pb.AggregatedAttestation{
{
Slot: currentSlot - 1,
Expand Down Expand Up @@ -427,10 +426,10 @@ func TestUpdateHead(t *testing.T) {

stateRoot, _ := tt.state.Hash()
block := types.NewBlock(&pb.BeaconBlock{
Slot: tt.blockSlot,
StateRoot: stateRoot[:],
AncestorHashes: [][]byte{genesisHash[:]},
PowChainRef: []byte("a"),
Slot: tt.blockSlot,
StateRootHash32: stateRoot[:],
AncestorHash32S: [][]byte{genesisHash[:]},
CandidatePowReceiptRootHash32: []byte("a"),
})
h, err := block.Hash()
if err != nil {
Expand Down Expand Up @@ -468,8 +467,8 @@ func TestUpdateBlockVoteCache(t *testing.T) {
t.Fatalf("failed to initialize genesis state: %v", err)
}
block := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
AncestorHashes: [][]byte{},
Slot: 1,
AncestorHash32S: [][]byte{},
Attestations: []*pb.AggregatedAttestation{
{
Slot: 0,
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/state/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewStateTransition(
return nil, fmt.Errorf("failed to update recent block hashes: %v", err)
}

newRandao := createRandaoMix(block.RandaoReveal(), st.RandaoMix())
newRandao := createRandaoMix(block.RandaoRevealHash32(), st.RandaoMix())
newState.SetRandaoMix(newRandao[:])

// The changes below are only applied if this is a cycle transition.
Expand Down
18 changes: 9 additions & 9 deletions beacon-chain/core/state/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestInitialDeriveState(t *testing.T) {
}

block := types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{{'A'}},
Slot: 0,
StateRoot: []byte{},
AncestorHash32S: [][]byte{{'A'}},
Slot: 0,
StateRootHash32: []byte{},
Attestations: []*pb.AggregatedAttestation{{
Slot: 0,
AttesterBitfield: attesterBitfield,
Expand Down Expand Up @@ -64,8 +64,8 @@ func TestNextDeriveSlot(t *testing.T) {
}

block := types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{{'A'}},
Slot: 0,
AncestorHash32S: [][]byte{{'A'}},
Slot: 0,
})

blockVoteCache := utils.NewBlockVoteCache()
Expand Down Expand Up @@ -93,8 +93,8 @@ func TestNextDeriveSlot(t *testing.T) {
beaconState.SetLatestBlockHashes(recentShardBlockHashes)
beaconState.SetLastStateRecalculationSlot(params.BeaconConfig().CycleLength - 1)
block = types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{{'A'}},
Slot: params.BeaconConfig().CycleLength,
AncestorHash32S: [][]byte{{'A'}},
Slot: params.BeaconConfig().CycleLength,
})
beaconState, err = NewStateTransition(beaconState, block, params.BeaconConfig().CycleLength, blockVoteCache)
if err != nil {
Expand All @@ -116,8 +116,8 @@ func TestNextDeriveSlot(t *testing.T) {
beaconState.SetLatestBlockHashes(recentShardBlockHashes)
beaconState.SetLastStateRecalculationSlot(2*params.BeaconConfig().CycleLength - 1)
block = types.NewBlock(&pb.BeaconBlock{
AncestorHashes: [][]byte{{'A'}},
Slot: params.BeaconConfig().CycleLength * 2,
AncestorHash32S: [][]byte{{'A'}},
Slot: params.BeaconConfig().CycleLength * 2,
})
beaconState, err = NewStateTransition(beaconState, block, params.BeaconConfig().CycleLength*2, blockVoteCache)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/state/validity_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ func IsValidBlock(
}

stateProposerRandaoSeed := beaconState.Validators()[proposerIndex].RandaoCommitmentHash32
blockRandaoReveal := block.RandaoReveal()
blockRandaoRevealHash32 := block.RandaoRevealHash32()

// If this is a block created by the simulator service (while in development
// mode), we skip the RANDAO validation condition.
isSimulatedBlock := bytes.Equal(blockRandaoReveal[:], params.BeaconConfig().SimulatedBlockRandao[:])
isSimulatedBlock := bytes.Equal(blockRandaoRevealHash32[:], params.BeaconConfig().SimulatedBlockRandao[:])
if !isSimulatedBlock && !block.IsRandaoValid(stateProposerRandaoSeed) {
return fmt.Errorf(
"pre-image of %#x is %#x, Got: %#x",
blockRandaoReveal[:],
hashutil.Hash(blockRandaoReveal[:]),
blockRandaoRevealHash32[:],
hashutil.Hash(blockRandaoRevealHash32[:]),
stateProposerRandaoSeed,
)
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/core/state/validity_conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestBlockValidity(t *testing.T) {
beaconState.SetLatestBlockHashes(recentBlockHashes)

b := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
RandaoReveal: randaoPreCommit[:],
Slot: 1,
RandaoRevealHash32: randaoPreCommit[:],
Attestations: []*pb.AggregatedAttestation{
{
Slot: 0,
Expand Down Expand Up @@ -89,8 +89,8 @@ func TestBlockValidityNoParentProposer(t *testing.T) {

// Test case with invalid RANDAO reveal.
badRandaoBlock := types.NewBlock(&pb.BeaconBlock{
Slot: 2,
RandaoReveal: []byte{'B'},
Slot: 2,
RandaoRevealHash32: []byte{'B'},
Attestations: []*pb.AggregatedAttestation{
{
Slot: 0,
Expand Down Expand Up @@ -124,8 +124,8 @@ func TestBlockValidityInvalidRandao(t *testing.T) {

// Test case with invalid RANDAO reveal.
badRandaoBlock := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
RandaoReveal: []byte{'B'},
Slot: 1,
RandaoRevealHash32: []byte{'B'},
Attestations: []*pb.AggregatedAttestation{
{
Slot: 0,
Expand Down
42 changes: 21 additions & 21 deletions beacon-chain/core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func NewBlock(data *pb.BeaconBlock) *Block {
// It is assumed when data==nil, a genesis block will be returned.
return &Block{
data: &pb.BeaconBlock{
AncestorHashes: ancestorHashes,
RandaoReveal: []byte{0},
PowChainRef: []byte{0},
StateRoot: []byte{0},
Specials: []*pb.SpecialRecord{},
AncestorHash32S: ancestorHashes,
RandaoRevealHash32: []byte{0},
CandidatePowReceiptRootHash32: []byte{0},
StateRootHash32: []byte{0},
Specials: []*pb.SpecialRecord{},
},
}
}
Expand All @@ -47,7 +47,7 @@ func NewGenesisBlock(stateRoot [32]byte) *Block {
protoGenesis, _ := ptypes.TimestampProto(params.BeaconConfig().GenesisTime)
gb := NewBlock(nil)
gb.data.Timestamp = protoGenesis
gb.data.StateRoot = stateRoot[:]
gb.data.StateRootHash32 = stateRoot[:]
return gb
}

Expand All @@ -59,7 +59,7 @@ func (b *Block) SlotNumber() uint64 {
// ParentHash corresponding to parent beacon block.
func (b *Block) ParentHash() [32]byte {
var h [32]byte
copy(h[:], b.data.AncestorHashes[0])
copy(h[:], b.data.AncestorHash32S[0])
return h
}

Expand Down Expand Up @@ -87,9 +87,9 @@ func (b *Block) Timestamp() (time.Time, error) {
return ptypes.Timestamp(b.data.Timestamp)
}

// AncestorHashes of the block.
func (b *Block) AncestorHashes() [][]byte {
return b.data.AncestorHashes
// AncestorHash32S of the block.
func (b *Block) AncestorHash32S() [][]byte {
return b.data.AncestorHash32S
}

// AttestationCount returns the number of attestations.
Expand All @@ -102,22 +102,22 @@ func (b *Block) Attestations() []*pb.AggregatedAttestation {
return b.data.Attestations
}

// PowChainRef returns a keccak256 hash corresponding to a PoW chain block.
func (b *Block) PowChainRef() common.Hash {
return common.BytesToHash(b.data.PowChainRef)
// CandidatePowReceiptRootHash32 returns a keccak256 hash corresponding to a PoW chain block.
func (b *Block) CandidatePowReceiptRootHash32() common.Hash {
return common.BytesToHash(b.data.CandidatePowReceiptRootHash32)
}

// RandaoReveal returns the blake2b randao hash.
func (b *Block) RandaoReveal() [32]byte {
// RandaoRevealHash32 returns the blake2b randao hash.
func (b *Block) RandaoRevealHash32() [32]byte {
var h [32]byte
copy(h[:], b.data.RandaoReveal)
copy(h[:], b.data.RandaoRevealHash32)
return h
}

// StateRoot returns the state hash.
func (b *Block) StateRoot() [32]byte {
// StateRootHash32 returns the state hash.
func (b *Block) StateRootHash32() [32]byte {
var h [32]byte
copy(h[:], b.data.StateRoot)
copy(h[:], b.data.StateRootHash32)
return h
}

Expand All @@ -126,8 +126,8 @@ func (b *Block) StateRoot() [32]byte {
func (b *Block) IsRandaoValid(stateRandao []byte) bool {
var h [32]byte
copy(h[:], stateRandao)
blockRandaoReveal := b.RandaoReveal()
return hashutil.Hash(blockRandaoReveal[:]) == h
blockRandaoRevealHash32 := b.RandaoRevealHash32()
return hashutil.Hash(blockRandaoRevealHash32[:]) == h
}

// IsSlotValid compares the slot to the system clock to determine if the block is valid.
Expand Down
14 changes: 7 additions & 7 deletions beacon-chain/core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ func TestGenesisBlock(t *testing.T) {
t.Errorf("genesis block hash should be identical: %#x %#x", h1, h2)
}

if b1.data.AncestorHashes == nil {
if b1.data.AncestorHash32S == nil {
t.Error("genesis block missing ParentHash field")
}

if b1.AttestationCount() > 0 || b1.Attestations() != nil {
t.Errorf("genesis block should have 0 attestations")
}

if b1.RandaoReveal() != [32]byte{} {
t.Error("genesis block missing RandaoReveal field")
if b1.RandaoRevealHash32() != [32]byte{} {
t.Error("genesis block missing RandaoRevealHash32 field")
}

if b1.PowChainRef() != common.BytesToHash([]byte{}) {
t.Error("genesis block missing PowChainRef field")
if b1.CandidatePowReceiptRootHash32() != common.BytesToHash([]byte{}) {
t.Error("genesis block missing CandidatePowReceiptRootHash32 field")
}

if b1.StateRoot() != stateHash {
t.Error("genesis block StateRoot isn't initialized correctly")
if b1.StateRootHash32() != stateHash {
t.Error("genesis block StateRootHash32 isn't initialized correctly")
}

rd := [32]byte{}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (b *BeaconState) CalculateNewBlockHashes(block *Block, parentSlot uint64) (
existing := b.data.LatestBlockHash32S
update := existing[distance:]
for len(update) < 2*int(params.BeaconConfig().CycleLength) {
update = append(update, block.AncestorHashes()[0])
update = append(update, block.AncestorHash32S()[0])
}
return update, nil
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/core/types/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func TestUpdateAttestationsAfterRecalc(t *testing.T) {

func TestUpdateLatestBlockHashes(t *testing.T) {
block := NewBlock(&pb.BeaconBlock{
Slot: 10,
AncestorHashes: [][]byte{{'A'}},
Slot: 10,
AncestorHash32S: [][]byte{{'A'}},
})

recentBlockHashes := [][]byte{}
Expand All @@ -152,8 +152,8 @@ func TestUpdateLatestBlockHashes(t *testing.T) {
if !areBytesEqual(updated[i], []byte{0}) {
t.Fatalf("update failed: expected %#x got %#x", []byte{0}, updated[i])
}
} else if !areBytesEqual(updated[i], block.data.AncestorHashes[0]) {
t.Fatalf("update failed: expected %#x got %#x", block.data.AncestorHashes[:], updated[i])
} else if !areBytesEqual(updated[i], block.data.AncestorHash32S[0]) {
t.Fatalf("update failed: expected %#x got %#x", block.data.AncestorHash32S[:], updated[i])
}
}
}
Expand All @@ -177,8 +177,8 @@ func TestCalculateNewBlockHashes_DoesNotMutateData(t *testing.T) {

block := &Block{
data: &pb.BeaconBlock{
Slot: 2,
AncestorHashes: [][]byte{{}},
Slot: 2,
AncestorHash32S: [][]byte{{}},
},
}

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/db/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func TestUpdateChainHead(t *testing.T) {
}

b2 := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
AncestorHashes: [][]byte{bHash[:]},
Slot: 1,
AncestorHash32S: [][]byte{bHash[:]},
})
b2Hash, err := b2.Hash()
if err != nil {
Expand Down
Loading

0 comments on commit 121654f

Please sign in to comment.