BlockManager => BlockProcessor

This commit is contained in:
obscuren 2015-01-05 00:18:44 +01:00
parent 987119cd4a
commit c1dee15144
7 changed files with 116 additions and 36 deletions

View File

@ -234,7 +234,7 @@ func BlockDo(ethereum *eth.Ethereum, hash []byte) error {
parent := ethereum.ChainManager().GetBlock(block.ParentHash()) parent := ethereum.ChainManager().GetBlock(block.ParentHash())
_, err := ethereum.BlockManager().TransitionState(parent.State(), parent, block) _, err := ethereum.BlockProcessor().TransitionState(parent.State(), parent, block)
if err != nil { if err != nil {
return err return err
} }

View File

@ -22,7 +22,7 @@ import (
var statelogger = logger.NewLogger("BLOCK") var statelogger = logger.NewLogger("BLOCK")
type EthManager interface { type EthManager interface {
BlockManager() *BlockManager BlockProcessor() *BlockProcessor
ChainManager() *ChainManager ChainManager() *ChainManager
TxPool() *TxPool TxPool() *TxPool
PeerCount() int PeerCount() int
@ -35,7 +35,7 @@ type EthManager interface {
EventMux() *event.TypeMux EventMux() *event.TypeMux
} }
type BlockManager struct { type BlockProcessor struct {
// Mutex for locking the block processor. Blocks can only be handled one at a time // Mutex for locking the block processor. Blocks can only be handled one at a time
mutex sync.Mutex mutex sync.Mutex
// Canonical block chain // Canonical block chain
@ -57,8 +57,8 @@ type BlockManager struct {
eventMux *event.TypeMux eventMux *event.TypeMux
} }
func NewBlockManager(txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockManager { func NewBlockProcessor(txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
sm := &BlockManager{ sm := &BlockProcessor{
mem: make(map[string]*big.Int), mem: make(map[string]*big.Int),
Pow: ezp.New(), Pow: ezp.New(),
bc: chainManager, bc: chainManager,
@ -69,7 +69,7 @@ func NewBlockManager(txpool *TxPool, chainManager *ChainManager, eventMux *event
return sm return sm
} }
func (sm *BlockManager) TransitionState(statedb *state.StateDB, parent, block *types.Block) (receipts types.Receipts, err error) { func (sm *BlockProcessor) TransitionState(statedb *state.StateDB, parent, block *types.Block) (receipts types.Receipts, err error) {
coinbase := statedb.GetOrNewStateObject(block.Header().Coinbase) coinbase := statedb.GetOrNewStateObject(block.Header().Coinbase)
coinbase.SetGasPool(CalcGasLimit(parent, block)) coinbase.SetGasPool(CalcGasLimit(parent, block))
@ -82,7 +82,7 @@ func (sm *BlockManager) TransitionState(statedb *state.StateDB, parent, block *t
return receipts, nil return receipts, nil
} }
func (self *BlockManager) ApplyTransactions(coinbase *state.StateObject, state *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, types.Transactions, types.Transactions, types.Transactions, error) { func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state *state.StateDB, block *types.Block, txs types.Transactions, transientProcess bool) (types.Receipts, types.Transactions, types.Transactions, types.Transactions, error) {
var ( var (
receipts types.Receipts receipts types.Receipts
handled, unhandled types.Transactions handled, unhandled types.Transactions
@ -149,7 +149,7 @@ done:
return receipts, handled, unhandled, erroneous, err return receipts, handled, unhandled, erroneous, err
} }
func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Messages, err error) { func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, msgs state.Messages, err error) {
// Processing a blocks may never happen simultaneously // Processing a blocks may never happen simultaneously
sm.mutex.Lock() sm.mutex.Lock()
defer sm.mutex.Unlock() defer sm.mutex.Unlock()
@ -167,7 +167,7 @@ func (sm *BlockManager) Process(block *types.Block) (td *big.Int, msgs state.Mes
return sm.ProcessWithParent(block, parent) return sm.ProcessWithParent(block, parent)
} }
func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.Int, messages state.Messages, err error) { func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big.Int, messages state.Messages, err error) {
sm.lastAttemptedBlock = block sm.lastAttemptedBlock = block
state := state.New(parent.Trie().Copy()) state := state.New(parent.Trie().Copy())
@ -234,7 +234,7 @@ func (sm *BlockManager) ProcessWithParent(block, parent *types.Block) (td *big.I
} }
} }
func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) { func (sm *BlockProcessor) CalculateTD(block *types.Block) (*big.Int, bool) {
uncleDiff := new(big.Int) uncleDiff := new(big.Int)
for _, uncle := range block.Uncles() { for _, uncle := range block.Uncles() {
uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty) uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
@ -257,7 +257,7 @@ func (sm *BlockManager) CalculateTD(block *types.Block) (*big.Int, bool) {
// Validates the current block. Returns an error if the block was invalid, // Validates the current block. Returns an error if the block was invalid,
// an uncle or anything that isn't on the current block chain. // an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult) // Validation validates easy over difficult (dagger takes longer time = difficult)
func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error { func (sm *BlockProcessor) ValidateBlock(block, parent *types.Block) error {
expd := CalcDifficulty(block, parent) expd := CalcDifficulty(block, parent)
if expd.Cmp(block.Header().Difficulty) < 0 { if expd.Cmp(block.Header().Difficulty) < 0 {
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Header().Difficulty, expd) return fmt.Errorf("Difficulty check failed for block %v, %v", block.Header().Difficulty, expd)
@ -283,7 +283,7 @@ func (sm *BlockManager) ValidateBlock(block, parent *types.Block) error {
return nil return nil
} }
func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent *types.Block) error { func (sm *BlockProcessor) AccumelateRewards(statedb *state.StateDB, block, parent *types.Block) error {
reward := new(big.Int).Set(BlockReward) reward := new(big.Int).Set(BlockReward)
knownUncles := set.New() knownUncles := set.New()
@ -338,7 +338,7 @@ func (sm *BlockManager) AccumelateRewards(statedb *state.StateDB, block, parent
return nil return nil
} }
func (sm *BlockManager) GetMessages(block *types.Block) (messages []*state.Message, err error) { func (sm *BlockProcessor) GetMessages(block *types.Block) (messages []*state.Message, err error) {
if !sm.bc.HasBlock(block.Header().ParentHash) { if !sm.bc.HasBlock(block.Header().ParentHash) {
return nil, ParentError(block.Header().ParentHash) return nil, ParentError(block.Header().ParentHash)
} }

View File

@ -104,7 +104,7 @@ func (self *Filter) Find() []*state.Message {
// current parameters // current parameters
if self.bloomFilter(block) { if self.bloomFilter(block) {
// Get the messages of the block // Get the messages of the block
msgs, err := self.eth.BlockManager().GetMessages(block) msgs, err := self.eth.BlockProcessor().GetMessages(block)
if err != nil { if err != nil {
chainlogger.Warnln("err: filter get messages ", err) chainlogger.Warnln("err: filter get messages ", err)

View File

@ -52,11 +52,11 @@ type Ethereum struct {
//*** SERVICES *** //*** SERVICES ***
// State manager for processing new blocks and managing the over all states // State manager for processing new blocks and managing the over all states
blockManager *core.BlockManager blockProcessor *core.BlockProcessor
txPool *core.TxPool txPool *core.TxPool
chainManager *core.ChainManager chainManager *core.ChainManager
blockPool *BlockPool blockPool *BlockPool
whisper *whisper.Whisper whisper *whisper.Whisper
net *p2p.Server net *p2p.Server
eventMux *event.TypeMux eventMux *event.TypeMux
@ -122,8 +122,8 @@ func New(config *Config) (*Ethereum, error) {
eth.chainManager = core.NewChainManager(eth.EventMux()) eth.chainManager = core.NewChainManager(eth.EventMux())
eth.txPool = core.NewTxPool(eth.EventMux()) eth.txPool = core.NewTxPool(eth.EventMux())
eth.blockManager = core.NewBlockManager(eth.txPool, eth.chainManager, eth.EventMux()) eth.blockProcessor = core.NewBlockProcessor(eth.txPool, eth.chainManager, eth.EventMux())
eth.chainManager.SetProcessor(eth.blockManager) eth.chainManager.SetProcessor(eth.blockProcessor)
eth.whisper = whisper.New() eth.whisper = whisper.New()
hasBlock := eth.chainManager.HasBlock hasBlock := eth.chainManager.HasBlock
@ -169,8 +169,8 @@ func (s *Ethereum) ChainManager() *core.ChainManager {
return s.chainManager return s.chainManager
} }
func (s *Ethereum) BlockManager() *core.BlockManager { func (s *Ethereum) BlockProcessor() *core.BlockProcessor {
return s.blockManager return s.blockProcessor
} }
func (s *Ethereum) TxPool() *core.TxPool { func (s *Ethereum) TxPool() *core.TxPool {

80
eth/wallet.go Normal file
View File

@ -0,0 +1,80 @@
package eth
/*
import (
"crypto/ecdsa"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
)
type Account struct {
w *Wallet
}
func (self *Account) Transact(to *Account, value, gas, price *big.Int, data []byte) error {
return self.w.transact(self, to, value, gas, price, data)
}
func (self *Account) Address() []byte {
return nil
}
func (self *Account) PrivateKey() *ecdsa.PrivateKey {
return nil
}
type Wallet struct{}
func NewWallet() *Wallet {
return &Wallet{}
}
func (self *Wallet) GetAccount(i int) *Account {
}
func (self *Wallet) transact(from, to *Account, value, gas, price *big.Int, data []byte) error {
if from.PrivateKey() == nil {
return errors.New("accounts is not owned (no private key available)")
}
var createsContract bool
if to == nil {
createsContract = true
}
var msg *types.Transaction
if contractCreation {
msg = types.NewContractCreationTx(value, gas, price, data)
} else {
msg = types.NewTransactionMessage(to.Address(), value, gas, price, data)
}
state := self.chainManager.TransState()
nonce := state.GetNonce(key.Address())
msg.SetNonce(nonce)
msg.SignECDSA(from.PriateKey())
// Do some pre processing for our "pre" events and hooks
block := self.chainManager.NewBlock(from.Address())
coinbase := state.GetOrNewStateObject(from.Address())
coinbase.SetGasPool(block.GasLimit())
self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true)
err := self.obj.TxPool().Add(tx)
if err != nil {
return nil, err
}
state.SetNonce(key.Address(), nonce+1)
if contractCreation {
addr := core.AddressFromMessage(tx)
pipelogger.Infof("Contract addr %x\n", addr)
}
return tx, nil
}
*/

View File

@ -174,9 +174,9 @@ func (self *Miner) reset() {
func (self *Miner) mine() { func (self *Miner) mine() {
var ( var (
blockManager = self.eth.BlockManager() blockProcessor = self.eth.BlockProcessor()
chainMan = self.eth.ChainManager() chainMan = self.eth.ChainManager()
block = chainMan.NewBlock(self.Coinbase) block = chainMan.NewBlock(self.Coinbase)
) )
// Apply uncles // Apply uncles
@ -194,7 +194,7 @@ func (self *Miner) mine() {
// Accumulate all valid transactions and apply them to the new state // Accumulate all valid transactions and apply them to the new state
// Error may be ignored. It's not important during mining // Error may be ignored. It's not important during mining
receipts, txs, _, erroneous, err := blockManager.ApplyTransactions(coinbase, state, block, transactions, true) receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true)
if err != nil { if err != nil {
minerlogger.Debugln(err) minerlogger.Debugln(err)
} }
@ -204,7 +204,7 @@ func (self *Miner) mine() {
block.SetReceipts(receipts) block.SetReceipts(receipts)
// Accumulate the rewards included for this block // Accumulate the rewards included for this block
blockManager.AccumelateRewards(state, block, parent) blockProcessor.AccumelateRewards(state, block, parent)
state.Update(ethutil.Big0) state.Update(ethutil.Big0)
block.SetRoot(state.Root()) block.SetRoot(state.Root())

View File

@ -20,19 +20,19 @@ type VmVars struct {
} }
type XEth struct { type XEth struct {
obj core.EthManager obj core.EthManager
blockManager *core.BlockManager blockProcessor *core.BlockProcessor
chainManager *core.ChainManager chainManager *core.ChainManager
world *World world *World
Vm VmVars Vm VmVars
} }
func New(obj core.EthManager) *XEth { func New(obj core.EthManager) *XEth {
pipe := &XEth{ pipe := &XEth{
obj: obj, obj: obj,
blockManager: obj.BlockManager(), blockProcessor: obj.BlockProcessor(),
chainManager: obj.ChainManager(), chainManager: obj.ChainManager(),
} }
pipe.world = NewWorld(pipe) pipe.world = NewWorld(pipe)
@ -141,7 +141,7 @@ func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *et
block := self.chainManager.NewBlock(key.Address()) block := self.chainManager.NewBlock(key.Address())
coinbase := state.GetOrNewStateObject(key.Address()) coinbase := state.GetOrNewStateObject(key.Address())
coinbase.SetGasPool(block.GasLimit()) coinbase.SetGasPool(block.GasLimit())
self.blockManager.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) self.blockProcessor.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true)
err := self.obj.TxPool().Add(tx) err := self.obj.TxPool().Add(tx)
if err != nil { if err != nil {