Setup genesis shard state, and fix other issues

pull/560/head
Rongjian Lan 6 years ago
parent f4748d7bcc
commit 8fa84d2230
  1. 8
      core/blockchain.go
  2. 6
      core/resharding.go
  3. 3
      node/node.go
  4. 4
      node/node_handler.go
  5. 1
      test/configs/beaconchain.txt

@ -1732,8 +1732,8 @@ func (bc *BlockChain) ValidateNewShardState(block *types.Block, stakeInfo *map[c
return nil
}
// InsertNewShardState insert new shard state into epoch block
func (bc *BlockChain) InsertNewShardState(block *types.Block, stakeInfo *map[common.Address]*structs.StakeInfo) {
// StoreNewShardState insert new shard state into epoch block
func (bc *BlockChain) StoreNewShardState(block *types.Block, stakeInfo *map[common.Address]*structs.StakeInfo) {
// write state into db.
shardState := bc.GetNewShardState(block, stakeInfo)
if shardState == nil {
@ -1742,8 +1742,8 @@ func (bc *BlockChain) InsertNewShardState(block *types.Block, stakeInfo *map[com
hash := block.Hash()
number := block.NumberU64()
rawdb.WriteShardState(bc.db, hash, number, shardState)
utils.GetLogInstance().Debug("[resharding] save new shard state success", "shardStateHash", shardState.Hash())
utils.GetLogInstance().Debug("[Resharding] Saved new shard state success", "shardStateHash", shardState.Hash())
for _, c := range shardState {
utils.GetLogInstance().Debug("[resharding] new shard information", "shardID", c.ShardID, "NodeList", c.NodeList)
utils.GetLogInstance().Debug("[Resharding] Shard Info", "shardID", c.ShardID, "NodeList", c.NodeList)
}
}

@ -86,9 +86,12 @@ func (ss *ShardingState) cuckooResharding(percent float64) {
// assignLeaders will first add new nodes into shards, then use cuckoo rule to reshard to get new shard state
func (ss *ShardingState) assignLeaders() {
for i := 0; i < ss.numShards; i++ {
// At genesis epoch, the shards are empty.
if len(ss.shardState[i].NodeList) > 0 {
Shuffle(ss.shardState[i].NodeList)
ss.shardState[i].Leader = ss.shardState[i].NodeList[0]
}
}
}
// Reshard will first add new nodes into shards, then use cuckoo rule to reshard to get new shard state
@ -143,6 +146,7 @@ func CalculateNewShardState(bc *BlockChain, epoch uint64, stakeInfo *map[common.
ss := GetShardingStateFromBlockChain(bc, epoch-1)
newNodeList := ss.UpdateShardingState(stakeInfo)
percent := ss.calculateKickoutRate(newNodeList)
utils.GetLogInstance().Info("Kickout Percentage", "percentage", percent)
ss.Reshard(newNodeList, percent)
return ss.shardState
}
@ -201,7 +205,7 @@ func GetInitShardState() types.ShardState {
if i == 0 {
for j := 0; j < GenesisShardSize; j++ {
priKey := bls.SecretKey{}
priKey.SetHexString(contract.InitialBeaconChainAccounts[i].Private)
priKey.SetHexString(contract.InitialBeaconChainAccounts[j].Private)
addrBytes := priKey.GetPublicKey().GetAddress()
address := hex.EncodeToString(addrBytes[:])
com.NodeList = append(com.NodeList, types.NodeID(address))

@ -240,6 +240,9 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N
os.Exit(1)
}
node.blockchain = chain
// Store the genesis shard state into db.
node.blockchain.StoreNewShardState(node.blockchain.CurrentBlock(), nil)
node.BlockChannel = make(chan *types.Block)
node.ConfirmedBlockChannel = make(chan *types.Block)
node.BeaconBlockChannel = make(chan *types.Block)

@ -154,7 +154,7 @@ func (node *Node) messageHandler(content []byte, sender string) {
utils.GetLogInstance().Info("NET: received message: Node/Control")
controlType := msgPayload[0]
if proto_node.ControlMessageType(controlType) == proto_node.STOP {
utils.GetLogInstance().Debug("Stopping Node", "node", node, "numBlocks", node.blockchain.CurrentBlock().NumberU64(), "numTxsProcessed", node.countNumTransactionsInBlockchain())
utils.GetLogInstance().Debug("Stopping Node", "numBlocks", node.blockchain.CurrentBlock().NumberU64(), "numTxsProcessed", node.countNumTransactionsInBlockchain())
var avgBlockSizeInBytes common.StorageSize
txCount := 0
@ -298,7 +298,7 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) {
func (node *Node) AddNewBlock(newBlock *types.Block) {
blockNum, err := node.blockchain.InsertChain([]*types.Block{newBlock})
node.blockchain.InsertNewShardState(newBlock, &node.CurrentStakes)
node.blockchain.StoreNewShardState(newBlock, &node.CurrentStakes)
if err != nil {
utils.GetLogInstance().Debug("Error adding new block to blockchain", "blockNum", blockNum, "Error", err)
} else {

@ -8,3 +8,4 @@
127.0.0.1 9007 validator 0
127.0.0.1 9008 validator 0
127.0.0.1 9009 validator 0
127.0.0.1 19999 client 0

Loading…
Cancel
Save