primitives

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ActivationFlag is a flag to indicate a validator is joining
	ActivationFlag = iota
	// ExitFlag is a flag to indeicate a validator is leaving
	ExitFlag
)
View Source
const (
	// AttestedToPreviousEpochJustifiedSlot is a reward when a validator attests to the previous epoch justified slot.
	AttestedToPreviousEpochJustifiedSlot = iota

	// AttestedToPreviousEpochBoundaryHash is a reward when a validator attests to the previous epoch boundary hash.
	AttestedToPreviousEpochBoundaryHash

	// AttestedToCorrectBlockHashInPreviousEpoch is a reward when a validator attests to the correct beacon block hash for their slot.
	AttestedToCorrectBlockHashInPreviousEpoch

	// AttestationInclusionDistanceReward is a reward for including attestations in beacon blocks.
	AttestationInclusionDistanceReward

	// DidNotAttestToCorrectBeaconBlock is a penalty for not attesting to the correct beacon block.
	DidNotAttestToCorrectBeaconBlock

	// DidNotAttestToPreviousEpochBoundary is a penalty for not attesting to the previous epoch boundary.
	DidNotAttestToPreviousEpochBoundary

	// DidNotAttestToPreviousJustifiedSlot is a penalty for not attesting to the previous justified slot.
	DidNotAttestToPreviousJustifiedSlot

	// InactivityPenalty is a penalty for being exited with penalty.
	InactivityPenalty

	// AttestationInclusionDistancePenalty is a penalty for not including attestations in beacon blocks.
	AttestationInclusionDistancePenalty

	// ProposerReward is a reward for the proposer of a beacon block.
	ProposerReward

	// AttestationParticipationReward is a reward for choosing the correct shard block hash.
	AttestationParticipationReward

	// AttestationNonparticipationPenalty is a penalty for not choosing the correct shard block hash.
	AttestationNonparticipationPenalty
)
View Source
const (
	// Active is a status for a validator that is active.
	Active = iota
	// ActivePendingExit is a status for a validator that is active but pending exit.
	ActivePendingExit
	// PendingActivation is a status for a newly added validator
	PendingActivation
	// ExitedWithoutPenalty is a validator that gracefully exited
	ExitedWithoutPenalty
	// ExitedWithPenalty is a validator that exited not-so-gracefully
	ExitedWithPenalty
)

Variables

This section is empty.

Functions

func GetActiveValidatorIndices

func GetActiveValidatorIndices(validators []Validator) []uint32

GetActiveValidatorIndices gets validator indices that are active.

func GetDomain

func GetDomain(forkData ForkData, slot uint64, domainType uint64) uint64

GetDomain gets the domain for a slot and type.

func GetNewShuffling

func GetNewShuffling(seed chainhash.Hash, validators []Validator, crosslinkingStart int, con *config.Config) [][]ShardAndCommittee

GetNewShuffling calculates the new shuffling of validators to slots and shards.

func GetNewValidatorRegistryDeltaChainTip

func GetNewValidatorRegistryDeltaChainTip(currentValidatorRegistryDeltaChainTip chainhash.Hash, validatorIndex uint32, pubkey [96]byte, flag uint64) (chainhash.Hash, error)

GetNewValidatorRegistryDeltaChainTip gets the new delta chain tip hash.

func MinEmptyValidator

func MinEmptyValidator(validators []Validator, validatorBalances []uint64, c *config.Config, currentSlot uint64) int

MinEmptyValidator finds the first validator slot that is empty.

func ReceiptTypeToMeaning added in v0.2.4

func ReceiptTypeToMeaning(t uint8) string

ReceiptTypeToMeaning converts a receipt type to a meaningful string.

func ShardCommitteeByShardID

func ShardCommitteeByShardID(shardID uint64, shardCommittees []ShardAndCommittee) ([]uint32, error)

ShardCommitteeByShardID gets the shards committee from a list of committees/shards in a list.

func ShuffleValidators

func ShuffleValidators(toShuffle []uint32, seed chainhash.Hash) []uint32

ShuffleValidators shuffles an array of ints given a seed.

func Split

func Split(l []uint32, splitCount uint32) [][]uint32

Split splits an array into N different sections.

Types

type Attestation

type Attestation struct {
	// Signed data
	Data AttestationData
	// Attester participation bitfield
	ParticipationBitfield []uint8
	// Proof of custody bitfield
	CustodyBitfield []uint8
	// BLS aggregate signature
	AggregateSig [48]byte
}

Attestation is a signed attestation of a shard block.

func AttestationFromProto

func AttestationFromProto(att *pb.Attestation) (*Attestation, error)

AttestationFromProto gets a new attestation from a protobuf attestation message.

func (*Attestation) Copy

func (a *Attestation) Copy() Attestation

Copy returns a copy of the attestation

func (*Attestation) ToProto

func (a *Attestation) ToProto() *pb.Attestation

ToProto gets the protobuf representation of the attestation

type AttestationData

type AttestationData struct {
	// Slot number
	Slot uint64
	// Shard number
	Shard               uint64
	BeaconBlockHash     chainhash.Hash
	EpochBoundaryHash   chainhash.Hash
	ShardBlockHash      chainhash.Hash
	LatestCrosslinkHash chainhash.Hash
	JustifiedSlot       uint64
	JustifiedBlockHash  chainhash.Hash
}

AttestationData is the part of the attestation that is signed.

func AttestationDataFromProto

func AttestationDataFromProto(att *pb.AttestationData) (*AttestationData, error)

AttestationDataFromProto converts the protobuf representation to an attestationdata item.

func (*AttestationData) Copy

func (a *AttestationData) Copy() AttestationData

Copy returns a copy of the data.

func (*AttestationData) Equals

func (a *AttestationData) Equals(other *AttestationData) bool

Equals checks if this attestation data is equal to another.

func (AttestationData) ToProto

func (a AttestationData) ToProto() *pb.AttestationData

ToProto converts the attestation to protobuf form.

type AttestationDataAndCustodyBit

type AttestationDataAndCustodyBit struct {
	Data   AttestationData
	PoCBit bool
}

AttestationDataAndCustodyBit is an attestation data and custody bit combined.

type Block

type Block struct {
	BlockHeader BlockHeader
	BlockBody   BlockBody
}

Block represents a single beacon chain block.

func BlockFromProto

func BlockFromProto(bl *pb.Block) (*Block, error)

BlockFromProto returns a block from the protobuf representation.

func (*Block) Copy

func (b *Block) Copy() Block

Copy returns a copy of the block.

func (*Block) ToProto

func (b *Block) ToProto() *pb.Block

ToProto gets the protobuf representation of the block.

type BlockBody

type BlockBody struct {
	Attestations      []Attestation
	ProposerSlashings []ProposerSlashing
	CasperSlashings   []CasperSlashing
	Deposits          []Deposit
	Exits             []Exit
}

BlockBody contains the beacon actions that happened this block.

func BlockBodyFromProto

func BlockBodyFromProto(body *pb.BlockBody) (*BlockBody, error)

BlockBodyFromProto converts a protobuf representation of a block body to a block body.

func (*BlockBody) Copy

func (bb *BlockBody) Copy() BlockBody

Copy returns a copy of the block body.

func (*BlockBody) ToProto

func (bb *BlockBody) ToProto() *pb.BlockBody

ToProto converts the block body to protobuf form

type BlockHeader

type BlockHeader struct {
	SlotNumber   uint64
	ParentRoot   chainhash.Hash
	StateRoot    chainhash.Hash
	RandaoReveal [48]byte
	Signature    [48]byte
}

BlockHeader is the header of the block.

func BlockHeaderFromProto

func BlockHeaderFromProto(header *pb.BlockHeader) (*BlockHeader, error)

BlockHeaderFromProto converts a protobuf representation of a block header to a block header.

func (*BlockHeader) Copy

func (bh *BlockHeader) Copy() BlockHeader

Copy returns a copy of the block header.

func (*BlockHeader) ToProto

func (bh *BlockHeader) ToProto() *pb.BlockHeader

ToProto converts to block header to protobuf form.

type BlockView

type BlockView interface {
	GetHashBySlot(slot uint64) (chainhash.Hash, error)
	Tip() (chainhash.Hash, error)
	SetTipSlot(slot uint64)
	GetLastStateRoot() (chainhash.Hash, error)
}

