This commit is contained in:
obscuren
2015-03-19 16:59:13 +01:00
12 changed files with 273 additions and 176 deletions

View File

@ -16,10 +16,6 @@ import (
"gopkg.in/fatih/set.v0"
)
type PendingBlockEvent struct {
Block *types.Block
}
var statelogger = logger.NewLogger("BLOCK")
type BlockProcessor struct {
@ -137,7 +133,7 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
block.Header().GasUsed = totalUsedGas
if transientProcess {
go self.eventMux.Post(PendingBlockEvent{block})
go self.eventMux.Post(PendingBlockEvent{block, statedb.Logs()})
}
return receipts, handled, unhandled, erroneous, err
@ -146,25 +142,25 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
// Process block will attempt to process the given block's transactions and applies them
// on top of the block's parent state (given it exists) and will return wether it was
// successful or not.
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, err error) {
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, logs state.Logs, err error) {
// Processing a blocks may never happen simultaneously
sm.mutex.Lock()
defer sm.mutex.Unlock()
header := block.Header()
if sm.bc.HasBlock(header.Hash()) {
return nil, &KnownBlockError{header.Number, header.Hash()}
return nil, nil, &KnownBlockError{header.Number, header.Hash()}
}
if !sm.bc.HasBlock(header.ParentHash) {
return nil, ParentError(header.ParentHash)
return nil, nil, ParentError(header.ParentHash)
}
parent := sm.bc.GetBlock(header.ParentHash)
return sm.processWithParent(block, parent)
}
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, err error) {
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, logs state.Logs, err error) {
sm.lastAttemptedBlock = block
// Create a new state based on the parent's root (e.g., create copy)
@ -177,7 +173,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
// There can be at most two uncles
if len(block.Uncles()) > 2 {
return nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
return nil, nil, ValidationError("Block can only contain one uncle (contained %v)", len(block.Uncles()))
}
receipts, err := sm.TransitionState(state, parent, block, false)
@ -236,7 +232,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big
chainlogger.Infof("processed block #%d (%x...)\n", header.Number, block.Hash().Bytes()[0:4])
return td, nil
return td, state.Logs(), nil
}
// Validates the current block. Returns an error if the block was invalid,

View File

@ -93,7 +93,7 @@ func makeChain(bman *BlockProcessor, parent *types.Block, max int, db common.Dat
blocks := make(types.Blocks, max)
for i := 0; i < max; i++ {
block := makeBlock(bman, parent, i, db, seed)
td, err := bman.processWithParent(block, parent)
td, _, err := bman.processWithParent(block, parent)
if err != nil {
fmt.Println("process with parent failed", err)
panic(err)

View File

@ -410,7 +410,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
for i, block := range chain {
// Call in to the block processor and check for errors. It's likely that if one block fails
// all others will fail too (unless a known block is returned).
td, err := self.processor.Process(block)
td, logs, err := self.processor.Process(block)
if err != nil {
if IsKnownBlockErr(err) {
continue
@ -438,29 +438,27 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
hash := block.Hash()
chainlogger.Infof("Split detected. New head #%v (%x) TD=%v, was #%v (%x) TD=%v\n", block.Header().Number, hash[:4], td, cblock.Header().Number, chash[:4], self.td)
queue[i] = ChainSplitEvent{block}
queue[i] = ChainSplitEvent{block, logs}
queueEvent.splitCount++
}
self.setTotalDifficulty(td)
self.insert(block)
/* XXX crashes
jsonlogger.LogJson(&logger.EthChainNewHead{
BlockHash: common.Bytes2Hex(block.Hash()),
BlockHash: block.Hash().Hex(),
BlockNumber: block.Number(),
ChainHeadHash: common.Bytes2Hex(cblock.Hash()),
BlockPrevHash: common.Bytes2Hex(block.ParentHash()),
ChainHeadHash: cblock.Hash().Hex(),
BlockPrevHash: block.ParentHash().Hex(),
})
*/
self.setTransState(state.New(block.Root(), self.stateDb))
self.setTxState(state.New(block.Root(), self.stateDb))
queue[i] = ChainEvent{block}
queue[i] = ChainEvent{block, logs}
queueEvent.canonicalCount++
} else {
queue[i] = ChainSideEvent{block}
queue[i] = ChainSideEvent{block, logs}
queueEvent.sideCount++
}
}

View File

@ -1,6 +1,9 @@
package core
import "github.com/ethereum/go-ethereum/core/types"
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/state"
)
// TxPreEvent is posted when a transaction enters the transaction pool.
type TxPreEvent struct{ Tx *types.Transaction }
@ -15,11 +18,25 @@ type NewBlockEvent struct{ Block *types.Block }
type NewMinedBlockEvent struct{ Block *types.Block }
// ChainSplit is posted when a new head is detected
type ChainSplitEvent struct{ Block *types.Block }
type ChainSplitEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainEvent struct{ Block *types.Block }
type ChainEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainSideEvent struct{ Block *types.Block }
type ChainSideEvent struct {
Block *types.Block
Logs state.Logs
}
type PendingBlockEvent struct {
Block *types.Block
Logs state.Logs
}
type ChainHeadEvent struct{ Block *types.Block }

View File

@ -33,8 +33,8 @@ type Filter struct {
max int
topics [][]common.Hash
BlockCallback func(*types.Block)
PendingCallback func(*types.Block)
BlockCallback func(*types.Block, state.Logs)
PendingCallback func(*types.Block, state.Logs)
LogsCallback func(state.Logs)
}

View File

@ -1,14 +1,16 @@
package types
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/common"
"fmt"
)
type BlockProcessor interface {
Process(*Block) (*big.Int, error)
Process(*Block) (*big.Int, state.Logs, error)
}
const bloomLength = 256