From 836ed9d6b70e3ae928624f9ed81ed206a66b85b8 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 22 Aug 2014 11:34:59 +0200 Subject: Write Protocol version to the db so we can perform sanity checks --- ethutil/value.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ethutil') diff --git a/ethutil/value.go b/ethutil/value.go index 608d332ba..b336345ca 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -141,6 +141,8 @@ func (val *Value) Bytes() []byte { return []byte(s) } else if s, ok := val.Val.(*big.Int); ok { return s.Bytes() + } else { + return big.NewInt(val.Int()).Bytes() } return []byte{} -- cgit v1.2.3 From cdbc3ecc2a738a4b8803c40dfc510fd099ce8584 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 24 Aug 2014 00:16:32 +0200 Subject: Serpent! :-) --- ethutil/script.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'ethutil') diff --git a/ethutil/script.go b/ethutil/script.go index b796e7c1e..bd087e7e0 100644 --- a/ethutil/script.go +++ b/ethutil/script.go @@ -2,9 +2,11 @@ package ethutil import ( "fmt" + "strings" + "github.com/obscuren/mutan" "github.com/obscuren/mutan/backends" - "strings" + "github.com/obscuren/serpent-go" ) // General compile function @@ -14,15 +16,13 @@ func Compile(script string, silent bool) (ret []byte, err error) { if len(line) > 1 && line[0:2] == "#!" { switch line { - /* - case "#!serpent": - byteCode, err := serpent.Compile(script) - if err != nil { - return nil, err - } + case "#!serpent": + byteCode, err := serpent.Compile(script) + if err != nil { + return nil, err + } - return byteCode, nil - */ + return byteCode, nil } } else { -- cgit v1.2.3 From 6afc16399f9624663579ad72950b4ea3b887db57 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 25 Aug 2014 12:53:06 +0200 Subject: Block size --- ethutil/size.go | 15 +++++++++++++++ ethutil/size_test.go | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 ethutil/size.go create mode 100644 ethutil/size_test.go (limited to 'ethutil') diff --git a/ethutil/size.go b/ethutil/size.go new file mode 100644 index 000000000..b4426465e --- /dev/null +++ b/ethutil/size.go @@ -0,0 +1,15 @@ +package ethutil + +import "fmt" + +type StorageSize float64 + +func (self StorageSize) String() string { + if self > 1000000 { + return fmt.Sprintf("%.2f mB", self/1000000) + } else if self > 1000 { + return fmt.Sprintf("%.2f kB", self/1000) + } else { + return fmt.Sprintf("%.2f B", self) + } +} diff --git a/ethutil/size_test.go b/ethutil/size_test.go new file mode 100644 index 000000000..82aa1c653 --- /dev/null +++ b/ethutil/size_test.go @@ -0,0 +1,12 @@ +package ethutil + +import ( + "fmt" + "testing" +) + +func TestSize(t *testing.T) { + fmt.Println(StorageSize(2381273)) + fmt.Println(StorageSize(2192)) + fmt.Println(StorageSize(12)) +} -- cgit v1.2.3 From ff27df78fc5d638b562bae9b4515eb5f5735d45d Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 7 Sep 2014 10:18:54 +0200 Subject: Added new list type which can embed any slice type --- ethutil/list.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ethutil/list.go (limited to 'ethutil') diff --git a/ethutil/list.go b/ethutil/list.go new file mode 100644 index 000000000..18bf04792 --- /dev/null +++ b/ethutil/list.go @@ -0,0 +1,42 @@ +package ethutil + +import "reflect" + +// The list type is an anonymous slice handler which can be used +// for containing any slice type to use in an environment which +// does not support slice types (e.g., JavaScript, QML) +type List struct { + list reflect.Value + Length int +} + +// Initialise a new list. Panics if non-slice type is given. +func NewList(t interface{}) *List { + list := reflect.ValueOf(t) + if list.Kind() != reflect.Slice { + panic("list container initialized with a non-slice type") + } + + return &List{list, list.Len()} +} + +// Get N element from the embedded slice. Returns nil if OOB. +func (self *List) Get(i int) interface{} { + if self.list.Len() > i { + return self.list.Index(i).Interface() + } + + return nil +} + +// Appends value at the end of the slice. Panics when incompatible value +// is given. +func (self *List) Append(v interface{}) { + self.list = reflect.Append(self.list, reflect.ValueOf(v)) + self.Length = self.list.Len() +} + +// Returns the underlying slice as interface. +func (self *List) Interface() interface{} { + return self.list.Interface() +} -- cgit v1.2.3 From 10564723b9ffa14904907a0ebc984fa25e4f1eab Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 13 Sep 2014 13:14:24 +0200 Subject: added string casting --- ethutil/value.go | 10 ++++++---- ethutil/value_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'ethutil') diff --git a/ethutil/value.go b/ethutil/value.go index b336345ca..e8148b990 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -1,9 +1,11 @@ package ethutil import ( + "bytes" "fmt" "math/big" "reflect" + "strconv" ) // Data values are returned by the rlp decoder. The data values represents @@ -93,6 +95,9 @@ func (val *Value) Int() int64 { return new(big.Int).SetBytes(Val).Int64() } else if Val, ok := val.Val.(*big.Int); ok { return Val.Int64() + } else if Val, ok := val.Val.(string); ok { + n, _ := strconv.Atoi(Val) + return int64(n) } return 0 @@ -246,10 +251,7 @@ func (val *Value) Cmp(o *Value) bool { } func (self *Value) DeepCmp(o *Value) bool { - a := NewValue(self.BigInt()) - b := NewValue(o.BigInt()) - - return a.Cmp(b) + return bytes.Compare(self.Bytes(), o.Bytes()) == 0 } func (val *Value) Encode() []byte { diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 710cbd887..5452a0790 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -2,6 +2,7 @@ package ethutil import ( "bytes" + "fmt" "math/big" "testing" ) @@ -78,3 +79,8 @@ func TestMath(t *testing.T) { t.Error("Expected 0, got", a) } } + +func TestString(t *testing.T) { + a := NewValue("10") + fmt.Println("VALUE WITH STRING:", a.Int()) +} -- cgit v1.2.3 From 2fb57b2ea7b7f697ddc4811c471d87116eae07cc Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 14 Sep 2014 00:13:23 +0200 Subject: Reworked filters --- ethutil/list.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethutil') diff --git a/ethutil/list.go b/ethutil/list.go index 18bf04792..0aa657a14 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -20,6 +20,10 @@ func NewList(t interface{}) *List { return &List{list, list.Len()} } +func EmptyList() *List { + return NewList([]interface{}{}) +} + // Get N element from the embedded slice. Returns nil if OOB. func (self *List) Get(i int) interface{} { if self.list.Len() > i { -- cgit v1.2.3 From 4db4ec1621767513ed2bd99d835c3e2f1c2b0e7e Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Sep 2014 01:10:50 +0200 Subject: Added unique set --- ethutil/set.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ethutil/set.go (limited to 'ethutil') diff --git a/ethutil/set.go b/ethutil/set.go new file mode 100644 index 000000000..80e2edde2 --- /dev/null +++ b/ethutil/set.go @@ -0,0 +1,32 @@ +package ethutil + +type Settable interface { + AsSet() UniqueSet +} + +type UniqueSet map[interface{}]struct{} + +func NewSet(v ...interface{}) UniqueSet { + set := make(UniqueSet) + for _, val := range v { + set.Insert(val) + } + + return set +} + +func (self UniqueSet) Insert(k interface{}) UniqueSet { + self[k] = struct{}{} + + return self +} + +func (self UniqueSet) Include(k interface{}) bool { + _, ok := self[k] + + return ok +} + +func Set(s Settable) UniqueSet { + return s.AsSet() +} -- cgit v1.2.3 From 33a0dec8a157b9687ca6038f4deb011f3f1f7bdc Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Sep 2014 15:42:12 +0200 Subject: Improved catching up and refactored --- ethutil/bytes.go | 16 ++++++++++++++++ ethutil/rlp.go | 2 ++ ethutil/set.go | 16 ++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) (limited to 'ethutil') diff --git a/ethutil/bytes.go b/ethutil/bytes.go index e38f89454..f151d16ee 100644 --- a/ethutil/bytes.go +++ b/ethutil/bytes.go @@ -9,6 +9,22 @@ import ( "strings" ) +type Bytes []byte + +func (self Bytes) String() string { + return string(self) +} + +func DeleteFromByteSlice(s [][]byte, hash []byte) [][]byte { + for i, h := range s { + if bytes.Compare(h, hash) == 0 { + return append(s[:i], s[i+1:]...) + } + } + + return s +} + // Number to bytes // // Returns the number in bytes with the specified base diff --git a/ethutil/rlp.go b/ethutil/rlp.go index 17ff627eb..febfb78e1 100644 --- a/ethutil/rlp.go +++ b/ethutil/rlp.go @@ -124,6 +124,8 @@ func Encode(object interface{}) []byte { } else { buff.Write(Encode(t.Bytes())) } + case Bytes: + buff.Write(Encode([]byte(t))) case []byte: if len(t) == 1 && t[0] <= 0x7f { buff.Write(t) diff --git a/ethutil/set.go b/ethutil/set.go index 80e2edde2..7955edac0 100644 --- a/ethutil/set.go +++ b/ethutil/set.go @@ -4,9 +4,13 @@ type Settable interface { AsSet() UniqueSet } -type UniqueSet map[interface{}]struct{} +type Stringable interface { + String() string +} + +type UniqueSet map[string]struct{} -func NewSet(v ...interface{}) UniqueSet { +func NewSet(v ...Stringable) UniqueSet { set := make(UniqueSet) for _, val := range v { set.Insert(val) @@ -15,14 +19,14 @@ func NewSet(v ...interface{}) UniqueSet { return set } -func (self UniqueSet) Insert(k interface{}) UniqueSet { - self[k] = struct{}{} +func (self UniqueSet) Insert(k Stringable) UniqueSet { + self[k.String()] = struct{}{} return self } -func (self UniqueSet) Include(k interface{}) bool { - _, ok := self[k] +func (self UniqueSet) Include(k Stringable) bool { + _, ok := self[k.String()] return ok } -- cgit v1.2.3 From fd041d91ee9f4be2d4705a32dc16698c89622c85 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 17 Sep 2014 15:57:07 +0200 Subject: Truncate when writing --- ethutil/path.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethutil') diff --git a/ethutil/path.go b/ethutil/path.go index 27022bcfa..cfbc38950 100644 --- a/ethutil/path.go +++ b/ethutil/path.go @@ -45,7 +45,7 @@ func ReadAllFile(filePath string) (string, error) { } func WriteFile(filePath string, content []byte) error { - fh, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, os.ModePerm) + fh, err := os.OpenFile(filePath, os.O_TRUNC|os.O_RDWR|os.O_CREATE, os.ModePerm) if err != nil { return err } -- cgit v1.2.3 From 80261c803a82e51413608a3dc5273c982844d135 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 19 Sep 2014 13:19:19 +0200 Subject: Fixed deref ptr --- ethutil/rlp.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethutil') diff --git a/ethutil/rlp.go b/ethutil/rlp.go index febfb78e1..55406133b 100644 --- a/ethutil/rlp.go +++ b/ethutil/rlp.go @@ -15,6 +15,10 @@ type RlpEncodeDecode interface { RlpValue() []interface{} } +func Rlp(encoder RlpEncode) []byte { + return encoder.RlpEncode() +} + type RlpEncoder struct { rlpData []byte } -- cgit v1.2.3 From 151f9c7f8214a2702a76f36f566b52266949ac89 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 22 Sep 2014 14:50:53 +0200 Subject: BigInt accept string --- ethutil/value.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'ethutil') diff --git a/ethutil/value.go b/ethutil/value.go index e8148b990..b1f887f29 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -118,6 +118,8 @@ func (val *Value) BigInt() *big.Int { return b } else if a, ok := val.Val.(*big.Int); ok { return a + } else if a, ok := val.Val.(string); ok { + return Big(a) } else { return big.NewInt(int64(val.Uint())) } -- cgit v1.2.3 From b65f29f8faa20a93bd83c18232326c935cb16981 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 22 Sep 2014 14:51:41 +0200 Subject: Added JavaScript JSON helper --- ethutil/list.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'ethutil') diff --git a/ethutil/list.go b/ethutil/list.go index 0aa657a14..9db96cf18 100644 --- a/ethutil/list.go +++ b/ethutil/list.go @@ -1,6 +1,9 @@ package ethutil -import "reflect" +import ( + "encoding/json" + "reflect" +) // The list type is an anonymous slice handler which can be used // for containing any slice type to use in an environment which @@ -44,3 +47,15 @@ func (self *List) Append(v interface{}) { func (self *List) Interface() interface{} { return self.list.Interface() } + +// For JavaScript <3 +func (self *List) ToJSON() string { + var list []interface{} + for i := 0; i < self.Length; i++ { + list = append(list, self.Get(i)) + } + + data, _ := json.Marshal(list) + + return string(data) +} -- cgit v1.2.3