BlockView is an interface the provides access to blocks.

type CasperSlashing

type CasperSlashing struct {
	Votes1 SlashableVoteData
	Votes2 SlashableVoteData
}

CasperSlashing is a claim to slash based on two votes.

func CasperSlashingFromProto

func CasperSlashingFromProto(slashing *pb.CasperSlashing) (*CasperSlashing, error)

CasperSlashingFromProto returns the casper slashing from the protobuf representation.

func (*CasperSlashing) Copy

func (cs *CasperSlashing) Copy() CasperSlashing

Copy returns a copy of the casper slashing.

func (*CasperSlashing) ToProto

func (cs *CasperSlashing) ToProto() *pb.CasperSlashing

ToProto gets the protobuf representaton of the casper slashing.

type Crosslink struct {
	// Slot is the slot within the current dynasty.
	Slot uint64

	// Shard chain block hash
	ShardBlockHash chainhash.Hash
}

Crosslink goes in a collation to represent the last crystallized beacon block.

func CrosslinkFromProto

func CrosslinkFromProto(crosslink *pb.Crosslink) (*Crosslink, error)

CrosslinkFromProto gets the crosslink for a protobuf representation

func (*Crosslink) ToProto

func (c *Crosslink) ToProto() *pb.Crosslink

ToProto gets the protobuf representation of the crosslink

type Deposit

type Deposit struct {
	Parameters DepositParameters
}

Deposit is a new deposit from a shard.

func DepositFromProto

func DepositFromProto(deposit *pb.Deposit) (*Deposit, error)

DepositFromProto gets the deposit from the protobuf representation.

func (Deposit) Copy

func (d Deposit) Copy() Deposit

Copy returns a copy of the deposit.

func (Deposit) ToProto

func (d Deposit) ToProto() *pb.Deposit

ToProto gets the protobuf representation of the deposit.

type DepositParameters

type DepositParameters struct {
	PubKey                [96]byte
	ProofOfPossession     [48]byte
	WithdrawalCredentials chainhash.Hash
}

DepositParameters are the parameters the depositer needs to provide.

func DepositParametersFromProto

func DepositParametersFromProto(parameters *pb.DepositParameters) (*DepositParameters, error)

DepositParametersFromProto gets the deposit parameters from the protobuf representation.

func (*DepositParameters) Copy

Copy returns a copy of the deposit parameters

func (*DepositParameters) ToProto

func (dp *DepositParameters) ToProto() *pb.DepositParameters

ToProto gets the protobuf representation of the deposit parameters.

type Exit

type Exit struct {
	Slot           uint64
	ValidatorIndex uint64
	Signature      [48]byte
}

Exit exits the validator.

func ExitFromProto

func ExitFromProto(exit *pb.Exit) (*Exit, error)

ExitFromProto gets the exit from the protobuf representation.

func (*Exit) Copy

func (e *Exit) Copy() Exit

Copy returns a copy of the exit.

func (*Exit) ToProto

func (e *Exit) ToProto() *pb.Exit

ToProto gets the protobuf representation of the exit.

type ForkData

type ForkData struct {
	// Previous fork version
	PreForkVersion uint64
	// Post fork version
	PostForkVersion uint64
	// Fork slot number
	ForkSlotNumber uint64
}

ForkData represents the fork information

func ForkDataFromProto

func ForkDataFromProto(data *pb.ForkData) (*ForkData, error)

ForkDataFromProto gets the fork data from the proto representation.

func (ForkData) Copy

func (f ForkData) Copy() ForkData

Copy returns a copy of the fork data.

func (*ForkData) GetVersionForSlot

func (f *ForkData) GetVersionForSlot(slot uint64) uint64

GetVersionForSlot returns the version for a specific slot number.

func (ForkData) ToProto

func (f ForkData) ToProto() *pb.ForkData

ToProto gets the protobuf representation of the fork data.

type PendingAttestation

type PendingAttestation struct {
	Data                  AttestationData
	ParticipationBitfield []byte
	CustodyBitfield       []byte
	InclusionDelay        uint64
	ProposerIndex         uint32
}

PendingAttestation is an attestation waiting to be included.

func PendingAttestationFromProto

