Commit c1cd62b7 authored by hezhengjun's avatar hezhengjun

support unpack both of event and output

parent abde63ec
...@@ -54,7 +54,7 @@ function evm_callQuery() { ...@@ -54,7 +54,7 @@ function evm_callQuery() {
local parameter=$1 local parameter=$1
local callerAddr=$2 local callerAddr=$2
local resok=$3 local resok=$3
local methodName=$4 local name=$4
req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"GetPackData","payload":{"abi":"'${erc20_abi}'","parameter":"'${parameter}'"}}]}' req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"GetPackData","payload":{"abi":"'${erc20_abi}'","parameter":"'${parameter}'"}}]}'
chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "GetPackData" ".result.packData" chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "GetPackData" ".result.packData"
...@@ -64,7 +64,7 @@ function evm_callQuery() { ...@@ -64,7 +64,7 @@ function evm_callQuery() {
chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "Query" ".result.rawData" chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "Query" ".result.rawData"
echo "$RETURN_RESP" echo "$RETURN_RESP"
req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"GetUnpackData","payload":{"abi":"'${erc20_abi}'","methodName":"'${methodName}'","data":"'${RETURN_RESP}'"}}]}' req='{"method":"Chain33.Query","params":[{"execer":"evm","funcName":"GetUnpackData","payload":{"abi":"'${erc20_abi}'","name":"'${name}'","data":"'${RETURN_RESP}'"}}]}'
chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "GetUnpackData" ".result.unpackData[0]" chain33_Http "$req" "${MAIN_HTTP}" '(.result != null)' "GetUnpackData" ".result.unpackData[0]"
echo "$RETURN_RESP" echo "$RETURN_RESP"
......
...@@ -153,6 +153,55 @@ func Unpack(data []byte, methodName, abiData string) (output []*Param, err error ...@@ -153,6 +153,55 @@ func Unpack(data []byte, methodName, abiData string) (output []*Param, err error
return return
} }
func UnpackOutputOrEvent(data []byte, name, abiData string) (output []*Param, err error) {
if len(data) == 0 {
log.Info("Unpack", "Data len", 0, "name", name)
return output, err
}
// 解析ABI数据结构,获取本次调用的方法对象
abi, err := JSON(strings.NewReader(abiData))
if err != nil {
return output, err
}
values := []interface{}{}
var arguments Arguments
if method, ok := abi.Methods[name]; ok {
if len(data)%32 != 0 {
return output, errors.New("UnpackOutputOrEvent: improperly formatted output")
}
values, err = method.Outputs.UnpackValues(data)
arguments = method.Outputs
} else if event, ok := abi.Events[name]; ok {
values, err = event.Inputs.UnpackValues(data)
arguments = event.Inputs
} else {
return output, errors.New("UnpackOutputOrEvent: could not locate named method or event")
}
if err != nil {
return output, err
}
if len(arguments) == 0 || len(values) != len(arguments) {
return output, errors.New("wrong data to unpack")
}
output = []*Param{}
for i, v := range values {
arg := arguments[i]
pval := &Param{Name: arg.Name, Type: arg.Type.String(), Value: v}
if arg.Type.String() == "address" {
pval.Value = v.(common.Hash160Address).ToAddress().String()
log.Info("Unpack address", "address", pval.Value)
}
output = append(output, pval)
}
return
}
// Param 返回值参数结构定义 // Param 返回值参数结构定义
type Param struct { type Param struct {
// Name 参数名称 // Name 参数名称
......
...@@ -204,7 +204,7 @@ func (evm *EVMExecutor) Query_GetUnpackData(in *evmtypes.EvmGetUnpackDataReq) (t ...@@ -204,7 +204,7 @@ func (evm *EVMExecutor) Query_GetUnpackData(in *evmtypes.EvmGetUnpackDataReq) (t
return nil, errors.New("common.FromHex failed due to:" + err.Error()) return nil, errors.New("common.FromHex failed due to:" + err.Error())
} }
outputs, err := evmAbi.Unpack(data, in.MethodName, in.Abi) outputs, err := evmAbi.UnpackOutputOrEvent(data, in.Name, in.Abi)
if err != nil { if err != nil {
return nil, errors.New("unpack evm return error" + err.Error()) return nil, errors.New("unpack evm return error" + err.Error())
} }
......
...@@ -202,7 +202,7 @@ message EvmGetPackDataRespose { ...@@ -202,7 +202,7 @@ message EvmGetPackDataRespose {
message EvmGetUnpackDataReq { message EvmGetUnpackDataReq {
string abi = 1; string abi = 1;
string methodName = 2; string name = 2;
string data = 3; string data = 3;
} }
......
...@@ -1866,9 +1866,9 @@ type EvmGetUnpackDataReq struct { ...@@ -1866,9 +1866,9 @@ type EvmGetUnpackDataReq struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Abi string `protobuf:"bytes,1,opt,name=abi,proto3" json:"abi,omitempty"` Abi string `protobuf:"bytes,1,opt,name=abi,proto3" json:"abi,omitempty"`
MethodName string `protobuf:"bytes,2,opt,name=methodName,proto3" json:"methodName,omitempty"` Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
Data string `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` Data string `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
} }
func (x *EvmGetUnpackDataReq) Reset() { func (x *EvmGetUnpackDataReq) Reset() {
...@@ -1910,9 +1910,9 @@ func (x *EvmGetUnpackDataReq) GetAbi() string { ...@@ -1910,9 +1910,9 @@ func (x *EvmGetUnpackDataReq) GetAbi() string {
return "" return ""
} }
func (x *EvmGetUnpackDataReq) GetMethodName() string { func (x *EvmGetUnpackDataReq) GetName() string {
if x != nil { if x != nil {
return x.MethodName return x.Name
} }
return "" return ""
} }
...@@ -2173,18 +2173,17 @@ var file_evmcontract_proto_rawDesc = []byte{ ...@@ -2173,18 +2173,17 @@ var file_evmcontract_proto_rawDesc = []byte{
0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x22, 0x33, 0x0a, 0x15, 0x45, 0x76, 0x6d, 0x47, 0x65, 0x74, 0x61, 0x6d, 0x65, 0x74, 0x65, 0x72, 0x22, 0x33, 0x0a, 0x15, 0x45, 0x76, 0x6d, 0x47, 0x65, 0x74,
0x50, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x65, 0x12, 0x50, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x65, 0x12,
0x1a, 0x0a, 0x08, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x1a, 0x0a, 0x08, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x08, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x22, 0x5b, 0x0a, 0x13, 0x45, 0x09, 0x52, 0x08, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x22, 0x4f, 0x0a, 0x13, 0x45,
0x76, 0x6d, 0x47, 0x65, 0x74, 0x55, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52, 0x76, 0x6d, 0x47, 0x65, 0x74, 0x55, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52,
0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x62, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x65, 0x71, 0x12, 0x10, 0x0a, 0x03, 0x61, 0x62, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x03, 0x61, 0x62, 0x69, 0x12, 0x1e, 0x0a, 0x0a, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4e, 0x61, 0x03, 0x61, 0x62, 0x69, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01,
0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61,
0x4e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x39, 0x0a, 0x17,
0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x39, 0x0a, 0x17, 0x45, 0x76, 0x6d, 0x47, 0x45, 0x76, 0x6d, 0x47, 0x65, 0x74, 0x55, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61,
0x65, 0x74, 0x55, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52, 0x65, 0x73, 0x70, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x73, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x75, 0x6e, 0x70, 0x61, 0x63,
0x6f, 0x73, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x75, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x75, 0x6e, 0x70,
0x61, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0a, 0x75, 0x6e, 0x70, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x74, 0x79,
0x61, 0x74, 0x61, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2e, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment