Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part 1 of Cleaning Up State Proto #1070

Merged
merged 10 commits into from
Dec 11, 2018
6 changes: 3 additions & 3 deletions beacon-chain/blockchain/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,16 @@ func TestUpdateHead(t *testing.T) {
// Higher slot, different crystallized state, but higher last finalized slot.
{
blockSlot: 64,
state: types.NewBeaconState(&pb.BeaconState{LastFinalizedSlot: 10}),
state: types.NewBeaconState(&pb.BeaconState{FinalizedSlot: 10}),
logAssert: "Chain head block and state updated",
},
// Higher slot, different crystallized state, same last finalized slot,
// but last justified slot.
{
blockSlot: 64,
state: types.NewBeaconState(&pb.BeaconState{
LastFinalizedSlot: 0,
LastJustifiedSlot: 10,
FinalizedSlot: 0,
JustifiedSlot: 10,
}),
logAssert: "Chain head block and state updated",
},
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/incentives/incentives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestComputeValidatorRewardsAndPenalties(t *testing.T) {
data := &pb.BeaconState{
ValidatorRegistry: validators,
ValidatorRegistryLastChangeSlot: 1,
LastJustifiedSlot: 4,
LastFinalizedSlot: 3,
JustifiedSlot: 4,
FinalizedSlot: 3,
}

activeValidatorIndices := make([]uint32, 0, len(validators))
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/state/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func UpdateLatestCrosslinks(slot uint64, voteBalance uint64, totalBalance uint64
voteMajority := 3*voteBalance >= 2*totalBalance
if voteMajority {
crosslinkRecords[attestation.Shard] = &pb.CrosslinkRecord{
ShardBlockHash: attestation.ShardBlockHash,
Slot: slot,
ShardBlockHash32: attestation.ShardBlockHash,
Slot: slot,
}
}
return crosslinkRecords
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/core/state/processing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func TestLatestCrosslinks(t *testing.T) {

crossLinks := []*pb.CrosslinkRecord{
{
ShardBlockHash: []byte{'A'},
Slot: 10,
ShardBlockHash32: []byte{'A'},
Slot: 10,
},
{
ShardBlockHash: []byte{'A'},
Slot: 10,
ShardBlockHash32: []byte{'A'},
Slot: 10,
},
}

Expand All @@ -94,8 +94,8 @@ func TestLatestCrosslinks(t *testing.T) {
crossLinks = UpdateLatestCrosslinks(10, voteBalance, totalBalance, attestation, crossLinks)
crossLinks = UpdateLatestCrosslinks(10, voteBalance, totalBalance, attestation, crossLinks)

if !bytes.Equal(crossLinks[1].GetShardBlockHash(), []byte{'B'}) {
t.Errorf("shard blockhash not saved in crosslink record %v", crossLinks[1].GetShardBlockHash())
if !bytes.Equal(crossLinks[1].GetShardBlockHash32(), []byte{'B'}) {
t.Errorf("shard blockhash not saved in crosslink record %v", crossLinks[1].GetShardBlockHash32())
}

}
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/state/state_transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestProcessLatestCrosslinks(t *testing.T) {
// Set up crosslink record for every shard.
var clRecords []*pb.CrosslinkRecord
for i := uint64(0); i < params.BeaconConfig().ShardCount; i++ {
clRecord := &pb.CrosslinkRecord{ShardBlockHash: []byte{'A'}, Slot: 1}
clRecord := &pb.CrosslinkRecord{ShardBlockHash32: []byte{'A'}, Slot: 1}
clRecords = append(clRecords, clRecord)
}

Expand Down Expand Up @@ -188,8 +188,8 @@ func TestProcessLatestCrosslinks(t *testing.T) {
if newLatestCrosslinks[1].Slot != params.BeaconConfig().CycleLength {
t.Errorf("Slot did not change for new cross link. Wanted: %d. Got: %d", params.BeaconConfig().CycleLength, newLatestCrosslinks[0].Slot)
}
if !bytes.Equal(newLatestCrosslinks[1].ShardBlockHash, []byte{'a'}) {
t.Errorf("ShardBlockHash did not change for new cross link. Wanted a. Got: %s", newLatestCrosslinks[0].ShardBlockHash)
if !bytes.Equal(newLatestCrosslinks[1].ShardBlockHash32, []byte{'a'}) {
t.Errorf("ShardBlockHash did not change for new cross link. Wanted a. Got: %s", newLatestCrosslinks[0].ShardBlockHash32)
}
//TODO(#538) Implement tests on balances of the validators in committee once big.Int is introduced.
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestPenalizedETH(t *testing.T) {
if err != nil {
t.Fatalf("Failed to initialize state: %v", err)
}
beaconState.SetDepositsPenalizedInPeriod([]uint64{100, 200, 300, 400, 500})
beaconState.SetLatestPenalizedExitBalances([]uint64{100, 200, 300, 400, 500})
beaconState.PenalizedETH(2)

tests := []struct {
Expand Down
46 changes: 23 additions & 23 deletions beacon-chain/core/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewGenesisBeaconState(genesisValidatorRegistry []*pb.ValidatorRecord) (*Bea
var crosslinks []*pb.CrosslinkRecord
for i := uint64(0); i < params.BeaconConfig().ShardCount; i++ {
crosslinks = append(crosslinks, &pb.CrosslinkRecord{
ShardBlockHash: make([]byte, 0, 32),
Slot: 0,
ShardBlockHash32: make([]byte, 0, 32),
Slot: 0,
})
}

Expand All @@ -56,15 +56,15 @@ func NewGenesisBeaconState(genesisValidatorRegistry []*pb.ValidatorRecord) (*Bea
data: &pb.BeaconState{
LastStateRecalculationSlot: 0,
JustifiedStreak: 0,
LastJustifiedSlot: 0,
LastFinalizedSlot: 0,
JustifiedSlot: 0,
FinalizedSlot: 0,
ValidatorRegistryLastChangeSlot: 0,
LatestCrosslinks: crosslinks,
ValidatorRegistry: genesisValidatorRegistry,
ShardAndCommitteesForSlots: shardAndCommitteesForSlots,
PendingAttestations: []*pb.AggregatedAttestation{},
LatestBlockHash32S: latestBlockHashes,
RandaoMix: make([]byte, 0, 32),
RandaoMixHash32: make([]byte, 0, 32),
ForkData: &pb.ForkData{
PreForkVersion: params.BeaconConfig().InitialForkVersion,
PostForkVersion: params.BeaconConfig().InitialForkVersion,
Expand All @@ -79,8 +79,8 @@ func (b *BeaconState) CopyState() *BeaconState {
crosslinks := make([]*pb.CrosslinkRecord, len(b.LatestCrosslinks()))
for index, crossLink := range b.LatestCrosslinks() {
crosslinks[index] = &pb.CrosslinkRecord{
ShardBlockHash: crossLink.GetShardBlockHash(),
Slot: crossLink.GetSlot(),
ShardBlockHash32: crossLink.GetShardBlockHash32(),
Slot: crossLink.GetSlot(),
}
}

Expand Down Expand Up @@ -112,13 +112,13 @@ func (b *BeaconState) CopyState() *BeaconState {
newState := BeaconState{&pb.BeaconState{
LastStateRecalculationSlot: b.LastStateRecalculationSlot(),
JustifiedStreak: b.JustifiedStreak(),
LastJustifiedSlot: b.LastJustifiedSlot(),
LastFinalizedSlot: b.LastFinalizedSlot(),
JustifiedSlot: b.LastJustifiedSlot(),
FinalizedSlot: b.LastFinalizedSlot(),
ValidatorRegistryLastChangeSlot: b.ValidatorRegistryLastChangeSlot(),
LatestCrosslinks: crosslinks,
ValidatorRegistry: validators,
ShardAndCommitteesForSlots: shardAndCommitteesForSlots,
DepositsPenalizedInPeriod: b.DepositsPenalizedInPeriod(),
LatestPenalizedExitBalances: b.LatestPenalizedExitBalances(),
ForkData: b.ForkData(),
}}

Expand Down Expand Up @@ -204,22 +204,22 @@ func (b *BeaconState) LastStateRecalculationSlot() uint64 {

// LastFinalizedSlot returns the last finalized Slot of the beacon chain.
func (b *BeaconState) LastFinalizedSlot() uint64 {
return b.data.LastFinalizedSlot
return b.data.FinalizedSlot
}

// LastJustifiedSlot return the last justified slot of the beacon chain.
func (b *BeaconState) LastJustifiedSlot() uint64 {
return b.data.LastJustifiedSlot
return b.data.JustifiedSlot
}

// JustifiedStreak returns number of consecutive justified slots ending at head.
func (b *BeaconState) JustifiedStreak() uint64 {
return b.data.JustifiedStreak
}

// DepositsPenalizedInPeriod returns total deposits penalized in the given withdrawal period.
func (b *BeaconState) DepositsPenalizedInPeriod() []uint64 {
return b.data.DepositsPenalizedInPeriod
// LatestPenalizedExitBalances returns total deposits penalized of the latest period.
func (b *BeaconState) LatestPenalizedExitBalances() []uint64 {
return b.data.LatestPenalizedExitBalances
}

// ForkData returns the relevant fork data for this beacon state.
Expand All @@ -244,14 +244,14 @@ func (b *BeaconState) PendingAttestations() []*pb.AggregatedAttestation {
// RandaoMix tracks the current RANDAO state.
func (b *BeaconState) RandaoMix() [32]byte {
var h [32]byte
copy(h[:], b.data.RandaoMix)
copy(h[:], b.data.RandaoMixHash32)
return h
}

// PenalizedETH calculates penalized total ETH during the last 3 withdrawal periods.
func (b *BeaconState) PenalizedETH(period uint64) uint64 {
var totalPenalty uint64
penalties := b.DepositsPenalizedInPeriod()
penalties := b.LatestPenalizedExitBalances()
totalPenalty += getPenaltyForPeriod(penalties, period)

if period >= 1 {
Expand Down Expand Up @@ -340,19 +340,19 @@ func (b *BeaconState) SetCrossLinks(crossLinks []*pb.CrosslinkRecord) {
b.data.LatestCrosslinks = crossLinks
}

// SetDepositsPenalizedInPeriod updates the inner proto's penalized deposits.
func (b *BeaconState) SetDepositsPenalizedInPeriod(penalizedDeposits []uint64) {
b.data.DepositsPenalizedInPeriod = penalizedDeposits
// SetLatestPenalizedExitBalances updates the inner proto's penalized deposits.
func (b *BeaconState) SetLatestPenalizedExitBalances(penalizedDeposits []uint64) {
b.data.LatestPenalizedExitBalances = penalizedDeposits
}

// SetLastJustifiedSlot updates the inner proto's last justified slot.
func (b *BeaconState) SetLastJustifiedSlot(justifiedSlot uint64) {
b.data.LastJustifiedSlot = justifiedSlot
b.data.JustifiedSlot = justifiedSlot
}

// SetLastFinalizedSlot updates the inner proto's last finalized slot.
func (b *BeaconState) SetLastFinalizedSlot(finalizedSlot uint64) {
b.data.LastFinalizedSlot = finalizedSlot
b.data.FinalizedSlot = finalizedSlot
}

// SetJustifiedStreak updates the inner proto's justified streak.
Expand All @@ -372,7 +372,7 @@ func (b *BeaconState) SetPendingAttestations(pendingAttestations []*pb.Aggregate

// SetRandaoMix updates the inner proto's randao mix.
func (b *BeaconState) SetRandaoMix(randaoMix []byte) {
b.data.RandaoMix = randaoMix
b.data.RandaoMixHash32 = randaoMix
}

// SetLatestBlockHashes updates the inner proto's recent block hashes.
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/types/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func TestCopyState(t *testing.T) {
)
}

state1.data.RandaoMix = []byte{22, 21}
state2.data.RandaoMix = []byte{40, 31}
if state1.data.RandaoMix[0] == state2.data.RandaoMix[0] {
state1.data.RandaoMixHash32 = []byte{22, 21}
state2.data.RandaoMixHash32 = []byte{40, 31}
if state1.data.RandaoMixHash32[0] == state2.data.RandaoMixHash32[0] {
t.Fatalf("The RandaoMix should not equal each other %d, %d",
state1.data.RandaoMix[0],
state2.data.RandaoMix[0],
state1.data.RandaoMixHash32[0],
state2.data.RandaoMixHash32[0],
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/dbcleanup/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestCleanBlockVoteCache(t *testing.T) {

// Now let the cleanup service do its job
cleanupService := createCleanupService(beaconDB)
state := types.NewBeaconState(&pb.BeaconState{LastFinalizedSlot: 1})
state := types.NewBeaconState(&pb.BeaconState{FinalizedSlot: 1})
if err = cleanupService.cleanBlockVoteCache(state.LastFinalizedSlot()); err != nil {
t.Fatalf("failed to clean block vote cache")
}
Expand Down
14 changes: 7 additions & 7 deletions beacon-chain/sync/initial-sync/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ func TestSavingBlocksInSync(t *testing.T) {
genericHash[0] = 'a'

beaconState := &pb.BeaconState{
LastFinalizedSlot: 99,
FinalizedSlot: 99,
}

stateResponse := &pb.BeaconStateResponse{
BeaconState: beaconState,
}

incorrectState := &pb.BeaconState{
LastFinalizedSlot: 9,
LastJustifiedSlot: 20,
FinalizedSlot: 9,
JustifiedSlot: 20,
}

incorrectStateResponse := &pb.BeaconStateResponse{
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestSavingBlocksInSync(t *testing.T) {
ss.blockBuf <- msg1
ss.stateBuf <- msg2

if ss.currentSlot == incorrectStateResponse.BeaconState.LastFinalizedSlot {
if ss.currentSlot == incorrectStateResponse.BeaconState.FinalizedSlot {
t.Fatalf("Crystallized state updated incorrectly: %d", ss.currentSlot)
}

Expand All @@ -196,8 +196,8 @@ func TestSavingBlocksInSync(t *testing.T) {
msg1 = getBlockResponseMsg(30)
ss.blockBuf <- msg1

if stateResponse.BeaconState.GetLastFinalizedSlot() != ss.currentSlot {
t.Fatalf("Slot saved when it was not supposed too: %v", stateResponse.BeaconState.GetLastFinalizedSlot())
if stateResponse.BeaconState.GetFinalizedSlot() != ss.currentSlot {
t.Fatalf("Slot saved when it was not supposed too: %v", stateResponse.BeaconState.GetFinalizedSlot())
}

msg1 = getBlockResponseMsg(100)
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestDelayChan(t *testing.T) {
genericHash[0] = 'a'

beaconState := &pb.BeaconState{
LastFinalizedSlot: 99,
FinalizedSlot: 99,
}

stateResponse := &pb.BeaconStateResponse{
Expand Down
Loading