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

Tidying up Godoc for Core Package #2762

Merged
merged 2 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions beacon-chain/core/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,6 @@ func NewGenesisBlock(stateRoot []byte) *pb.BeaconBlock {
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:
// def get_block_root(state: BeaconState, slot: int) -> Hash32:
// """
// returns the block root at a recent ``slot``.
// """
// assert state.slot <= slot + SLOTS_PER_HISTORICAL_ROOT
// assert slot < state.slote
// return state.latest_block_roots[slot % SLOTS_PER_HISTORICAL_ROOT]
func BlockRoot(state *pb.BeaconState, slot uint64) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete these methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

earliestSlot := uint64(0)
if state.Slot > params.BeaconConfig().SlotsPerHistoricalRoot {
earliestSlot = state.Slot - params.BeaconConfig().SlotsPerHistoricalRoot
}

if slot < earliestSlot || slot >= state.Slot {
return []byte{}, fmt.Errorf("slot %d is not within expected range of %d to %d",
slot,
earliestSlot,
state.Slot,
)
}

return state.LatestBlockRoots[slot%params.BeaconConfig().SlotsPerHistoricalRoot], nil
}

// ProcessBlockRoots processes the previous block root into the state, by appending it
// to the most recent block roots.
// 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) % SLOTS_PER_HISTORICAL_ROOT] = previous_block_root.
func ProcessBlockRoots(state *pb.BeaconState, parentRoot [32]byte) *pb.BeaconState {
state.LatestBlockRoots[(state.Slot-1)%params.BeaconConfig().SlotsPerHistoricalRoot] = parentRoot[:]
return state
}

// BlockFromHeader manufactures a block from its header. It contains all its fields,
// expect for the block body.
func BlockFromHeader(header *pb.BeaconBlockHeader) *pb.BeaconBlock {
Expand Down
278 changes: 142 additions & 136 deletions beacon-chain/core/blocks/block_operations.go

Large diffs are not rendered by default.

117 changes: 0 additions & 117 deletions beacon-chain/core/blocks/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package blocks

import (
"bytes"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -40,122 +39,6 @@ func TestGenesisBlock_InitializedCorrectly(t *testing.T) {
}
}

func TestBlockRootAtSlot_AccurateBlockRoot(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("slotsPerEpoch should be 64 for these tests to pass")
}
var blockRoots [][]byte

for i := uint64(0); i < params.BeaconConfig().SlotsPerHistoricalRoot; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
state := &pb.BeaconState{
LatestBlockRoots: blockRoots,
}

tests := []struct {
slot uint64
stateSlot uint64
expectedRoot []byte
}{
{
slot: 0,
stateSlot: 1,
expectedRoot: []byte{0},
},
{
slot: 2,
stateSlot: 5,
expectedRoot: []byte{2},
},
{
slot: 64,
stateSlot: 128,
expectedRoot: []byte{64},
}, {
slot: 2999,
stateSlot: 3000,
expectedRoot: []byte{183},
}, {
slot: 2873,
stateSlot: 3000,
expectedRoot: []byte{57},
},
}
for _, tt := range tests {
state.Slot = tt.stateSlot
wantedSlot := tt.slot
result, err := BlockRoot(state, wantedSlot)
if err != nil {
t.Errorf("failed to get block root at slot %d: %v", wantedSlot, err)
}
if !bytes.Equal(result, tt.expectedRoot) {
t.Errorf(
"result block root was an unexpected value. Wanted %d, got %d",
tt.expectedRoot,
result,
)
}
}
}

func TestBlockRootAtSlot_OutOfBounds(t *testing.T) {
if params.BeaconConfig().SlotsPerEpoch != 64 {
t.Errorf("slotsPerEpoch should be 64 for these tests to pass")
}

var blockRoots [][]byte

for i := uint64(0); i < params.BeaconConfig().SlotsPerHistoricalRoot; i++ {
blockRoots = append(blockRoots, []byte{byte(i)})
}
state := &pb.BeaconState{
LatestBlockRoots: blockRoots,
}

tests := []struct {
slot uint64
stateSlot uint64
expectedErr string
}{
{
slot: 1000,
stateSlot: 500,
expectedErr: fmt.Sprintf("slot %d is not within expected range of %d to %d",
1000,
0,
500),
},
{
slot: 129,
stateSlot: 400,
expectedErr: "slot 129 is not within expected range of 272 to 399",
},
}
for _, tt := range tests {
state.Slot = tt.stateSlot
_, err := BlockRoot(state, tt.slot)
if err != nil && err.Error() != tt.expectedErr {
t.Errorf("Expected error \"%s\" got \"%v\"", tt.expectedErr, err)
}
}
}

func TestProcessBlockRoots_AccurateMerkleTree(t *testing.T) {
state := &pb.BeaconState{}

state.LatestBlockRoots = make([][]byte, params.BeaconConfig().SlotsPerHistoricalRoot)
state.Slot = params.BeaconConfig().SlotsPerHistoricalRoot + 1

testRoot := [32]byte{'a'}

newState := ProcessBlockRoots(state, testRoot)
if !bytes.Equal(newState.LatestBlockRoots[0], testRoot[:]) {
t.Fatalf("Latest Block root hash not saved."+
" Supposed to get %#x , but got %#x", testRoot, newState.LatestBlockRoots[0])
}
}

