diff --git a/cmd/contract_cmd.go b/cmd/contract_cmd.go index da09857f61..576b1516e2 100644 --- a/cmd/contract_cmd.go +++ b/cmd/contract_cmd.go @@ -27,6 +27,7 @@ import ( "github.com/ontio/ontology/common/config" "github.com/ontio/ontology/core/payload" httpcom "github.com/ontio/ontology/http/base/common" + "github.com/ontio/ontology/smartcontract/states" "github.com/urfave/cli" "io/ioutil" "strings" @@ -308,7 +309,7 @@ func invokeContract(ctx *cli.Context) error { PrintInfoMsg("Invoke:%x Params:%s", contractAddr[:], paramData) if ctx.IsSet(utils.GetFlagName(utils.ContractPrepareInvokeFlag)) { - var preResult *httpcom.PreExecuteResult + var preResult *states.PreExecResult if vmtype == payload.NEOVM_TYPE { preResult, err = utils.PrepareInvokeNeoVMContract(contractAddr, params) diff --git a/cmd/utils/ont.go b/cmd/utils/ont.go index 269d4572f4..fecf72313b 100644 --- a/cmd/utils/ont.go +++ b/cmd/utils/ont.go @@ -34,9 +34,10 @@ import ( "github.com/ontio/ontology/core/types" cutils "github.com/ontio/ontology/core/utils" httpcom "github.com/ontio/ontology/http/base/common" - rpccommon "github.com/ontio/ontology/http/base/common" + "github.com/ontio/ontology/smartcontract/event" "github.com/ontio/ontology/smartcontract/service/native/ont" "github.com/ontio/ontology/smartcontract/service/native/utils" + states2 "github.com/ontio/ontology/smartcontract/states" "io" "math/rand" "sort" @@ -453,12 +454,12 @@ func SendRawTransactionData(txData string) (string, error) { return hexHash, nil } -func PrepareSendRawTransaction(txData string) (*rpccommon.PreExecuteResult, error) { +func PrepareSendRawTransaction(txData string) (*states2.PreExecResult, error) { data, ontErr := sendRpcRequest("sendrawtransaction", []interface{}{txData, 1}) if ontErr != nil { return nil, ontErr.Error } - preResult := &rpccommon.PreExecuteResult{} + preResult := &states2.PreExecResult{} err := json.Unmarshal(data, &preResult) if err != nil { return nil, fmt.Errorf("json.Unmarshal PreExecResult:%s error:%s", data, err) @@ -467,7 +468,7 @@ func PrepareSendRawTransaction(txData string) (*rpccommon.PreExecuteResult, erro } //GetSmartContractEvent return smart contract event execute by invoke transaction by hex string code -func GetSmartContractEvent(txHash string) (*rpccommon.ExecuteNotify, error) { +func GetSmartContractEvent(txHash string) (*event.ExecuteNotify, error) { data, ontErr := sendRpcRequest("getsmartcodeevent", []interface{}{txHash}) if ontErr != nil { switch ontErr.ErrorCode { @@ -476,7 +477,7 @@ func GetSmartContractEvent(txHash string) (*rpccommon.ExecuteNotify, error) { } return nil, ontErr.Error } - notifies := &rpccommon.ExecuteNotify{} + notifies := &event.ExecuteNotify{} err := json.Unmarshal(data, ¬ifies) if err != nil { return nil, fmt.Errorf("json.Unmarshal SmartContactEvent:%s error:%s", data, err) @@ -627,7 +628,7 @@ func PrepareDeployContract( cversion, cauthor, cemail, - cdesc string) (*httpcom.PreExecuteResult, error) { + cdesc string) (*states2.PreExecResult, error) { c, err := hex.DecodeString(code) if err != nil { return nil, fmt.Errorf("hex.DecodeString error:%s", err) @@ -692,7 +693,7 @@ func InvokeSmartContract(signer *account.Account, tx *types.MutableTransaction) func PrepareInvokeNeoVMContract( contractAddress common.Address, params []interface{}, -) (*rpccommon.PreExecuteResult, error) { +) (*states2.PreExecResult, error) { mutable, err := httpcom.NewNeovmInvokeTransaction(0, 0, contractAddress, params) if err != nil { return nil, err @@ -707,7 +708,7 @@ func PrepareInvokeNeoVMContract( return PrepareSendRawTransaction(txData) } -func PrepareInvokeCodeNeoVMContract(code []byte) (*rpccommon.PreExecuteResult, error) { +func PrepareInvokeCodeNeoVMContract(code []byte) (*states2.PreExecResult, error) { mutable, err := httpcom.NewSmartContractTransaction(0, 0, code) if err != nil { return nil, err @@ -721,7 +722,7 @@ func PrepareInvokeCodeNeoVMContract(code []byte) (*rpccommon.PreExecuteResult, e } //prepare invoke wasm -func PrepareInvokeWasmVMContract(contractAddress common.Address, params []interface{}) (*rpccommon.PreExecuteResult, error) { +func PrepareInvokeWasmVMContract(contractAddress common.Address, params []interface{}) (*states2.PreExecResult, error) { mutable, err := cutils.NewWasmVMInvokeTransaction(0, 0, contractAddress, params) if err != nil { return nil, err @@ -740,7 +741,7 @@ func PrepareInvokeNativeContract( contractAddress common.Address, version byte, method string, - params []interface{}) (*httpcom.PreExecuteResult, error) { + params []interface{}) (*states2.PreExecResult, error) { mutable, err := httpcom.NewNativeInvokeTransaction(0, 0, contractAddress, version, method, params) if err != nil { return nil, err diff --git a/http/base/common/common.go b/http/base/common/common.go index 21bfe244ba..4ed4e5fe26 100644 --- a/http/base/common/common.go +++ b/http/base/common/common.go @@ -36,7 +36,6 @@ import ( "github.com/ontio/ontology/smartcontract/event" "github.com/ontio/ontology/smartcontract/service/native/ont" "github.com/ontio/ontology/smartcontract/service/native/utils" - cstate "github.com/ontio/ontology/smartcontract/states" "github.com/ontio/ontology/vm/neovm" "io" "strings" @@ -67,25 +66,6 @@ type LogEventArgs struct { Message string } -type ExecuteNotify struct { - TxHash string - State byte - GasConsumed uint64 - Notify []NotifyEventInfo -} - -type PreExecuteResult struct { - State byte - Gas uint64 - Result interface{} - Notify []NotifyEventInfo -} - -type NotifyEventInfo struct { - ContractAddress string - States interface{} -} - type TxAttributeInfo struct { Usage types.TransactionAttributeUsage Data string @@ -178,23 +158,14 @@ func GetLogEvent(obj *event.LogEventArgs) (map[string]bool, LogEventArgs) { return contractAddrs, LogEventArgs{hash.ToHexString(), addr, obj.Message} } -func GetExecuteNotify(obj *event.ExecuteNotify) (map[string]bool, ExecuteNotify) { - evts := []NotifyEventInfo{} +func GetExecuteNotify(obj *event.ExecuteNotify) (map[string]bool, event.ExecuteNotify) { + evts := []*event.NotifyEventInfo{} var contractAddrs = make(map[string]bool) for _, v := range obj.Notify { - evts = append(evts, NotifyEventInfo{v.ContractAddress.ToHexString(), v.States}) + evts = append(evts, &event.NotifyEventInfo{v.ContractAddress, v.States}) contractAddrs[v.ContractAddress.ToHexString()] = true } - txhash := obj.TxHash.ToHexString() - return contractAddrs, ExecuteNotify{txhash, obj.State, obj.GasConsumed, evts} -} - -func ConvertPreExecuteResult(obj *cstate.PreExecResult) PreExecuteResult { - evts := []NotifyEventInfo{} - for _, v := range obj.Notify { - evts = append(evts, NotifyEventInfo{v.ContractAddress.ToHexString(), v.States}) - } - return PreExecuteResult{obj.State, obj.Gas, obj.Result, evts} + return contractAddrs, event.ExecuteNotify{obj.TxHash, obj.State, obj.GasConsumed, evts} } func TransArryByteToHexString(ptx *types.Transaction) *Transactions { diff --git a/http/base/rest/interfaces.go b/http/base/rest/interfaces.go index 221ef87376..49ae7d81ef 100644 --- a/http/base/rest/interfaces.go +++ b/http/base/rest/interfaces.go @@ -28,6 +28,7 @@ import ( bactor "github.com/ontio/ontology/http/base/actor" bcomn "github.com/ontio/ontology/http/base/common" berr "github.com/ontio/ontology/http/base/error" + "github.com/ontio/ontology/smartcontract/event" "github.com/ontio/ontology/smartcontract/service/native/utils" "strconv" ) @@ -278,7 +279,7 @@ func SendRawTransaction(cmd map[string]interface{}) map[string]interface{} { resp["Result"] = err.Error() return resp } - resp["Result"] = bcomn.ConvertPreExecuteResult(rst) + resp["Result"] = rst return resp } } @@ -317,7 +318,7 @@ func GetSmartCodeEventTxsByHeight(cmd map[string]interface{}) map[string]interfa } return ResponsePack(berr.INTERNAL_ERROR) } - eInfos := make([]*bcomn.ExecuteNotify, 0, len(eventInfos)) + eInfos := make([]*event.ExecuteNotify, 0, len(eventInfos)) for _, eventInfo := range eventInfos { _, notify := bcomn.GetExecuteNotify(eventInfo) eInfos = append(eInfos, ¬ify) diff --git a/http/base/rpc/interfaces.go b/http/base/rpc/interfaces.go index 3852e1a77c..b9c65e38b7 100644 --- a/http/base/rpc/interfaces.go +++ b/http/base/rpc/interfaces.go @@ -30,6 +30,7 @@ import ( bactor "github.com/ontio/ontology/http/base/actor" bcomn "github.com/ontio/ontology/http/base/common" berr "github.com/ontio/ontology/http/base/error" + "github.com/ontio/ontology/smartcontract/event" "github.com/ontio/ontology/smartcontract/service/native/utils" ) @@ -295,7 +296,7 @@ func SendRawTransaction(params []interface{}) map[string]interface{} { log.Infof("PreExec: ", err) return responsePack(berr.SMARTCODE_ERROR, err.Error()) } - return responseSuccess(bcomn.ConvertPreExecuteResult(result)) + return responseSuccess(result) } } } @@ -379,7 +380,7 @@ func GetSmartCodeEvent(params []interface{}) map[string]interface{} { } return responsePack(berr.INTERNAL_ERROR, "") } - eInfos := make([]*bcomn.ExecuteNotify, 0, len(eventInfos)) + eInfos := make([]*event.ExecuteNotify, 0, len(eventInfos)) for _, eventInfo := range eventInfos { _, notify := bcomn.GetExecuteNotify(eventInfo) eInfos = append(eInfos, ¬ify) diff --git a/smartcontract/event/notify_event_args.go b/smartcontract/event/notify_event_args.go index 32270b6d9f..7017a432f0 100644 --- a/smartcontract/event/notify_event_args.go +++ b/smartcontract/event/notify_event_args.go @@ -19,6 +19,7 @@ package event import ( + "encoding/json" "github.com/ontio/ontology/common" ) @@ -33,9 +34,80 @@ type NotifyEventInfo struct { States interface{} } +func (self NotifyEventInfo) MarshalJSON() ([]byte, error) { + type Internal struct { + ContractAddress string + States interface{} + } + in := Internal{ + ContractAddress: self.ContractAddress.ToHexString(), + States: self.States, + } + return json.Marshal(in) +} + +func (self *NotifyEventInfo) UnmarshalJSON(data []byte) error { + type Internal struct { + ContractAddress string + States interface{} + } + in := Internal{} + err := json.Unmarshal(data, &in) + if err != nil { + return err + } + self.States = in.States + contractAddr, err := common.AddressFromHexString(in.ContractAddress) + if err != nil { + return err + } + self.ContractAddress = contractAddr + return nil +} + type ExecuteNotify struct { TxHash common.Uint256 State byte GasConsumed uint64 Notify []*NotifyEventInfo } + +func (self ExecuteNotify) MarshalJSON() ([]byte, error) { + type Internal struct { + TxHash string + State byte + GasConsumed uint64 + Notify []*NotifyEventInfo + } + in := Internal{ + TxHash: self.TxHash.ToHexString(), + State: self.State, + GasConsumed: self.GasConsumed, + Notify: self.Notify, + } + return json.Marshal(in) +} + +func (self *ExecuteNotify) UnmarshalJSON(data []byte) error { + type Internal struct { + TxHash string + State byte + GasConsumed uint64 + Notify []*NotifyEventInfo + } + + in := Internal{} + err := json.Unmarshal(data, &in) + if err != nil { + return err + } + self.Notify = in.Notify + hash, err := common.Uint256FromHexString(in.TxHash) + if err != nil { + return err + } + self.TxHash = hash + self.GasConsumed = in.GasConsumed + self.State = in.State + return nil +} diff --git a/smartcontract/states/contract_test.go b/smartcontract/states/contract_test.go index d47f60dc28..c63d813c35 100644 --- a/smartcontract/states/contract_test.go +++ b/smartcontract/states/contract_test.go @@ -20,7 +20,11 @@ package states import ( "testing" + "encoding/json" + "fmt" "github.com/ontio/ontology/common" + "github.com/ontio/ontology/smartcontract/event" + "github.com/stretchr/testify/assert" ) func TestContract_Serialize_Deserialize(t *testing.T) { @@ -41,3 +45,30 @@ func TestContract_Serialize_Deserialize(t *testing.T) { t.Fatalf("ContractInvokeParam deserialize error: %v", err) } } + +func TestContractInvokeParam_Deserialization(t *testing.T) { + evts := make([]*event.NotifyEventInfo, 0) + contractAddr := common.AddressFromVmCode([]byte{0, 1}) + e := event.NotifyEventInfo{ + ContractAddress: contractAddr, + States: []interface{}{"tranfer", 10}, + } + evts = append(evts, &e) + per := PreExecResult{ + State: 1, + Gas: 20000, + Result: "result", + Notify: evts, + } + bs, _ := json.Marshal(per) + fmt.Println(string(bs)) + + per2 := PreExecResult{} + err := json.Unmarshal(bs, &per2) + + assert.Nil(t, err) + + bs2, _ := json.Marshal(per2) + + assert.Equal(t, string(bs), string(bs2)) +}