-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cleaning & refactoring * merging proposer, voter & observer behavior roles * added Voter registration in the node cli * added Voter registration and management smart-contract deployment in the node cli
- Loading branch information
ovcharovvladimir
committed
Dec 24, 2018
1 parent
28f8759
commit 31ee0af
Showing
267 changed files
with
7,754 additions
and
41,620 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Prysmatic Labs Beacon Chain Implementation | ||
|
||
This is the main project folder for the beacon chain implementation of Ethereum 2.0 in Golang by [Prysmatic Labs](https://prysmaticlabs.com). Before you begin, check out our [Contribution Guidelines](#contribution-guidelines) and join our active chat room on Gitter below: | ||
|
||
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/prysmaticlabs/prysm=badge&utm_medium=badge&utm_campaign=pr-badge) | ||
|
||
Also, read the latest sharding + casper [design spec](https://ethresear.ch/t/convenience-link-to-full-casper-chain-v2-spec/2332), this design spec serves as a source of truth for the beacon chain implementation we follow at prysmatic labs. | ||
Refer this page on [why](http://email.mg2.substack.com/c/eJwlj9GOhCAMRb9G3jRQQPGBh5mM8xsbhKrsDGIAM9m_X9xN2qZtbpt7rCm4xvSjj5gLOTOmL-809CMbKXFaOKakIl4DZYr2AGyQIGjHOnWH22OiYnoIxmDijaBhhS6fcy7GvjobA9m0mSXOcnZq5GBqLkilXBZhBsus5ZK89VbKkRt-a-BZI6DzZ7iur1lQ953KJ9bemnxgahuQU9XJu6pFPdu8meT8vragzEjpMCwMGLlgLo6h5z1JumQTu4IJd4v15xqMf_8ZLP_Y1bSLdbnrD-LL71i2Kj7DLxaWWF4) | ||
we are comibing sharding and casper together. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ovcharovvladimir/essentiaHybrid/common" | ||
"github.com/ovcharovvladimir/essentiaHybrid/essdb" | ||
"github.com/ovcharovvladimir/essentiaHybrid/rlp" | ||
"github.com/ovcharovvladimir/Prysm/sness/params" | ||
"github.com/ovcharovvladimir/Prysm/sness/powchain" | ||
"github.com/ovcharovvladimir/Prysm/sness/types" | ||
"github.com/ovcharovvladimir/Prysm/sness/utils" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var stateLookupKey = "beaconchainstate" | ||
|
||
// BeaconChain represents the core PoS blockchain object containing | ||
// both a crystallized and active state. | ||
type BeaconChain struct { | ||
state *beaconState | ||
lock sync.Mutex | ||
db essdb.Database | ||
} | ||
|
||
type beaconState struct { | ||
ActiveState *types.ActiveState | ||
CrystallizedState *types.CrystallizedState | ||
} | ||
|
||
// NewBeaconChain initializes an instance using genesis state parameters if | ||
// none provided. | ||
func NewBeaconChain(db essdb.Database) (*BeaconChain, error) { | ||
beaconChain := &BeaconChain{ | ||
db: db, | ||
state: &beaconState{}, | ||
} | ||
has, err := db.Has([]byte(stateLookupKey)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !has { | ||
log.Info("No chainstate found on disk, initializing beacon from genesis") | ||
active, crystallized := types.NewGenesisStates() | ||
beaconChain.state.ActiveState = active | ||
beaconChain.state.CrystallizedState = crystallized | ||
return beaconChain, nil | ||
} | ||
enc, err := db.Get([]byte(stateLookupKey)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Deserializes the encoded object into a beacon chain. | ||
if err := rlp.DecodeBytes(enc, &beaconChain.state); err != nil { | ||
return nil, fmt.Errorf("could not deserialize chainstate from disk: %v", err) | ||
} | ||
return beaconChain, nil | ||
} | ||
|
||
// ActiveState exposes a getter to external services. | ||
func (b *BeaconChain) ActiveState() *types.ActiveState { | ||
return b.state.ActiveState | ||
} | ||
|
||
// CrystallizedState exposes a getter to external services. | ||
func (b *BeaconChain) CrystallizedState() *types.CrystallizedState { | ||
return b.state.CrystallizedState | ||
} | ||
|
||
// GenesisBlock returns the canonical, genesis block. | ||
func (b *BeaconChain) GenesisBlock() *types.Block { | ||
return types.NewGenesisBlock() | ||
} | ||
|
||
// MutateActiveState allows external services to modify the active state. | ||
func (b *BeaconChain) MutateActiveState(activeState *types.ActiveState) error { | ||
defer b.lock.Unlock() | ||
b.lock.Lock() | ||
b.state.ActiveState = activeState | ||
return b.persist() | ||
} | ||
|
||
// MutateCrystallizedState allows external services to modify the crystallized state. | ||
func (b *BeaconChain) MutateCrystallizedState(crystallizedState *types.CrystallizedState) error { | ||
defer b.lock.Unlock() | ||
b.lock.Lock() | ||
b.state.CrystallizedState = crystallizedState | ||
return b.persist() | ||
} | ||
|
||
// CanProcessBlock decides if an incoming p2p block can be processed into the chain's block trie. | ||
func (b *BeaconChain) CanProcessBlock(fetcher powchain.POWBlockFetcher, block *types.Block) (bool, error) { | ||
mainchainBlock, err := fetcher.BlockByHash(context.Background(), block.Data().MainChainRef) | ||
if err != nil { | ||
return false, err | ||
} | ||
// There needs to be a valid mainchain block for the reference hash in a beacon block. | ||
if mainchainBlock == nil { | ||
return false, nil | ||
} | ||
// TODO: check if the parentHash pointed by the beacon block is in the beaconDB. | ||
|
||
// Calculate the timestamp validity condition. | ||
slotDuration := time.Duration(block.Data().SlotNumber*params.SlotLength) * time.Second | ||
validTime := time.Now().After(b.GenesisBlock().Data().Timestamp.Add(slotDuration)) | ||
return validTime, nil | ||
} | ||
|
||
// persist stores the RLP encoding of the latest beacon chain state into the db. | ||
func (b *BeaconChain) persist() error { | ||
encodedState, err := rlp.EncodeToBytes(b.state) | ||
if err != nil { | ||
return err | ||
} | ||
return b.db.Put([]byte(stateLookupKey), encodedState) | ||
} | ||
|
||
// computeNewActiveState computes a new active state for every beacon block. | ||
func (b *BeaconChain) computeNewActiveState(seed common.Hash) (*types.ActiveState, error) { | ||
|
||
attesters, proposer, err := b.getAttestersProposer(seed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// TODO: Verify attestations from attesters. | ||
log.WithFields(logrus.Fields{"attestersIndices": attesters}).Debug("Attester indices") | ||
|
||
// TODO: Verify main signature from proposer. | ||
log.WithFields(logrus.Fields{"proposerIndex": proposer}).Debug("Proposer index") | ||
|
||
// TODO: Update crosslink records (post Ruby release). | ||
|
||
// TODO: Track reward for the proposer that just proposed the latest beacon block. | ||
|
||
// TODO: Verify randao reveal from validator's hash pre image. | ||
|
||
return &types.ActiveState{ | ||
TotalAttesterDeposits: 0, | ||
AttesterBitfields: []byte{}, | ||
}, nil | ||
} | ||
|
||
// getAttestersProposer returns lists of random sampled attesters and proposer indices. | ||
func (b *BeaconChain) getAttestersProposer(seed common.Hash) ([]int, int, error) { | ||
attesterCount := math.Min(params.AttesterCount, float64(len(b.CrystallizedState().ActiveValidators))) | ||
indices, err := utils.ShuffleIndices(seed, len(b.CrystallizedState().ActiveValidators)) | ||
if err != nil { | ||
return nil, -1, err | ||
} | ||
return indices[:int(attesterCount)], indices[len(indices)-1], nil | ||
} |
Oops, something went wrong.