Skip to content

Commit

Permalink
Remove Deprecated Protobuf State Fields (#2713)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain authored May 28, 2019
1 parent 371c389 commit 6c0b926
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 810 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/fork_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,7 @@ func setupFFGTest(t *testing.T) ([32]byte, *pb.BeaconBlock, *pb.BeaconState, []*
LatestRandaoMixes: latestRandaoMixes,
LatestActiveIndexRoots: make([][]byte, params.BeaconConfig().LatestActiveIndexRootsLength),
LatestSlashedBalances: make([]uint64, params.BeaconConfig().LatestSlashedExitLength),
LatestCrosslinks: crosslinks,
CurrentCrosslinks: crosslinks,
ValidatorRegistry: validatorRegistry,
Balances: validatorBalances,
LatestBlock: gBlock,
Expand Down
3 changes: 0 additions & 3 deletions beacon-chain/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ func (c *ChainService) initializeBeaconChain(genesisTime time.Time, deposits []*
return nil, fmt.Errorf("could not hash beacon block: %v", err)
}

// TODO(#2011): Remove this in state caching.
beaconState.LatestBlock = genBlock

if err := c.beaconDB.SaveBlock(genBlock); err != nil {
return nil, fmt.Errorf("could not save genesis block to disk: %v", err)
}
Expand Down
6 changes: 0 additions & 6 deletions beacon-chain/core/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/prysmaticlabs/prysm/beacon-chain/utils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)

Expand Down Expand Up @@ -69,12 +68,7 @@ func BlockRoot(state *pb.BeaconState, slot uint64) ([]byte, error) {
// Spec:
// Let previous_block_root be the tree_hash_root of the previous beacon block processed in the chain.
// Set state.latest_block_roots[(state.slot - 1) % LATEST_BLOCK_ROOTS_LENGTH] = previous_block_root.
// If state.slot % LATEST_BLOCK_ROOTS_LENGTH == 0 append merkle_root(state.latest_block_roots) to state.batched_block_roots.
func ProcessBlockRoots(state *pb.BeaconState, parentRoot [32]byte) *pb.BeaconState {
state.LatestBlockRoots[(state.Slot-1)%params.BeaconConfig().LatestBlockRootsLength] = parentRoot[:]
if state.Slot%params.BeaconConfig().LatestBlockRootsLength == 0 {
merkleRoot := hashutil.MerkleRoot(state.LatestBlockRoots)
state.BatchedBlockRootHash32S = append(state.BatchedBlockRootHash32S, merkleRoot)
}
return state
}
15 changes: 0 additions & 15 deletions beacon-chain/core/blocks/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"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"
)

Expand Down Expand Up @@ -154,18 +153,4 @@ func TestProcessBlockRoots_AccurateMerkleTree(t *testing.T) {
t.Fatalf("Latest Block root hash not saved."+
" Supposed to get %#x , but got %#x", testRoot, newState.LatestBlockRoots[0])
}

newState.Slot = newState.Slot - 1

newState = ProcessBlockRoots(newState, testRoot)
expectedHashes := make([][]byte, params.BeaconConfig().LatestBlockRootsLength)
expectedHashes[0] = testRoot[:]
expectedHashes[params.BeaconConfig().LatestBlockRootsLength-1] = testRoot[:]

expectedRoot := hashutil.MerkleRoot(expectedHashes)

if !bytes.Equal(newState.BatchedBlockRootHash32S[0], expectedRoot[:]) {
t.Errorf("saved merkle root is not equal to expected merkle root"+
"\n expected %#x but got %#x", expectedRoot, newState.BatchedBlockRootHash32S[0])
}
}
35 changes: 14 additions & 21 deletions beacon-chain/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ func GenesisBeaconState(
latestActiveIndexRoots[i] = zeroHash
}

