Reworked filters

This commit is contained in:
obscuren 2014-09-14 00:13:47 +02:00
parent 893e9256a0
commit 91ca5d724e
5 changed files with 834 additions and 789 deletions

View File

@ -1,31 +1,49 @@
var ethx = {
prototype: Object,
watch: function(options) {
return new Filter(options);
},
note: function() {
var args = Array.prototype.slice.call(arguments, 0);
var o = []
for(var i = 0; i < args.length; i++) {
o.push(args[i].toString())
}
eth.notef(o);
},
};
var Filter = function(options) { var Filter = function(options) {
this.callbacks = {}; this.callbacks = [];
this.seed = Math.floor(Math.random() * 1000000);
this.options = options; this.options = options;
if(options === "chain") { if(options === "chain") {
eth.registerFilterString(options, this.seed); this.id = eth.newFilterString(options);
} else if(typeof options === "object") { } else if(typeof options === "object") {
eth.registerFilter(options, this.seed); this.id = eth.newFilter(options);
} }
}; };
Filter.prototype.changed = function(callback) { Filter.prototype.changed = function(callback) {
var cbseed = Math.floor(Math.random() * 1000000); this.callbacks.push(callback);
eth.registerFilterCallback(this.seed, cbseed);
var self = this; var self = this;
message.connect(function(messages, seed, callbackSeed) { message.connect(function(messages, id) {
if(seed == self.seed && callbackSeed == cbseed) { if(id == self.id) {
callback.call(self, messages); for(var i = 0; i < self.callbacks.length; i++) {
self.callbacks[i].call(self, messages);
}
} }
}); });
}; };
Filter.prototype.uninstall = function() { Filter.prototype.uninstall = function() {
eth.uninstallFilter(this.seed) eth.uninstallFilter(this.id)
} }
Filter.prototype.messages = function() { Filter.prototype.messages = function() {
return JSON.parse(eth.messages(this.options)) return eth.messages(this.id)
} }

View File

