Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl MarshalJSON and UnmarshalJSON for NotifyEventInfo and ExecuteNotify #1160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/contract_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
21 changes: 11 additions & 10 deletions cmd/utils/ont.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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, &notifies)
if err != nil {
return nil, fmt.Errorf("json.Unmarshal SmartContactEvent:%s error:%s", data, err)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
37 changes: 4 additions & 33 deletions http/base/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions http/base/rest/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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, &notify)
Expand Down
5 changes: 3 additions & 2 deletions http/base/rpc/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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, &notify)
Expand Down
72 changes: 72 additions & 0 deletions smartcontract/event/notify_event_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package event

import (
"encoding/json"
"github.com/ontio/ontology/common"
)

Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest moving out

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
}
31 changes: 31 additions & 0 deletions smartcontract/states/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
}