From e0a6d6bb3fe98778c45b91071e4289962406a93b Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Mon, 19 Nov 2018 19:52:37 -0800 Subject: [PATCH] add transactions into blocks in account model --- consensus/bft.go | 10 ++++++++++ core/blockchain.go | 4 +++- core/evm.go | 6 +++--- harmony/main.go | 27 ++++++++++++++++++++++++--- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/consensus/bft.go b/consensus/bft.go index 641c60b38..ee4002f43 100644 --- a/consensus/bft.go +++ b/consensus/bft.go @@ -4,6 +4,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/crypto/sha3" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/core/types" ) @@ -78,6 +79,8 @@ func (bft *Bft) Prepare(chain ChainReader, header *types.Header) error { func (bft *Bft) Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) { // Accumulate any block and uncle rewards and commit the final state root // Header seems complete, assemble into a block and return + accumulateRewards(chain.Config(), state, header, uncles) + header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number)) return types.NewBlock(header, txs, uncles, receipts), nil } @@ -107,3 +110,10 @@ func (bft *Bft) SealHash(header *types.Header) (hash common.Hash) { func (bft *Bft) Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { return nil } + +// AccumulateRewards credits the coinbase of the given block with the mining +// reward. The total reward consists of the static block reward and rewards for +// included uncles. The coinbase of each uncle block is also rewarded. +func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header, uncles []*types.Header) { + +} diff --git a/core/blockchain.go b/core/blockchain.go index a76e5878d..37eb7e042 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -897,7 +897,9 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types. currentBlock := bc.CurrentBlock() localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) - externTd := new(big.Int).Add(block.Difficulty(), ptd) + // Set the block's own difficulty to 1 + // TODO: fix the difficulty issue + externTd := new(big.Int).Add(big.NewInt(1), ptd) // Irrelevant of the canonical status, write the block itself to the database if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil { diff --git a/core/evm.go b/core/evm.go index 164e11adb..a04c76a1d 100644 --- a/core/evm.go +++ b/core/evm.go @@ -52,9 +52,9 @@ func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author Coinbase: beneficiary, BlockNumber: new(big.Int).Set(header.Number), Time: new(big.Int).Set(header.Time), - Difficulty: new(big.Int).Set(header.Difficulty), - GasLimit: header.GasLimit, - GasPrice: new(big.Int).Set(msg.GasPrice()), + //Difficulty: new(big.Int).Set(header.Difficulty), + GasLimit: header.GasLimit, + GasPrice: new(big.Int).Set(msg.GasPrice()), } } diff --git a/harmony/main.go b/harmony/main.go index 02668d59f..c68ed6356 100644 --- a/harmony/main.go +++ b/harmony/main.go @@ -29,6 +29,17 @@ var ( newTxs []*types.Transaction ) +func init() { + tx1, _ := types.SignTx(types.NewTransaction(0, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + tx2, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + tx3, _ := types.SignTx(types.NewTransaction(2, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + pendingTxs = append(pendingTxs, tx1) + pendingTxs = append(pendingTxs, tx2) + pendingTxs = append(pendingTxs, tx3) + tx4, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) + newTxs = append(newTxs, tx4) +} + type testWorkerBackend struct { db ethdb.Database txPool *core.TxPool @@ -36,7 +47,6 @@ type testWorkerBackend struct { } func main() { - var ( database = ethdb.NewMemDatabase() gspec = core.Genesis{ @@ -49,7 +59,10 @@ func main() { chain, _ := core.NewBlockChain(database, nil, gspec.Config, consensus.NewFaker(), vm.Config{}, nil) - fmt.Println(chain) + fmt.Println(testBankAddress) + fmt.Println(testUserAddress) + fmt.Println(genesis.Root()) + txpool := core.NewTxPool(core.DefaultTxPoolConfig, chainConfig, chain) backend := &testWorkerBackend{ @@ -60,13 +73,21 @@ func main() { backend.txPool.AddLocals(pendingTxs) // Generate a small n-block chain and an uncle block for it - n := 10 + n := 3 if n > 0 { blocks, _ := core.GenerateChain(chainConfig, genesis, consensus.NewFaker(), database, n, func(i int, gen *core.BlockGen) { gen.SetCoinbase(testBankAddress) + gen.AddTx(pendingTxs[i]) }) if _, err := chain.InsertChain(blocks); err != nil { fmt.Errorf("failed to insert origin chain: %v", err) } + fmt.Println(blocks[0].NumberU64()) + fmt.Println(chain.GetBlockByNumber(0).Root()) + fmt.Println(chain.GetBlockByNumber(1).Root()) + fmt.Println(blocks[1].Root()) + fmt.Println(chain.GetBlockByNumber(2).Root()) + fmt.Println(blocks[2].Root()) + fmt.Println("Yeah") } }