Skip to content

Commit

Permalink
Port over challenge code, in packages now
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Mar 5, 2019
0 parents commit fc6224b
Show file tree
Hide file tree
Showing 22 changed files with 2,120 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 @protolambda

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Beacon transition - Go

Originally "beacon challenge", put together under a line limit.

This new repo aims to provide a good interface, and a minimal implementation, without code-golfing.


## License

MIT, see license file.

27 changes: 27 additions & 0 deletions eth2/beacon/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package beacon

import "go-beacon-transition/eth2"

type BeaconBlock struct {
// Header
Slot eth2.Slot
Parent_root eth2.Root
State_root eth2.Root
Randao_reveal [96]byte
Eth1_data Eth1Data

// Body
Body BeaconBlockBody
// Signature
Signature eth2.BLSSignature
}

type BeaconBlockBody struct {
Proposer_slashings []ProposerSlashing
Attester_slashings []AttesterSlashing
Attestations []Attestation
Deposits []Deposit
Voluntary_exits []VoluntaryExit
Transfers []Transfer
}

208 changes: 208 additions & 0 deletions eth2/beacon/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package beacon

import (
"go-beacon-transition/eth2"
"go-beacon-transition/eth2/util/bitfield"
)

// NOTE: these containers are going to be moved to sub-packages, per-topic.

type ProposerSlashing struct {
// Proposer index
Proposer_index eth2.ValidatorIndex
// First proposal
Proposal_1 Proposal
// Second proposal
Proposal_2 Proposal
}

type Proposal struct {
// Slot number
Slot eth2.Slot
// Shard number (`BEACON_CHAIN_SHARD_NUMBER` for beacon chain)
Shard eth2.Shard
// Block root
Block_root eth2.Root
// Signature
Signature eth2.BLSSignature
}

type AttesterSlashing struct {
// First slashable attestation
Slashable_attestation_1 SlashableAttestation
// Second slashable attestation
Slashable_attestation_2 SlashableAttestation
}

type SlashableAttestation struct {
// Validator indices
Validator_indices []eth2.ValidatorIndex
// Attestation data
Data AttestationData
// Custody bitfield
Custody_bitfield bitfield.Bitfield
// Aggregate signature
Aggregate_signature eth2.BLSSignature
}

type Attestation struct {
// Attester aggregation bitfield
Aggregation_bitfield bitfield.Bitfield
// Attestation data
Data AttestationData
// Custody bitfield
Custody_bitfield bitfield.Bitfield
// BLS aggregate signature
Aggregate_signature eth2.BLSSignature
}

type AttestationData struct {
// Slot number
Slot eth2.Slot
// Shard number
Shard eth2.Shard
// Root of the signed beacon block
Beacon_block_root eth2.Root
// Root of the ancestor at the epoch boundary
Epoch_boundary_root eth2.Root
// Data from the shard since the last attestation
Crosslink_data_root eth2.Root
// Last crosslink
Latest_crosslink Crosslink
// Last justified epoch in the beacon state
Justified_epoch eth2.Epoch
// Hash of the last justified beacon block
Justified_block_root eth2.Root
}

type AttestationDataAndCustodyBit struct {
// Attestation data
Data AttestationData
// Custody bit
Custody_bit bool
}

type Crosslink struct {
// Epoch number
Epoch eth2.Epoch
// Shard data since the previous crosslink
Crosslink_data_root eth2.Root
}

type Deposit struct {
// Branch in the deposit tree
Branch []eth2.Root
// Index in the deposit tree
Index eth2.DepositIndex
// Data
Deposit_data DepositData
}

type DepositData struct {
// Amount in Gwei
Amount eth2.Gwei
// Timestamp from deposit contract
Timestamp eth2.Timestamp
// Deposit input
Deposit_input DepositInput
}

type DepositInput struct {
// BLS pubkey
Pubkey eth2.BLSPubkey
// Withdrawal credentials
Withdrawal_credentials eth2.Root
// A BLS signature of this `DepositInput`
Proof_of_possession eth2.BLSSignature
}

type VoluntaryExit struct {
// Minimum epoch for processing exit
Epoch eth2.Epoch
// Index of the exiting validator
Validator_index eth2.ValidatorIndex
// Validator signature
Signature eth2.BLSSignature
}

type Transfer struct {
// Sender index
From eth2.ValidatorIndex
// Recipient index
To eth2.ValidatorIndex
// Amount in Gwei
Amount eth2.Gwei
// Fee in Gwei for block proposer
Fee eth2.Gwei
// Inclusion slot
Slot eth2.Slot
// Sender withdrawal pubkey
Pubkey eth2.BLSPubkey
// Sender signature
Signature eth2.BLSSignature
}


