Added a callback mechanism to chain adding.

Not sure if this is the right approach. Why? BlockChain shouldn't need
the "Ethereum" object. BlockChain shouldn't need to worry about
notifying listeners or message propagation.
This commit is contained in:
obscuren 2014-11-18 19:44:17 +01:00
parent a1b6a9ac29
commit f8d0cd9906
4 changed files with 17 additions and 13 deletions

@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/chain/types"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/wire" "github.com/ethereum/go-ethereum/wire"
) )
@ -310,10 +311,6 @@ out:
} }
} }
// TODO figure out whether we were catching up
// If caught up and just a new block has been propagated:
// sm.eth.EventMux().Post(NewBlockEvent{block})
// otherwise process and don't emit anything
if len(blocks) > 0 { if len(blocks) > 0 {
chainManager := self.eth.ChainManager() chainManager := self.eth.ChainManager()
// Test and import // Test and import
@ -335,10 +332,13 @@ out:
self.peer = nil self.peer = nil
} else { } else {
if !chain.IsTDError(err) { if !chain.IsTDError(err) {
chainManager.InsertChain(bchain) chainManager.InsertChain(bchain, func(block *types.Block, messages state.Messages) {
for _, block := range blocks { self.eth.EventMux().Post(chain.NewBlockEvent{block})
self.eth.EventMux().Post(messages)
self.Remove(block.Hash()) self.Remove(block.Hash())
} })
} }
} }
} }

@ -271,14 +271,14 @@ func (self *ChainManager) NewIterator(startHash []byte) *ChainIterator {
} }
// This function assumes you've done your checking. No checking is done at this stage anymore // This function assumes you've done your checking. No checking is done at this stage anymore
func (self *ChainManager) InsertChain(chain *BlockChain) { func (self *ChainManager) InsertChain(chain *BlockChain, call func(*types.Block, state.Messages)) {
for e := chain.Front(); e != nil; e = e.Next() { for e := chain.Front(); e != nil; e = e.Next() {
link := e.Value.(*link) link := e.Value.(*link)
self.add(link.block) self.add(link.block)
self.SetTotalDifficulty(link.td) self.SetTotalDifficulty(link.td)
//self.eth.EventMux().Post(NewBlockEvent{link.block})
//self.eth.EventMux().Post(link.messages) call(link.block, link.messages)
} }
b, e := chain.Front(), chain.Back() b, e := chain.Front(), chain.Back()

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain"
"github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/chain/types"
@ -218,8 +219,10 @@ func (self *Miner) mine() {
if err != nil { if err != nil {
minerlogger.Infoln(err) minerlogger.Infoln(err)
} else { } else {
chainMan.InsertChain(lchain) chainMan.InsertChain(lchain, func(block *types.Block, _ state.Messages) {
//self.eth.EventMux().Post(chain.NewBlockEvent{block}) self.eth.EventMux().Post(chain.NewBlockEvent{block})
})
self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val}) self.eth.Broadcast(wire.MsgBlockTy, []interface{}{block.Value().Val})
minerlogger.Infof("🔨 Mined block %x\n", block.Hash()) minerlogger.Infof("🔨 Mined block %x\n", block.Hash())

@ -11,6 +11,7 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/ethereum/go-ethereum/chain/types" "github.com/ethereum/go-ethereum/chain/types"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
@ -23,7 +24,7 @@ const (
// The size of the output buffer for writing messages // The size of the output buffer for writing messages
outputBufferSize = 50 outputBufferSize = 50
// Current protocol version // Current protocol version
ProtocolVersion = 42 ProtocolVersion = 43
// Current P2P version // Current P2P version
P2PVersion = 2 P2PVersion = 2
// Ethereum network version // Ethereum network version