diff options
-rw-r--r-- | accounts/abi/abi.go | 90 | ||||
-rw-r--r-- | accounts/abi/abi_test.go | 567 | ||||
-rw-r--r-- | accounts/abi/bind/backend.go | 10 | ||||
-rw-r--r-- | accounts/abi/bind/backends/remote.go | 15 | ||||
-rw-r--r-- | accounts/abi/bind/backends/simulated.go | 11 | ||||
-rw-r--r-- | accounts/abi/bind/bind_test.go | 28 | ||||
-rw-r--r-- | accounts/abi/error.go | 79 | ||||
-rw-r--r-- | accounts/abi/method.go | 39 | ||||
-rw-r--r-- | accounts/abi/numbers.go | 4 | ||||
-rw-r--r-- | accounts/abi/packing.go | 65 | ||||
-rw-r--r-- | accounts/abi/reflect.go | 64 | ||||
-rw-r--r-- | accounts/abi/type.go | 187 | ||||
-rw-r--r-- | accounts/watch.go | 2 | ||||
-rw-r--r-- | accounts/watch_fallback.go | 2 | ||||
-rw-r--r-- | cmd/geth/js.go | 16 | ||||
-rw-r--r-- | cmd/geth/main.go | 23 | ||||
-rw-r--r-- | cmd/geth/usage.go | 7 | ||||
-rw-r--r-- | cmd/utils/flags.go | 13 | ||||
-rw-r--r-- | common/types.go | 2 | ||||
-rw-r--r-- | common/types_test.go | 38 | ||||
-rw-r--r-- | core/blockchain.go | 3 | ||||
-rw-r--r-- | core/genesis.go | 2 | ||||
-rw-r--r-- | eth/api.go | 15 | ||||
-rw-r--r-- | eth/filters/filter_system.go | 2 | ||||
-rw-r--r-- | jsre/jsre.go | 9 |
25 files changed, 902 insertions, 391 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 9ef7c0f0d..32df6f19d 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -48,42 +48,6 @@ func JSON(reader io.Reader) (ABI, error) { return abi, nil } -// tests, tests whether the given input would result in a successful -// call. Checks argument list count and matches input to `input`. -func (abi ABI) pack(method Method, args ...interface{}) ([]byte, error) { - // variable input is the output appended at the end of packed - // output. This is used for strings and bytes types input. - var variableInput []byte - - var ret []byte - for i, a := range args { - input := method.Inputs[i] - // pack the input - packed, err := input.Type.pack(a) - if err != nil { - return nil, fmt.Errorf("`%s` %v", method.Name, err) - } - - // check for a slice type (string, bytes, slice) - if input.Type.T == StringTy || input.Type.T == BytesTy || input.Type.IsSlice { - // calculate the offset - offset := len(method.Inputs)*32 + len(variableInput) - // set the offset - ret = append(ret, packNum(reflect.ValueOf(offset), UintTy)...) - // Append the packed output to the variable input. The variable input - // will be appended at the end of the input. - variableInput = append(variableInput, packed...) - } else { - // append the packed value to the input - ret = append(ret, packed...) - } - } - // append the variable input at the end of the packed input - ret = append(ret, variableInput...) - - return ret, nil -} - // Pack the given method name to conform the ABI. Method call's data // will consist of method_id, args0, arg1, ... argN. Method id consists // of 4 bytes and arguments are all 32 bytes. @@ -102,11 +66,7 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { } method = m } - // Make sure arguments match up and pack them - if len(args) != len(method.Inputs) { - return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs)) - } - arguments, err := abi.pack(method, args...) + arguments, err := method.pack(method, args...) if err != nil { return nil, err } @@ -126,18 +86,21 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { if index+32 > len(output) { return nil, fmt.Errorf("abi: cannot marshal in to go slice: insufficient size output %d require %d", len(output), index+32) } + elem := t.Type.Elem // first we need to create a slice of the type var refSlice reflect.Value - switch t.Type.T { + switch elem.T { case IntTy, UintTy, BoolTy: // int, uint, bool can all be of type big int. refSlice = reflect.ValueOf([]*big.Int(nil)) case AddressTy: // address must be of slice Address refSlice = reflect.ValueOf([]common.Address(nil)) case HashTy: // hash must be of slice hash refSlice = reflect.ValueOf([]common.Hash(nil)) + case FixedBytesTy: + refSlice = reflect.ValueOf([]byte(nil)) default: // no other types are supported - return nil, fmt.Errorf("abi: unsupported slice type %v", t.Type.T) + return nil, fmt.Errorf("abi: unsupported slice type %v", elem.T) } // get the offset which determines the start of this array ... offset := int(common.BytesToBig(output[index : index+32]).Uint64()) @@ -164,7 +127,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { ) // set inter to the correct type (cast) - switch t.Type.T { + switch elem.T { case IntTy, UintTy: inter = common.BytesToBig(returnOutput) case BoolTy: @@ -186,7 +149,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { // argument in T. func toGoType(i int, t Argument, output []byte) (interface{}, error) { // we need to treat slices differently - if t.Type.IsSlice { + if (t.Type.IsSlice || t.Type.IsArray) && t.Type.T != BytesTy && t.Type.T != StringTy && t.Type.T != FixedBytesTy { return toGoSlice(i, t, output) } @@ -217,12 +180,33 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) { returnOutput = output[index : index+32] } - // cast bytes to abi return type + // convert the bytes to whatever is specified by the ABI. switch t.Type.T { - case IntTy: - return common.BytesToBig(returnOutput), nil - case UintTy: - return common.BytesToBig(returnOutput), nil + case IntTy, UintTy: + bigNum := common.BytesToBig(returnOutput) + + // If the type is a integer convert to the integer type + // specified by the ABI. + switch t.Type.Kind { + case reflect.Uint8: + return uint8(bigNum.Uint64()), nil + case reflect.Uint16: + return uint16(bigNum.Uint64()), nil + case reflect.Uint32: + return uint32(bigNum.Uint64()), nil + case reflect.Uint64: + return uint64(bigNum.Uint64()), nil + case reflect.Int8: + return int8(bigNum.Int64()), nil + case reflect.Int16: + return int16(bigNum.Int64()), nil + case reflect.Int32: + return int32(bigNum.Int64()), nil + case reflect.Int64: + return int64(bigNum.Int64()), nil + case reflect.Ptr: + return bigNum, nil + } case BoolTy: return common.BytesToBig(returnOutput).Uint64() > 0, nil case AddressTy: @@ -328,10 +312,12 @@ func set(dst, src reflect.Value, output Argument) error { return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem()) } - if dst.Len() < output.Type.Size { - return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.Size, dst.Len()) + if dst.Len() < output.Type.SliceSize { + return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len()) } reflect.Copy(dst, src) + case dstType.Kind() == reflect.Interface: + dst.Set(src) default: return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type()) } diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index a1b3e62d9..05535b3b5 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -29,66 +29,391 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) -const jsondata = ` -[ - { "type" : "function", "name" : "balance", "const" : true }, - { "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] } -]` +// formatSilceOutput add padding to the value and adds a size +func formatSliceOutput(v ...[]byte) []byte { + off := common.LeftPadBytes(big.NewInt(int64(len(v))).Bytes(), 32) + output := append(off, make([]byte, 0, len(v)*32)...) -const jsondata2 = ` -[ - { "type" : "function", "name" : "balance", "const" : true }, - { "type" : "function", "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, - { "type" : "function", "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] }, - { "type" : "function", "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, - { "type" : "function", "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, - { "type" : "function", "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] }, - { "type" : "function", "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] }, - { "type" : "function", "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] }, - { "type" : "function", "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] }, - { "type" : "function", "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] }, - { "type" : "function", "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, - { "type" : "function", "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] }, - { "type" : "function", "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }, - { "type" : "function", "name" : "sliceAddress", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] }, - { "type" : "function", "name" : "sliceMultiAddress", "const" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] } -]` + for _, value := range v { + output = append(output, common.LeftPadBytes(value, 32)...) + } + return output +} -func TestType(t *testing.T) { - typ, err := NewType("uint32") - if err != nil { - t.Error(err) +// quick helper padding +func pad(input []byte, size int, left bool) []byte { + if left { + return common.LeftPadBytes(input, size) } - if typ.Kind != reflect.Uint { - t.Error("expected uint32 to have kind Ptr") + return common.RightPadBytes(input, size) +} + +func TestTypeCheck(t *testing.T) { + for i, test := range []struct { + typ string + input interface{} + err string + }{ + {"uint", big.NewInt(1), ""}, + {"int", big.NewInt(1), ""}, + {"uint30", big.NewInt(1), ""}, + {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, + {"uint16", uint16(1), ""}, + {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, + {"uint16[]", []uint16{1, 2, 3}, ""}, + {"uint16[]", [3]uint16{1, 2, 3}, ""}, + {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"}, + {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, + {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, + {"uint16[3]", []uint16{1, 2, 3}, ""}, + {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, + {"address[]", []common.Address{common.Address{1}}, ""}, + {"address[1]", []common.Address{common.Address{1}}, ""}, + {"address[1]", [1]common.Address{common.Address{1}}, ""}, + {"address[2]", [1]common.Address{common.Address{1}}, "abi: cannot use [1]array as type [2]array as argument"}, + {"bytes32", [32]byte{}, ""}, + {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, + {"bytes32", common.Hash{1}, ""}, + {"bytes31", [31]byte{}, ""}, + {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, + {"bytes", []byte{0, 1}, ""}, + {"bytes", [2]byte{0, 1}, ""}, + {"bytes", common.Hash{1}, ""}, + {"string", "hello world", ""}, + {"bytes32[]", [][32]byte{[32]byte{}}, ""}, + } { + typ, err := NewType(test.typ) + if err != nil { + t.Fatal("unexpected parse error:", err) + } + + err = typeCheck(typ, reflect.ValueOf(test.input)) + if err != nil && len(test.err) == 0 { + t.Errorf("%d failed. Expected no err but got: %v", i, err) + continue + } + if err == nil && len(test.err) != 0 { + t.Errorf("%d failed. Expected err: %v but got none", i, test.err) + continue + } + + if err != nil && len(test.err) != 0 && err.Error() != test.err { + t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) + } } +} - typ, err = NewType("uint32[]") +func TestSimpleMethodUnpack(t *testing.T) { + for i, test := range []struct { + def string // definition of the **output** ABI params + marshalledOutput []byte // evm return data + expectedOut interface{} // the expected output + outVar string // the output variable (e.g. uint32, *big.Int, etc) + err string // empty or error if expected + }{ + { + `[ { "type": "uint32" } ]`, + pad([]byte{1}, 32, true), + uint32(1), + "uint32", + "", + }, + { + `[ { "type": "uint32" } ]`, + pad([]byte{1}, 32, true), + nil, + "uint16", + "abi: cannot unmarshal uint32 in to uint16", + }, + { + `[ { "type": "uint17" } ]`, + pad([]byte{1}, 32, true), + nil, + "uint16", + "abi: cannot unmarshal *big.Int in to uint16", + }, + { + `[ { "type": "uint17" } ]`, + pad([]byte{1}, 32, true), + big.NewInt(1), + "*big.Int", + "", + }, + + { + `[ { "type": "int32" } ]`, + pad([]byte{1}, 32, true), + int32(1), + "int32", + "", + }, + { + `[ { "type": "int32" } ]`, + pad([]byte{1}, 32, true), + nil, + "int16", + "abi: cannot unmarshal int32 in to int16", + }, + { + `[ { "type": "int17" } ]`, + pad([]byte{1}, 32, true), + nil, + "int16", + "abi: cannot unmarshal *big.Int in to int16", + }, + { + `[ { "type": "int17" } ]`, + pad([]byte{1}, 32, true), + big.NewInt(1), + "*big.Int", + "", + }, + + { + `[ { "type": "address" } ]`, + pad(pad([]byte{1}, 20, false), 32, true), + common.Address{1}, + "address", + "", + }, + { + `[ { "type": "bytes32" } ]`, + pad([]byte{1}, 32, false), + pad([]byte{1}, 32, false), + "bytes", + "", + }, + { + `[ { "type": "bytes32" } ]`, + pad([]byte{1}, 32, false), + pad([]byte{1}, 32, false), + "hash", + "", + }, + { + `[ { "type": "bytes32" } ]`, + pad([]byte{1}, 32, false), + pad([]byte{1}, 32, false), + "interface", + "", + }, + } { + abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) + abi, err := JSON(strings.NewReader(abiDefinition)) + if err != nil { + t.Errorf("%d failed. %v", i, err) + continue + } + + var outvar interface{} + switch test.outVar { + case "uint8": + var v uint8 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "uint16": + var v uint16 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "uint32": + var v uint32 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "uint64": + var v uint64 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "int8": + var v int8 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "int16": + var v int16 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "int32": + var v int32 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "int64": + var v int64 + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "*big.Int": + var v *big.Int + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "address": + var v common.Address + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "bytes": + var v []byte + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "hash": + var v common.Hash + err = abi.Unpack(&v, "method", test.marshalledOutput) + outvar = v + case "interface": + err = abi.Unpack(&outvar, "method", test.marshalledOutput) + default: + t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar) + continue + } + + if err != nil && len(test.err) == 0 { + t.Errorf("%d failed. Expected no err but got: %v", i, err) + continue + } + if err == nil && len(test.err) != 0 { + t.Errorf("%d failed. Expected err: %v but got none", i, test.err) + continue + } + if err != nil && len(test.err) != 0 && err.Error() != test.err { + t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) + continue + } + + if err == nil { + // bit of an ugly hack for hash type but I don't feel like finding a proper solution + if test.outVar == "hash" { + tmp := outvar.(common.Hash) // without assignment it's unaddressable + outvar = tmp[:] + } + + if !reflect.DeepEqual(test.expectedOut, outvar) { + t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar) + } + } + } +} + +func TestPack(t *testing.T) { + for i, test := range []struct { + typ string + + input interface{} + output []byte + }{ + {"uint16", uint16(2), pad([]byte{2}, 32, true)}, + {"uint16[]", []uint16{1, 2}, formatSliceOutput([]byte{1}, []byte{2})}, + {"bytes20", [20]byte{1}, pad([]byte{1}, 32, false)}, + {"uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, formatSliceOutput([]byte{1}, []byte{2})}, + {"address[]", []common.Address{common.Address{1}, common.Address{2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))}, + {"bytes32[]", []common.Hash{common.Hash{1}, common.Hash{2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))}, + } { + typ, err := NewType(test.typ) + if err != nil { + t.Fatal("unexpected parse error:", err) + } + + output, err := typ.pack(reflect.ValueOf(test.input)) + if err != nil { + t.Fatal("unexpected pack error:", err) + } + + if !bytes.Equal(output, test.output) { + t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output) + } + } +} + +func TestMethodPack(t *testing.T) { + abi, err := JSON(strings.NewReader(jsondata2)) + if err != nil { + t.Fatal(err) + } + + sig := abi.Methods["slice"].Id() + sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + + packed, err := abi.Pack("slice", []uint32{1, 2}) if err != nil { t.Error(err) } - if !typ.IsSlice { - t.Error("expected uint32[] to be slice") + + if !bytes.Equal(packed, sig) { + t.Errorf("expected %x got %x", sig, packed) } - if typ.Type != ubig_t { - t.Error("expcted uith32[] to have type uint64") + + var addrA, addrB = common.Address{1}, common.Address{2} + sig = abi.Methods["sliceAddress"].Id() + sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) + sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) + + packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(packed, sig) { + t.Errorf("expected %x got %x", sig, packed) } - typ, err = NewType("uint32[2]") + var addrC, addrD = common.Address{3}, common.Address{4} + sig = abi.Methods["sliceMultiAddress"].Id() + sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) + sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) + sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) + + packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) if err != nil { - t.Error(err) + t.Fatal(err) } - if !typ.IsSlice { - t.Error("expected uint32[2] to be slice") + if !bytes.Equal(packed, sig) { + t.Errorf("expected %x got %x", sig, packed) } - if typ.Type != ubig_t { - t.Error("expcted uith32[2] to have type uint64") + + sig = abi.Methods["slice256"].Id() + sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) + sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) + + packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) + if err != nil { + t.Error(err) } - if typ.SliceSize != 2 { - t.Error("expected uint32[2] to have a size of 2") + + if !bytes.Equal(packed, sig) { + t.Errorf("expected %x got %x", sig, packed) } } +const jsondata = ` +[ + { "type" : "function", "name" : "balance", "constant" : true }, + { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] } +]` + +const jsondata2 = ` +[ + { "type" : "function", "name" : "balance", "constant" : true }, + { "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }, + { "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] }, + { "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] }, + { "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] }, + { "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] }, + { "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] }, + { "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] }, + { "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] }, + { "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] }, + { "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] }, + { "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }, + { "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] }, + { "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] } +]` + func TestReader(t *testing.T) { Uint256, _ := NewType("uint256") exp := ABI{ @@ -164,21 +489,6 @@ func TestTestString(t *testing.T) { if _, err := abi.Pack("string", "hello world"); err != nil { t.Error(err) } - - str10 := string(make([]byte, 10)) - if _, err := abi.Pack("string32", str10); err != nil { - t.Error(err) - } - - str32 := string(make([]byte, 32)) - if _, err := abi.Pack("string32", str32); err != nil { - t.Error(err) - } - - str33 := string(make([]byte, 33)) - if _, err := abi.Pack("string32", str33); err == nil { - t.Error("expected str33 to throw out of bound error") - } } func TestTestBool(t *testing.T) { @@ -210,26 +520,10 @@ func TestTestSlice(t *testing.T) { } } -func TestImplicitTypeCasts(t *testing.T) { - abi, err := JSON(strings.NewReader(jsondata2)) - if err != nil { - t.Error(err) - t.FailNow() - } - - slice := make([]uint8, 2) - _, err = abi.Pack("uint64[2]", slice) - expStr := "`uint64[2]` abi: cannot use type uint8 as type uint64" - if err.Error() != expStr { - t.Errorf("expected %v, got %v", expStr, err) - } -} - func TestMethodSignature(t *testing.T) { String, _ := NewType("string") - String32, _ := NewType("string32") - m := Method{"foo", false, []Argument{Argument{"bar", String32, false}, Argument{"baz", String, false}}, nil} - exp := "foo(string32,string)" + m := Method{"foo", false, []Argument{Argument{"bar", String, false}, Argument{"baz", String, false}}, nil} + exp := "foo(string,string)" if m.Sig() != exp { t.Error("signature mismatch", exp, "!=", m.Sig()) } @@ -247,28 +541,6 @@ func TestMethodSignature(t *testing.T) { } } -func TestPack(t *testing.T) { - abi, err := JSON(strings.NewReader(jsondata2)) - if err != nil { - t.Error(err) - t.FailNow() - } - - sig := crypto.Keccak256([]byte("foo(uint32)"))[:4] - sig = append(sig, make([]byte, 32)...) - sig[35] = 10 - - packed, err := abi.Pack("foo", uint32(10)) - if err != nil { - t.Error(err) - t.FailNow() - } - - if !bytes.Equal(packed, sig) { - t.Errorf("expected %x got %x", sig, packed) - } -} - func TestMultiPack(t *testing.T) { abi, err := JSON(strings.NewReader(jsondata2)) if err != nil { @@ -292,77 +564,6 @@ func TestMultiPack(t *testing.T) { } } -func TestPackSlice(t *testing.T) { - abi, err := JSON(strings.NewReader(jsondata2)) - if err != nil { - t.Error(err) - t.FailNow() - } - - sig := crypto.Keccak256([]byte("slice(uint32[2])"))[:4] - sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - - packed, err := abi.Pack("slice", []uint32{1, 2}) - if err != nil { - t.Error(err) - } - - if !bytes.Equal(packed, sig) { - t.Errorf("expected %x got %x", sig, packed) - } - - var addrA, addrB = common.Address{1}, common.Address{2} - sig = abi.Methods["sliceAddress"].Id() - sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) - sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) - - packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) - if err != nil { - t.Fatal(err) - } - if !bytes.Equal(packed, sig) { - t.Errorf("expected %x got %x", sig, packed) - } - - var addrC, addrD = common.Address{3}, common.Address{4} - sig = abi.Methods["sliceMultiAddress"].Id() - sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) - sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) - sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) - - packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) - if err != nil { - t.Fatal(err) - } - if !bytes.Equal(packed, sig) { - t.Errorf("expected %x got %x", sig, packed) - } - - sig = crypto.Keccak256([]byte("slice256(uint256[2])"))[:4] - sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) - sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) - - packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) - if err != nil { - t.Error(err) - } - - if !bytes.Equal(packed, sig) { - t.Errorf("expected %x got %x", sig, packed) - } -} func ExampleJSON() { const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]` @@ -382,9 +583,9 @@ func ExampleJSON() { func TestInputVariableInputLength(t *testing.T) { const definition = `[ - { "type" : "function", "name" : "strOne", "const" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] }, - { "type" : "function", "name" : "bytesOne", "const" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] }, - { "type" : "function", "name" : "strTwo", "const" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] } + { "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] }, + { "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] }, + { "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] } ]` abi, err := JSON(strings.NewReader(definition)) @@ -546,7 +747,7 @@ func TestBareEvents(t *testing.T) { func TestMultiReturnWithStruct(t *testing.T) { const definition = `[ - { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` + { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` abi, err := JSON(strings.NewReader(definition)) if err != nil { @@ -599,7 +800,7 @@ func TestMultiReturnWithStruct(t *testing.T) { func TestMultiReturnWithSlice(t *testing.T) { const definition = `[ - { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` + { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` abi, err := JSON(strings.NewReader(definition)) if err != nil { @@ -635,8 +836,8 @@ func TestMultiReturnWithSlice(t *testing.T) { func TestMarshalArrays(t *testing.T) { const definition = `[ - { "name" : "bytes32", "const" : false, "outputs": [ { "type": "bytes32" } ] }, - { "name" : "bytes10", "const" : false, "outputs": [ { "type": "bytes10" } ] } + { "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, + { "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] } ]` abi, err := JSON(strings.NewReader(definition)) @@ -694,14 +895,14 @@ func TestMarshalArrays(t *testing.T) { func TestUnmarshal(t *testing.T) { const definition = `[ - { "name" : "int", "const" : false, "outputs": [ { "type": "uint256" } ] }, - { "name" : "bool", "const" : false, "outputs": [ { "type": "bool" } ] }, - { "name" : "bytes", "const" : false, "outputs": [ { "type": "bytes" } ] }, - { "name" : "fixed", "const" : false, "outputs": [ { "type": "bytes32" } ] }, - { "name" : "multi", "const" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, - { "name" : "addressSliceSingle", "const" : false, "outputs": [ { "type": "address[]" } ] }, - { "name" : "addressSliceDouble", "const" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, - { "name" : "mixedBytes", "const" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` + { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, + { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, + { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, + { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, + { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, + { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, + { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, + { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` abi, err := JSON(strings.NewReader(definition)) if err != nil { diff --git a/accounts/abi/bind/backend.go b/accounts/abi/bind/backend.go index 328f9f3b7..7442557cc 100644 --- a/accounts/abi/bind/backend.go +++ b/accounts/abi/bind/backend.go @@ -17,12 +17,22 @@ package bind import ( + "errors" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" ) +// ErrNoCode is returned by call and transact operations for which the requested +// recipient contract to operate on does not exist in the state db or does not +// have any code associated with it (i.e. suicided). +// +// Please note, this error string is part of the RPC API and is expected by the +// native contract bindings to signal this particular error. Do not change this +// as it will break all dependent code! +var ErrNoCode = errors.New("no contract code at given address") + // ContractCaller defines the methods needed to allow operating with contract on a read // only basis. type ContractCaller interface { diff --git a/accounts/abi/bind/backends/remote.go b/accounts/abi/bind/backends/remote.go index 8e990f076..9b3647192 100644 --- a/accounts/abi/bind/backends/remote.go +++ b/accounts/abi/bind/backends/remote.go @@ -66,10 +66,16 @@ type request struct { type response struct { JSONRPC string `json:"jsonrpc"` // Version of the JSON RPC protocol, always set to 2.0 ID int `json:"id"` // Auto incrementing ID number for this request - Error json.RawMessage `json:"error"` // Any error returned by the remote side + Error *failure `json:"error"` // Any error returned by the remote side Result json.RawMessage `json:"result"` // Whatever the remote side sends us in reply } +// failure is a JSON RPC response error field sent back from the API server. +type failure struct { + Code int `json:"code"` // JSON RPC error code associated with the failure + Message string `json:"message"` // Specific error message of the failure +} + // request forwards an API request to the RPC server, and parses the response. // // This is currently painfully non-concurrent, but it will have to do until we @@ -96,8 +102,11 @@ func (b *rpcBackend) request(method string, params []interface{}) (json.RawMessa if err := b.client.Recv(res); err != nil { return nil, err } - if len(res.Error) > 0 { - return nil, fmt.Errorf("remote error: %s", string(res.Error)) + if res.Error != nil { + if res.Error.Message == bind.ErrNoCode.Error() { + return nil, bind.ErrNoCode + } + return nil, fmt.Errorf("remote error: %s", res.Error.Message) } return res.Result, nil } diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 6cdb9a0cc..4866c4f58 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -92,6 +92,10 @@ func (b *SimulatedBackend) ContractCall(contract common.Address, data []byte, pe block = b.blockchain.CurrentBlock() statedb, _ = b.blockchain.State() } + // If there's no code to interact with, respond with an appropriate error + if code := statedb.GetCode(contract); len(code) == 0 { + return nil, bind.ErrNoCode + } // Set infinite balance to the a fake caller account from := statedb.GetOrNewStateObject(common.Address{}) from.SetBalance(common.MaxBig) @@ -134,7 +138,12 @@ func (b *SimulatedBackend) EstimateGasLimit(sender common.Address, contract *com block = b.pendingBlock statedb = b.pendingState.Copy() ) - + // If there's no code to interact with, respond with an appropriate error + if contract != nil { + if code := statedb.GetCode(*contract); len(code) == 0 { + return nil, bind.ErrNoCode + } + } // Set infinite balance to the a fake caller account from := statedb.GetOrNewStateObject(sender) from.SetBalance(common.MaxBig) diff --git a/accounts/abi/bind/bind_test.go b/accounts/abi/bind/bind_test.go index 5c36bc48f..f9cc8aba4 100644 --- a/accounts/abi/bind/bind_test.go +++ b/accounts/abi/bind/bind_test.go @@ -303,6 +303,34 @@ var bindTests = []struct { } `, }, + // Tests that non-existent contracts are reported as such (though only simulator test) + { + `NonExistent`, + ` + contract NonExistent { + function String() constant returns(string) { + return "I don't exist"; + } + } + `, + `6060604052609f8060106000396000f3606060405260e060020a6000350463f97a60058114601a575b005b600060605260c0604052600d60809081527f4920646f6e27742065786973740000000000000000000000000000000000000060a052602060c0908152600d60e081905281906101009060a09080838184600060046012f15050815172ffffffffffffffffffffffffffffffffffffff1916909152505060405161012081900392509050f3`, + `[{"constant":true,"inputs":[],"name":"String","outputs":[{"name":"","type":"string"}],"type":"function"}]`, + ` + // Create a simulator and wrap a non-deployed contract + sim := backends.NewSimulatedBackend() + + nonexistent, err := NewNonExistent(common.Address{}, sim) + if err != nil { + t.Fatalf("Failed to access non-existent contract: %v", err) + } + // Ensure that contract calls fail with the appropriate error + if res, err := nonexistent.String(nil); err == nil { + t.Fatalf("Call succeeded on non-existent contract: %v", res) + } else if (err != bind.ErrNoCode) { + t.Fatalf("Error mismatch: have %v, want %v", err, bind.ErrNoCode) + } + `, + }, } // Tests that packages generated by the binder can be successfully compiled and diff --git a/accounts/abi/error.go b/accounts/abi/error.go new file mode 100644 index 000000000..67739c21d --- /dev/null +++ b/accounts/abi/error.go @@ -0,0 +1,79 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package abi + +import ( + "fmt" + "reflect" +) + +// formatSliceString formats the reflection kind with the given slice size +// and returns a formatted string representation. +func formatSliceString(kind reflect.Kind, sliceSize int) string { + if sliceSize == -1 { + return fmt.Sprintf("[]%v", kind) + } + return fmt.Sprintf("[%d]%v", sliceSize, kind) +} + +// sliceTypeCheck checks that the given slice can by assigned to the reflection +// type in t. +func sliceTypeCheck(t Type, val reflect.Value) error { + if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { + return typeErr(formatSliceString(t.Kind, t.SliceSize), val.Type()) + } + if t.IsArray && val.Len() != t.SliceSize { + return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), formatSliceString(val.Type().Elem().Kind(), val.Len())) + } + + if t.Elem.IsSlice { + if val.Len() > 0 { + return sliceTypeCheck(*t.Elem, val.Index(0)) + } + } else if t.Elem.IsArray { + return sliceTypeCheck(*t.Elem, val.Index(0)) + } + + if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind { + return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), val.Type()) + } + return nil +} + +// typeCheck checks that the given reflection value can be assigned to the reflection +// type in t. +func typeCheck(t Type, value reflect.Value) error { + if t.IsSlice || t.IsArray { + return sliceTypeCheck(t, value) + } + + // Check base type validity. Element types will be checked later on. + if t.Kind != value.Kind() { + return typeErr(t.Kind, value.Kind()) + } + return nil +} + +// varErr returns a formatted error. +func varErr(expected, got reflect.Kind) error { + return typeErr(expected, got) +} + +// typeErr returns a formatted type casting error. +func typeErr(expected, got interface{}) error { + return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected) +} diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 206c7d408..f3d1a44b5 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -18,6 +18,7 @@ package abi import ( "fmt" + "reflect" "strings" "github.com/ethereum/go-ethereum/crypto" @@ -38,6 +39,44 @@ type Method struct { Outputs []Argument } +func (m Method) pack(method Method, args ...interface{}) ([]byte, error) { + // Make sure arguments match up and pack them + if len(args) != len(method.Inputs) { + return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs)) + } + // variable input is the output appended at the end of packed + // output. This is used for strings and bytes types input. + var variableInput []byte + + var ret []byte + for i, a := range args { + input := method.Inputs[i] + // pack the input + packed, err := input.Type.pack(reflect.ValueOf(a)) + if err != nil { + return nil, fmt.Errorf("`%s` %v", method.Name, err) + } + + // check for a slice type (string, bytes, slice) + if input.Type.requiresLengthPrefix() { + // calculate the offset + offset := len(method.Inputs)*32 + len(variableInput) + // set the offset + ret = append(ret, packNum(reflect.ValueOf(offset), UintTy)...) + // Append the packed output to the variable input. The variable input + // will be appended at the end of the input. + variableInput = append(variableInput, packed...) + } else { + // append the packed value to the input + ret = append(ret, packed...) + } + } + // append the variable input at the end of the packed input + ret = append(ret, variableInput...) + + return ret, nil +} + // Sig returns the methods string signature according to the ABI spec. // // Example diff --git a/accounts/abi/numbers.go b/accounts/abi/numbers.go index 084701de5..5a31cf2b5 100644 --- a/accounts/abi/numbers.go +++ b/accounts/abi/numbers.go @@ -24,8 +24,8 @@ import ( ) var ( - big_t = reflect.TypeOf(&big.Int{}) - ubig_t = reflect.TypeOf(&big.Int{}) + big_t = reflect.TypeOf(big.Int{}) + ubig_t = reflect.TypeOf(big.Int{}) byte_t = reflect.TypeOf(byte(0)) byte_ts = reflect.TypeOf([]byte(nil)) uint_t = reflect.TypeOf(uint(0)) diff --git a/accounts/abi/packing.go b/accounts/abi/packing.go new file mode 100644 index 000000000..c765dfdf3 --- /dev/null +++ b/accounts/abi/packing.go @@ -0,0 +1,65 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package abi + +import ( + "reflect" + + "github.com/ethereum/go-ethereum/common" +) + +// 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) + return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...) +} + +// packElement packs the given reflect value according to the abi specification in +// t. +func packElement(t Type, reflectValue reflect.Value) []byte { + switch t.T { + case IntTy, UintTy: + return packNum(reflectValue, t.T) + case StringTy: + return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()) + case AddressTy: + if reflectValue.Kind() == reflect.Array { + reflectValue = mustArrayToByteSlice(reflectValue) + } + + return common.LeftPadBytes(reflectValue.Bytes(), 32) + case BoolTy: + if reflectValue.Bool() { + return common.LeftPadBytes(common.Big1.Bytes(), 32) + } else { + return common.LeftPadBytes(common.Big0.Bytes(), 32) + } + case BytesTy: + if reflectValue.Kind() == reflect.Array { + reflectValue = mustArrayToByteSlice(reflectValue) + } + return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()) + case FixedBytesTy: + if reflectValue.Kind() == reflect.Array { + reflectValue = mustArrayToByteSlice(reflectValue) + } + + return common.RightPadBytes(reflectValue.Bytes(), 32) + } + panic("abi: fatal error") +} diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go new file mode 100644 index 000000000..780c64c66 --- /dev/null +++ b/accounts/abi/reflect.go @@ -0,0 +1,64 @@ +// Copyright 2016 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. + +package abi + +import "reflect" + +// indirect recursively dereferences the value until it either gets the value +// or finds a big.Int +func indirect(v reflect.Value) reflect.Value { + if v.Kind() == reflect.Ptr && v.Elem().Type() != big_t { + return indirect(v.Elem()) + } + return v +} + +// reflectIntKind returns the reflect using the given size and +// unsignedness. +func reflectIntKind(unsigned bool, size int) reflect.Kind { + switch size { + case 8: + if unsigned { + return reflect.Uint8 + } + return reflect.Int8 + case 16: + if unsigned { + return reflect.Uint16 + } + return reflect.Int16 + case 32: + if unsigned { + return reflect.Uint32 + } + return reflect.Int32 + case 64: + if unsigned { + return reflect.Uint64 + } + return reflect.Int64 + } + return reflect.Ptr +} + +// mustArrayToBytesSlice creates a new byte slice with the exact same size as value +// and copies the bytes in value to the new slice. +func mustArrayToByteSlice(value reflect.Value) reflect.Value { + slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) + reflect.Copy(slice, value) + return slice +} diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 5a5a5ac49..2235bad61 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -21,8 +21,6 @@ import ( "reflect" "regexp" "strconv" - - "github.com/ethereum/go-ethereum/common" ) const ( @@ -40,53 +38,60 @@ const ( // Type is the reflection of the supported argument type type Type struct { - IsSlice bool - SliceSize int + IsSlice, IsArray bool + SliceSize int + + Elem *Type + + Kind reflect.Kind + Type reflect.Type + Size int + T byte // Our own type checking - Kind reflect.Kind - Type reflect.Type - Size int - T byte // Our own type checking stringKind string // holds the unparsed string for deriving signatures } var ( + // fullTypeRegex parses the abi types + // + // Types can be in the format of: + // + // Input = Type [ "[" [ Number ] "]" ] Name . + // Type = [ "u" ] "int" [ Number ] . + // + // Examples: + // + // string int uint real + // string32 int8 uint8 uint[] + // address int256 uint256 real[2] fullTypeRegex = regexp.MustCompile("([a-zA-Z0-9]+)(\\[([0-9]*)?\\])?") - typeRegex = regexp.MustCompile("([a-zA-Z]+)([0-9]*)?") + // typeRegex parses the abi sub types + typeRegex = regexp.MustCompile("([a-zA-Z]+)([0-9]*)?") ) -// NewType returns a fully parsed Type given by the input string or an error if it can't be parsed. -// -// Strings can be in the format of: -// -// Input = Type [ "[" [ Number ] "]" ] Name . -// Type = [ "u" ] "int" [ Number ] . -// -// Examples: -// -// string int uint real -// string32 int8 uint8 uint[] -// address int256 uint256 real[2] +// NewType creates a new reflection type of abi type given in t. func NewType(t string) (typ Type, err error) { - // 1. full string 2. type 3. (opt.) is slice 4. (opt.) size - // parse the full representation of the abi-type definition; including: - // * full string - // * type - // * is slice - // * slice size res := fullTypeRegex.FindAllStringSubmatch(t, -1)[0] - // check if type is slice and parse type. switch { case res[3] != "": // err is ignored. Already checked for number through the regexp typ.SliceSize, _ = strconv.Atoi(res[3]) - typ.IsSlice = true + typ.IsArray = true case res[2] != "": typ.IsSlice, typ.SliceSize = true, -1 case res[0] == "": return Type{}, fmt.Errorf("abi: type parse error: %s", t) } + if typ.IsArray || typ.IsSlice { + sliceType, err := NewType(res[1]) + if err != nil { + return Type{}, err + } + typ.Elem = &sliceType + typ.stringKind = sliceType.stringKind + t[len(res[1]):] + return typ, nil + } // parse the type and size of the abi-type. parsedType := typeRegex.FindAllStringSubmatch(res[1], -1)[0] @@ -106,24 +111,24 @@ func NewType(t string) (typ Type, err error) { varSize = 256 t += "256" } + typ.stringKind = t switch varType { case "int": - typ.Kind = reflect.Int + typ.Kind = reflectIntKind(false, varSize) typ.Type = big_t typ.Size = varSize typ.T = IntTy case "uint": - typ.Kind = reflect.Uint + typ.Kind = reflectIntKind(true, varSize) typ.Type = ubig_t typ.Size = varSize typ.T = UintTy case "bool": typ.Kind = reflect.Bool typ.T = BoolTy - case "real": // TODO - typ.Kind = reflect.Invalid case "address": + typ.Kind = reflect.Array typ.Type = address_t typ.Size = 20 typ.T = AddressTy @@ -131,123 +136,55 @@ func NewType(t string) (typ Type, err error) { typ.Kind = reflect.String typ.Size = -1 typ.T = StringTy - if varSize > 0 { - typ.Size = 32 - } - case "hash": - typ.Kind = reflect.Array - typ.Size = 32 - typ.Type = hash_t - typ.T = HashTy case "bytes": - typ.Kind = reflect.Array - typ.Type = byte_ts - typ.Size = varSize + sliceType, _ := NewType("uint8") + typ.Elem = &sliceType if varSize == 0 { + typ.IsSlice = true typ.T = BytesTy + typ.SliceSize = -1 } else { + typ.IsArray = true typ.T = FixedBytesTy + typ.SliceSize = varSize } default: return Type{}, fmt.Errorf("unsupported arg type: %s", t) } - typ.stringKind = t return } +// String implements Stringer func (t Type) String() (out string) { return t.stringKind } -// 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) - return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...) -} - -// Test the given input parameter `v` and checks if it matches certain -// criteria -// * Big integers are checks for ptr types and if the given value is -// assignable -// * Integer are checked for size -// * Strings, addresses and bytes are checks for type and size -func (t Type) pack(v interface{}) ([]byte, error) { - value := reflect.ValueOf(v) - switch kind := value.Kind(); kind { - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - // check input is unsigned - if t.Type != ubig_t { - return nil, fmt.Errorf("abi: type mismatch: %s for %T", t.Type, v) - } - - // no implicit type casting - if int(value.Type().Size()*8) != t.Size { - return nil, fmt.Errorf("abi: cannot use type %T as type uint%d", v, t.Size) - } - - return packNum(value, t.T), nil - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if t.Type != ubig_t { - return nil, fmt.Errorf("type mismatch: %s for %T", t.Type, v) - } - - // no implicit type casting - if int(value.Type().Size()*8) != t.Size { - return nil, fmt.Errorf("abi: cannot use type %T as type uint%d", v, t.Size) - } - return packNum(value, t.T), nil - case reflect.Ptr: - // If the value is a ptr do a assign check (only used by - // big.Int for now) - if t.Type == ubig_t && value.Type() != ubig_t { - return nil, fmt.Errorf("type mismatch: %s for %T", t.Type, v) - } - return packNum(value, t.T), nil - case reflect.String: - if t.Size > -1 && value.Len() > t.Size { - return nil, fmt.Errorf("%v out of bound. %d for %d", value.Kind(), value.Len(), t.Size) - } - - return packBytesSlice([]byte(value.String()), value.Len()), nil - case reflect.Slice: - // Byte slice is a special case, it gets treated as a single value - if t.T == BytesTy { - return packBytesSlice(value.Bytes(), value.Len()), nil - } - - if t.SliceSize > -1 && value.Len() > t.SliceSize { - return nil, fmt.Errorf("%v out of bound. %d for %d", value.Kind(), value.Len(), t.Size) - } +func (t Type) pack(v reflect.Value) ([]byte, error) { + // dereference pointer first if it's a pointer + v = indirect(v) - // Signed / Unsigned check - if value.Type() == big_t && (t.T != IntTy && isSigned(value)) || (t.T == UintTy && isSigned(value)) { - return nil, fmt.Errorf("slice of incompatible types.") - } + if err := typeCheck(t, v); err != nil { + return nil, err + } + if (t.IsSlice || t.IsArray) && t.T != BytesTy && t.T != FixedBytesTy { var packed []byte - for i := 0; i < value.Len(); i++ { - val, err := t.pack(value.Index(i).Interface()) + for i := 0; i < v.Len(); i++ { + val, err := t.Elem.pack(v.Index(i)) if err != nil { return nil, err } packed = append(packed, val...) } - return packBytesSlice(packed, value.Len()), nil - case reflect.Bool: - if value.Bool() { - return common.LeftPadBytes(common.Big1.Bytes(), 32), nil - } else { - return common.LeftPadBytes(common.Big0.Bytes(), 32), nil - } - case reflect.Array: - if v, ok := value.Interface().(common.Address); ok { - return common.LeftPadBytes(v[:], 32), nil - } else if v, ok := value.Interface().(common.Hash); ok { - return v[:], nil - } + return packBytesSlice(packed, v.Len()), nil } - return nil, fmt.Errorf("ABI: bad input given %v", value.Kind()) + return packElement(t, v), nil +} + +// requireLengthPrefix returns whether the type requires any sort of length +// prefixing. +func (t Type) requiresLengthPrefix() bool { + return t.T != FixedBytesTy && (t.T == StringTy || t.T == BytesTy || t.IsSlice || t.IsArray) } diff --git a/accounts/watch.go b/accounts/watch.go index 19d304fcc..309e4d458 100644 --- a/accounts/watch.go +++ b/accounts/watch.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. -// +build darwin,!ios freebsd linux netbsd solaris windows +// +build darwin,!ios freebsd linux,!arm64 netbsd solaris windows package accounts diff --git a/accounts/watch_fallback.go b/accounts/watch_fallback.go index 0b7016167..7b5e221df 100644 --- a/accounts/watch_fallback.go +++ b/accounts/watch_fallback.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. -// +build ios !darwin,!freebsd,!linux,!netbsd,!solaris,!windows +// +build ios linux,arm64 !darwin,!freebsd,!linux,!netbsd,!solaris,!windows // This is the fallback implementation of directory watching. // It is used on unsupported platforms. diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 767b513c1..2b64303b2 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -123,7 +123,7 @@ func (self *jsre) batch(statement string) { err := self.re.EvalAndPrettyPrint(statement) if err != nil { - fmt.Printf("error: %v", err) + fmt.Printf("%v", jsErrorString(err)) } if self.atexit != nil { @@ -301,21 +301,19 @@ func (self *jsre) preloadJSFiles(ctx *cli.Context) error { for _, file := range jsFiles { filename := common.AbsolutePath(assetPath, strings.TrimSpace(file)) if err := self.re.Exec(filename); err != nil { - return fmt.Errorf("%s: %v", file, err) + return fmt.Errorf("%s: %v", file, jsErrorString(err)) } } } return nil } -// exec executes the JS file with the given filename and stops the JSRE -func (self *jsre) exec(filename string) error { - if err := self.re.Exec(filename); err != nil { - self.re.Stop(false) - return fmt.Errorf("Javascript Error: %v", err) +// jsErrorString adds a backtrace to errors generated by otto. +func jsErrorString(err error) string { + if ottoErr, ok := err.(*otto.Error); ok { + return ottoErr.String() } - self.re.Stop(true) - return nil + return err.Error() } func (self *jsre) interactive() { diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 6ab4ed45b..ffeb7c1e5 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -21,6 +21,7 @@ import ( "fmt" "io/ioutil" "os" + "os/signal" "path/filepath" "runtime" "strconv" @@ -205,6 +206,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso utils.NetworkIdFlag, utils.RPCCORSDomainFlag, utils.MetricsEnabledFlag, + utils.FakePoWFlag, utils.SolcPathFlag, utils.GpoMinGasPriceFlag, utils.GpoMaxGasPriceFlag, @@ -353,7 +355,7 @@ func console(ctx *cli.Context) { // preload user defined JS files into the console err = repl.preloadJSFiles(ctx) if err != nil { - utils.Fatalf("unable to preload JS file %v", err) + utils.Fatalf("%v", err) } // in case the exec flag holds a JS statement execute it and return @@ -372,6 +374,7 @@ func execScripts(ctx *cli.Context) { // Create and start the node based on the CLI flags node := utils.MakeSystemNode(ClientIdentifier, nodeNameVersion, makeDefaultExtra(), ctx) startNode(ctx, node) + defer node.Stop() // Attach to the newly started node and execute the given scripts client, err := node.Attach() @@ -383,10 +386,24 @@ func execScripts(ctx *cli.Context) { ctx.GlobalString(utils.RPCCORSDomainFlag.Name), client, false) + // Run all given files. for _, file := range ctx.Args() { - repl.exec(file) + if err = repl.re.Exec(file); err != nil { + break + } } - node.Stop() + if err != nil { + utils.Fatalf("JavaScript Error: %v", jsErrorString(err)) + } + // JS files loaded successfully. + // Wait for pending callbacks, but stop for Ctrl-C. + abort := make(chan os.Signal, 1) + signal.Notify(abort, os.Interrupt) + go func() { + <-abort + repl.re.Stop(false) + }() + repl.re.Stop(true) } // startNode boots up the system node and all registered protocols, after which diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 278a55980..90019d7b9 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -150,8 +150,11 @@ var AppHelpFlagGroups = []flagGroup{ }, }, { - Name: "LOGGING AND DEBUGGING", - Flags: append([]cli.Flag{utils.MetricsEnabledFlag}, debug.Flags...), + Name: "LOGGING AND DEBUGGING", + Flags: append([]cli.Flag{ + utils.MetricsEnabledFlag, + utils.FakePoWFlag, + }, debug.Flags...), }, { Name: "EXPERIMENTAL", diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 52060c795..8d55ac8b9 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -47,6 +47,7 @@ import ( "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/whisper" ) @@ -228,6 +229,10 @@ var ( Name: metrics.MetricsEnabledFlag, Usage: "Enable metrics collection and reporting", } + FakePoWFlag = cli.BoolFlag{ + Name: "fakepow", + Usage: "Disables proof-of-work verification", + } // RPC settings RPCEnabledFlag = cli.BoolFlag{ @@ -842,11 +847,13 @@ func MakeChain(ctx *cli.Context) (chain *core.BlockChain, chainDb ethdb.Database glog.Fatalln(err) } } - chainConfig := MustMakeChainConfigFromDb(ctx, chainDb) - var eventMux event.TypeMux - chain, err = core.NewBlockChain(chainDb, chainConfig, ethash.New(), &eventMux) + pow := pow.PoW(core.FakePow{}) + if !ctx.GlobalBool(FakePoWFlag.Name) { + pow = ethash.New() + } + chain, err = core.NewBlockChain(chainDb, chainConfig, pow, new(event.TypeMux)) if err != nil { Fatalf("Could not start chainmanager: %v", err) } diff --git a/common/types.go b/common/types.go index fec986164..d00884484 100644 --- a/common/types.go +++ b/common/types.go @@ -167,7 +167,7 @@ func (a Address) MarshalJSON() ([]byte, error) { // Parse address from raw json data func (a *Address) UnmarshalJSON(data []byte) error { if len(data) > 2 && data[0] == '"' && data[len(data)-1] == '"' { - data = data[:len(data)-1][1:] + data = data[1 : len(data)-1] } if len(data) > 2 && data[0] == '0' && data[1] == 'x' { diff --git a/common/types_test.go b/common/types_test.go index f2dfbf0c9..de67cfcb5 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -16,7 +16,10 @@ package common -import "testing" +import ( + "math/big" + "testing" +) func TestBytesConversion(t *testing.T) { bytes := []byte{5} @@ -47,7 +50,38 @@ func TestHashJsonValidation(t *testing.T) { } for i, test := range tests { if err := h.UnmarshalJSON(append([]byte(test.Prefix), make([]byte, test.Size)...)); err != test.Error { - t.Error(i, "expected", test.Error, "got", err) + t.Errorf("test #%d: error mismatch: have %v, want %v", i, err, test.Error) + } + } +} + +func TestAddressUnmarshalJSON(t *testing.T) { + var a Address + var tests = []struct { + Input string + ShouldErr bool + Output *big.Int + }{ + {"", true, nil}, + {`""`, true, nil}, + {`"0x"`, true, nil}, + {`"0x00"`, true, nil}, + {`"0xG000000000000000000000000000000000000000"`, true, nil}, + {`"0x0000000000000000000000000000000000000000"`, false, big.NewInt(0)}, + {`"0x0000000000000000000000000000000000000010"`, false, big.NewInt(16)}, + } + for i, test := range tests { + err := a.UnmarshalJSON([]byte(test.Input)) + if err != nil && !test.ShouldErr { + t.Errorf("test #%d: unexpected error: %v", i, err) + } + if err == nil { + if test.ShouldErr { + t.Errorf("test #%d: expected error, got none", i) + } + if a.Big().Cmp(test.Output) != 0 { + t.Errorf("test #%d: address mismatch: have %v, want %v", i, a.Big(), test.Output) + } } } } diff --git a/core/blockchain.go b/core/blockchain.go index ecf8297cb..4598800d5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1213,3 +1213,6 @@ func (self *BlockChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []c func (self *BlockChain) GetHeaderByNumber(number uint64) *types.Header { return self.hc.GetHeaderByNumber(number) } + +// Config retrieves the blockchain's chain configuration. +func (self *BlockChain) Config() *ChainConfig { return self.config } diff --git a/core/genesis.go b/core/genesis.go index 5c69b216c..40d799621 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -43,7 +43,7 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block, } var genesis struct { - ChainConfig *ChainConfig + ChainConfig *ChainConfig `json:"config"` Nonce string Timestamp string ParentHash string diff --git a/eth/api.go b/eth/api.go index 4ebc9b2a0..2c84cf471 100644 --- a/eth/api.go +++ b/eth/api.go @@ -52,6 +52,15 @@ import ( "golang.org/x/net/context" ) +// ErrNoCode is returned by call and transact operations for which the requested +// recipient contract to operate on does not exist in the state db or does not +// have any code associated with it (i.e. suicided). +// +// Please note, this error string is part of the RPC API and is expected by the +// native contract bindings to signal this particular error. Do not change this +// as it will break all dependent code! +var ErrNoCode = errors.New("no contract code at given address") + const defaultGas = uint64(90000) // blockByNumber is a commonly used helper function which retrieves and returns @@ -705,6 +714,12 @@ func (s *PublicBlockChainAPI) doCall(args CallArgs, blockNr rpc.BlockNumber) (st } stateDb = stateDb.Copy() + // If there's no code to interact with, respond with an appropriate error + if args.To != nil { + if code := stateDb.GetCode(*args.To); len(code) == 0 { + return "0x", nil, ErrNoCode + } + } // Retrieve the account state object to interact with var from *state.StateObject if args.From == (common.Address{}) { diff --git a/eth/filters/filter_system.go b/eth/filters/filter_system.go index 29968530a..4343dfa21 100644 --- a/eth/filters/filter_system.go +++ b/eth/filters/filter_system.go @@ -164,7 +164,7 @@ func (fs *FilterSystem) filterLoop() { fs.filterMu.RLock() for _, filter := range fs.logFilters { if filter.LogCallback != nil && !filter.created.After(event.Time) { - for _, removedLog := range ev.Logs { + for _, removedLog := range filter.FilterLogs(ev.Logs) { filter.LogCallback(removedLog, true) } } diff --git a/jsre/jsre.go b/jsre/jsre.go index 7df022cb1..59730bc0d 100644 --- a/jsre/jsre.go +++ b/jsre/jsre.go @@ -235,7 +235,14 @@ func (self *JSRE) Exec(file string) error { if err != nil { return err } - self.Do(func(vm *otto.Otto) { _, err = vm.Run(code) }) + var script *otto.Script + self.Do(func(vm *otto.Otto) { + script, err = vm.Compile(file, code) + if err != nil { + return + } + _, err = vm.Run(script) + }) return err } |