diff --git a/execution/evm/abi/abi.go b/execution/evm/abi/abi.go index d0d71eb8162821f30eeeb53c774bfc27e466a12e..4f9c164edfc4ba9425e99b0cc2b05feb96278eb5 100644 --- a/execution/evm/abi/abi.go +++ b/execution/evm/abi/abi.go @@ -529,7 +529,8 @@ type EVMBytes struct { } func (e EVMBytes) getGoType() interface{} { - return make([]byte, e.M) + v := make([]byte, e.M) + return &v } func (e EVMBytes) pack(v interface{}) ([]byte, error) { @@ -573,8 +574,9 @@ func (e EVMBytes) unpack(data []byte, offset int, v interface{}) (int, error) { return s.unpack(data, offset, v) } - switch v := v.(type) { - case *string: + v2 := reflect.ValueOf(v).Elem() + switch v2.Type().Kind() { + case reflect.String: start := 0 end := int(e.M) @@ -584,9 +586,11 @@ func (e EVMBytes) unpack(data []byte, offset int, v interface{}) (int, error) { for end > start && data[offset+end-1] == 0 { end-- } - *v = string(data[offset+start : offset+end]) - case *[]byte: - *v = data[offset : offset+int(e.M)] + v2.SetString(string(data[offset+start : offset+end])) + case reflect.Array: + fallthrough + case reflect.Slice: + v2.SetBytes(data[offset : offset+int(e.M)]) default: return 0, fmt.Errorf("cannot map EVM %s to %s", e.GetSignature(), reflect.ValueOf(v).Kind().String()) } @@ -699,6 +703,7 @@ type Argument struct { EVM EVMType IsArray bool Indexed bool + Hashed bool ArrayLength uint64 } @@ -872,6 +877,7 @@ func ReadAbiSpec(specBytes []byte) (*AbiSpec, error) { if inputs[i].Indexed && inputs[i].EVM.isDynamic() { // For Dynamic types, the hash is stored in stead inputs[i].EVM = EVMBytes{M: 32} + inputs[i].Hashed = true } } abiSpec.Events[s.Name] = Event{Inputs: inputs, Anonymous: s.Anonymous} diff --git a/execution/solidity/event_emitter.sol b/execution/solidity/event_emitter.sol index 3ab387b46ae77bb183d8eeda962557c8e2e49fe2..fe63f055d3943967f6e6eab480f91399e62154ff 100644 --- a/execution/solidity/event_emitter.sol +++ b/execution/solidity/event_emitter.sol @@ -6,7 +6,7 @@ contract EventEmitter { bytes32 indexed direction, bool trueism, string german , - int indexed newDepth, + int64 indexed newDepth, string indexed hash) anonymous; diff --git a/execution/solidity/event_emitter.sol.go b/execution/solidity/event_emitter.sol.go index b47c23f0666906d402b248ce549eefe76937d0ea..dc0a0f8eadb7b26443708d08153e690ad6a393cc 100644 --- a/execution/solidity/event_emitter.sol.go +++ b/execution/solidity/event_emitter.sol.go @@ -2,5 +2,5 @@ package solidity import "github.com/tmthrgd/go-hex" -var Bytecode_EventEmitter = hex.MustDecodeString("6080604052348015600f57600080fd5b506101848061001f6000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063e8e49a7114610046575b600080fd5b34801561005257600080fd5b5061005b61005d565b005b60405180807f68617368000000000000000000000000000000000000000000000000000000008152506004019050604051809103902060667f446f776e736965210000000000000000000000000000000000000000000000006001604051808215151515815260200180602001828103825260518152602001807f446f6e617564616d7066736368696666666168727473656c656b7472697a697481526020017fc3a474656e686175707462657472696562737765726b626175756e746572626581526020017f616d74656e676573656c6c7363686166740000000000000000000000000000008152506060019250505060405180910390a35600a165627a7a72305820f3fba03c463579cc5b976d8a660fbecb3b45a67db1f160f999901b0b3822aa840029") -var Abi_EventEmitter = []byte(`[{"constant":false,"inputs":[],"name":"EmitOne","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":true,"inputs":[{"indexed":true,"name":"direction","type":"bytes32"},{"indexed":false,"name":"trueism","type":"bool"},{"indexed":false,"name":"german","type":"string"},{"indexed":true,"name":"newDepth","type":"int256"},{"indexed":true,"name":"hash","type":"string"}],"name":"ManyTypes","type":"event"}]`) +var Bytecode_EventEmitter = hex.MustDecodeString("6080604052348015600f57600080fd5b506101848061001f6000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063e8e49a7114610046575b600080fd5b34801561005257600080fd5b5061005b61005d565b005b60405180807f68617368000000000000000000000000000000000000000000000000000000008152506004019050604051809103902060667f446f776e736965210000000000000000000000000000000000000000000000006001604051808215151515815260200180602001828103825260518152602001807f446f6e617564616d7066736368696666666168727473656c656b7472697a697481526020017fc3a474656e686175707462657472696562737765726b626175756e746572626581526020017f616d74656e676573656c6c7363686166740000000000000000000000000000008152506060019250505060405180910390a35600a165627a7a72305820cce41c19422f23aeff2c5d49dd42145ea2327e00112ebcef35976afeecf7a54c0029") +var Abi_EventEmitter = []byte(`[{"constant":false,"inputs":[],"name":"EmitOne","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":true,"inputs":[{"indexed":true,"name":"direction","type":"bytes32"},{"indexed":false,"name":"trueism","type":"bool"},{"indexed":false,"name":"german","type":"string"},{"indexed":true,"name":"newDepth","type":"int64"},{"indexed":true,"name":"hash","type":"string"}],"name":"ManyTypes","type":"event"}]`) diff --git a/integration/rpctransact/call_test.go b/integration/rpctransact/call_test.go index 490267981a62d8332f7b3a93310a88d63f4f80cd..0e9274df4c73f00694874d72ce8fcf81df18f208 100644 --- a/integration/rpctransact/call_test.go +++ b/integration/rpctransact/call_test.go @@ -3,6 +3,7 @@ package rpctransact import ( + "bytes" "context" "fmt" "sync" @@ -293,28 +294,62 @@ func TestEventEmitter(t *testing.T) { address := lastCall(createTxe.Events).CallData.Callee spec, err := abi.ReadAbiSpec(solidity.Abi_EventEmitter) require.NoError(t, err) - data, err := spec.Pack("EmitOne") + calldata, err := spec.Pack("EmitOne") require.NoError(t, err) - callTxe := rpctest.CallContract(t, cli, inputAddress, address, data) + callTxe := rpctest.CallContract(t, cli, inputAddress, address, calldata) evs := filterLogs(callTxe.Events) log := evs[0] - var direction string - var truism bool - var depth int64 - var german string - var hash []byte - err = abi.UnpackEvent(spec.Events["ManyTypes"], log.Topics, log.Data.Bytes(), &direction, &truism, &german, &depth, &hash) + evAbi := spec.Events["ManyTypes"] + data := abi.GetPackingTypes(evAbi.Inputs) + err = abi.UnpackEvent(evAbi, log.Topics, log.Data.Bytes(), data...) + require.NoError(t, err) + + h := sha3.NewKeccak256() + h.Write([]byte("hash")) + expectedHash := h.Sum(nil) + // "Downsie!", true, "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft", 102, [0xcd, 0x9a, 0xf3], 'hash') + b := *data[0].(*[]byte) + assert.Equal(t, "Downsie!", string(bytes.Trim(b, "\x00"))) + assert.Equal(t, true, *data[1].(*bool)) + assert.Equal(t, "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft", *data[2].(*string)) + assert.Equal(t, int64(102), *data[3].(*int64)) + assert.Equal(t, expectedHash, *data[4].(*[]byte)) +} + +/* + * Any indexed string (or dynamic array) will be hashed, so we might want to store strings + * in bytes32. This shows how we would automatically map this to string + */ +func TestEventEmitterBytes32isString(t *testing.T) { + cli := rpctest.NewTransactClient(t, testConfig.RPC.GRPC.ListenAddress) + createTxe := rpctest.CreateContract(t, cli, inputAddress, solidity.Bytecode_EventEmitter) + address := lastCall(createTxe.Events).CallData.Callee + spec, err := abi.ReadAbiSpec(solidity.Abi_EventEmitter) + require.NoError(t, err) + calldata, err := spec.Pack("EmitOne") + require.NoError(t, err) + callTxe := rpctest.CallContract(t, cli, inputAddress, address, calldata) + evs := filterLogs(callTxe.Events) + log := evs[0] + evAbi := spec.Events["ManyTypes"] + data := abi.GetPackingTypes(evAbi.Inputs) + for i, a := range evAbi.Inputs { + if a.Indexed && !a.Hashed && a.EVM.GetSignature() == "bytes32" { + data[i] = new(string) + } + } + err = abi.UnpackEvent(evAbi, log.Topics, log.Data.Bytes(), data...) require.NoError(t, err) h := sha3.NewKeccak256() h.Write([]byte("hash")) expectedHash := h.Sum(nil) // "Downsie!", true, "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft", 102, [0xcd, 0x9a, 0xf3], 'hash') - assert.Equal(t, "Downsie!", direction) - assert.Equal(t, true, truism) - assert.Equal(t, "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft", german) - assert.Equal(t, int64(102), depth) - assert.Equal(t, expectedHash, hash) + assert.Equal(t, "Downsie!", *data[0].(*string)) + assert.Equal(t, true, *data[1].(*bool)) + assert.Equal(t, "Donaudampfschifffahrtselektrizitätenhauptbetriebswerkbauunterbeamtengesellschaft", *data[2].(*string)) + assert.Equal(t, int64(102), *data[3].(*int64)) + assert.Equal(t, expectedHash, *data[4].(*[]byte)) } func TestRevert(t *testing.T) {