From 5127ec10cb84a615f4d5b314e4c3c102efefe4c9 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 7 Apr 2016 11:39:22 +0200 Subject: accouns/abi: refactored ABI package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored the abi package parsing and type handling. Relying mostly on package reflect as opposed to most of our own type reflection. Our own type reflection is still used however for cases such as Bytes and FixedBytes (abi: bytes•). This also inclused several fixes for slice handling of arbitrary and fixed size for all supported types. This also further removes implicit type casting such as assigning, for example `[2]T{} = []T{1}` will fail, however `[2]T{} == []T{1, 2}` (notice assigning *slice* to fixed size *array*). Assigning arrays to slices will always succeed if they are of the same element type. Incidentally also fixes #2379 --- accounts/abi/method.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'accounts/abi/method.go') diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 206c7d408..cad0cd27f 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.T == StringTy || input.Type.T == BytesTy || input.Type.IsSlice || input.Type.IsArray { + // 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 -- cgit v1.2.3 From 4880868c88b8d82cda8ea615bf82548667a95da2 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 20 Apr 2016 21:30:02 +0200 Subject: accounts/abi: fixed string and fixed size bytes packing --- accounts/abi/method.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'accounts/abi/method.go') diff --git a/accounts/abi/method.go b/accounts/abi/method.go index cad0cd27f..f3d1a44b5 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -58,7 +58,7 @@ func (m Method) pack(method Method, args ...interface{}) ([]byte, error) { } // check for a slice type (string, bytes, slice) - if input.Type.T == StringTy || input.Type.T == BytesTy || input.Type.IsSlice || input.Type.IsArray { + if input.Type.requiresLengthPrefix() { // calculate the offset offset := len(method.Inputs)*32 + len(variableInput) // set the offset -- cgit v1.2.3