func TestHeaderFromBlock(t *testing.T) {
dummyBody := &pb.BeaconBlockBody{
RandaoReveal: []byte("Reveal"),
Expand Down
9 changes: 0 additions & 9 deletions beacon-chain/core/blocks/validity_conditions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Package blocks contains block processing libraries. These libraries
// process and verify block specific messages such as PoW receipt root,
// RANDAO, validator deposits, exits and slashing proofs.
package blocks

import (
Expand All @@ -19,12 +16,6 @@ import (
var log = logrus.WithField("prefix", "core/blocks")

// IsValidBlock ensures that the block is compliant with the block processing validity conditions.
// Spec:
// For a beacon chain block, block, to be processed by a node, the following conditions must be met:
// The parent block with root block.parent_root has been processed and accepted.
// The node has processed its state up to slot, block.slot - 1.
// The Ethereum 1.0 block pointed to by the state.processed_pow_receipt_root has been processed and accepted.
// The node's local clock time is greater than or equal to state.genesis_time + block.slot * SECONDS_PER_SLOT.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete all of this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are old, the variable names are outdated and it's no longer specified in the spec. They are moved under ProcessBlockHeader:
https://github.com/prysmaticlabs/prysm/blob/spec-v0.6/beacon-chain/core/blocks/block_operations.go#L84

func IsValidBlock(
ctx context.Context,
state *pb.BeaconState,
Expand Down
7 changes: 5 additions & 2 deletions beacon-chain/core/helpers/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

// BlockRootAtSlot returns the block root stored in the BeaconState for a recent slot.
// It returns an error if the requested block root is not within the slot range.
//
// Spec pseudocode definition:
// def get_block_root_at_slot(state: BeaconState,
// def get_block_root_at_slot(state: BeaconState,
// slot: Slot) -> Bytes32:
// """
// Return the block root at a recent ``slot``.
Expand All @@ -33,7 +34,9 @@ func BlockRootAtSlot(state *pb.BeaconState, slot uint64) ([]byte, error) {
}

// BlockRoot returns the block root stored in the BeaconState for epoch start slot.
// def get_block_root(state: BeaconState,
//
// Spec pseudocode definition:
// def get_block_root(state: BeaconState,
// epoch: Epoch) -> Bytes32:
// """
// Return the block root at a recent ``epoch``.
Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/core/helpers/committee.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package helpers contains helper functions outlined in ETH2.0 spec:
// https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#helper-functions
// Package helpers contains helper functions outlined in ETH2.0 spec beacon chain spec
package helpers

import (
Expand Down Expand Up @@ -61,7 +60,7 @@ func EpochCommitteeCount(state *pb.BeaconState, epoch uint64) (uint64, error) {
// CrosslinkCommitteeAtEpoch returns the crosslink committee of a given epoch.
//
// Spec pseudocode definition:
// def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> List[ValidatorIndex]:
// def get_crosslink_committee(state: BeaconState, epoch: Epoch, shard: Shard) -> List[ValidatorIndex]:
// return compute_committee(
// indices=get_active_validator_indices(state, epoch),
// seed=generate_seed(state, epoch),
Expand Down Expand Up @@ -283,7 +282,7 @@ func CommitteeAssignment(
// ShardDelta returns the minimum number of shards get processed in one epoch.
//
// Spec pseudocode definition:
// def get_shard_delta(state: BeaconState, epoch: Epoch) -> int:
// def get_shard_delta(state: BeaconState, epoch: Epoch) -> int:
// return min(get_epoch_committee_count(state, epoch), SHARD_COUNT - SHARD_COUNT // SLOTS_PER_EPOCH)
func ShardDelta(beaconState *pb.BeaconState, epoch uint64) (uint64, error) {
shardCount := params.BeaconConfig().ShardCount
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/helpers/randao.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var currentEpochSeed = cache.NewSeedCache()
// GenerateSeed generates the randao seed of a given epoch.
//
// Spec pseudocode definition:
// def generate_seed(state: BeaconState,
// def generate_seed(state: BeaconState,
// epoch: Epoch) -> Bytes32:
// """
// Generate a seed for the given ``epoch``.
Expand Down
5 changes: 3 additions & 2 deletions beacon-chain/core/helpers/rewards_penalties.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TotalActiveBalance(state *pb.BeaconState) (uint64, error) {
// IncreaseBalance increases validator with the given 'index' balance by 'delta' in Gwei.
//
// Spec pseudocode definition:
// def increase_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
// def increase_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
// """
// Increase validator balance by ``delta``.
// """
Expand All @@ -87,7 +87,8 @@ func IncreaseBalance(state *pb.BeaconState, idx uint64, delta uint64) *pb.Beacon

// DecreaseBalance decreases validator with the given 'index' balance by 'delta' in Gwei.
//
// def decrease_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
// Spec pseudocode definition:
// def decrease_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
// """
// Decrease validator balance by ``delta`` with underflow protection.
// """
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/helpers/slot_epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// SlotToEpoch returns the epoch number of the input slot.
//
// Spec pseudocode definition:
// def slot_to_epoch(slot: Slot) -> Epoch:
// def slot_to_epoch(slot: Slot) -> Epoch:
// """
// Return the epoch number of the given ``slot``.
// """
Expand All @@ -21,7 +21,7 @@ func SlotToEpoch(slot uint64) uint64 {
// the slot number stored in beacon state.
//
// Spec pseudocode definition:
// def get_current_epoch(state: BeaconState) -> Epoch:
// def get_current_epoch(state: BeaconState) -> Epoch:
// """
// Return the current epoch of the given ``state``.
// """
Expand All @@ -35,7 +35,7 @@ func CurrentEpoch(state *pb.BeaconState) uint64 {
// underflow condition.
//
// Spec pseudocode definition:
// def get_previous_epoch(state: BeaconState) -> Epoch:
// def get_previous_epoch(state: BeaconState) -> Epoch:
// """`
// Return the previous epoch of the given ``state``.
// Return the current epoch if it's genesis epoch.
Expand All @@ -60,7 +60,7 @@ func NextEpoch(state *pb.BeaconState) uint64 {
// current epoch.
//
// Spec pseudocode definition:
// def get_epoch_start_slot(epoch: Epoch) -> Slot:
// def get_epoch_start_slot(epoch: Epoch) -> Slot:
// """
// Return the starting slot of the given ``epoch``.
// """
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func DelayedActivationExitEpoch(epoch uint64) uint64 {
// enter and exit validator pool for an epoch.
//
// Spec pseudocode definition:
// def get_churn_limit(state: BeaconState) -> int:
// def get_churn_limit(state: BeaconState) -> int:
// return max(
// MIN_PER_EPOCH_CHURN_LIMIT,
// len(get_active_validator_indices(state, get_current_epoch(state))) // CHURN_LIMIT_QUOTIENT
Expand Down Expand Up @@ -217,6 +217,7 @@ func BeaconProposerIndex(state *pb.BeaconState) (uint64, error) {

// DomainVersion returns the domain version for BLS private key to sign and verify.
//
// Spec pseudocode definition:
// def get_domain(state: BeaconState,
// domain_type: int,
// message_epoch: int=None) -> int:
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

// GenesisBeaconState gets called when DepositsForChainStart count of
// full deposits were made to the deposit contract and the ChainStart log gets emitted.
//
// Spec pseudocode definition:
// def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
// def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
// genesis_time: int,
// genesis_eth1_data: Eth1Data) -> BeaconState:
// """
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/state/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func ProcessBlock(

// ProcessEpoch describes the per epoch operations that are performed on the
// beacon state. It focuses on the validator registry, adjusting balances, and finalizing slots.
//
// Spec pseudocode definition:
//
// def process_epoch(state: BeaconState) -> None:
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/validators/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func ProcessDeposit(
// validator's activation slot.
//
// Spec pseudocode definition:
// def activate_validator(state: BeaconState, index: ValidatorIndex, is_genesis: bool) -> None:
// def activate_validator(state: BeaconState, index: ValidatorIndex, is_genesis: bool) -> None:
// """
// Activate the validator of the given ``index``.
// Note that this function mutates ``state``.
Expand Down Expand Up @@ -120,7 +120,7 @@ func ActivateValidator(state *pb.BeaconState, idx uint64, genesis bool) (*pb.Bea
// validator with correct voluntary exit parameters.
//
// Spec pseudocode definition:
// def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
// def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
// """
// Initiate the exit of the validator of the given ``index``.
// """
Expand Down Expand Up @@ -184,7 +184,7 @@ func InitiateValidatorExit(state *pb.BeaconState, idx uint64) (*pb.BeaconState,
// keeping work to exit validator with entry exit delay.
//
// Spec pseudocode definition:
// def exit_validator(state: BeaconState, index: ValidatorIndex) -> None:
// def exit_validator(state: BeaconState, index: ValidatorIndex) -> None:
// """
// Exit the validator of the given ``index``.
// Note that this function mutates ``state``.
Expand All @@ -210,7 +210,7 @@ func ExitValidator(state *pb.BeaconState, idx uint64) *pb.BeaconState {
// the whistleblower's balance.
//
// Spec pseudocode definition:
// def slash_validator(state: BeaconState, index: ValidatorIndex) -> None:
// def slash_validator(state: BeaconState, index: ValidatorIndex) -> None:
// """
// Slash the validator of the given ``index``.
// Note that this function mutates ``state``.
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/rpc/attester_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand Down Expand Up @@ -132,7 +131,7 @@ func (as *AttesterServer) AttestationDataAtSlot(ctx context.Context, req *pb.Att
if epochStartSlot == headState.Slot {
epochBoundaryRoot = headRoot[:]
} else {
epochBoundaryRoot, err = blocks.BlockRoot(headState, epochStartSlot)
epochBoundaryRoot, err = helpers.BlockRootAtSlot(headState, epochStartSlot)
if err != nil {
return nil, fmt.Errorf("could not get epoch boundary block for slot %d: %v",
epochStartSlot, err)
Expand Down