func PendingAttestationFromProto(pa *pb.PendingAttestation) (*PendingAttestation, error)

PendingAttestationFromProto converts a protobuf attestation to a pending attestation.

func (*PendingAttestation) Copy

Copy copies a pending attestation

func (*PendingAttestation) ToProto

func (pa *PendingAttestation) ToProto() *pb.PendingAttestation

ToProto returns a protobuf representation of the pending attestation.

type ProposalSignedData

type ProposalSignedData struct {
	Slot      uint64
	Shard     uint64
	BlockHash chainhash.Hash
}

ProposalSignedData is a block proposal for a shard or beacon chain.

func ProposalSignedDataFromProto

func ProposalSignedDataFromProto(data *pb.ProposalSignedData) (*ProposalSignedData, error)

ProposalSignedDataFromProto gets the proposal for the protobuf representation.

func (*ProposalSignedData) Copy

Copy returns a copy of the proposal signed data.

func (*ProposalSignedData) ToProto

func (psd *ProposalSignedData) ToProto() *pb.ProposalSignedData

ToProto gets the protobuf representation of a proposal signed data object

type ProposerSlashing

type ProposerSlashing struct {
	ProposerIndex      uint32
	ProposalData1      ProposalSignedData
	ProposalSignature1 [48]byte
	ProposalData2      ProposalSignedData
	ProposalSignature2 [48]byte
}

ProposerSlashing is a slashing request for a proposal violation.

func ProposerSlashingFromProto

func ProposerSlashingFromProto(slashing *pb.ProposerSlashing) (*ProposerSlashing, error)

ProposerSlashingFromProto gets the proposer slashing from the protobuf representation

func (*ProposerSlashing) Copy

Copy returns a copy of the proposer slashing.

func (*ProposerSlashing) ToProto

func (ps *ProposerSlashing) ToProto() *pb.ProposerSlashing

ToProto gets the protobuf representation of the proposer slashing.

type Receipt added in v0.1.1

type Receipt struct {
	Slot   uint64
	Type   uint8
	Amount int64
	Index  uint32
}

Receipt is a way of representing slashings or rewards.

type ShardAndCommittee

type ShardAndCommittee struct {
	// Shard number
	Shard uint64

	// Validator indices
	Committee []uint32

	// Total validator count (for proofs of custody)
	TotalValidatorCount uint64
}

ShardAndCommittee keeps track of the validators assigned to a specific shard.

func ShardAndCommitteeFromProto

func ShardAndCommitteeFromProto(committee *pb.ShardCommittee) (*ShardAndCommittee, error)

ShardAndCommitteeFromProto gets the shard and committee for the protobuf representation.

func (*ShardAndCommittee) Copy

Copy copies the ShardAndCommittee

func (*ShardAndCommittee) ToProto

func (sc *ShardAndCommittee) ToProto() *pb.ShardCommittee

ToProto gets the protobuf representation of the shard and committee

type SlashableVoteData

type SlashableVoteData struct {
	AggregateSignaturePoC0Indices []uint32
	AggregateSignaturePoC1Indices []uint32
	Data                          AttestationData
	AggregateSignature            [48]byte
}

SlashableVoteData is the vote data that should be slashed.

func SlashableVoteDataFromProto

func SlashableVoteDataFromProto(voteData *pb.SlashableVoteData) (*SlashableVoteData, error)

SlashableVoteDataFromProto returns the vote data from the protobuf representation

func (*SlashableVoteData) Copy

Copy returns a copy of the slashable vote data.

func (*SlashableVoteData) ToProto

func (svd *SlashableVoteData) ToProto() *pb.SlashableVoteData

ToProto returns the protobuf representation of the slashable vote data.

type State

