diff options
Diffstat (limited to 'ethereal/ui')
-rw-r--r-- | ethereal/ui/debugger.go | 225 | ||||
-rw-r--r-- | ethereal/ui/gui.go | 127 | ||||
-rw-r--r-- | ethereal/ui/ui_lib.go | 86 |
3 files changed, 372 insertions, 66 deletions
diff --git a/ethereal/ui/debugger.go b/ethereal/ui/debugger.go new file mode 100644 index 000000000..919407b34 --- /dev/null +++ b/ethereal/ui/debugger.go @@ -0,0 +1,225 @@ +package ethui + +import ( + "fmt" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethutil" + "github.com/go-qml/qml" + "math/big" + "strings" +) + +type DebuggerWindow struct { + win *qml.Window + engine *qml.Engine + lib *UiLib + Db *Debugger +} + +func NewDebuggerWindow(lib *UiLib) *DebuggerWindow { + engine := qml.NewEngine() + component, err := engine.LoadFile(lib.AssetPath("debugger/debugger.qml")) + if err != nil { + fmt.Println(err) + + return nil + } + + 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} +} + +func (self *DebuggerWindow) Show() { + context := self.engine.Context() + context.SetVar("dbg", self) + + go func() { + self.win.Show() + self.win.Wait() + }() +} + +func (self *DebuggerWindow) SetCode(code string) { + self.win.Set("codeText", code) +} + +func (self *DebuggerWindow) SetData(data string) { + self.win.Set("dataText", data) +} +func (self *DebuggerWindow) SetAsm(data string) { + dis := ethchain.Disassemble(ethutil.FromHex(data)) + for _, str := range dis { + self.win.Root().Call("setAsm", str) + } +} + +func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, dataStr string) { + if !self.Db.done { + self.Db.Q <- true + } + + defer func() { + if r := recover(); r != nil { + self.Logf("compile FAULT: %v", r) + } + }() + + data := ethutil.StringToByteFunc(dataStr, func(s string) (ret []byte) { + slice := strings.Split(dataStr, "\n") + for _, dataItem := range slice { + d := ethutil.FormatData(dataItem) + ret = append(ret, d...) + } + return + }) + + var err error + script := ethutil.StringToByteFunc(scriptStr, func(s string) (ret []byte) { + ret, err = ethutil.Compile(s) + fmt.Printf("%x\n", ret) + return + }) + + if err != nil { + self.Logln(err) + + return + } + + dis := ethchain.Disassemble(script) + self.win.Root().Call("clearAsm") + self.win.Root().Call("clearLog") + + for _, str := range dis { + self.win.Root().Call("setAsm", str) + } + + gas := ethutil.Big(gasStr) + gasPrice := ethutil.Big(gasPriceStr) + // Contract addr as test address + keyPair := ethutil.GetKeyRing().Get(0) + callerTx := ethchain.NewContractCreationTx(ethutil.Big(valueStr), gas, gasPrice, script) + callerTx.Sign(keyPair.PrivateKey) + + state := self.lib.eth.BlockChain().CurrentBlock.State() + account := self.lib.eth.StateManager().TransState().GetAccount(keyPair.Address()) + contract := ethchain.MakeContract(callerTx, state) + callerClosure := ethchain.NewClosure(account, contract, script, state, gas, gasPrice) + + block := self.lib.eth.BlockChain().CurrentBlock + vm := ethchain.NewVm(state, self.lib.eth.StateManager(), ethchain.RuntimeVars{ + Origin: account.Address(), + BlockNumber: block.BlockInfo().Number, + PrevHash: block.PrevHash, + Coinbase: block.Coinbase, + Time: block.Time, + Diff: block.Difficulty, + Value: ethutil.Big(valueStr), + }) + + self.Db.done = false + self.Logf("callsize %d", len(script)) + go func() { + ret, g, err := callerClosure.Call(vm, data, self.Db.halting) + tot := new(big.Int).Mul(g, gasPrice) + self.Logf("gas usage %v total price = %v (%v)", g, tot, ethutil.CurrencyToString(tot)) + if err != nil { + self.Logln("exited with errors:", err) + } else { + if len(ret) > 0 { + self.Logf("exited: % x", ret) + } else { + self.Logf("exited: nil") + } + } + + state.Reset() + + if !self.Db.interrupt { + self.Db.done = true + } else { + self.Db.interrupt = false + } + }() +} + +func (self *DebuggerWindow) Logf(format string, v ...interface{}) { + self.win.Root().Call("setLog", fmt.Sprintf(format, v...)) +} + +func (self *DebuggerWindow) Logln(v ...interface{}) { + str := fmt.Sprintln(v...) + self.Logf("%s", str[:len(str)-1]) +} + +func (self *DebuggerWindow) Next() { + self.Db.Next() +} + +type Debugger struct { + win *qml.Window + N chan bool + Q chan bool + done, interrupt bool +} + +type storeVal struct { + Key, Value string +} + +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") + d.win.Root().Call("clearStack") + d.win.Root().Call("clearStorage") + + addr := 0 + for i := 0; i+32 <= mem.Len(); i += 32 { + d.win.Root().Call("setMem", memAddr{fmt.Sprintf("%03d", addr), fmt.Sprintf("% x", mem.Data()[i:i+32])}) + addr++ + } + + for _, val := range stack.Data() { + d.win.Root().Call("setStack", val.String()) + } + + stateObject.State().EachStorage(func(key string, node *ethutil.Value) { + d.win.Root().Call("setStorage", storeVal{fmt.Sprintf("% x", key), fmt.Sprintf("% x", node.Str())}) + }) + +out: + for { + select { + case <-d.N: + break out + case <-d.Q: + d.interrupt = true + d.clearBuffers() + + return false + } + } + + return true +} + +func (d *Debugger) clearBuffers() { +out: + // drain + for { + select { + case <-d.N: + case <-d.Q: + default: + break out + } + } +} + +func (d *Debugger) Next() { + if !d.done { + d.N <- true + } +} diff --git a/ethereal/ui/gui.go b/ethereal/ui/gui.go index 022f192bf..2ba89ce22 100644 --- a/ethereal/ui/gui.go +++ b/ethereal/ui/gui.go @@ -8,9 +8,11 @@ import ( "github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethutil" + "github.com/ethereum/go-ethereum/utils" "github.com/go-qml/qml" "math/big" "strings" + "time" ) type Gui struct { @@ -54,7 +56,7 @@ func New(ethereum *eth.Ethereum) *Gui { } func (gui *Gui) Start(assetPath string) { - const version = "0.5.0 RC8" + const version = "0.5.0 RC12" defer gui.txDb.Close() @@ -63,10 +65,12 @@ func (gui *Gui) Start(assetPath string) { Init: func(p *ethpub.PBlock, obj qml.Object) { p.Number = 0; p.Hash = "" }, }, { Init: func(p *ethpub.PTx, obj qml.Object) { p.Value = ""; p.Hash = ""; p.Address = "" }, + }, { + Init: func(p *ethpub.KeyVal, obj qml.Object) { p.Key = ""; p.Value = "" }, }}) ethutil.Config.SetClientString(fmt.Sprintf("/Ethereal v%s", version)) - ethutil.Config.Log.Infoln("[GUI] Starting GUI") + // Create a new QML engine gui.engine = qml.NewEngine() context := gui.engine.Context() @@ -86,19 +90,36 @@ func (gui *Gui) Start(assetPath string) { win, err = gui.showKeyImport(context) } else { win, err = gui.showWallet(context) + + ethutil.Config.Log.AddLogSystem(gui) } if err != nil { - ethutil.Config.Log.Infoln("FATAL: asset not found: you can set an alternative asset path on on the command line using option 'asset_path'") + ethutil.Config.Log.Infoln("FATAL: asset not found: you can set an alternative asset path on on the command line using option 'asset_path'", err) panic(err) } + ethutil.Config.Log.Infoln("[GUI] Starting GUI") + win.Show() win.Wait() gui.eth.Stop() } +func (gui *Gui) ToggleMining() { + var txt string + if gui.eth.Mining { + utils.StopMining(gui.eth) + txt = "Start mining" + } else { + utils.StartMining(gui.eth) + txt = "Stop mining" + } + + gui.win.Root().Set("miningButtonText", txt) +} + func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) { component, err := gui.engine.LoadFile(gui.uiLib.AssetPath("qml/wallet.qml")) if err != nil { @@ -107,8 +128,11 @@ func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) { win := gui.createWindow(component) - go gui.setInitialBlockChain() - go gui.readPreviousTransactions() + gui.setInitialBlockChain() + gui.loadAddressBook() + gui.readPreviousTransactions() + gui.setPeerInfo() + go gui.update() return win, nil @@ -130,20 +154,46 @@ func (gui *Gui) createWindow(comp qml.Object) *qml.Window { gui.win = win gui.uiLib.win = win - db := &Debugger{gui.win, make(chan bool)} + db := &Debugger{gui.win, make(chan bool), make(chan bool), true, false} gui.lib.Db = db gui.uiLib.Db = db return gui.win } - func (gui *Gui) setInitialBlockChain() { - // Load previous 10 blocks - chain := gui.eth.BlockChain().GetChain(gui.eth.BlockChain().CurrentBlock.Hash(), 10) - for _, block := range chain { - gui.processBlock(block) + sBlk := gui.eth.BlockChain().LastBlockHash + blk := gui.eth.BlockChain().GetBlock(sBlk) + for ; blk != nil; blk = gui.eth.BlockChain().GetBlock(sBlk) { + sBlk = blk.PrevHash + + // Loop through all transactions to see if we missed any while being offline + for _, tx := range blk.Transactions() { + if bytes.Compare(tx.Sender(), gui.addr) == 0 || bytes.Compare(tx.Recipient, gui.addr) == 0 { + if ok, _ := gui.txDb.Get(tx.Hash()); ok == nil { + gui.txDb.Put(tx.Hash(), tx.RlpEncode()) + } + + } + } + + gui.processBlock(blk, true) } +} + +type address struct { + Name, Address string +} +var namereg = ethutil.FromHex("bb5f186604d057c1c5240ca2ae0f6430138ac010") + +func (gui *Gui) loadAddressBook() { + gui.win.Root().Call("clearAddress") + stateObject := gui.eth.StateManager().CurrentState().GetStateObject(namereg) + if stateObject != nil { + stateObject.State().EachStorage(func(name string, value *ethutil.Value) { + gui.win.Root().Call("addAddress", struct{ Name, Address string }{name, ethutil.Hex(value.Bytes())}) + }) + } } func (gui *Gui) readPreviousTransactions() { @@ -151,13 +201,21 @@ func (gui *Gui) readPreviousTransactions() { for it.Next() { tx := ethchain.NewTransactionFromBytes(it.Value()) - gui.win.Root().Call("addTx", ethpub.NewPTx(tx)) + var inout string + if bytes.Compare(tx.Sender(), gui.addr) == 0 { + inout = "send" + } else { + inout = "recv" + } + + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), inout) + } it.Release() } -func (gui *Gui) processBlock(block *ethchain.Block) { - gui.win.Root().Call("addBlock", ethpub.NewPBlock(block)) +func (gui *Gui) processBlock(block *ethchain.Block, initial bool) { + gui.win.Root().Call("addBlock", ethpub.NewPBlock(block), initial) } func (gui *Gui) setWalletValue(amount, unconfirmedFunds *big.Int) { @@ -182,10 +240,16 @@ func (gui *Gui) update() { 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) + reactor.Subscribe("object:"+string(namereg), objectChan) + reactor.Subscribe("peerList", peerChan) + + ticker := time.NewTicker(5 * time.Second) state := gui.eth.StateManager().TransState() @@ -196,6 +260,7 @@ func (gui *Gui) update() { select { case b := <-blockChan: block := b.Resource.(*ethchain.Block) + gui.processBlock(block, false) if bytes.Compare(block.Coinbase, gui.addr) == 0 { gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.addr).Amount, nil) } @@ -207,12 +272,12 @@ func (gui *Gui) update() { object := state.GetAccount(gui.addr) if bytes.Compare(tx.Sender(), gui.addr) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx)) + 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.addr) == 0 { - gui.win.Root().Call("addTx", ethpub.NewPTx(tx)) + gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv") gui.txDb.Put(tx.Hash(), tx.RlpEncode()) unconfirmedFunds.Add(unconfirmedFunds, tx.Value) @@ -231,10 +296,25 @@ func (gui *Gui) update() { state.UpdateStateObject(object) } + case <-objectChan: + gui.loadAddressBook() + case <-peerChan: + gui.setPeerInfo() + case <-ticker.C: + gui.setPeerInfo() } } } +func (gui *Gui) setPeerInfo() { + gui.win.Root().Call("setPeers", fmt.Sprintf("%d / %d", gui.eth.PeerCount(), gui.eth.MaxPeers)) + + gui.win.Root().Call("resetPeers") + for _, peer := range gui.pub.GetPeers() { + gui.win.Root().Call("addPeer", peer) + } +} + // Logging functions that log directly to the GUI interface func (gui *Gui) Println(v ...interface{}) { str := strings.TrimRight(fmt.Sprintln(v...), "\n") @@ -251,6 +331,11 @@ func (gui *Gui) Printf(format string, v ...interface{}) { gui.win.Root().Call("addLog", line) } } +func (gui *Gui) RegisterName(name string) { + keyPair := ethutil.GetKeyRing().Get(0) + name = fmt.Sprintf("\"%s\"\n1", name) + gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), "namereg", "1000", "1000000", "150", name) +} func (gui *Gui) Transact(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) { keyPair := ethutil.GetKeyRing().Get(0) @@ -261,7 +346,13 @@ func (gui *Gui) Transact(recipient, value, gas, gasPrice, data string) (*ethpub. func (gui *Gui) Create(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) { keyPair := ethutil.GetKeyRing().Get(0) - //mainInput, initInput := mutan.PreParse(data) + return gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), recipient, value, gas, gasPrice, data) +} + +func (gui *Gui) ChangeClientId(id string) { + ethutil.Config.SetIdentifier(id) +} - return gui.pub.Create(ethutil.Hex(keyPair.PrivateKey), value, gas, gasPrice, data) +func (gui *Gui) ClientId() string { + return ethutil.Config.Identifier } diff --git a/ethereal/ui/ui_lib.go b/ethereal/ui/ui_lib.go index 1c88b0181..9f2cca1e0 100644 --- a/ethereal/ui/ui_lib.go +++ b/ethereal/ui/ui_lib.go @@ -2,13 +2,10 @@ package ethui import ( "bitbucket.org/kardianos/osext" - "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethutil" - "github.com/ethereum/go-ethereum/utils" "github.com/go-qml/qml" - "github.com/obscuren/mutan" "os" "path" "path/filepath" @@ -27,8 +24,9 @@ type UiLib struct { connected bool assetPath string // The main application window - win *qml.Window - Db *Debugger + win *qml.Window + Db *Debugger + DbWindow *DebuggerWindow } func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { @@ -91,6 +89,29 @@ func (ui *UiLib) ConnectToPeer(addr string) { func (ui *UiLib) AssetPath(p string) string { return path.Join(ui.assetPath, p) } +func (self *UiLib) StartDbWithContractAndData(contractHash, data string) { + dbWindow := NewDebuggerWindow(self) + object := self.eth.StateManager().CurrentState().GetStateObject(ethutil.FromHex(contractHash)) + if len(object.Script()) > 0 { + dbWindow.SetCode("0x" + ethutil.Hex(object.Script())) + } + dbWindow.SetData("0x" + data) + + dbWindow.Show() +} + +func (self *UiLib) StartDbWithCode(code string) { + dbWindow := NewDebuggerWindow(self) + dbWindow.SetCode("0x" + code) + dbWindow.Show() +} + +func (self *UiLib) StartDebugger() { + dbWindow := NewDebuggerWindow(self) + //self.DbWindow = dbWindow + + dbWindow.Show() +} func DefaultAssetPath() string { var base string @@ -121,27 +142,28 @@ func DefaultAssetPath() string { func (ui *UiLib) DebugTx(recipient, valueStr, gasStr, gasPriceStr, data string) { state := ui.eth.BlockChain().CurrentBlock.State() - mainInput, _ := mutan.PreParse(data) - callerScript, err := utils.Compile(mainInput) + script, err := ethutil.Compile(data) if err != nil { ethutil.Config.Log.Debugln(err) return } - dis := ethchain.Disassemble(callerScript) + dis := ethchain.Disassemble(script) ui.win.Root().Call("clearAsm") for _, str := range dis { ui.win.Root().Call("setAsm", str) } - callerTx := ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), nil) - // Contract addr as test address keyPair := ethutil.GetKeyRing().Get(0) + callerTx := + ethchain.NewContractCreationTx(ethutil.Big(valueStr), ethutil.Big(gasStr), ethutil.Big(gasPriceStr), script) + callerTx.Sign(keyPair.PrivateKey) + account := ui.eth.StateManager().TransState().GetStateObject(keyPair.Address()) - c := ethchain.MakeContract(callerTx, state) - callerClosure := ethchain.NewClosure(account, c, c.Script(), state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr)) + contract := ethchain.MakeContract(callerTx, state) + callerClosure := ethchain.NewClosure(account, contract, contract.Init(), state, ethutil.Big(gasStr), ethutil.Big(gasPriceStr)) block := ui.eth.BlockChain().CurrentBlock vm := ethchain.NewVm(state, ui.eth.StateManager(), ethchain.RuntimeVars{ @@ -151,50 +173,18 @@ func (ui *UiLib) DebugTx(recipient, valueStr, gasStr, gasPriceStr, data string) Coinbase: block.Coinbase, Time: block.Time, Diff: block.Difficulty, - TxData: nil, }) + ui.Db.done = false go func() { - callerClosure.Call(vm, nil, ui.Db.halting) + callerClosure.Call(vm, contract.Init(), ui.Db.halting) state.Reset() + + ui.Db.done = true }() } func (ui *UiLib) Next() { ui.Db.Next() } - -type Debugger struct { - win *qml.Window - N chan bool -} - -func (d *Debugger) halting(pc int, op ethchain.OpCode, mem *ethchain.Memory, stack *ethchain.Stack) { - d.win.Root().Call("setInstruction", pc) - d.win.Root().Call("clearMem") - d.win.Root().Call("clearStack") - - addr := 0 - for i := 0; i+32 <= mem.Len(); i += 32 { - d.win.Root().Call("setMem", memAddr{fmt.Sprintf("%03d", addr), fmt.Sprintf("% x", mem.Data()[i:i+32])}) - addr++ - } - - for _, val := range stack.Data() { - d.win.Root().Call("setStack", val.String()) - } - -out: - for { - select { - case <-d.N: - break out - default: - } - } -} - -func (d *Debugger) Next() { - d.N <- true -} |