type Validator struct {
// BLS public key
Pubkey eth2.BLSPubkey
// Withdrawal credentials
Withdrawal_credentials eth2.Root
// Epoch when validator activated
Activation_epoch eth2.Epoch
// Epoch when validator exited
Exit_epoch eth2.Epoch
// Epoch when validator is eligible to withdraw
Withdrawable_epoch eth2.Epoch
// Did the validator initiate an exit
Initiated_exit bool
// Was the validator slashed
Slashed bool
}

func (v *Validator) IsActive(epoch eth2.Epoch) bool {
return v.Activation_epoch <= epoch && epoch < v.Exit_epoch
}

type PendingAttestation struct {
// Attester aggregation bitfield
Aggregation_bitfield bitfield.Bitfield
// Attestation data
Data AttestationData
// Custody bitfield
Custody_bitfield bitfield.Bitfield
// Inclusion slot
Inclusion_slot eth2.Slot
}

type Fork struct {
// TODO: fork versions are 64 bits, but usage is 32 bits in BLS domain. Spec unclear about it.
// Previous fork version
Previous_version uint64
// Current fork version
Current_version uint64
// Fork epoch number
Epoch eth2.Epoch
}

// Return the fork version of the given epoch
func (f Fork) GetVersion(epoch eth2.Epoch) uint64 {
if epoch < f.Epoch {
return f.Previous_version
}
return f.Current_version
}

type Eth1Data struct {
// Root of the deposit tree
Deposit_root eth2.Root
// Block hash
Block_hash eth2.Root
}

type Eth1DataVote struct {
// Data being voted for
Eth1_data Eth1Data
// Vote count
Vote_count uint64
}
76 changes: 76 additions & 0 deletions eth2/beacon/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package beacon

import "go-beacon-transition/eth2"

type BeaconState struct {
// Misc
Slot eth2.Slot
Genesis_time eth2.Timestamp
Fork Fork // For versioning hard forks

// Validator registry
Validator_registry []Validator
Validator_balances []eth2.Gwei
Validator_registry_update_epoch eth2.Epoch

// Randomness and committees
Latest_randao_mixes [eth2.LATEST_RANDAO_MIXES_LENGTH]eth2.Bytes32
Previous_shuffling_start_shard eth2.Shard
Current_shuffling_start_shard eth2.Shard
Previous_shuffling_epoch eth2.Epoch
Current_shuffling_epoch eth2.Epoch
Previous_shuffling_seed eth2.Bytes32
Current_shuffling_seed eth2.Bytes32

// Finality
Previous_justified_epoch eth2.Epoch
Justified_epoch eth2.Epoch
Justification_bitfield uint64
Finalized_epoch eth2.Epoch

// Recent state
Latest_crosslinks [eth2.SHARD_COUNT]Crosslink
Latest_block_roots [eth2.LATEST_BLOCK_ROOTS_LENGTH]eth2.Root
Latest_active_index_roots [eth2.LATEST_ACTIVE_INDEX_ROOTS_LENGTH]eth2.Root
// Balances slashed at every withdrawal period
Latest_slashed_balances [eth2.LATEST_SLASHED_EXIT_LENGTH]eth2.Gwei
Latest_attestations []PendingAttestation
Batched_block_roots []eth2.Root

// Ethereum 1.0 chain data
Latest_eth1_data Eth1Data
Eth1_data_votes []Eth1DataVote
Deposit_index eth2.DepositIndex
}

// Make a deep copy of the state object
func (st *BeaconState) Copy() *BeaconState {
// copy over state
stUn := *st
res := &stUn
// manually copy over slices
// validators
copy(res.Validator_registry, st.Validator_registry)
copy(res.Validator_balances, st.Validator_balances)
// recent state
copy(res.Latest_attestations, st.Latest_attestations)
copy(res.Batched_block_roots, st.Batched_block_roots)
// eth1
copy(res.Eth1_data_votes, st.Eth1_data_votes)
return res
}

// Get current epoch
func (st *BeaconState) Epoch() eth2.Epoch {
return st.Slot.ToEpoch()
}

// Return previous epoch. Not just current - 1: it's clipped to genesis.
func (st *BeaconState) PreviousEpoch() eth2.Epoch {
epoch := st.Epoch()
if epoch < eth2.GENESIS_EPOCH {
return eth2.GENESIS_EPOCH
} else {
return epoch
}
}
Loading

0 comments on commit fc6224b

Please sign in to comment.