Add ShardId into genesis block generation

pull/105/head
Rongjian Lan 6 years ago
parent 4176ffe50c
commit e2ee1e0597
  1. 5
      core/chain_makers.go
  2. 2
      core/genesis.go
  3. 7
      core/types/block.go
  4. 9
      harmony/main.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.
//

@ -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,

@ -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[:])

@ -53,6 +53,7 @@ func main() {
gspec = core.Genesis{
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))
}

Loading…
Cancel
Save