@ -5,7 +5,6 @@ import QtQuick.Dialogs 1.0;
import QtQuick.Window 2.1; import QtQuick.Window 2.1;
import QtQuick.Controls.Styles 1.1 import QtQuick.Controls.Styles 1.1
import Ethereum 1.0 import Ethereum 1.0
import "../../ext/filter.js" as Eth
Rectangle { Rectangle {
id: root id: root
@ -152,7 +151,7 @@ Rectangle {
model: ListModel { model: ListModel {
id: txModel id: txModel
Component.onCompleted: { Component.onCompleted: {
var filter = new Eth.Filter({latest: -1, from: eth.key().address}) var filter = ethx.watch({latest: -1, from: eth.key().address});
filter.changed(addTxs) filter.changed(addTxs)
addTxs(filter.messages()) addTxs(filter.messages())
@ -160,7 +159,7 @@ Rectangle {
function addTxs(messages) { function addTxs(messages) {
for(var i = 0; i < messages.length; i++) { for(var i = 0; i < messages.length; i++) {
var message = messages[i]; var message = messages.get(i);
txModel.insert(0, {num: txModel.count, from: message.from, to: message.to, value: eth.numberToHuman(message.value)}) txModel.insert(0, {num: txModel.count, from: message.from, to: message.to, value: eth.numberToHuman(message.value)})
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,8 @@
package main package main
// #include "/Users/jeffrey/go/src/github.com/go-qml/qml/cpp/capi.h"
import "C"
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
@ -10,7 +13,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unsafe"
"bitbucket.org/binet/go-ffi/pkg/ffi"
"github.com/ethereum/eth-go" "github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethchain" "github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethdb" "github.com/ethereum/eth-go/ethdb"
@ -23,6 +28,29 @@ import (
"gopkg.in/qml.v1" "gopkg.in/qml.v1"
) )
func LoadExtension(path string) (uintptr, error) {
lib, err := ffi.NewLibrary(path)
if err != nil {
return 0, err
}
so, err := lib.Fct("sharedObject", ffi.Pointer, nil)
if err != nil {
return 0, err
}
ptr := so()
/*
err = lib.Close()
if err != nil {
return 0, err
}
*/
return ptr.Interface().(uintptr), nil
}
var logger = ethlog.NewLogger("GUI") var logger = ethlog.NewLogger("GUI")
type Gui struct { type Gui struct {
@ -91,6 +119,14 @@ func (gui *Gui) Start(assetPath string) {
context.SetVar("gui", gui) context.SetVar("gui", gui)
context.SetVar("eth", gui.uiLib) context.SetVar("eth", gui.uiLib)
vec, errr := LoadExtension("/Users/jeffrey/Desktop/build-libqmltest-Desktop_Qt_5_2_1_clang_64bit-Debug/liblibqmltest_debug.dylib")
fmt.Printf("Fetched vec with addr: %#x\n", vec)
if errr != nil {
fmt.Println(errr)
} else {
context.SetVar("vec", (unsafe.Pointer)(vec))
}
// Load the main QML interface // Load the main QML interface
data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))

View File

@ -37,15 +37,15 @@ type UiLib struct {
jsEngine *javascript.JSRE jsEngine *javascript.JSRE
filterCallbacks map[int][]int filterCallbacks map[int][]int
filters map[int]*GuiFilter //filters map[int]*ethpipe.JSFilter
} }
func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib {
return &UiLib{JSPipe: ethpipe.NewJSPipe(eth), engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(eth), filterCallbacks: make(map[int][]int), filters: make(map[int]*GuiFilter)} return &UiLib{JSPipe: ethpipe.NewJSPipe(eth), engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(eth), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*ethpipe.JSFilter)}
} }
func (self *UiLib) Note(msg string) { func (self *UiLib) Notef(args []interface{}) {
logger.Infoln(msg) logger.Infoln(args...)
} }
func (self *UiLib) LookupDomain(domain string) string { func (self *UiLib) LookupDomain(domain string) string {
@ -164,46 +164,37 @@ func (self *UiLib) StartDebugger() {
dbWindow.Show() dbWindow.Show()
} }
func (self *UiLib) RegisterFilter(object map[string]interface{}, seed int) { func (self *UiLib) NewFilter(object map[string]interface{}) int {
filter := &GuiFilter{ethpipe.NewJSFilterFromMap(object, self.eth), seed} filter, id := self.eth.InstallFilter(object)
self.filters[seed] = filter
filter.MessageCallback = func(messages ethstate.Messages) { filter.MessageCallback = func(messages ethstate.Messages) {
for _, callbackSeed := range self.filterCallbacks[seed] { self.win.Root().Call("invokeFilterCallback", ethpipe.ToJSMessages(messages), id)
self.win.Root().Call("invokeFilterCallback", filter.MessagesToJson(messages), seed, callbackSeed)
}
} }
return id
} }
func (self *UiLib) RegisterFilterString(typ string, seed int) { func (self *UiLib) NewFilterString(typ string) int {
filter := &GuiFilter{ethpipe.NewJSFilterFromMap(nil, self.eth), seed} filter, id := self.eth.InstallFilter(nil)
self.filters[seed] = filter filter.BlockCallback = func(block *ethchain.Block) {
self.win.Root().Call("invokeFilterCallback", "{}", id)
if typ == "chain" {
filter.BlockCallback = func(block *ethchain.Block) {
for _, callbackSeed := range self.filterCallbacks[seed] {
self.win.Root().Call("invokeFilterCallback", "{}", seed, callbackSeed)
}
}
} }
return id
} }
func (self *UiLib) RegisterFilterCallback(seed, cbSeed int) { func (self *UiLib) Messages(id int) *ethutil.List {
self.filterCallbacks[seed] = append(self.filterCallbacks[seed], cbSeed) filter := self.eth.GetFilter(id)
}
func (self *UiLib) UninstallFilter(seed int) {
filter := self.filters[seed]
if filter != nil { if filter != nil {
filter.Uninstall() messages := filter.Find()
delete(self.filters, seed)
return ethpipe.ToJSMessages(messages)
} }
return ethutil.EmptyList()
} }
type GuiFilter struct { func (self *UiLib) UninstallFilter(id int) {
*ethpipe.JSFilter self.eth.UninstallFilter(id)
seed int
} }
func (self *UiLib) Transact(object map[string]interface{}) (*ethpipe.JSReceipt, error) { func (self *UiLib) Transact(object map[string]interface{}) (*ethpipe.JSReceipt, error) {