type State struct {
	// MISC ITEMS
	// Slot is the current slot.
	Slot uint64

	EpochIndex uint64

	// GenesisTime is the time of the genesis block.
	GenesisTime uint64

	// ForkData is the versioning data for hard forks.
	ForkData ForkData

	// VALIDATOR REGISTRY
	// ValidatorRegistry is the registry mapping IDs to validators
	ValidatorRegistry []Validator

	// ValidatorBalances are the balances corresponding to each validator.
	ValidatorBalances []uint64

	// ValidatorRegistryLatestChangeSlot is the slot where the validator registry
	// last changed.
	ValidatorRegistryLatestChangeSlot uint64

	// ValidatorRegistryExitCount is the number of validators that are exited.
	ValidatorRegistryExitCount uint64

	// ValidatorSetDeltaHashChange is for light clients to keep track of validator
	// registry changes.
	ValidatorRegistryDeltaChainTip chainhash.Hash

	// RANDOMNESS
	// RandaoMix is the mix of randao reveals to provide entropy.
	RandaoMix chainhash.Hash

	// NextSeed is the next RANDAO seed.
	NextSeed chainhash.Hash

	// COMMITTEES
	// ShardAndCommitteeForSlots is a list of committee members
	// and their assigned shard, per slot
	ShardAndCommitteeForSlots [][]ShardAndCommittee

	// FINALITY
	PreviousJustifiedSlot uint64
	JustifiedSlot         uint64
	JustificationBitfield uint64
	FinalizedSlot         uint64

	// RECENT STATE
	LatestCrosslinks            []Crosslink
	LatestBlockHashes           []chainhash.Hash
	LatestPenalizedExitBalances []uint64
	LatestAttestations          []PendingAttestation
	BatchedBlockRoots           []chainhash.Hash
}

State is the state of a beacon block

func StateFromProto

func StateFromProto(s *pb.State) (*State, error)

StateFromProto gets the state fromo the protobuf representation.

func (*State) ActivateValidator

func (s *State) ActivateValidator(index uint32) error

ActivateValidator activates a validator in the state at a certain index.

func (*State) ApplyCasperSlashing

func (s *State) ApplyCasperSlashing(casperSlashing CasperSlashing, c *config.Config) error

ApplyCasperSlashing applies a casper slashing claim to the current state.

func (*State) ApplyExit

func (s *State) ApplyExit(exit Exit, config *config.Config) error

ApplyExit validates and applies an exit.

func (*State) ApplyProposerSlashing

func (s *State) ApplyProposerSlashing(proposerSlashing ProposerSlashing, config *config.Config) error

ApplyProposerSlashing applies a proposer slashing if valid.

func (*State) Copy

func (s *State) Copy() State

Copy deep-copies the state.

func (*State) ExitValidator

func (s *State) ExitValidator(index uint32, status uint64, c *config.Config) error

ExitValidator handles state changes when a validator exits.

func (*State) GetAttestationParticipants

func (s *State) GetAttestationParticipants(data AttestationData, participationBitfield []byte, c *config.Config, stateSlot uint64) ([]uint32, error)

GetAttestationParticipants gets the indices of participants.

func (*State) GetAttesterCommitteeSize

func (s *State) GetAttesterCommitteeSize(slot uint64, con *config.Config) uint32

GetAttesterCommitteeSize gets the size of committee

func (*State) GetBeaconProposerIndex

func (s *State) GetBeaconProposerIndex(stateSlot uint64, slot uint64, c *config.Config) (uint32, error)

GetBeaconProposerIndex gets the validator index of the block proposer at a certain slot.

func (*State) GetCommitteeIndices

func (s *State) GetCommitteeIndices(stateSlot uint64, slot uint64, shardID uint64, con *config.Config) ([]uint32, error)

GetCommitteeIndices gets all of the validator indices involved with the committee assigned to the shard and slot of the committee.

func (*State) GetEffectiveBalance

func (s *State) GetEffectiveBalance(index uint32, c *config.Config) uint64

GetEffectiveBalance gets the effective balance for a validator

func (*State) GetShardCommitteesAtSlot

func (s *State) GetShardCommitteesAtSlot(stateSlot uint64, slot uint64, c *config.Config) ([]ShardAndCommittee, error)

GetShardCommitteesAtSlot gets the committees assigned to a specific slot.

func (*State) GetTotalBalance

func (s *State) GetTotalBalance(activeValidators []uint32, c *config.Config) uint64

GetTotalBalance gets the total balance of the provided validator indices.

func (*State) GetTotalBalanceMap

func (s *State) GetTotalBalanceMap(activeValidators map[uint32]struct{}, c *config.Config) uint64

GetTotalBalanceMap gets the total balance of the provided validator indices with a map input.