latestCrosslinks := make([]*pb.Crosslink, params.BeaconConfig().ShardCount)
for i := 0; i < len(latestCrosslinks); i++ {
latestCrosslinks[i] = &pb.Crosslink{
Epoch: 0,
CrosslinkDataRootHash32: zeroHash,
crosslinks := make([]*pb.Crosslink, params.BeaconConfig().ShardCount)
for i := 0; i < len(crosslinks); i++ {
crosslinks[i] = &pb.Crosslink{
Shard: uint64(i),
}
}

Expand Down Expand Up @@ -107,18 +106,11 @@ func GenesisBeaconState(
},

// Validator registry fields.
ValidatorRegistry: validatorRegistry,
Balances: latestBalances,
ValidatorRegistryUpdateEpoch: 0,
ValidatorRegistry: validatorRegistry,
Balances: latestBalances,

// Randomness and committees.
LatestRandaoMixes: latestRandaoMixes,
PreviousShufflingStartShard: params.BeaconConfig().GenesisStartShard,
CurrentShufflingStartShard: params.BeaconConfig().GenesisStartShard,
PreviousShufflingEpoch: 0,
CurrentShufflingEpoch: 0,
PreviousShufflingSeedHash32: zeroHash,
CurrentShufflingSeedHash32: zeroHash,
LatestRandaoMixes: latestRandaoMixes,

// Finality.
PreviousJustifiedEpoch: 0,
Expand All @@ -130,12 +122,13 @@ func GenesisBeaconState(
FinalizedRoot: params.BeaconConfig().ZeroHash[:],

// Recent state.
LatestCrosslinks: latestCrosslinks,
LatestActiveIndexRoots: latestActiveIndexRoots,
LatestBlockRoots: latestBlockRoots,
LatestSlashedBalances: latestSlashedExitBalances,
LatestAttestations: []*pb.PendingAttestation{},
BatchedBlockRootHash32S: [][]byte{},
CurrentCrosslinks: crosslinks,
PreviousCrosslinks: crosslinks,
LatestActiveIndexRoots: latestActiveIndexRoots,
LatestBlockRoots: latestBlockRoots,
LatestSlashedBalances: latestSlashedExitBalances,
CurrentEpochAttestations: []*pb.PendingAttestation{},
PreviousEpochAttestations: []*pb.PendingAttestation{},

// Eth1 data.
LatestEth1Data: eth1Data,
Expand Down
19 changes: 10 additions & 9 deletions beacon-chain/core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ func TestGenesisBeaconState_OK(t *testing.T) {
}

// Validator registry fields checks.
if newState.ValidatorRegistryUpdateEpoch != 0 {
t.Error("ValidatorRegistryUpdateSlot was not correctly initialized")
}
if len(newState.ValidatorRegistry) != depositsForChainStart {
t.Error("ValidatorRegistry was not correctly initialized")
}
Expand Down Expand Up @@ -134,18 +131,22 @@ func TestGenesisBeaconState_OK(t *testing.T) {
}

// Recent state checks.
if len(newState.LatestCrosslinks) != shardCount {
t.Error("Length of LatestCrosslinks was not correctly initialized")
if len(newState.CurrentCrosslinks) != shardCount {
t.Error("Length of CurrentCrosslinks was not correctly initialized")
}
if len(newState.PreviousCrosslinks) != shardCount {
t.Error("Length of PreviousCrosslinks was not correctly initialized")
}
if !reflect.DeepEqual(newState.LatestSlashedBalances, make([]uint64, params.BeaconConfig().LatestSlashedExitLength)) {
t.Error("LatestSlashedBalances was not correctly initialized")
}
if !reflect.DeepEqual(newState.LatestAttestations, []*pb.PendingAttestation{}) {
t.Error("LatestAttestations was not correctly initialized")
if !reflect.DeepEqual(newState.CurrentEpochAttestations, []*pb.PendingAttestation{}) {
t.Error("CurrentEpochAttestations was not correctly initialized")
}
if !reflect.DeepEqual(newState.BatchedBlockRootHash32S, [][]byte{}) {
t.Error("BatchedBlockRootHash32S was not correctly initialized")
if !reflect.DeepEqual(newState.PreviousEpochAttestations, []*pb.PendingAttestation{}) {
t.Error("PreviousEpochAttestations was not correctly initialized")
}

activeValidators := helpers.ActiveValidatorIndices(newState, 0)
indicesBytes := []byte{}
for _, val := range activeValidators {
Expand Down
3 changes: 0 additions & 3 deletions beacon-chain/core/state/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ func ProcessBlock(
return nil, fmt.Errorf("could not hash block: %v", err)
}

// Save latest block.
state.LatestBlock = block

// Verify block RANDAO.
state, err = b.ProcessRandao(state, block.Body, config.VerifySignatures, config.Logging)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/state/transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func TestProcessBlock_IncorrectProcessExits(t *testing.T) {
blockRoots = append(blockRoots, []byte{byte(i)})
}
beaconState.LatestBlockRoots = blockRoots
beaconState.LatestCrosslinks = []*pb.Crosslink{
beaconState.CurrentCrosslinks = []*pb.Crosslink{
{
CrosslinkDataRootHash32: []byte{1},
DataRoot: []byte{1},
},
}
blockAtt := &pb.Attestation{
Expand Down Expand Up @@ -376,9 +376,9 @@ func TestProcessBlock_PassesProcessingConditions(t *testing.T) {
blockRoots = append(blockRoots, []byte{byte(i)})
}
beaconState.LatestBlockRoots = blockRoots
beaconState.LatestCrosslinks = []*pb.Crosslink{
beaconState.CurrentCrosslinks = []*pb.Crosslink{
{
CrosslinkDataRootHash32: []byte{1},
DataRoot: []byte{1},
},
}
slotsPerEpoch := params.BeaconConfig().SlotsPerEpoch
Expand Down
24 changes: 12 additions & 12 deletions beacon-chain/operations/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func TestRetrieveAttestations_OK(t *testing.T) {
if err := beaconDB.SaveState(context.Background(), &pb.BeaconState{
Slot: 64,
LatestBlock: &pb.BeaconBlock{Slot: 0},
LatestCrosslinks: []*pb.Crosslink{{
Epoch: 0,
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
CurrentCrosslinks: []*pb.Crosslink{{
Epoch: 0,
DataRoot: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
t.Fatal(err)
}
// Test we can retrieve attestations from slot1 - slot61.
Expand Down Expand Up @@ -176,9 +176,9 @@ func TestRetrieveAttestations_PruneInvalidAtts(t *testing.T) {
// At slot 200 only attestations up to from slot 137 to 139 are valid attestations.
if err := beaconDB.SaveState(context.Background(), &pb.BeaconState{
Slot: 200,
LatestCrosslinks: []*pb.Crosslink{{
Epoch: 2,
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
CurrentCrosslinks: []*pb.Crosslink{{
Epoch: 2,
DataRoot: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
t.Fatal(err)
}
attestations, err := service.PendingAttestations(context.Background())
Expand Down Expand Up @@ -218,9 +218,9 @@ func TestRemoveProcessedAttestations_Ok(t *testing.T) {
}
if err := db.SaveState(context.Background(), &pb.BeaconState{
Slot: 15,
LatestCrosslinks: []*pb.Crosslink{{
Epoch: 0,
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
CurrentCrosslinks: []*pb.Crosslink{{
Epoch: 0,
DataRoot: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -298,9 +298,9 @@ func TestReceiveBlkRemoveOps_Ok(t *testing.T) {

if err := db.SaveState(context.Background(), &pb.BeaconState{
Slot: 15,
LatestCrosslinks: []*pb.Crosslink{{
Epoch: 0,
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
CurrentCrosslinks: []*pb.Crosslink{{
Epoch: 0,
DataRoot: params.BeaconConfig().ZeroHash[:]}}}); err != nil {
t.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/attester_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (as *AttesterServer) AttestationDataAtSlot(ctx context.Context, req *pb.Att
EpochBoundaryRootHash32: epochBoundaryRoot,
JustifiedEpoch: headState.CurrentJustifiedEpoch,
JustifiedBlockRootHash32: justifiedBlockRoot,
LatestCrosslink: headState.LatestCrosslinks[req.Shard],
LatestCrosslink: headState.CurrentCrosslinks[req.Shard],
}
if err := as.cache.Put(ctx, req, res); err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/rpc/attester_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func TestAttestationDataAtSlot_OK(t *testing.T) {
Slot: 3*params.BeaconConfig().SlotsPerEpoch + 1,
CurrentJustifiedEpoch: 2 + 0,
LatestBlockRoots: make([][]byte, params.BeaconConfig().LatestBlockRootsLength),
LatestCrosslinks: []*pbp2p.Crosslink{
CurrentCrosslinks: []*pbp2p.Crosslink{
{
CrosslinkDataRootHash32: []byte("A"),
DataRoot: []byte("A"),
},
},
CurrentJustifiedRoot: justifiedBlockRoot[:],
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestAttestationDataAtSlot_OK(t *testing.T) {
JustifiedEpoch: 2 + 0,
JustifiedBlockRootHash32: justifiedBlockRoot[:],
LatestCrosslink: &pbp2p.Crosslink{
CrosslinkDataRootHash32: []byte("A"),
DataRoot: []byte("A"),
},
}

Expand Down Expand Up @@ -181,9 +181,9 @@ func TestAttestationDataAtSlot_handlesFarAwayJustifiedEpoch(t *testing.T) {
Slot: 10000,
CurrentJustifiedEpoch: helpers.SlotToEpoch(1500),
LatestBlockRoots: make([][]byte, params.BeaconConfig().LatestBlockRootsLength),
LatestCrosslinks: []*pbp2p.Crosslink{
CurrentCrosslinks: []*pbp2p.Crosslink{
{
CrosslinkDataRootHash32: []byte("A"),
DataRoot: []byte("A"),
},
},
CurrentJustifiedRoot: justifiedBlockRoot[:],
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestAttestationDataAtSlot_handlesFarAwayJustifiedEpoch(t *testing.T) {
JustifiedEpoch: helpers.SlotToEpoch(1500),
JustifiedBlockRootHash32: justifiedBlockRoot[:],
LatestCrosslink: &pbp2p.Crosslink{
CrosslinkDataRootHash32: []byte("A"),
DataRoot: []byte("A"),
},
}

Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/rpc/proposer_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestPendingAttestations_FiltersWithinInclusionDelay(t *testing.T) {
beaconState := &pbp2p.BeaconState{
Slot: stateSlot,
ValidatorRegistry: validators,
LatestCrosslinks: []*pbp2p.Crosslink{{
CurrentCrosslinks: []*pbp2p.Crosslink{{
Epoch: 1,
DataRoot: params.BeaconConfig().ZeroHash[:],
}},
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestPendingAttestations_FiltersExpiredAttestations(t *testing.T) {
) - 1

expectedEpoch := uint64(100)
crosslink := &pbp2p.Crosslink{Epoch: 9, CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:]}
crosslink := &pbp2p.Crosslink{Epoch: 9, DataRoot: params.BeaconConfig().ZeroHash[:]}
encoded, err := ssz.TreeHash(crosslink)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -315,8 +315,8 @@ func TestPendingAttestations_FiltersExpiredAttestations(t *testing.T) {
CurrentJustifiedEpoch: expectedEpoch,
PreviousJustifiedEpoch: expectedEpoch,
CurrentCrosslinks: []*pbp2p.Crosslink{{
Epoch: 9,
CrosslinkDataRootHash32: params.BeaconConfig().ZeroHash[:],
Epoch: 9,
DataRoot: params.BeaconConfig().ZeroHash[:],
}},
LatestRandaoMixes: make([][]byte, params.BeaconConfig().LatestRandaoMixesLength),
LatestActiveIndexRoots: make([][]byte, params.BeaconConfig().LatestActiveIndexRootsLength),
Expand Down
3 changes: 1 addition & 2 deletions proto/beacon/p2p/v1/messages.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6c0b926

Please sign in to comment.