diff --git a/core/blockchain.go b/core/blockchain.go index 1ad96c429..fddf75b52 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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) } } diff --git a/core/resharding.go b/core/resharding.go index 18de1795a..c07aa44a1 100644 --- a/core/resharding.go +++ b/core/resharding.go @@ -86,8 +86,11 @@ 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++ { - Shuffle(ss.shardState[i].NodeList) - ss.shardState[i].Leader = ss.shardState[i].NodeList[0] + // 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] + } } } @@ -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)) diff --git a/node/node.go b/node/node.go index 73adfb43a..a536e2bff 100644 --- a/node/node.go +++ b/node/node.go @@ -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) diff --git a/node/node_handler.go b/node/node_handler.go index 6ea3e0a06..9dbf0cefa 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -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 { diff --git a/test/configs/beaconchain.txt b/test/configs/beaconchain.txt index a6cfdea2e..cf674290b 100644 --- a/test/configs/beaconchain.txt +++ b/test/configs/beaconchain.txt @@ -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