mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
accounts/abi: other things needed to make the previous commits work
Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
parent
de3d8771cd
commit
5c2d42ff01
4 changed files with 10 additions and 14 deletions
|
|
@ -91,6 +91,8 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// since there can't be naming collisions with contracts and events,
|
||||||
|
// we need to decide whether we're calling a method or an event
|
||||||
var unpack unpacker
|
var unpack unpacker
|
||||||
if method, ok := abi.Methods[name]; ok {
|
if method, ok := abi.Methods[name]; ok {
|
||||||
unpack = method
|
unpack = method
|
||||||
|
|
@ -100,10 +102,10 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
|
||||||
return fmt.Errorf("abi: could not locate named method or event.")
|
return fmt.Errorf("abi: could not locate named method or event.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if unpack.tupleReturn() {
|
// requires a struct to unpack into for a tuple return...
|
||||||
|
if unpack.isTupleReturn() {
|
||||||
return unpack.tupleUnpack(v, output)
|
return unpack.tupleUnpack(v, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
return unpack.singleUnpack(v, output)
|
return unpack.singleUnpack(v, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,10 @@ func packElement(t Type, reflectValue reflect.Value) []byte {
|
||||||
reflectValue = mustArrayToByteSlice(reflectValue)
|
reflectValue = mustArrayToByteSlice(reflectValue)
|
||||||
}
|
}
|
||||||
return common.RightPadBytes(reflectValue.Bytes(), 32)
|
return common.RightPadBytes(reflectValue.Bytes(), 32)
|
||||||
}
|
default:
|
||||||
panic("abi: fatal error")
|
panic("abi: fatal error")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
|
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
|
||||||
func packNum(value reflect.Value) []byte {
|
func packNum(value reflect.Value) []byte {
|
||||||
|
|
@ -74,6 +75,8 @@ func packNum(value reflect.Value) []byte {
|
||||||
return U256(big.NewInt(value.Int()))
|
return U256(big.NewInt(value.Int()))
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return U256(value.Interface().(*big.Int))
|
return U256(value.Interface().(*big.Int))
|
||||||
|
default:
|
||||||
|
panic("abi: fatal error")
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -435,7 +435,4 @@ func TestPackNumber(t *testing.T) {
|
||||||
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
|
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if packed := packNum(reflect.ValueOf("string")); packed != nil {
|
|
||||||
t.Errorf("expected 'string' to pack to nil. got %x instead", packed)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,15 +73,9 @@ func mustArrayToByteSlice(value reflect.Value) reflect.Value {
|
||||||
func set(dst, src reflect.Value, output Argument) error {
|
func set(dst, src reflect.Value, output Argument) error {
|
||||||
dstType := dst.Type()
|
dstType := dst.Type()
|
||||||
srcType := src.Type()
|
srcType := src.Type()
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case dstType.AssignableTo(src.Type()):
|
case dstType.AssignableTo(srcType):
|
||||||
dst.Set(src)
|
dst.Set(src)
|
||||||
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
reflect.Copy(dst, src)
|
|
||||||
case dstType.Kind() == reflect.Interface:
|
case dstType.Kind() == reflect.Interface:
|
||||||
dst.Set(src)
|
dst.Set(src)
|
||||||
case dstType.Kind() == reflect.Ptr:
|
case dstType.Kind() == reflect.Ptr:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue