Add ShardState

pull/839/head
Eugene Kim 6 years ago
parent 8a22ae1aca
commit c04b94858d
  1. 11
      consensus/consensus_validator_test.go
  2. 2
      core/genesis.go
  3. 11
      core/types/block.go
  4. 2
      core/types/shard_state.go
  5. 21
      node/node_genesis.go
  6. 11
      node/node_newblock.go

@ -117,10 +117,10 @@ func TestProcessMessageValidatorAnnounce(test *testing.T) {
func testBlockBytes() ([]byte, error) {
return hex.DecodeString("" +
// BEGIN 673-byte Block
"f902a1" +
// BEGIN 668-byte header
"f9029c" +
// BEGIN 674-byte Block
"f902a2" +
// BEGIN 669-byte header
"f9029d" +
// 32-byte ParentHash
"a0" +
"0000000000000000000000000000000000000000000000000000000000000000" +
@ -195,6 +195,9 @@ func testBlockBytes() ([]byte, error) {
// 32-byte ShardStateHash
"a0" +
"0000000000000000000000000000000000000000000000000000000000000000" +
// BEGIN 0-byte ShardState
"c0" +
// END ShardState
// END header
// BEGIN 0-byte uncles
"c0" +

@ -56,6 +56,7 @@ type Genesis struct {
Coinbase common.Address `json:"coinbase"`
Alloc GenesisAlloc `json:"alloc" gencodec:"required"`
ShardStateHash common.Hash `json:"shardStateHash"`
ShardState types.ShardState `json:"shardState"`
// These fields are used for consensus tests. Please don't use them
// in actual genesis blocks.
@ -251,6 +252,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
Coinbase: g.Coinbase,
Root: root,
ShardStateHash: g.ShardStateHash,
ShardState: g.ShardState,
}
if g.GasLimit == 0 {
head.GasLimit = 10000000000 // TODO(RJ): figure out better solution. // params.GenesisGasLimit

@ -92,6 +92,7 @@ type Header struct {
RandPreimage [32]byte `json:"randPreimage"`
RandSeed [32]byte `json:"randSeed"`
ShardStateHash common.Hash `json:"shardStateRoot"`
ShardState ShardState `json:"shardState"`
}
// field type overrides for gencodec
@ -475,7 +476,11 @@ func (b *Block) AddRandPreimage(pRnd [32]byte) {
b.header.RandPreimage = pRnd
}
// AddShardStateHash add shardStateHash into block header
func (b *Block) AddShardStateHash(shardStateHash common.Hash) {
b.header.ShardStateHash = shardStateHash
// AddShardState add shardState into block header
func (b *Block) AddShardState(shardState ShardState) {
// Make a copy because ShardState.Hash() internally sorts entries.
// Store the sorted copy.
shardState = append(shardState[:0:0], shardState...)
b.header.ShardStateHash = shardState.Hash()
b.header.ShardState = shardState
}

@ -156,6 +156,8 @@ func GetHashFromNodeList(nodeList []NodeID) []byte {
// Hash is the root hash of ShardState
func (ss ShardState) Hash() (h common.Hash) {
// TODO ek – this sorting really doesn't belong here; it should instead
// be made an explicit invariant to be maintained and, if needed, checked.
sort.Slice(ss, func(i, j int) bool {
return ss[i].ShardID < ss[j].ShardID
})

@ -12,7 +12,9 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/internal/utils/contract"
)
@ -27,6 +29,9 @@ const (
// GenesisBlockSetup setups a genesis blockchain.
func (node *Node) GenesisBlockSetup(db ethdb.Database, shardID uint32, isArchival bool) (*core.BlockChain, error) {
logger := utils.GetLogInstance().New(
"shardID", shardID,
"isArchival", isArchival)
// Initialize genesis block and blockchain
// Tests account for txgen to use
@ -53,11 +58,25 @@ func (node *Node) GenesisBlockSetup(db ethdb.Database, shardID uint32, isArchiva
// TODO: create separate chain config instead of using the same pointer reference
chainConfig := *params.TestChainConfig
chainConfig.ChainID = big.NewInt(int64(shardID)) // Use ChainID as piggybacked ShardID
shardState := core.GetInitShardState()
var localShardState types.ShardState
var localShardStateHash common.Hash
for idx, oneShardState := range shardState {
if oneShardState.ShardID == shardID {
localShardState = shardState[idx : idx+1]
localShardStateHash = localShardState.Hash()
break
}
}
if localShardState == nil {
logger.Warn("initial shard state did not contain our shard")
}
gspec := core.Genesis{
Config: &chainConfig,
Alloc: genesisAlloc,
ShardID: shardID,
ShardStateHash: core.GetInitShardState().Hash(),
ShardStateHash: localShardStateHash,
ShardState: localShardState,
}
// Store genesis block into db.

@ -63,7 +63,7 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}, stopChan chan
if node.Consensus.ShardID == 0 {
// add new shard state if it's epoch block
// TODO: bug fix - the stored shard state between here and PostConsensusProcessing are different.
//node.addNewShardStateHash(block)
//node.addNewShardState(block)
}
newBlock = block
utils.GetLogInstance().Debug("Successfully proposed new block", "blockNum", block.NumberU64(), "numTxs", block.Transactions().Len())
@ -85,12 +85,13 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}, stopChan chan
}()
}
func (node *Node) addNewShardStateHash(block *types.Block) {
func (node *Node) addNewShardState(block *types.Block) {
logger := utils.GetLogInstance().New("blockHash", block.Hash())
shardState := node.blockchain.GetNewShardState(block, &node.CurrentStakes)
if shardState != nil {
shardHash := shardState.Hash()
utils.GetLogInstance().Debug("[Shard State Hash] adding new shard state", "shardHash", shardHash)
block.AddShardStateHash(shardHash)
logger.Debug("[Shard State Hash] adding new shard state", "shardHash", shardHash)
block.AddShardState(shardState)
}
}
@ -144,7 +145,7 @@ func (node *Node) WaitForConsensusReadyv2(readySignal chan struct{}, stopChan ch
if node.Consensus.ShardID == 0 {
// add new shard state if it's epoch block
// TODO: bug fix - the stored shard state between here and PostConsensusProcessing are different.
//node.addNewShardStateHash(block)
//node.addNewShardState(block)
}
newBlock := block
utils.GetLogInstance().Debug("Successfully proposed new block", "blockNum", block.NumberU64(), "numTxs", block.Transactions().Len())

Loading…
Cancel
Save