func (*State) InitiateValidatorExit

func (s *State) InitiateValidatorExit(index uint32) error

InitiateValidatorExit moves a validator from active to pending exit.

func (*State) ProcessBlock

func (s *State) ProcessBlock(block *Block, con *config.Config, view BlockView, verifySignature bool) error

ProcessBlock tries to apply a block to the state.

func (*State) ProcessDeposit

func (s *State) ProcessDeposit(pubkey *bls.PublicKey, amount uint64, proofOfPossession [48]byte, withdrawalCredentials chainhash.Hash, skipValidation bool, c *config.Config) (uint32, error)

ProcessDeposit processes a deposit with the context of the current state.

func (*State) ProcessEpochTransition

func (s *State) ProcessEpochTransition(c *config.Config, view BlockView) ([]Receipt, error)

ProcessEpochTransition processes an epoch transition and modifies state.

func (*State) ProcessSlot

func (s *State) ProcessSlot(previousBlockRoot chainhash.Hash, c *config.Config) error

ProcessSlot processes a single slot which should happen before the block transition and the epoch transition.

func (*State) ProcessSlots

func (s *State) ProcessSlots(upTo uint64, view BlockView, c *config.Config) error

ProcessSlots uses the current head to process slots up to a certain slot, applying slot transitions and epoch transitions, and returns the updated state. Note that this should only process up to the current slot number so that the lastBlockHash remains constant.

func (*State) ToProto

func (s *State) ToProto() *pb.State

ToProto gets the protobuf representation of the state.

func (*State) UpdateValidatorRegistry

func (s *State) UpdateValidatorRegistry(c *config.Config) error

UpdateValidatorRegistry updates the registry and updates validator pending activation or exit.

func (*State) UpdateValidatorStatus

func (s *State) UpdateValidatorStatus(index uint32, status uint64, c *config.Config) error

UpdateValidatorStatus moves a validator to a specific status.

func (*State) ValidateAttestation added in v0.2.4

func (s *State) ValidateAttestation(att Attestation, verifySignature bool, view BlockView, c *config.Config, stateSlot uint64) error

ValidateAttestation checks if the attestation is valid.

func (*State) ValidateProofOfPossession

func (s *State) ValidateProofOfPossession(pubkey *bls.PublicKey, proofOfPossession bls.Signature, withdrawalCredentials chainhash.Hash) (bool, error)

ValidateProofOfPossession validates a proof of possession for a new validator.

type Validator

type Validator struct {
	// BLS public key
	Pubkey [96]byte
	// XXXPubkeyCached is the cached deserialized public key.
	XXXPubkeyCached *bls.PublicKey
	// Withdrawal credentials
	WithdrawalCredentials chainhash.Hash
	// Status code
	Status uint64
	// Slot when validator last changed status (or 0)
	LatestStatusChangeSlot uint64
	// Sequence number when validator exited (or 0)
	ExitCount uint64
	// LastPoCChangeSlot is the last time the PoC was changed
	LastPoCChangeSlot uint64
	// SecondLastPoCChangeSlot is the second to last time the PoC was changed
	SecondLastPoCChangeSlot uint64
}

Validator is a single validator session (logging in and out)

func ValidatorFromProto

func ValidatorFromProto(validator *pb.Validator) (*Validator, error)

ValidatorFromProto gets the validator for the protobuf representation

func (*Validator) Copy

func (v *Validator) Copy() Validator

Copy copies a validator instance.

func (*Validator) GetPublicKey

func (v *Validator) GetPublicKey() (*bls.PublicKey, error)

GetPublicKey gets the cached validator pubkey.

func (Validator) IsActive

func (v Validator) IsActive() bool

IsActive checks if the validator is active.

func (*Validator) ToProto

func (v *Validator) ToProto() *pb.Validator

ToProto creates a ProtoBuf ValidatorResponse from a Validator

type ValidatorRegistryDeltaBlock

type ValidatorRegistryDeltaBlock struct {
	LatestRegistryDeltaRoot chainhash.Hash
	ValidatorIndex          uint32
	Pubkey                  [96]byte
	Flag                    uint64
}

ValidatorRegistryDeltaBlock is a validator change hash.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL