Merge pull request #78 from harmony-one/rj_branch

add transactions into blocks in account model
pull/81/head
Rongjian Lan 6 years ago committed by GitHub
commit 23eab54a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      consensus/bft.go
  2. 4
      core/blockchain.go
  3. 6
      core/evm.go
  4. 27
      harmony/main.go

@ -4,6 +4,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/crypto/sha3" "github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/harmony-one/harmony/core/types" "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) { 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 // Accumulate any block and uncle rewards and commit the final state root
// Header seems complete, assemble into a block and return // 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 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 { func (bft *Bft) Seal(chain ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
return nil 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) {
}

@ -897,7 +897,9 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
currentBlock := bc.CurrentBlock() currentBlock := bc.CurrentBlock()
localTd := bc.GetTd(currentBlock.Hash(), currentBlock.NumberU64()) 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 // Irrelevant of the canonical status, write the block itself to the database
if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil { if err := bc.hc.WriteTd(block.Hash(), block.NumberU64(), externTd); err != nil {

@ -52,9 +52,9 @@ func NewEVMContext(msg Message, header *types.Header, chain ChainContext, author
Coinbase: beneficiary, Coinbase: beneficiary,
BlockNumber: new(big.Int).Set(header.Number), BlockNumber: new(big.Int).Set(header.Number),
Time: new(big.Int).Set(header.Time), Time: new(big.Int).Set(header.Time),
Difficulty: new(big.Int).Set(header.Difficulty), //Difficulty: new(big.Int).Set(header.Difficulty),
GasLimit: header.GasLimit, GasLimit: header.GasLimit,
GasPrice: new(big.Int).Set(msg.GasPrice()), GasPrice: new(big.Int).Set(msg.GasPrice()),
} }
} }

@ -29,6 +29,17 @@ var (
newTxs []*types.Transaction 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 { type testWorkerBackend struct {
db ethdb.Database db ethdb.Database
txPool *core.TxPool txPool *core.TxPool
@ -36,7 +47,6 @@ type testWorkerBackend struct {
} }
func main() { func main() {
var ( var (
database = ethdb.NewMemDatabase() database = ethdb.NewMemDatabase()
gspec = core.Genesis{ gspec = core.Genesis{
@ -49,7 +59,10 @@ func main() {
chain, _ := core.NewBlockChain(database, nil, gspec.Config, consensus.NewFaker(), vm.Config{}, nil) 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) txpool := core.NewTxPool(core.DefaultTxPoolConfig, chainConfig, chain)
backend := &testWorkerBackend{ backend := &testWorkerBackend{
@ -60,13 +73,21 @@ func main() {
backend.txPool.AddLocals(pendingTxs) backend.txPool.AddLocals(pendingTxs)
// Generate a small n-block chain and an uncle block for it // Generate a small n-block chain and an uncle block for it
n := 10 n := 3
if n > 0 { if n > 0 {
blocks, _ := core.GenerateChain(chainConfig, genesis, consensus.NewFaker(), database, n, func(i int, gen *core.BlockGen) { blocks, _ := core.GenerateChain(chainConfig, genesis, consensus.NewFaker(), database, n, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testBankAddress) gen.SetCoinbase(testBankAddress)
gen.AddTx(pendingTxs[i])
}) })
if _, err := chain.InsertChain(blocks); err != nil { if _, err := chain.InsertChain(blocks); err != nil {
fmt.Errorf("failed to insert origin chain: %v", err) 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")
} }
} }

Loading…
Cancel
Save