From 1e4ae24126d21305c8fcbebd057ce4ec28ddd242 Mon Sep 17 00:00:00 2001 From: zelig Date: Fri, 4 Jul 2014 19:48:37 +0100 Subject: [PATCH 1/3] use ethreact.Event, unbuffered event channels, subscribe after loop reading from channel starts --- ethereal/ext_app.go | 9 +-- ethereal/gui.go | 107 +++++++++++++++++---------------- ethereum/javascript_runtime.go | 16 ++--- 3 files changed, 68 insertions(+), 64 deletions(-) diff --git a/ethereal/ext_app.go b/ethereal/ext_app.go index 17c342a1b..c02f5ccb3 100644 --- a/ethereal/ext_app.go +++ b/ethereal/ext_app.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/go-qml/qml" ) @@ -24,8 +25,8 @@ type AppContainer interface { type ExtApplication struct { *ethpub.PEthereum - blockChan chan ethutil.React - changeChan chan ethutil.React + blockChan chan ethreact.Event + changeChan chan ethreact.Event quitChan chan bool watcherQuitChan chan bool @@ -37,8 +38,8 @@ type ExtApplication struct { func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication { app := &ExtApplication{ ethpub.NewPEthereum(lib.eth), - make(chan ethutil.React, 1), - make(chan ethutil.React, 1), + make(chan ethreact.Event), + make(chan ethreact.Event), make(chan bool), make(chan bool), container, diff --git a/ethereal/gui.go b/ethereal/gui.go index 9f28045f8..2bad90361 100644 --- a/ethereal/gui.go +++ b/ethereal/gui.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" "github.com/ethereum/go-ethereum/utils" @@ -143,7 +144,7 @@ func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) { gui.readPreviousTransactions() gui.setPeerInfo() - go gui.update() + gui.update() return win, nil } @@ -266,20 +267,10 @@ func (gui *Gui) setWalletValue(amount, unconfirmedFunds *big.Int) { func (gui *Gui) update() { reactor := gui.eth.Reactor() - blockChan := make(chan ethutil.React, 1) - txChan := make(chan ethutil.React, 1) - objectChan := make(chan ethutil.React, 1) - peerChan := make(chan ethutil.React, 1) - - reactor.Subscribe("newBlock", blockChan) - reactor.Subscribe("newTx:pre", txChan) - reactor.Subscribe("newTx:post", txChan) - - nameReg := ethpub.EthereumConfig(gui.eth.StateManager()).NameReg() - if nameReg != nil { - reactor.Subscribe("object:"+string(nameReg.Address()), objectChan) - } - reactor.Subscribe("peerList", peerChan) + blockChan := make(chan ethreact.Event) + txChan := make(chan ethreact.Event) + objectChan := make(chan ethreact.Event) + peerChan := make(chan ethreact.Event) ticker := time.NewTicker(5 * time.Second) @@ -288,54 +279,66 @@ func (gui *Gui) update() { unconfirmedFunds := new(big.Int) gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.address()).Amount))) - for { - select { - case b := <-blockChan: - block := b.Resource.(*ethchain.Block) - gui.processBlock(block, false) - if bytes.Compare(block.Coinbase, gui.address()) == 0 { - gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil) - } + go func() { + for { + select { + case b := <-blockChan: + block := b.Resource.(*ethchain.Block) + gui.processBlock(block, false) + if bytes.Compare(block.Coinbase, gui.address()) == 0 { + gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil) + } - case txMsg := <-txChan: - tx := txMsg.Resource.(*ethchain.Transaction) + case txMsg := <-txChan: + tx := txMsg.Resource.(*ethchain.Transaction) - if txMsg.Event == "newTx:pre" { - object := state.GetAccount(gui.address()) + if txMsg.Name == "newTx:pre" { + object := state.GetAccount(gui.address()) - if bytes.Compare(tx.Sender(), gui.address()) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send") - gui.txDb.Put(tx.Hash(), tx.RlpEncode()) + if bytes.Compare(tx.Sender(), gui.address()) == 0 { + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send") + gui.txDb.Put(tx.Hash(), tx.RlpEncode()) - unconfirmedFunds.Sub(unconfirmedFunds, tx.Value) - } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv") - gui.txDb.Put(tx.Hash(), tx.RlpEncode()) + unconfirmedFunds.Sub(unconfirmedFunds, tx.Value) + } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv") + gui.txDb.Put(tx.Hash(), tx.RlpEncode()) - unconfirmedFunds.Add(unconfirmedFunds, tx.Value) - } + unconfirmedFunds.Add(unconfirmedFunds, tx.Value) + } - gui.setWalletValue(object.Amount, unconfirmedFunds) - } else { - object := state.GetAccount(gui.address()) - if bytes.Compare(tx.Sender(), gui.address()) == 0 { - object.SubAmount(tx.Value) - } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { - object.AddAmount(tx.Value) - } + gui.setWalletValue(object.Amount, unconfirmedFunds) + } else { + object := state.GetAccount(gui.address()) + if bytes.Compare(tx.Sender(), gui.address()) == 0 { + object.SubAmount(tx.Value) + } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { + object.AddAmount(tx.Value) + } - gui.setWalletValue(object.Amount, nil) + gui.setWalletValue(object.Amount, nil) - state.UpdateStateObject(object) + state.UpdateStateObject(object) + } + case <-objectChan: + gui.loadAddressBook() + case <-peerChan: + gui.setPeerInfo() + case <-ticker.C: + gui.setPeerInfo() } - case <-objectChan: - gui.loadAddressBook() - case <-peerChan: - gui.setPeerInfo() - case <-ticker.C: - gui.setPeerInfo() } + }() + reactor.Subscribe("newBlock", blockChan) + reactor.Subscribe("newTx:pre", txChan) + reactor.Subscribe("newTx:post", txChan) + + nameReg := ethpub.EthereumConfig(gui.eth.StateManager()).NameReg() + if nameReg != nil { + reactor.Subscribe("object:"+string(nameReg.Address()), objectChan) } + reactor.Subscribe("peerList", peerChan) + } func (gui *Gui) setPeerInfo() { diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 852a50487..6e107e376 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" @@ -22,8 +23,8 @@ type JSRE struct { vm *otto.Otto lib *ethpub.PEthereum - blockChan chan ethutil.React - changeChan chan ethutil.React + blockChan chan ethreact.Event + changeChan chan ethreact.Event quitChan chan bool objectCb map[string][]otto.Value @@ -48,8 +49,8 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { ethereum, otto.New(), ethpub.NewPEthereum(ethereum), - make(chan ethutil.React, 1), - make(chan ethutil.React, 1), + make(chan ethreact.Event), + make(chan ethreact.Event), make(chan bool), make(map[string][]otto.Value), } @@ -63,6 +64,9 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { // We have to make sure that, whoever calls this, calls "Stop" go re.mainLoop() + // Subscribe to events + reactor := ethereum.Reactor() + reactor.Subscribe("newBlock", re.blockChan) re.Bind("eth", &JSEthereum{re.lib, re.vm}) @@ -108,10 +112,6 @@ func (self *JSRE) Stop() { } func (self *JSRE) mainLoop() { - // Subscribe to events - reactor := self.ethereum.Reactor() - reactor.Subscribe("newBlock", self.blockChan) - out: for { select { From 8e5117444ebfe4eac4aa96ae431dba5c67b2fcc6 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 5 Jul 2014 13:25:16 +0200 Subject: [PATCH 2/3] New type of debugger --- ethereal/debugger.go | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/ethereal/debugger.go b/ethereal/debugger.go index 89e0f36eb..64ca316f8 100644 --- a/ethereal/debugger.go +++ b/ethereal/debugger.go @@ -15,9 +15,8 @@ type DebuggerWindow struct { engine *qml.Engine lib *UiLib - vm *ethchain.Vm - Db *Debugger - breakPoints []int64 + vm *ethchain.Vm + Db *Debugger } func NewDebuggerWindow(lib *UiLib) *DebuggerWindow { @@ -30,9 +29,11 @@ func NewDebuggerWindow(lib *UiLib) *DebuggerWindow { } win := component.CreateWindow(nil) - db := &Debugger{win, make(chan bool), make(chan bool), true, false} - return &DebuggerWindow{engine: engine, win: win, lib: lib, Db: db, vm: ðchain.Vm{}} + w := &DebuggerWindow{engine: engine, win: win, lib: lib, vm: ðchain.Vm{}} + w.Db = NewDebugger(w) + + return w } func (self *DebuggerWindow) Show() { @@ -138,8 +139,7 @@ func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, data Value: ethutil.Big(valueStr), }) vm.Verbose = true - vm.Hook = self.Db.halting - vm.BreakPoints = self.breakPoints + vm.Dbg = self.Db self.vm = vm self.Db.done = false @@ -201,8 +201,7 @@ func (self *DebuggerWindow) ExecCommand(command string) { self.Logln(err) break } - self.breakPoints = append(self.breakPoints, int64(lineNo)) - self.vm.BreakPoints = self.breakPoints + self.Db.breakPoints = append(self.Db.breakPoints, int64(lineNo)) self.Logf("break point set on instruction %d", lineNo) } else { self.Logf("'%s' requires line number", cmd[0]) @@ -211,8 +210,7 @@ func (self *DebuggerWindow) ExecCommand(command string) { if len(cmd) > 1 { switch cmd[1] { case "break", "bp": - self.breakPoints = nil - self.vm.BreakPoints = nil + self.Db.breakPoints = nil self.Logln("Breakpoints cleared") case "log": @@ -231,16 +229,38 @@ func (self *DebuggerWindow) ExecCommand(command string) { } type Debugger struct { - win *qml.Window N chan bool Q chan bool done, interrupt bool + breakPoints []int64 + main *DebuggerWindow + win *qml.Window +} + +func NewDebugger(main *DebuggerWindow) *Debugger { + db := &Debugger{make(chan bool), make(chan bool), true, false, nil, main, main.win} + + return db } type storeVal struct { Key, Value string } +func (self *Debugger) BreakHook(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool { + self.main.Logln("break on instr:", pc) + + return self.halting(pc, op, mem, stack, stateObject) +} + +func (self *Debugger) StepHook(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool { + return self.halting(pc, op, mem, stack, stateObject) +} + +func (self *Debugger) BreakPoints() []int64 { + return self.breakPoints +} + func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack, stateObject *ethchain.StateObject) bool { d.win.Root().Call("setInstruction", pc) d.win.Root().Call("clearMem") From 0c132e4c9e88bc0e13ad424f4c3b51cd4f5e8354 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 7 Jul 2014 10:58:42 +0200 Subject: [PATCH 3/3] Revert "ethreact - Feature/ethutil refactor" --- ethereal/ext_app.go | 9 ++- ethereal/gui.go | 107 ++++++++++++++++----------------- ethereum/javascript_runtime.go | 16 ++--- 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/ethereal/ext_app.go b/ethereal/ext_app.go index c02f5ccb3..17c342a1b 100644 --- a/ethereal/ext_app.go +++ b/ethereal/ext_app.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethpub" - "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/go-qml/qml" ) @@ -25,8 +24,8 @@ type AppContainer interface { type ExtApplication struct { *ethpub.PEthereum - blockChan chan ethreact.Event - changeChan chan ethreact.Event + blockChan chan ethutil.React + changeChan chan ethutil.React quitChan chan bool watcherQuitChan chan bool @@ -38,8 +37,8 @@ type ExtApplication struct { func NewExtApplication(container AppContainer, lib *UiLib) *ExtApplication { app := &ExtApplication{ ethpub.NewPEthereum(lib.eth), - make(chan ethreact.Event), - make(chan ethreact.Event), + make(chan ethutil.React, 1), + make(chan ethutil.React, 1), make(chan bool), make(chan bool), container, diff --git a/ethereal/gui.go b/ethereal/gui.go index 2bad90361..9f28045f8 100644 --- a/ethereal/gui.go +++ b/ethereal/gui.go @@ -8,7 +8,6 @@ import ( "github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" - "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/eth-go/ethwire" "github.com/ethereum/go-ethereum/utils" @@ -144,7 +143,7 @@ func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) { gui.readPreviousTransactions() gui.setPeerInfo() - gui.update() + go gui.update() return win, nil } @@ -267,10 +266,20 @@ func (gui *Gui) setWalletValue(amount, unconfirmedFunds *big.Int) { func (gui *Gui) update() { reactor := gui.eth.Reactor() - blockChan := make(chan ethreact.Event) - txChan := make(chan ethreact.Event) - objectChan := make(chan ethreact.Event) - peerChan := make(chan ethreact.Event) + blockChan := make(chan ethutil.React, 1) + txChan := make(chan ethutil.React, 1) + objectChan := make(chan ethutil.React, 1) + peerChan := make(chan ethutil.React, 1) + + reactor.Subscribe("newBlock", blockChan) + reactor.Subscribe("newTx:pre", txChan) + reactor.Subscribe("newTx:post", txChan) + + nameReg := ethpub.EthereumConfig(gui.eth.StateManager()).NameReg() + if nameReg != nil { + reactor.Subscribe("object:"+string(nameReg.Address()), objectChan) + } + reactor.Subscribe("peerList", peerChan) ticker := time.NewTicker(5 * time.Second) @@ -279,66 +288,54 @@ func (gui *Gui) update() { unconfirmedFunds := new(big.Int) gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.address()).Amount))) - go func() { - for { - select { - case b := <-blockChan: - block := b.Resource.(*ethchain.Block) - gui.processBlock(block, false) - if bytes.Compare(block.Coinbase, gui.address()) == 0 { - gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil) - } + for { + select { + case b := <-blockChan: + block := b.Resource.(*ethchain.Block) + gui.processBlock(block, false) + if bytes.Compare(block.Coinbase, gui.address()) == 0 { + gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil) + } - case txMsg := <-txChan: - tx := txMsg.Resource.(*ethchain.Transaction) + case txMsg := <-txChan: + tx := txMsg.Resource.(*ethchain.Transaction) - if txMsg.Name == "newTx:pre" { - object := state.GetAccount(gui.address()) + if txMsg.Event == "newTx:pre" { + object := state.GetAccount(gui.address()) - if bytes.Compare(tx.Sender(), gui.address()) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send") - gui.txDb.Put(tx.Hash(), tx.RlpEncode()) + if bytes.Compare(tx.Sender(), gui.address()) == 0 { + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send") + gui.txDb.Put(tx.Hash(), tx.RlpEncode()) - unconfirmedFunds.Sub(unconfirmedFunds, tx.Value) - } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv") - gui.txDb.Put(tx.Hash(), tx.RlpEncode()) + unconfirmedFunds.Sub(unconfirmedFunds, tx.Value) + } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv") + gui.txDb.Put(tx.Hash(), tx.RlpEncode()) - unconfirmedFunds.Add(unconfirmedFunds, tx.Value) - } + unconfirmedFunds.Add(unconfirmedFunds, tx.Value) + } - gui.setWalletValue(object.Amount, unconfirmedFunds) - } else { - object := state.GetAccount(gui.address()) - if bytes.Compare(tx.Sender(), gui.address()) == 0 { - object.SubAmount(tx.Value) - } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { - object.AddAmount(tx.Value) - } + gui.setWalletValue(object.Amount, unconfirmedFunds) + } else { + object := state.GetAccount(gui.address()) + if bytes.Compare(tx.Sender(), gui.address()) == 0 { + object.SubAmount(tx.Value) + } else if bytes.Compare(tx.Recipient, gui.address()) == 0 { + object.AddAmount(tx.Value) + } - gui.setWalletValue(object.Amount, nil) + gui.setWalletValue(object.Amount, nil) - state.UpdateStateObject(object) - } - case <-objectChan: - gui.loadAddressBook() - case <-peerChan: - gui.setPeerInfo() - case <-ticker.C: - gui.setPeerInfo() + state.UpdateStateObject(object) } + case <-objectChan: + gui.loadAddressBook() + case <-peerChan: + gui.setPeerInfo() + case <-ticker.C: + gui.setPeerInfo() } - }() - reactor.Subscribe("newBlock", blockChan) - reactor.Subscribe("newTx:pre", txChan) - reactor.Subscribe("newTx:post", txChan) - - nameReg := ethpub.EthereumConfig(gui.eth.StateManager()).NameReg() - if nameReg != nil { - reactor.Subscribe("object:"+string(nameReg.Address()), objectChan) } - reactor.Subscribe("peerList", peerChan) - } func (gui *Gui) setPeerInfo() { diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 6e107e376..852a50487 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -6,7 +6,6 @@ import ( "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethpub" - "github.com/ethereum/eth-go/ethreact" "github.com/ethereum/eth-go/ethutil" "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" @@ -23,8 +22,8 @@ type JSRE struct { vm *otto.Otto lib *ethpub.PEthereum - blockChan chan ethreact.Event - changeChan chan ethreact.Event + blockChan chan ethutil.React + changeChan chan ethutil.React quitChan chan bool objectCb map[string][]otto.Value @@ -49,8 +48,8 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { ethereum, otto.New(), ethpub.NewPEthereum(ethereum), - make(chan ethreact.Event), - make(chan ethreact.Event), + make(chan ethutil.React, 1), + make(chan ethutil.React, 1), make(chan bool), make(map[string][]otto.Value), } @@ -64,9 +63,6 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { // We have to make sure that, whoever calls this, calls "Stop" go re.mainLoop() - // Subscribe to events - reactor := ethereum.Reactor() - reactor.Subscribe("newBlock", re.blockChan) re.Bind("eth", &JSEthereum{re.lib, re.vm}) @@ -112,6 +108,10 @@ func (self *JSRE) Stop() { } func (self *JSRE) mainLoop() { + // Subscribe to events + reactor := self.ethereum.Reactor() + reactor.Subscribe("newBlock", self.blockChan) + out: for { select {