mirror of https://github.com/ethereum/go-ethereum
The new event machine will be used for loose coupling and handle the communications between the services: 1) Block pool finds blocks which "links" with our current canonical chain 2) Posts the blocks on to the event machine 3) State manager receives blocks & processes them 4) Broadcasts new post block eventpull/150/head
parent
ea0357bf02
commit
ab6ede51d7
@ -0,0 +1,88 @@ |
||||
package ethchain |
||||
|
||||
import ( |
||||
"container/list" |
||||
"fmt" |
||||
|
||||
"github.com/ethereum/eth-go/ethcrypto" |
||||
"github.com/ethereum/eth-go/ethdb" |
||||
"github.com/ethereum/eth-go/ethreact" |
||||
"github.com/ethereum/eth-go/ethutil" |
||||
"github.com/ethereum/eth-go/ethwire" |
||||
) |
||||
|
||||
// Implement our EthTest Manager
|
||||
type TestManager struct { |
||||
stateManager *StateManager |
||||
reactor *ethreact.ReactorEngine |
||||
|
||||
txPool *TxPool |
||||
blockChain *BlockChain |
||||
Blocks []*Block |
||||
} |
||||
|
||||
func (s *TestManager) IsListening() bool { |
||||
return false |
||||
} |
||||
|
||||
func (s *TestManager) IsMining() bool { |
||||
return false |
||||
} |
||||
|
||||
func (s *TestManager) PeerCount() int { |
||||
return 0 |
||||
} |
||||
|
||||
func (s *TestManager) Peers() *list.List { |
||||
return list.New() |
||||
} |
||||
|
||||
func (s *TestManager) BlockChain() *BlockChain { |
||||
return s.blockChain |
||||
} |
||||
|
||||
func (tm *TestManager) TxPool() *TxPool { |
||||
return tm.txPool |
||||
} |
||||
|
||||
func (tm *TestManager) StateManager() *StateManager { |
||||
return tm.stateManager |
||||
} |
||||
|
||||
func (tm *TestManager) Reactor() *ethreact.ReactorEngine { |
||||
return tm.reactor |
||||
} |
||||
func (tm *TestManager) Broadcast(msgType ethwire.MsgType, data []interface{}) { |
||||
fmt.Println("Broadcast not implemented") |
||||
} |
||||
|
||||
func (tm *TestManager) ClientIdentity() ethwire.ClientIdentity { |
||||
return nil |
||||
} |
||||
func (tm *TestManager) KeyManager() *ethcrypto.KeyManager { |
||||
return nil |
||||
} |
||||
|
||||
func (tm *TestManager) Db() ethutil.Database { return nil } |
||||
func NewTestManager() *TestManager { |
||||
ethutil.ReadConfig(".ethtest", "/tmp/ethtest", "ETH") |
||||
|
||||
db, err := ethdb.NewMemDatabase() |
||||
if err != nil { |
||||
fmt.Println("Could not create mem-db, failing") |
||||
return nil |
||||
} |
||||
ethutil.Config.Db = db |
||||
|
||||
testManager := &TestManager{} |
||||
testManager.reactor = ethreact.New() |
||||
|
||||
testManager.txPool = NewTxPool(testManager) |
||||
testManager.blockChain = NewBlockChain(testManager) |
||||
testManager.stateManager = NewStateManager(testManager) |
||||
|
||||
// Start the tx pool
|
||||
testManager.txPool.Start() |
||||
|
||||
return testManager |
||||
} |
@ -0,0 +1,79 @@ |
||||
package eventer |
||||
|
||||
// Basic receiver interface.
|
||||
type Receiver interface { |
||||
Send(Event) |
||||
} |
||||
|
||||
// Receiver as channel
|
||||
type Channel chan Event |
||||
|
||||
func (self Channel) Send(ev Event) { |
||||
self <- ev |
||||
} |
||||
|
||||
// Receiver as function
|
||||
type Function func(ev Event) |
||||
|
||||
func (self Function) Send(ev Event) { |
||||
self(ev) |
||||
} |
||||
|
||||
type Event struct { |
||||
Type string |
||||
Data interface{} |
||||
} |
||||
|
||||
type Channels map[string][]Receiver |
||||
|
||||
type EventMachine struct { |
||||
channels Channels |
||||
} |
||||
|
||||
func New() *EventMachine { |
||||
return &EventMachine{ |
||||
channels: make(Channels), |
||||
} |
||||
} |
||||
|
||||
func (self *EventMachine) add(typ string, r Receiver) { |
||||
self.channels[typ] = append(self.channels[typ], r) |
||||
} |
||||
|
||||
// Generalised methods for the known receiver types
|
||||
// * Channel
|
||||
// * Function
|
||||
func (self *EventMachine) On(typ string, r interface{}) { |
||||
if eventFunc, ok := r.(func(Event)); ok { |
||||
self.RegisterFunc(typ, eventFunc) |
||||
} else if eventChan, ok := r.(Channel); ok { |
||||
self.RegisterChannel(typ, eventChan) |
||||
} else { |
||||
panic("Invalid type for EventMachine::On") |
||||
} |
||||
} |
||||
|
||||
func (self *EventMachine) RegisterChannel(typ string, c Channel) { |
||||
self.add(typ, c) |
||||
} |
||||
|
||||
func (self *EventMachine) RegisterFunc(typ string, f Function) { |
||||
self.add(typ, f) |
||||
} |
||||
|
||||
func (self *EventMachine) Register(typ string) Channel { |
||||
c := make(Channel, 1) |
||||
self.add(typ, c) |
||||
|
||||
return c |
||||
} |
||||
|
||||
func (self *EventMachine) Post(typ string, data interface{}) { |
||||
if self.channels[typ] != nil { |
||||
ev := Event{typ, data} |
||||
for _, receiver := range self.channels[typ] { |
||||
// Blocking is OK. These are internals and need to be handled
|
||||
receiver.Send(ev) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,66 @@ |
||||
package eventer |
||||
|
||||
import "testing" |
||||
|
||||
func TestChannel(t *testing.T) { |
||||
eventer := New(nil) |
||||
|
||||
c := make(Channel, 1) |
||||
eventer.RegisterChannel("test", c) |
||||
eventer.Post("test", "hello world") |
||||
|
||||
res := <-c |
||||
|
||||
if res.Data.(string) != "hello world" { |
||||
t.Error("Expected event with data 'hello world'. Got", res.Data) |
||||
} |
||||
} |
||||
|
||||
func TestFunction(t *testing.T) { |
||||
eventer := New(nil) |
||||
|
||||
var data string |
||||
eventer.RegisterFunc("test", func(ev Event) { |
||||
data = ev.Data.(string) |
||||
}) |
||||
eventer.Post("test", "hello world") |
||||
|
||||
if data != "hello world" { |
||||
t.Error("Expected event with data 'hello world'. Got", data) |
||||
} |
||||
} |
||||
|
||||
func TestRegister(t *testing.T) { |
||||
eventer := New(nil) |
||||
|
||||
c := eventer.Register("test") |
||||
eventer.Post("test", "hello world") |
||||
|
||||
res := <-c |
||||
|
||||
if res.Data.(string) != "hello world" { |
||||
t.Error("Expected event with data 'hello world'. Got", res.Data) |
||||
} |
||||
} |
||||
|
||||
func TestOn(t *testing.T) { |
||||
eventer := New(nil) |
||||
|
||||
c := make(Channel, 1) |
||||
eventer.On("test", c) |
||||
|
||||
var data string |
||||
eventer.On("test", func(ev Event) { |
||||
data = ev.Data.(string) |
||||
}) |
||||
eventer.Post("test", "hello world") |
||||
|
||||
res := <-c |
||||
if res.Data.(string) != "hello world" { |
||||
t.Error("Expected channel event with data 'hello world'. Got", res.Data) |
||||
} |
||||
|
||||
if data != "hello world" { |
||||
t.Error("Expected function event with data 'hello world'. Got", data) |
||||
} |
||||
} |
@ -0,0 +1 @@ |
||||
package types |
Loading…
Reference in new issue