Skip to content

Commit

Permalink
Upgraded sness
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 267 changed files with 7,754 additions and 41,620 deletions.
15 changes: 0 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,3 @@ bazel-*
# Coverage outputs
coverage.txt
profile.out

build/_workspace
/src

\.directory

build/bin/

voter/voter

beacon-chain/beacon-chain

*.debug

sness/sness
9 changes: 9 additions & 0 deletions beacon-chain/README.md
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.
12 changes: 6 additions & 6 deletions sness/VOTER_REGISTER.md → beacon-chain/VALIDATOR_REGISTER.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Validator Registration Workflow

This doc summarizes the work flow of registering to become a voter in the beacon chain. The scope is within Ruby Release.
This doc summarizes the work flow of registering to become a validator in the beacon chain. The scope is within Ruby Release.

### Step 1: Deploy voter registration contract if it hasn't been done
To deploy VRC, we can use [deployVRC](https://github.com/terenc3t/gess-sharding/tree/contract-util/contracts/deployVRC) utility.
### Step 1: Deploy validator registration contract if it hasn't been done
To deploy VRC, we can use [deployVRC](https://github.com/terenc3t/geth-sharding/tree/contract-util/contracts/deployVRC) utility.
Once we get the VRC contract address, we can launch our beacon chain node
```
# Deploy VRC with keystore UTCJSON and password
Expand All @@ -14,10 +14,10 @@ go run deployVRC.go --privKey 8a6db3b30934439c9f71f1fa777019810fd538c9c1e396809b
INFO[0039] New contract deployed at 0x559eDab2b5896C2Bc37951325666ed08CD41099d
```

### Step 2: Launch beacon chain nodeccount holder's publi
Launch beacon chain node with ac key and the VRC address we just deployed
### Step 2: Launch beacon chain node
Launch beacon chain node with account holder's public key and the VRC address we just deployed
```
./bazel-nbin/path/to/your/beacon-chain/binary --vrcaddr 0x527580dd995c0ab81d01f9993eb39166796877a1 --pubkey aaace816cdab194b4bc6c0de3575ccf917a9b9ecfead263720968e0e1b45739c
./bazel-bin/path/to/your/beacon-chain/binary --vrcaddr 0x527580dd995c0ab81d01f9993eb39166796877a1 --pubkey aaace816cdab194b4bc6c0de3575ccf917a9b9ecfead263720968e0e1b45739c
```

Expand Down
155 changes: 155 additions & 0 deletions beacon-chain/blockchain/core.go
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
}
Loading

0 comments on commit 31ee0af

Please sign in to comment.