diff --git a/core/chain_makers.go b/core/chain_makers.go index 6ecf0014b..6da870aa2 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -71,6 +71,11 @@ func (b *BlockGen) SetNonce(nonce types.BlockNonce) { b.header.Nonce = nonce } +// SetShardId sets the shardId field of the generated block. +func (b *BlockGen) SetShardId(shardId types.ShardId) { + b.header.ShardId = shardId +} + // AddTx adds a transaction to the generated block. If no coinbase has // been set, the block's coinbase is set to the zero address. // diff --git a/core/genesis.go b/core/genesis.go index f73900b8d..4828a4805 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -45,6 +45,7 @@ var errGenesisNoConfig = errors.New("genesis has no chain configuration") type Genesis struct { Config *params.ChainConfig `json:"config"` Nonce uint64 `json:"nonce"` + ShardId uint16 `json:"shardId"` Timestamp uint64 `json:"timestamp"` ExtraData []byte `json:"extraData"` GasLimit uint64 `json:"gasLimit" gencodec:"required"` @@ -234,6 +235,7 @@ func (g *Genesis) ToBlock(db hdb.Database) *types.Block { head := &types.Header{ Number: new(big.Int).SetUint64(g.Number), Nonce: types.EncodeNonce(g.Nonce), + ShardId: types.EncodeShardId(g.ShardId), Time: new(big.Int).SetUint64(g.Timestamp), ParentHash: g.ParentHash, Extra: g.ExtraData, diff --git a/core/types/block.go b/core/types/block.go index 28454d09b..bb6d161df 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -52,6 +52,13 @@ func EncodeNonce(i uint64) BlockNonce { return n } +// EncodeShardId converts the given integer to a shard id. +func EncodeShardId(i uint16) ShardId { + var n ShardId + binary.BigEndian.PutUint16(n[:], i) + return n +} + // Uint64 returns the integer value of a block nonce. func (n BlockNonce) Uint64() uint64 { return binary.BigEndian.Uint64(n[:]) diff --git a/harmony/main.go b/harmony/main.go index 99ca260d5..be0df947d 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -51,8 +51,9 @@ func main() { var ( database = db.NewMemDatabase() gspec = core.Genesis{ - Config: chainConfig, - Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, + Config: chainConfig, + Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, + ShardId: 10, } ) @@ -74,6 +75,7 @@ func main() { if n > 0 { blocks, _ := core.GenerateChain(chainConfig, genesis, consensus.NewFaker(), database, n, func(i int, gen *core.BlockGen) { gen.SetCoinbase(testBankAddress) + gen.SetShardId(types.EncodeShardId(10)) gen.AddTx(pendingTxs[i]) }) if _, err := chain.InsertChain(blocks); err != nil { @@ -83,16 +85,13 @@ func main() { txs := make([]*types.Transaction, 100) worker := worker.New(params.TestChainConfig, chain, consensus.NewFaker()) - fmt.Println(worker.GetCurrentState().GetBalance(testBankAddress)) - + nonce := worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(testBankKey.PublicKey)) for i, _ := range txs { randomUserKey, _ := crypto.GenerateKey() randomUserAddress := crypto.PubkeyToAddress(randomUserKey.PublicKey) - tx, _ := types.SignTx(types.NewTransaction(worker.GetCurrentState().GetNonce(crypto.PubkeyToAddress(testBankKey.PublicKey)), randomUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + tx, _ := types.SignTx(types.NewTransaction(nonce+uint64(i), randomUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) txs[i] = tx } worker.CommitTransactions(txs, crypto.PubkeyToAddress(testBankKey.PublicKey)) - - fmt.Println(worker.GetCurrentState().GetBalance(testBankAddress)) }