fix int and fixed bytes

This commit is contained in:
Jeff Wentworth 2019-11-12 21:15:22 +09:00
parent d94234d5f9
commit 65364fbc96
2 changed files with 14 additions and 11 deletions

View file

@ -219,8 +219,7 @@ func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics
case abi.BoolTy: case abi.BoolTy:
out[arg.Name] = topics[0][common.HashLength-1] == 1 out[arg.Name] = topics[0][common.HashLength-1] == 1
case abi.IntTy, abi.UintTy: case abi.IntTy, abi.UintTy:
num := new(big.Int).SetBytes(topics[0][:]) out[arg.Name] = abi.ReadInteger(arg.Type.T, arg.Type.Kind, topics[0].Bytes())
out[arg.Name] = num
case abi.AddressTy: case abi.AddressTy:
var addr common.Address var addr common.Address
copy(addr[:], topics[0][common.HashLength-common.AddressLength:]) copy(addr[:], topics[0][common.HashLength-common.AddressLength:])
@ -228,9 +227,11 @@ func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics
case abi.HashTy: case abi.HashTy:
out[arg.Name] = topics[0] out[arg.Name] = topics[0]
case abi.FixedBytesTy: case abi.FixedBytesTy:
array := reflect.New(arg.Type.Type).Elem() array, err := abi.ReadFixedBytes(arg.Type, topics[0].Bytes())
reflect.Copy(array, reflect.ValueOf(topics[0][:arg.Type.Size])) if err != nil {
out[arg.Name] = array.Interface() return err
}
out[arg.Name] = array
case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy: case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy:
// Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash // Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash
// whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash // whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash

View file

@ -26,16 +26,18 @@ import (
) )
var ( var (
// MaxUint256 is the maximum value that can be represented by a uint256
MaxUint256 = big.NewInt(0).Add( MaxUint256 = big.NewInt(0).Add(
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil),
big.NewInt(-1)) big.NewInt(-1))
// MaxInt256 is the maximum value that can be represented by a int256
MaxInt256 = big.NewInt(0).Add( MaxInt256 = big.NewInt(0).Add(
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil),
big.NewInt(-1)) big.NewInt(-1))
) )
// reads the integer based on its kind // ReadInteger reads the integer based on its kind and returns the appropriate value
func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} { func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
switch kind { switch kind {
case reflect.Uint8: case reflect.Uint8:
return b[len(b)-1] return b[len(b)-1]
@ -102,8 +104,8 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
return return
} }
// through reflection, creates a fixed array to be read from // ReadFixedBytes uses reflection to create a fixed array to be read from
func readFixedBytes(t Type, word []byte) (interface{}, error) { func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
if t.T != FixedBytesTy { if t.T != FixedBytesTy {
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array") return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
} }
@ -230,7 +232,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
case StringTy: // variable arrays are written at the end of the return bytes case StringTy: // variable arrays are written at the end of the return bytes
return string(output[begin : begin+length]), nil return string(output[begin : begin+length]), nil
case IntTy, UintTy: case IntTy, UintTy:
return readInteger(t.T, t.Kind, returnOutput), nil return ReadInteger(t.T, t.Kind, returnOutput), nil
case BoolTy: case BoolTy:
return readBool(returnOutput) return readBool(returnOutput)
case AddressTy: case AddressTy:
@ -240,7 +242,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
case BytesTy: case BytesTy:
return output[begin : begin+length], nil return output[begin : begin+length], nil
case FixedBytesTy: case FixedBytesTy:
return readFixedBytes(t, returnOutput) return ReadFixedBytes(t, returnOutput)
case FunctionTy: case FunctionTy:
return readFunctionType(t, returnOutput) return readFunctionType(t, returnOutput)
default: default: