From f6aabb7a90903a681eca44976301620756124137 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 23 Jun 2014 16:25:57 +0200 Subject: [PATCH 1/5] Implements QML Apps. Implements #47 You are welcome Stephan. --- ethereal/assets/qml/QmlApp.qml | 22 +++++++++ ethereal/assets/qml/test_app.qml | 81 +++++++++++++++++++++++--------- ethereal/assets/qml/wallet.qml | 10 +++- ethereal/ui/qml_app.go | 59 +++++++++++++++++++++++ ethereal/ui/ui_lib.go | 15 ++---- 5 files changed, 152 insertions(+), 35 deletions(-) create mode 100644 ethereal/assets/qml/QmlApp.qml create mode 100644 ethereal/ui/qml_app.go diff --git a/ethereal/assets/qml/QmlApp.qml b/ethereal/assets/qml/QmlApp.qml new file mode 100644 index 0000000000..f5c503f4c3 --- /dev/null +++ b/ethereal/assets/qml/QmlApp.qml @@ -0,0 +1,22 @@ +import QtQuick 2.0 +import QtQuick.Controls 1.0; +import QtQuick.Layouts 1.0; +import Ethereum 1.0 + +ApplicationWindow { + minimumWidth: 500 + maximumWidth: 500 + maximumHeight: 400 + minimumHeight: 400 + + function onNewBlockCb(block) { + console.log("Please overwrite onNewBlock(block):", block) + } + function onObjectChangeCb(stateObject) { + console.log("Please overwrite onObjectChangeCb(object)", stateObject) + } + function onStorageChangeCb(storageObject) { + var ev = ["storage", storageObject.stateAddress, storageObject.address].join(":"); + console.log("Please overwrite onStorageChangeCb(object)", ev) + } +} diff --git a/ethereal/assets/qml/test_app.qml b/ethereal/assets/qml/test_app.qml index aace4e881f..c69587839e 100644 --- a/ethereal/assets/qml/test_app.qml +++ b/ethereal/assets/qml/test_app.qml @@ -3,33 +3,68 @@ import QtQuick.Controls 1.0; import QtQuick.Layouts 1.0; import Ethereum 1.0 -ApplicationWindow { - minimumWidth: 500 - maximumWidth: 500 - maximumHeight: 100 - minimumHeight: 100 +QmlApp { + minimumWidth: 350 + maximumWidth: 350 + maximumHeight: 80 + minimumHeight: 80 - title: "Ethereum Dice" + title: "Generic Coin" - TextField { - id: textField - anchors.verticalCenter: parent.verticalCenter - anchors.horizontalCenter: parent.horizontalCenter - placeholderText: "Amount" + property string contractAddr: "f299f6c74515620e4c4cd8fe3d205b5c4f2e25c8" + property string addr: "2ef47100e0787b915105fd5e3f4ff6752079d5cb" + + Component.onCompleted: { + eth.watch(contractAddr, addr) + eth.watch(addr, contractAddr) + setAmount() } - Label { - id: txHash - anchors.bottom: textField.top - anchors.bottomMargin: 5 - anchors.horizontalCenter: parent.horizontalCenter + + function onStorageChangeCb(storageObject) { + setAmount() } - Button { - anchors.top: textField.bottom - anchors.horizontalCenter: parent.horizontalCenter - anchors.topMargin: 5 - text: "Place bet" - onClicked: { - txHash.text = eth.createTx("e6716f9544a56c530d868e4bfbacb172315bdead", textField.text) + + function setAmount(){ + var state = eth.getStateObject(contractAddr) + var storage = state.getStorage(addr) + amountLabel.text = storage + } + Column { + spacing: 5 + Row { + spacing: 20 + Label { + id: genLabel + text: "Generic coin balance:" + } + Label { + id: amountLabel + } + } + Row { + spacing: 20 + TextField { + id: address + placeholderText: "Address" + } + TextField { + id: amount + placeholderText: "Amount" + } + } + Button { + text: "Send coins" + onClicked: { + var privKey = eth.getKey().privateKey + if(privKey){ + var result = eth.transact(privKey, contractAddr, 0,"100000","250", "0x" + address.text + "\n" + amount.text) + resultTx.text = result.hash + } + } + } + Label { + id: resultTx } } + } diff --git a/ethereal/assets/qml/wallet.qml b/ethereal/assets/qml/wallet.qml index a7c03f6d18..454d0f3f01 100644 --- a/ethereal/assets/qml/wallet.qml +++ b/ethereal/assets/qml/wallet.qml @@ -372,7 +372,15 @@ ApplicationWindow { onAccepted: { //ui.open(openAppDialog.fileUrl.toString()) //ui.openHtml(Qt.resolvedUrl(ui.assetPath("test.html"))) - ui.openHtml(openAppDialog.fileUrl.toString()) + var path = openAppDialog.fileUrl.toString() + console.log(path) + var ext = path.split('.').pop() + console.log(ext) + if(ext == "html" || ext == "htm") { + ui.openHtml(path) + }else if(ext == "qml"){ + ui.openQml(path) + } } } diff --git a/ethereal/ui/qml_app.go b/ethereal/ui/qml_app.go new file mode 100644 index 0000000000..d47751616e --- /dev/null +++ b/ethereal/ui/qml_app.go @@ -0,0 +1,59 @@ +package ethui + +import ( + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethutil" + "github.com/go-qml/qml" +) + +type QmlApplication struct { + win *qml.Window + engine *qml.Engine + lib *UiLib + path string +} + +func NewQmlApplication(path string, lib *UiLib) *QmlApplication { + engine := qml.NewEngine() + return &QmlApplication{engine: engine, path: path, lib: lib} +} + +func (app *QmlApplication) Create() error { + component, err := app.engine.LoadFile(app.path) + if err != nil { + ethutil.Config.Log.Debugln(err) + } + app.win = component.CreateWindow(nil) + + return nil +} + +func (app *QmlApplication) Destroy() { + app.engine.Destroy() +} + +func (app *QmlApplication) NewWatcher(quitChan chan bool) { +} + +// Events +func (app *QmlApplication) NewBlock(block *ethchain.Block) { + pblock := ðpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())} + app.win.Call("onNewBlockCb", pblock) +} + +func (app *QmlApplication) ObjectChanged(stateObject *ethchain.StateObject) { + app.win.Call("onObjectChangeCb", ethpub.NewPStateObject(stateObject)) +} + +func (app *QmlApplication) StorageChanged(storageObject *ethchain.StorageState) { + app.win.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject)) +} + +// Getters +func (app *QmlApplication) Engine() *qml.Engine { + return app.engine +} +func (app *QmlApplication) Window() *qml.Window { + return app.win +} diff --git a/ethereal/ui/ui_lib.go b/ethereal/ui/ui_lib.go index 791d4fe097..908497be3e 100644 --- a/ethereal/ui/ui_lib.go +++ b/ethereal/ui/ui_lib.go @@ -35,18 +35,11 @@ func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { return &UiLib{engine: engine, eth: eth, assetPath: assetPath} } -// Opens a QML file (external application) -func (ui *UiLib) Open(path string) { - component, err := ui.engine.LoadFile(path[7:]) - if err != nil { - ethutil.Config.Log.Debugln(err) - } - win := component.CreateWindow(nil) +func (ui *UiLib) OpenQml(path string) { + container := NewQmlApplication(path[7:], ui) + app := NewExtApplication(container, ui) - go func() { - win.Show() - win.Wait() - }() + go app.run() } func (ui *UiLib) OpenHtml(path string) { From 17e8d7519b1aac322db08b857e63db82a322d6cf Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 24 Jun 2014 09:36:05 +0200 Subject: [PATCH 2/5] Renamed execBlock --- ethereum/javascript_runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 737f7663f2..92d9c119fa 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -138,7 +138,7 @@ func (self *JSRE) initStdFuncs() { eth.Set("require", self.require) eth.Set("stopMining", self.stopMining) eth.Set("startMining", self.startMining) - eth.Set("blockDo", self.execBlock) + eth.Set("execBlock", self.execBlock) } /* From fd1ddbce6892e3f0e09eec68687b6ef34b216888 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 24 Jun 2014 10:09:02 +0200 Subject: [PATCH 3/5] Save repl history to file and recall on next session --- ethereum/repl.go | 27 ++++++++++++++++++++++++++- ethereum/repl_darwin.go | 4 +++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ethereum/repl.go b/ethereum/repl.go index 0208459ad2..dd8d081793 100644 --- a/ethereum/repl.go +++ b/ethereum/repl.go @@ -1,10 +1,15 @@ package main import ( + "bufio" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethutil" "github.com/obscuren/otto" + "io" + "os" + "path" ) type Repl interface { @@ -16,18 +21,38 @@ type JSRepl struct { re *JSRE prompt string + + history *os.File } func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { - return &JSRepl{re: NewJSRE(ethereum), prompt: "> "} + hist, err := os.OpenFile(path.Join(ethutil.Config.ExecPath, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm) + if err != nil { + panic(err) + } + + return &JSRepl{re: NewJSRE(ethereum), prompt: "> ", history: hist} } func (self *JSRepl) Start() { + reader := bufio.NewReader(self.history) + for { + line, err := reader.ReadString('\n') + if err != nil && err == io.EOF { + break + } else if err != nil { + fmt.Println("error reading history", err) + break + } + + addHistory(line[:len(line)-1]) + } self.read() } func (self *JSRepl) Stop() { self.re.Stop() + self.history.Close() } func (self *JSRepl) parseInput(code string) { diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index b61d4edd7e..62b40059ac 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -102,7 +102,9 @@ L: break L } - addHistory(str[:len(str)-1]) //allow user to recall this line + hist := str[:len(str)-1] + addHistory(hist) //allow user to recall this line + self.history.WriteString(str) self.parseInput(str) From 1e965cb8f5c63d73a5aac1556a2638345ba2824c Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 25 Jun 2014 09:47:11 +0200 Subject: [PATCH 4/5] Moved BlockDo to utils --- ethereum/javascript_runtime.go | 2 +- utils/cmd.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go index 92d9c119fa..a9b12629ab 100644 --- a/ethereum/javascript_runtime.go +++ b/ethereum/javascript_runtime.go @@ -215,7 +215,7 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() } - err = self.ethereum.BlockDo(ethutil.FromHex(hash)) + err = utils.BlockDo(self.ethereum, ethutil.FromHex(hash)) if err != nil { fmt.Println(err) return otto.FalseValue() diff --git a/utils/cmd.go b/utils/cmd.go index e66bb2612a..368f2381e7 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethminer" "github.com/ethereum/eth-go/ethpub" @@ -74,3 +75,21 @@ func StartMining(ethereum *eth.Ethereum) bool { return false } + +// Replay block +func BlockDo(ethereum *eth.Ethereum, hash []byte) error { + block := ethereum.BlockChain().GetBlock(hash) + if block == nil { + return fmt.Errorf("unknown block %x", hash) + } + + parent := ethereum.BlockChain().GetBlock(block.PrevHash) + + _, err := ethereum.StateManager().ApplyDiff(parent.State(), parent, block) + if err != nil { + return err + } + + return nil + +} From 9654b809120d1cc3c53ffe268fe47869ef0dc0a8 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 25 Jun 2014 16:12:53 +0200 Subject: [PATCH 5/5] Implemented TX History for ethjs --- ethereal/assets/ext/ethereum.js | 3 +++ ethereal/assets/qml/webapp.qml | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/ethereal/assets/ext/ethereum.js b/ethereal/assets/ext/ethereum.js index c58fe24c24..de6fb02553 100644 --- a/ethereal/assets/ext/ethereum.js +++ b/ethereal/assets/ext/ethereum.js @@ -58,6 +58,9 @@ window.eth = { getBalanceAt: function(address, cb) { postData({call: "getBalance", args: [address]}, cb); }, + getTransactionsFor: function(address, cb) { + postData({call: "getTransactionsFor", args: [address]}, cb); + }, getSecretToAddress: function(sec, cb) { postData({call: "getSecretToAddress", args: [sec]}, cb); diff --git a/ethereal/assets/qml/webapp.qml b/ethereal/assets/qml/webapp.qml index 4a5a1293a3..63927f0ebe 100644 --- a/ethereal/assets/qml/webapp.qml +++ b/ethereal/assets/qml/webapp.qml @@ -103,6 +103,12 @@ ApplicationWindow { postData(data._seed,stateObject) break + case "getTransactionsFor": + require(1); + var txs = eth.getTransactionsFor(data.args[0], true) + postData(data._seed, txs) + + break case "getBalance": require(1);