From 55a2f35a648ef70cdcc88bd751265e30831b54e5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 20 Aug 2014 13:05:26 +0200 Subject: [PATCH] JS Filter --- ethchain/filter.go | 4 +--- ethpipe/js_pipe.go | 35 ++++++++++++++++++++++++++++++----- ethutil/bytes.go | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/ethchain/filter.go b/ethchain/filter.go index c4c403cf7..5ed9af977 100644 --- a/ethchain/filter.go +++ b/ethchain/filter.go @@ -72,8 +72,6 @@ func NewFilterFromMap(object map[string]interface{}, eth EthManager) *Filter { filter.altered = makeAltered(object["altered"]) } - fmt.Println("ALTERED", filter.altered) - return filter } @@ -123,7 +121,7 @@ func (self *Filter) SetTo(addr [][]byte) { } func (self *Filter) AddTo(addr []byte) { - self.from = append(self.to, addr) + self.to = append(self.to, addr) } func (self *Filter) SetMax(max int) { diff --git a/ethpipe/js_pipe.go b/ethpipe/js_pipe.go index 0d0928fc3..4b0d6a849 100644 --- a/ethpipe/js_pipe.go +++ b/ethpipe/js_pipe.go @@ -1,6 +1,7 @@ package ethpipe import ( + "bytes" "encoding/json" "fmt" "sync/atomic" @@ -119,6 +120,28 @@ func (self *JSPipe) EachStorage(addr string) string { return string(valuesJson) } +func (self *JSPipe) ToAscii(str string) string { + padded := ethutil.RightPadBytes([]byte(str), 32) + + return "0x" + ethutil.Bytes2Hex(padded) +} + +func (self *JSPipe) FromAscii(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return string(bytes.Trim(ethutil.Hex2Bytes(str), "\x00")) +} + +func (self *JSPipe) FromNumber(str string) string { + if ethutil.IsHex(str) { + str = str[2:] + } + + return ethutil.BigD(ethutil.Hex2Bytes(str)).String() +} + func (self *JSPipe) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (*JSReceipt, error) { var hash []byte var contractCreation bool @@ -200,8 +223,7 @@ func (self *JSPipe) Watch(object map[string]interface{}) *JSFilter { func (self *JSPipe) Messages(object map[string]interface{}) string { filter := self.Watch(object) - - defer filter.Uninstall() + filter.Uninstall() return filter.Messages() @@ -247,8 +269,8 @@ func (self *JSFilter) Messages() string { } func (self *JSFilter) mainLoop() { - blockChan := make(chan ethreact.Event, 1) - messageChan := make(chan ethreact.Event, 1) + blockChan := make(chan ethreact.Event, 5) + messageChan := make(chan ethreact.Event, 5) // Subscribe to events reactor := self.eth.Reactor() reactor.Subscribe("newBlock", blockChan) @@ -267,8 +289,11 @@ out: case msg := <-messageChan: if messages, ok := msg.Resource.(ethstate.Messages); ok { if self.MessageCallback != nil { + println("messages!") msgs := self.FilterMessages(messages) - self.MessageCallback(msgs) + if len(msgs) > 0 { + self.MessageCallback(msgs) + } } } } diff --git a/ethutil/bytes.go b/ethutil/bytes.go index 49fc229d3..63c1606c2 100644 --- a/ethutil/bytes.go +++ b/ethutil/bytes.go @@ -173,6 +173,28 @@ func LeftPadBytes(slice []byte, l int) []byte { return padded } +func LeftPadString(str string, l int) string { + if l < len(str) { + return str + } + + zeros := Bytes2Hex(make([]byte, (l-len(str))/2)) + + return zeros + str + +} + +func RightPadString(str string, l int) string { + if l < len(str) { + return str + } + + zeros := Bytes2Hex(make([]byte, (l-len(str))/2)) + + return str + zeros + +} + func Address(slice []byte) (addr []byte) { if len(slice) < 20 { addr = LeftPadBytes(slice, 20)