2014-02-21 13:37:40 +02:00
|
|
|
package ethui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ethereum/eth-go"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/niemeyer/qml"
|
|
|
|
"strings"
|
2014-02-21 14:23:35 +02:00
|
|
|
"time"
|
2014-02-21 13:37:40 +02:00
|
|
|
)
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Block interface exposed to QML
|
|
|
|
type Block struct {
|
|
|
|
Number int
|
|
|
|
Hash string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new QML Block from a chain block
|
|
|
|
func NewBlockFromBlock(block *ethchain.Block) *Block {
|
|
|
|
info := block.BlockInfo()
|
|
|
|
hash := hex.EncodeToString(block.Hash())
|
|
|
|
|
|
|
|
return &Block{Number: int(info.Number), Hash: hash}
|
|
|
|
}
|
|
|
|
|
2014-02-21 13:37:40 +02:00
|
|
|
type Gui struct {
|
2014-02-23 00:19:38 +02:00
|
|
|
// The main application window
|
|
|
|
win *qml.Window
|
|
|
|
// QML Engine
|
2014-02-21 13:37:40 +02:00
|
|
|
engine *qml.Engine
|
|
|
|
component *qml.Common
|
2014-02-23 00:19:38 +02:00
|
|
|
// The ethereum interface
|
|
|
|
eth *eth.Ethereum
|
2014-02-21 18:29:59 +02:00
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// The public Ethereum library
|
2014-02-21 18:29:59 +02:00
|
|
|
lib *EthLib
|
2014-02-21 13:37:40 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Create GUI, but doesn't start it
|
2014-02-21 13:37:40 +02:00
|
|
|
func New(ethereum *eth.Ethereum) *Gui {
|
2014-02-21 18:29:59 +02:00
|
|
|
lib := &EthLib{blockManager: ethereum.BlockManager, blockChain: ethereum.BlockManager.BlockChain(), txPool: ethereum.TxPool}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
|
|
|
|
keyRing := ethutil.NewValueFromBytes(data)
|
|
|
|
addr := keyRing.Get(1).Bytes()
|
2014-02-21 13:37:40 +02:00
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
ethereum.BlockManager.WatchAddr(addr)
|
2014-02-21 13:37:40 +02:00
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
return &Gui{eth: ethereum, lib: lib}
|
2014-02-21 13:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *Gui) Start() {
|
2014-02-23 00:19:38 +02:00
|
|
|
// Register ethereum functions
|
|
|
|
qml.RegisterTypes("Ethereum", 1, 0, []qml.TypeSpec{{
|
2014-02-21 13:37:40 +02:00
|
|
|
Init: func(p *Block, obj qml.Object) { p.Number = 0; p.Hash = "" },
|
|
|
|
}})
|
|
|
|
|
|
|
|
ethutil.Config.Log.Infoln("[GUI] Starting GUI")
|
2014-02-23 00:19:38 +02:00
|
|
|
// Create a new QML engine
|
2014-02-21 13:37:40 +02:00
|
|
|
ui.engine = qml.NewEngine()
|
2014-02-23 00:19:38 +02:00
|
|
|
// Load the main QML interface
|
2014-02-21 13:37:40 +02:00
|
|
|
component, err := ui.engine.LoadFile("wallet.qml")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.win = component.CreateWindow(nil)
|
|
|
|
|
|
|
|
context := ui.engine.Context()
|
2014-02-23 00:19:38 +02:00
|
|
|
|
|
|
|
// Expose the eth library and the ui library to QML
|
2014-02-21 18:29:59 +02:00
|
|
|
context.SetVar("eth", ui.lib)
|
2014-02-22 02:52:47 +02:00
|
|
|
context.SetVar("ui", &UiLib{engine: ui.engine, eth: ui.eth})
|
2014-02-21 13:37:40 +02:00
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Register the ui as a block processor
|
2014-02-21 13:37:40 +02:00
|
|
|
ui.eth.BlockManager.SecondaryBlockProcessor = ui
|
2014-02-21 14:06:17 +02:00
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Add the ui as a log system so we can log directly to the UGI
|
2014-02-22 02:52:47 +02:00
|
|
|
ethutil.Config.Log.AddLogSystem(ui)
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Loads previous blocks
|
2014-02-21 14:06:17 +02:00
|
|
|
go ui.setInitialBlockChain()
|
2014-02-21 14:23:35 +02:00
|
|
|
go ui.updatePeers()
|
2014-02-21 13:37:40 +02:00
|
|
|
|
|
|
|
ui.win.Show()
|
|
|
|
ui.win.Wait()
|
|
|
|
}
|
|
|
|
|
2014-02-21 14:06:17 +02:00
|
|
|
func (ui *Gui) setInitialBlockChain() {
|
2014-02-23 00:19:38 +02:00
|
|
|
// Load previous 10 blocks
|
2014-02-21 14:06:17 +02:00
|
|
|
chain := ui.eth.BlockManager.BlockChain().GetChain(ui.eth.BlockManager.BlockChain().CurrentBlock.Hash(), 10)
|
|
|
|
for _, block := range chain {
|
|
|
|
ui.ProcessBlock(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-21 13:37:40 +02:00
|
|
|
func (ui *Gui) ProcessBlock(block *ethchain.Block) {
|
|
|
|
ui.win.Root().Call("addBlock", NewBlockFromBlock(block))
|
|
|
|
}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Logging functions that log directly to the GUI interface
|
2014-02-22 02:52:47 +02:00
|
|
|
func (ui *Gui) Println(v ...interface{}) {
|
2014-02-23 00:19:38 +02:00
|
|
|
str := strings.TrimRight(fmt.Sprintln(v...), "\n")
|
|
|
|
lines := strings.Split(str, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
ui.win.Root().Call("addLog", line)
|
|
|
|
}
|
2014-02-22 02:52:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *Gui) Printf(format string, v ...interface{}) {
|
|
|
|
str := strings.TrimRight(fmt.Sprintf(format, v...), "\n")
|
2014-02-23 00:19:38 +02:00
|
|
|
lines := strings.Split(str, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
ui.win.Root().Call("addLog", line)
|
|
|
|
}
|
2014-02-22 02:52:47 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Simple go routine function that updates the list of peers in the GUI
|
2014-02-21 14:23:35 +02:00
|
|
|
func (ui *Gui) updatePeers() {
|
|
|
|
for {
|
|
|
|
ui.win.Root().Call("setPeers", fmt.Sprintf("%d / %d", ui.eth.Peers().Len(), ui.eth.MaxPeers))
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// UI Library that has some basic functionality exposed
|
2014-02-21 18:29:59 +02:00
|
|
|
type UiLib struct {
|
2014-02-23 00:19:38 +02:00
|
|
|
engine *qml.Engine
|
|
|
|
eth *eth.Ethereum
|
|
|
|
connected bool
|
2014-02-21 18:29:59 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 00:19:38 +02:00
|
|
|
// Opens a QML file (external application)
|
2014-02-21 18:29:59 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
win.Show()
|
|
|
|
win.Wait()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-02-22 02:52:47 +02:00
|
|
|
func (ui *UiLib) Connect() {
|
2014-02-23 00:19:38 +02:00
|
|
|
if !ui.connected {
|
|
|
|
ui.eth.Start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *UiLib) ConnectToPeer(addr string) {
|
|
|
|
ui.eth.ConnectToPeer(addr)
|
2014-02-22 02:52:47 +02:00
|
|
|
}
|
|
|
|
|
2014-02-21 13:37:40 +02:00
|
|
|
type Tester struct {
|
|
|
|
root qml.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tester) Compile(area qml.Object) {
|
|
|
|
fmt.Println(area)
|
|
|
|
ethutil.Config.Log.Infoln("[TESTER] Compiling")
|
|
|
|
|
|
|
|
code := area.String("text")
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(code))
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines = append(lines, scanner.Text())
|
|
|
|
}
|
|
|
|
}
|