aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--accounts/abi/method.go2
-rw-r--r--accounts/abi/numbers.go42
-rw-r--r--accounts/abi/numbers_test.go26
-rw-r--r--accounts/abi/packing.go4
-rw-r--r--console/console.go47
-rw-r--r--console/console_test.go46
6 files changed, 101 insertions, 66 deletions
diff --git a/accounts/abi/method.go b/accounts/abi/method.go
index f3d1a44b5..d56f3bc3d 100644
--- a/accounts/abi/method.go
+++ b/accounts/abi/method.go
@@ -62,7 +62,7 @@ func (m Method) pack(method Method, args ...interface{}) ([]byte, error) {
// calculate the offset
offset := len(method.Inputs)*32 + len(variableInput)
// set the offset
- ret = append(ret, packNum(reflect.ValueOf(offset), UintTy)...)
+ ret = append(ret, packNum(reflect.ValueOf(offset))...)
// Append the packed output to the variable input. The variable input
// will be appended at the end of the input.
variableInput = append(variableInput, packed...)
diff --git a/accounts/abi/numbers.go b/accounts/abi/numbers.go
index 5a31cf2b5..06c4422f9 100644
--- a/accounts/abi/numbers.go
+++ b/accounts/abi/numbers.go
@@ -61,54 +61,20 @@ func U256(n *big.Int) []byte {
return common.LeftPadBytes(common.U256(n).Bytes(), 32)
}
-func S256(n *big.Int) []byte {
- sint := common.S256(n)
- ret := common.LeftPadBytes(sint.Bytes(), 32)
- if sint.Cmp(common.Big0) < 0 {
- for i, b := range ret {
- if b == 0 {
- ret[i] = 1
- continue
- }
- break
- }
- }
-
- return ret
-}
-
// S256 will ensure signed 256bit on big nums
func U2U256(n uint64) []byte {
return U256(big.NewInt(int64(n)))
}
-func S2S256(n int64) []byte {
- return S256(big.NewInt(n))
-}
-
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
-func packNum(value reflect.Value, to byte) []byte {
+func packNum(value reflect.Value) []byte {
switch kind := value.Kind(); kind {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- if to == UintTy {
- return U2U256(value.Uint())
- } else {
- return S2S256(int64(value.Uint()))
- }
+ return U2U256(value.Uint())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- if to == UintTy {
- return U2U256(uint64(value.Int()))
- } else {
- return S2S256(value.Int())
- }
+ return U2U256(uint64(value.Int()))
case reflect.Ptr:
- // This only takes care of packing and casting. No type checking is done here. It should be done prior to using this function.
- if to == UintTy {
- return U256(value.Interface().(*big.Int))
- } else {
- return S256(value.Interface().(*big.Int))
- }
-
+ return U256(value.Interface().(*big.Int))
}
return nil
diff --git a/accounts/abi/numbers_test.go b/accounts/abi/numbers_test.go
index d66a43258..f409aa60f 100644
--- a/accounts/abi/numbers_test.go
+++ b/accounts/abi/numbers_test.go
@@ -26,48 +26,28 @@ import (
func TestNumberTypes(t *testing.T) {
ubytes := make([]byte, 32)
ubytes[31] = 1
- sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
unsigned := U256(big.NewInt(1))
if !bytes.Equal(unsigned, ubytes) {
t.Errorf("expected %x got %x", ubytes, unsigned)
}
-
- signed := S256(big.NewInt(1))
- if !bytes.Equal(signed, ubytes) {
- t.Errorf("expected %x got %x", ubytes, unsigned)
- }
-
- signed = S256(big.NewInt(-1))
- if !bytes.Equal(signed, sbytesmin) {
- t.Errorf("expected %x got %x", ubytes, unsigned)
- }
}
func TestPackNumber(t *testing.T) {
ubytes := make([]byte, 32)
ubytes[31] = 1
- sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
maxunsigned := []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
- packed := packNum(reflect.ValueOf(1), IntTy)
- if !bytes.Equal(packed, ubytes) {
- t.Errorf("expected %x got %x", ubytes, packed)
- }
- packed = packNum(reflect.ValueOf(-1), IntTy)
- if !bytes.Equal(packed, sbytesmin) {
- t.Errorf("expected %x got %x", ubytes, packed)
- }
- packed = packNum(reflect.ValueOf(1), UintTy)
+ packed := packNum(reflect.ValueOf(1))
if !bytes.Equal(packed, ubytes) {
t.Errorf("expected %x got %x", ubytes, packed)
}
- packed = packNum(reflect.ValueOf(-1), UintTy)
+ packed = packNum(reflect.ValueOf(-1))
if !bytes.Equal(packed, maxunsigned) {
t.Errorf("expected %x got %x", maxunsigned, packed)
}
- packed = packNum(reflect.ValueOf("string"), UintTy)
+ packed = packNum(reflect.ValueOf("string"))
if packed != nil {
t.Errorf("expected 'string' to pack to nil. got %x instead", packed)
}
diff --git a/accounts/abi/packing.go b/accounts/abi/packing.go
index c765dfdf3..0c37edf17 100644
--- a/accounts/abi/packing.go
+++ b/accounts/abi/packing.go
@@ -25,7 +25,7 @@ import (
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
// bytes slice
func packBytesSlice(bytes []byte, l int) []byte {
- len := packNum(reflect.ValueOf(l), UintTy)
+ len := packNum(reflect.ValueOf(l))
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
}
@@ -34,7 +34,7 @@ func packBytesSlice(bytes []byte, l int) []byte {
func packElement(t Type, reflectValue reflect.Value) []byte {
switch t.T {
case IntTy, UintTy:
- return packNum(reflectValue, t.T)
+ return packNum(reflectValue)
case StringTy:
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len())
case AddressTy:
diff --git a/console/console.go b/console/console.go
index ab0c1ea58..00d1fea1d 100644
--- a/console/console.go
+++ b/console/console.go
@@ -331,11 +331,11 @@ func (c *Console) Interactive() {
// Append the line to the input and check for multi-line interpretation
input += line + "\n"
- indents = strings.Count(input, "{") + strings.Count(input, "(") - strings.Count(input, "}") - strings.Count(input, ")")
+ indents = countIndents(input)
if indents <= 0 {
prompt = c.prompt
} else {
- prompt = strings.Repeat("..", indents*2) + " "
+ prompt = strings.Repeat(".", indents*3) + " "
}
// If all the needed lines are present, save the command and run
if indents <= 0 {
@@ -354,6 +354,49 @@ func (c *Console) Interactive() {
}
}
+// countIndents returns the number of identations for the given input.
+// In case of invalid input such as var a = } the result can be negative.
+func countIndents(input string) int {
+ var (
+ indents = 0
+ inString = false
+ strOpenChar = ' ' // keep track of the string open char to allow var str = "I'm ....";
+ charEscaped = false // keep track if the previous char was the '\' char, allow var str = "abc\"def";
+ )
+
+ for _, c := range input {
+ switch c {
+ case '\\':
+ // indicate next char as escaped when in string and previous char isn't escaping this backslash
+ if !charEscaped && inString {
+ charEscaped = true
+ }
+ case '\'', '"':
+ if inString && !charEscaped && strOpenChar == c { // end string
+ inString = false
+ } else if !inString && !charEscaped { // begin string
+ inString = true
+ strOpenChar = c
+ }
+ charEscaped = false
+ case '{', '(':
+ if !inString { // ignore brackets when in string, allow var str = "a{"; without indenting
+ indents++
+ }
+ charEscaped = false
+ case '}', ')':
+ if !inString {
+ indents--
+ }
+ charEscaped = false
+ default:
+ charEscaped = false
+ }
+ }
+
+ return indents
+}
+
// Execute runs the JavaScript file specified as the argument.
func (c *Console) Execute(path string) error {
return c.jsre.Exec(path)
diff --git a/console/console_test.go b/console/console_test.go
index 911087824..7738d0c44 100644
--- a/console/console_test.go
+++ b/console/console_test.go
@@ -294,3 +294,49 @@ func TestPrettyError(t *testing.T) {
t.Fatalf("pretty error mismatch: have %s, want %s", output, want)
}
}
+
+// Tests that tests if the number of indents for JS input is calculated correct.
+func TestIndenting(t *testing.T) {
+ testCases := []struct {
+ input string
+ expectedIndentCount int
+ }{
+ {`var a = 1;`, 0},
+ {`"some string"`, 0},
+ {`"some string with (parentesis`, 0},
+ {`"some string with newline
+ ("`, 0},
+ {`function v(a,b) {}`, 0},
+ {`function f(a,b) { var str = "asd("; };`, 0},
+ {`function f(a) {`, 1},
+ {`function f(a, function(b) {`, 2},
+ {`function f(a, function(b) {
+ var str = "a)}";
+ });`, 0},
+ {`function f(a,b) {
+ var str = "a{b(" + a, ", " + b;
+ }`, 0},
+ {`var str = "\"{"`, 0},
+ {`var str = "'("`, 0},
+ {`var str = "\\{"`, 0},
+ {`var str = "\\\\{"`, 0},
+ {`var str = 'a"{`, 0},
+ {`var obj = {`, 1},
+ {`var obj = { {a:1`, 2},
+ {`var obj = { {a:1}`, 1},
+ {`var obj = { {a:1}, b:2}`, 0},
+ {`var obj = {}`, 0},
+ {`var obj = {
+ a: 1, b: 2
+ }`, 0},
+ {`var test = }`, -1},
+ {`var str = "a\""; var obj = {`, 1},
+ }
+
+ for i, tt := range testCases {
+ counted := countIndents(tt.input)
+ if counted != tt.expectedIndentCount {
+ t.Errorf("test %d: invalid indenting: have %d, want %d", i, counted, tt.expectedIndentCount)
+ }
+ }
+}