aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-07-29 16:33:30 +0800
committerobscuren <geffobscura@gmail.com>2014-07-29 16:33:30 +0800
commit8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6 (patch)
treef5af7ab86fd4d52240ed3a2bf96272f54153b8d0 /ethutil
parent41bd38147c2e5968283facf641b2444c09f53d14 (diff)
downloaddexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar.gz
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar.bz2
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar.lz
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar.xz
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.tar.zst
dexon-8e7c4f91e33bd99d3a4d320cdc59cf0bab3831b6.zip
Added ops
Diffstat (limited to 'ethutil')
-rw-r--r--ethutil/value.go60
-rw-r--r--ethutil/value_test.go15
2 files changed, 73 insertions, 2 deletions
diff --git a/ethutil/value.go b/ethutil/value.go
index 735a71dbc..635683e66 100644
--- a/ethutil/value.go
+++ b/ethutil/value.go
@@ -1,7 +1,6 @@
package ethutil
import (
- "bytes"
"fmt"
"math/big"
"reflect"
@@ -67,7 +66,8 @@ func (val *Value) Uint() uint64 {
} else if Val, ok := val.Val.(uint); ok {
return uint64(Val)
} else if Val, ok := val.Val.([]byte); ok {
- return ReadVarint(bytes.NewReader(Val))
+ return new(big.Int).SetBytes(Val).Uint64()
+ //return ReadVarint(bytes.NewReader(Val))
} else if Val, ok := val.Val.(*big.Int); ok {
return Val.Uint64()
}
@@ -207,6 +207,13 @@ func (val *Value) Cmp(o *Value) bool {
return reflect.DeepEqual(val.Val, o.Val)
}
+func (self *Value) DeepCmp(o *Value) bool {
+ a := NewValue(self.BigInt())
+ b := NewValue(o.BigInt())
+
+ return a.Cmp(b)
+}
+
func (val *Value) Encode() []byte {
return Encode(val.Val)
}
@@ -262,6 +269,55 @@ func (val *Value) Append(v interface{}) *Value {
return val
}
+const (
+ valOpAdd = iota
+ valOpDiv
+ valOpMul
+ valOpPow
+ valOpSub
+)
+
+// Math stuff
+func (self *Value) doOp(op int, other interface{}) *Value {
+ left := self.BigInt()
+ right := NewValue(other).BigInt()
+
+ switch op {
+ case valOpAdd:
+ self.Val = left.Add(left, right)
+ case valOpDiv:
+ self.Val = left.Div(left, right)
+ case valOpMul:
+ self.Val = left.Mul(left, right)
+ case valOpPow:
+ self.Val = left.Exp(left, right, Big0)
+ case valOpSub:
+ self.Val = left.Sub(left, right)
+ }
+
+ return self
+}
+
+func (self *Value) Add(other interface{}) *Value {
+ return self.doOp(valOpAdd, other)
+}
+
+func (self *Value) Sub(other interface{}) *Value {
+ return self.doOp(valOpSub, other)
+}
+
+func (self *Value) Div(other interface{}) *Value {
+ return self.doOp(valOpDiv, other)
+}
+
+func (self *Value) Mul(other interface{}) *Value {
+ return self.doOp(valOpMul, other)
+}
+
+func (self *Value) Pow(other interface{}) *Value {
+ return self.doOp(valOpPow, other)
+}
+
type ValueIterator struct {
value *Value
currentValue *Value
diff --git a/ethutil/value_test.go b/ethutil/value_test.go
index a100f44bc..710cbd887 100644
--- a/ethutil/value_test.go
+++ b/ethutil/value_test.go
@@ -63,3 +63,18 @@ func TestIterator(t *testing.T) {
i++
}
}
+
+func TestMath(t *testing.T) {
+ a := NewValue(1)
+ a.Add(1).Add(1)
+
+ if !a.DeepCmp(NewValue(3)) {
+ t.Error("Expected 3, got", a)
+ }
+
+ a = NewValue(2)
+ a.Sub(1).Sub(1)
+ if !a.DeepCmp(NewValue(0)) {
+ t.Error("Expected 0, got", a)
+ }
+}