From 39d904811d2583312a8a166329d44fd61d868654 Mon Sep 17 00:00:00 2001 From: lucas7788 Date: Tue, 25 Feb 2020 18:27:34 +0800 Subject: [PATCH 1/3] impl MarshalJSON and UnmarshalJSON for NotifyEventInfo and ExecuteNotify --- cmd/utils/ont.go | 6 +- http/base/common/common.go | 37 ++---------- http/base/rest/interfaces.go | 5 +- http/base/rpc/interfaces.go | 5 +- smartcontract/event/notify_event_args.go | 72 ++++++++++++++++++++++++ smartcontract/states/contract_test.go | 31 ++++++++++ 6 files changed, 116 insertions(+), 40 deletions(-) diff --git a/cmd/utils/ont.go b/cmd/utils/ont.go index 269d4572f4..2ff0159a92 100644 --- a/cmd/utils/ont.go +++ b/cmd/utils/ont.go @@ -34,7 +34,6 @@ 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/service/native/ont" "github.com/ontio/ontology/smartcontract/service/native/utils" "io" @@ -43,6 +42,7 @@ import ( "strconv" "strings" "time" + "github.com/ontio/ontology/smartcontract/event" ) const ( @@ -467,7 +467,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 +476,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) 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..77c74f7711 100644 --- a/http/base/rest/interfaces.go +++ b/http/base/rest/interfaces.go @@ -30,6 +30,7 @@ import ( berr "github.com/ontio/ontology/http/base/error" "github.com/ontio/ontology/smartcontract/service/native/utils" "strconv" + "github.com/ontio/ontology/smartcontract/event" ) const TLS_PORT int = 443 @@ -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..3e7f9d8c75 100644 --- a/smartcontract/states/contract_test.go +++ b/smartcontract/states/contract_test.go @@ -21,6 +21,10 @@ import ( "testing" "github.com/ontio/ontology/common" + "github.com/ontio/ontology/smartcontract/event" + "encoding/json" + "fmt" + "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)) +} \ No newline at end of file From f334bb39a43ec47f708513c0c23b77346ff3e9d6 Mon Sep 17 00:00:00 2001 From: lucas7788 Date: Tue, 25 Feb 2020 18:55:51 +0800 Subject: [PATCH 2/3] replace httpcommon.PreExecuteResult with states.PreExecResult --- cmd/contract_cmd.go | 3 ++- cmd/utils/ont.go | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) 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 2ff0159a92..fecf72313b 100644 --- a/cmd/utils/ont.go +++ b/cmd/utils/ont.go @@ -34,15 +34,16 @@ import ( "github.com/ontio/ontology/core/types" cutils "github.com/ontio/ontology/core/utils" httpcom "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" "strconv" "strings" "time" - "github.com/ontio/ontology/smartcontract/event" ) const ( @@ -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) @@ -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 From 8f041044963a415c7b277a2c404582596d51fef8 Mon Sep 17 00:00:00 2001 From: lucas7788 Date: Wed, 26 Feb 2020 17:39:39 +0800 Subject: [PATCH 3/3] go fmt --- http/base/rest/interfaces.go | 2 +- smartcontract/states/contract_test.go | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/http/base/rest/interfaces.go b/http/base/rest/interfaces.go index 77c74f7711..49ae7d81ef 100644 --- a/http/base/rest/interfaces.go +++ b/http/base/rest/interfaces.go @@ -28,9 +28,9 @@ 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" - "github.com/ontio/ontology/smartcontract/event" ) const TLS_PORT int = 443 diff --git a/smartcontract/states/contract_test.go b/smartcontract/states/contract_test.go index 3e7f9d8c75..c63d813c35 100644 --- a/smartcontract/states/contract_test.go +++ b/smartcontract/states/contract_test.go @@ -20,10 +20,10 @@ package states import ( "testing" - "github.com/ontio/ontology/common" - "github.com/ontio/ontology/smartcontract/event" "encoding/json" "fmt" + "github.com/ontio/ontology/common" + "github.com/ontio/ontology/smartcontract/event" "github.com/stretchr/testify/assert" ) @@ -50,25 +50,25 @@ 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}, + ContractAddress: contractAddr, + States: []interface{}{"tranfer", 10}, } evts = append(evts, &e) per := PreExecResult{ - State:1, - Gas:20000, - Result:"result", - Notify:evts, + State: 1, + Gas: 20000, + Result: "result", + Notify: evts, } bs, _ := json.Marshal(per) fmt.Println(string(bs)) - per2 := PreExecResult{} + per2 := PreExecResult{} err := json.Unmarshal(bs, &per2) - assert.Nil(t, err) + assert.Nil(t, err) bs2, _ := json.Marshal(per2) assert.Equal(t, string(bs), string(bs2)) -} \ No newline at end of file +}