Resolved some bugs in the miner

* TODO nonce error sometimes persists
* Fixed mining on wrong blocks
* Fixed state error & receipt fail
This commit is contained in:
obscuren 2015-02-15 16:16:27 +01:00
parent c924a841c7
commit 2c3a014f03
3 changed files with 12 additions and 13 deletions

@ -177,7 +177,7 @@ Rectangle {
mainContractColumn.state = "ERROR" mainContractColumn.state = "ERROR"
} else { } else {
txResult.text = "Your transaction has been submitted:\n" txResult.text = "Your transaction has been submitted:\n"
txOutput.text = res[0].address txOutput.text = res.toString()
mainContractColumn.state = "DONE" mainContractColumn.state = "DONE"
console.log(res) console.log(res)

@ -392,7 +392,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
self.setTotalDifficulty(td) self.setTotalDifficulty(td)
self.insert(block) self.insert(block)
self.transState = state.New(cblock.Root(), self.db) //state.New(cblock.Trie().Copy()) self.transState = state.New(cblock.Root(), self.db)
self.eventMux.Post(ChainEvent{block, td}) self.eventMux.Post(ChainEvent{block, td})
} }

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"sort" "sort"
"sync"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -55,6 +56,7 @@ type Agent interface {
} }
type worker struct { type worker struct {
mu sync.Mutex
agents []Agent agents []Agent
recv chan Work recv chan Work
mux *event.TypeMux mux *event.TypeMux
@ -115,9 +117,7 @@ out:
select { select {
case event := <-events.Chan(): case event := <-events.Chan():
switch event.(type) { switch event.(type) {
case core.ChainEvent: case core.ChainEvent, core.TxPreEvent:
self.commitNewWork()
case core.TxPreEvent:
self.commitNewWork() self.commitNewWork()
} }
case <-self.quit: case <-self.quit:
@ -163,6 +163,9 @@ func (self *worker) push() {
} }
func (self *worker) commitNewWork() { func (self *worker) commitNewWork() {
self.mu.Lock()
defer self.mu.Unlock()
self.current = env(self.chain.NewBlock(self.coinbase), self.eth) self.current = env(self.chain.NewBlock(self.coinbase), self.eth)
parent := self.chain.GetBlock(self.current.block.ParentHash()) parent := self.chain.GetBlock(self.current.block.ParentHash())
self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block))
@ -176,12 +179,11 @@ func (self *worker) commitNewWork() {
err := self.commitTransaction(tx) err := self.commitTransaction(tx)
switch { switch {
case core.IsNonceErr(err): case core.IsNonceErr(err):
// Remove invalid transactions
remove = append(remove, tx) remove = append(remove, tx)
case core.IsGasLimitErr(err): case core.IsGasLimitErr(err):
// Break on gas limit // Break on gas limit
break break
default:
remove = append(remove, tx)
} }
if err != nil { if err != nil {
@ -227,16 +229,13 @@ func (self *worker) commitUncle(uncle *types.Header) error {
func (self *worker) commitTransaction(tx *types.Transaction) error { func (self *worker) commitTransaction(tx *types.Transaction) error {
snapshot := self.current.state.Copy() snapshot := self.current.state.Copy()
receipt, txGas, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true) receipt, _, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true)
if err != nil { if err != nil && (core.IsNonceErr(err) || core.IsGasLimitErr(err)) {
if core.IsNonceErr(err) || core.IsGasLimitErr(err) { self.current.state.Set(snapshot)
self.current.state.Set(snapshot)
}
return err return err
} }
self.current.totalUsedGas.Add(self.current.totalUsedGas, txGas)
self.current.block.AddTransaction(tx) self.current.block.AddTransaction(tx)
self.current.block.AddReceipt(receipt) self.current.block.AddReceipt(receipt)