From 8ba45563dac62b5627be894291ff12f1b8bb898d Mon Sep 17 00:00:00 2001 From: Daniel Liu <33597115+skyinglyh1@users.noreply.github.com> Date: Thu, 23 Jul 2020 11:48:13 +0800 Subject: [PATCH] support getCrossChainMsg and getCrossStateProof through rpc client, upgrade ontology to v1.10.0, ontology-crypto to v1.0.9 (#116) --- client/client.go | 25 +++++++++++++++++++++++++ client/define.go | 4 ++++ client/rest.go | 8 ++++++++ client/rpc.go | 8 ++++++++ client/ws.go | 8 ++++++++ go.mod | 6 +++--- utils/api.go | 19 +++++++++++++++++++ 7 files changed, 75 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 0553fa7..de67c17 100644 --- a/client/client.go +++ b/client/client.go @@ -25,6 +25,7 @@ import ( "github.com/ontio/ontology/common" "github.com/ontio/ontology/core/payload" "github.com/ontio/ontology/core/types" + bc "github.com/ontio/ontology/http/base/common" "sync/atomic" "time" ) @@ -237,6 +238,30 @@ func (this *ClientMgr) GetMerkleProof(txHash string) (*sdkcom.MerkleProof, error return utils.GetMerkleProof(data) } +func (this *ClientMgr) GetCrossStatesProof(height uint32, key []byte) (*bc.CrossStatesProof, error) { + client := this.getClient() + if client == nil { + return nil, fmt.Errorf("don't have available client of ontology") + } + data, err := client.getCrossStatesProof(this.getNextQid(), height, key) + if err != nil { + return nil, err + } + return utils.GetCrossStatesProof(data) +} + +func (this *ClientMgr) GetCrossChainMsg(height uint32) (string, error) { + client := this.getClient() + if client == nil { + return "", fmt.Errorf("don't have available client of ontology") + } + data, err := client.getCrossChainMsg(this.getNextQid(), height) + if err != nil { + return "", err + } + return utils.GetCrossChainMsg(data) +} + func (this *ClientMgr) GetMemPoolTxState(txHash string) (*sdkcom.MemPoolTxState, error) { client := this.getClient() if client == nil { diff --git a/client/define.go b/client/define.go index 5916ebe..6b967fe 100644 --- a/client/define.go +++ b/client/define.go @@ -40,6 +40,8 @@ type OntologyClient interface { getSmartContractEventByBlock(qid string, blockHeight uint32) ([]byte, error) getStorage(qid, contractAddress string, key []byte) ([]byte, error) getMerkleProof(qid, txHash string) ([]byte, error) + getCrossStatesProof(qid string, height uint32, key []byte) ([]byte, error) + getCrossChainMsg(qid string, height uint32) ([]byte, error) getMemPoolTxState(qid, txHash string) ([]byte, error) getMemPoolTxCount(qid string) ([]byte, error) sendRawTransaction(qid string, tx *types.Transaction, isPreExec bool) ([]byte, error) @@ -59,6 +61,8 @@ const ( RPC_GET_SMART_CONTRACT = "getcontractstate" RPC_GET_GENERATE_BLOCK_TIME = "getgenerateblocktime" RPC_GET_MERKLE_PROOF = "getmerkleproof" + RPC_GET_CROSS_STATES_PROOF = "getcrossstatesproof" + RPC_GET_CROSS_CHAIN_MSG = "getcrosschainmsg" RPC_GET_NETWORK_ID = "getnetworkid" RPC_GET_MEM_POOL_TX_COUNT = "getmempooltxcount" RPC_GET_MEM_POOL_TX_STATE = "getmempooltxstate" diff --git a/client/rest.go b/client/rest.go index d3daef5..2ae6b2d 100644 --- a/client/rest.go +++ b/client/rest.go @@ -155,6 +155,14 @@ func (this RestClient) getMerkleProof(qid, txHash string) ([]byte, error) { return this.sendRestGetRequest(reqPath) } +func (this *RestClient) getCrossStatesProof(qid string, height uint32, key []byte) ([]byte, error) { + return nil, fmt.Errorf("getCrossStateProof not supported by RestClient") +} + +func (this *RestClient) getCrossChainMsg(qid string, height uint32) ([]byte, error) { + return nil, fmt.Errorf("getCrossChainMsg not supported by RestClient") +} + func (this *RestClient) getMemPoolTxState(qid, txHash string) ([]byte, error) { reqPath := GET_MEMPOOL_TXSTATE + txHash return this.sendRestGetRequest(reqPath) diff --git a/client/rpc.go b/client/rpc.go index c908e3b..3dfc598 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -146,6 +146,14 @@ func (this *RpcClient) getMerkleProof(qid, txHash string) ([]byte, error) { return this.sendRpcRequest(qid, RPC_GET_MERKLE_PROOF, []interface{}{txHash}) } +func (this *RpcClient) getCrossStatesProof(qid string, height uint32, key []byte) ([]byte, error) { + return this.sendRpcRequest(qid, RPC_GET_CROSS_STATES_PROOF, []interface{}{height, hex.EncodeToString(key)}) +} + +func (this *RpcClient) getCrossChainMsg(qid string, height uint32) ([]byte, error) { + return this.sendRpcRequest(qid, RPC_GET_CROSS_CHAIN_MSG, []interface{}{height}) +} + func (this *RpcClient) getMemPoolTxState(qid, txHash string) ([]byte, error) { return this.sendRpcRequest(qid, RPC_GET_MEM_POOL_TX_STATE, []interface{}{txHash}) } diff --git a/client/ws.go b/client/ws.go index cde3d92..9d1a617 100644 --- a/client/ws.go +++ b/client/ws.go @@ -657,6 +657,14 @@ func (this *WSClient) getMerkleProof(qid, txHash string) ([]byte, error) { return this.sendSyncWSRequest(qid, WS_ACTION_GET_MERKLE_PROOF, map[string]interface{}{"Hash": txHash}) } +func (this *WSClient) getCrossStatesProof(qid string, height uint32, key []byte) ([]byte, error) { + return nil, fmt.Errorf("getCrossStateProof not supported by WSClient") +} + +func (this *WSClient) getCrossChainMsg(qid string, height uint32) ([]byte, error) { + return nil, fmt.Errorf("getCrossChainMsg not supported by WSClient") +} + func (this *WSClient) getSmartContractEvent(qid, txHash string) ([]byte, error) { return this.sendSyncWSRequest(qid, WS_ACTION_GET_SMARTCONTRACT_BY_HASH, map[string]interface{}{"Hash": txHash}) } diff --git a/go.mod b/go.mod index 068272e..edbc2b2 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,11 @@ require ( github.com/gorilla/websocket v1.4.1 github.com/itchyny/base58-go v0.1.0 github.com/ontio/go-bip32 v0.0.0-20190520025953-d3cea6894a2b - github.com/ontio/ontology v1.8.2 - github.com/ontio/ontology-crypto v1.0.7 github.com/satori/go.uuid v1.2.0 + github.com/ontio/ontology v1.10.0 + github.com/ontio/ontology-crypto v1.0.9 github.com/stretchr/testify v1.4.0 github.com/tyler-smith/go-bip39 v1.0.2 - golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 + golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4 launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect ) diff --git a/utils/api.go b/utils/api.go index 0ba2d88..f2b688a 100644 --- a/utils/api.go +++ b/utils/api.go @@ -25,6 +25,7 @@ import ( "github.com/ontio/ontology/common" "github.com/ontio/ontology/core/payload" "github.com/ontio/ontology/core/types" + bc "github.com/ontio/ontology/http/base/common" ) func GetVersion(data []byte) (string, error) { @@ -173,6 +174,24 @@ func GetMerkleProof(data []byte) (*sdkcom.MerkleProof, error) { return proof, nil } +func GetCrossStatesProof(data []byte) (*bc.CrossStatesProof, error) { + proof := &bc.CrossStatesProof{} + err := json.Unmarshal(data, proof) + if err != nil { + return nil, fmt.Errorf("json.Unmarshal error:%s", err) + } + return proof, nil +} + +func GetCrossChainMsg(data []byte) (string, error) { + crossChainMsg := "" + err := json.Unmarshal(data, &crossChainMsg) + if err != nil { + return "", fmt.Errorf("json.Unmarshal error:%s", err) + } + return crossChainMsg, nil +} + func GetBlockTxHashes(data []byte) (*sdkcom.BlockTxHashes, error) { blockTxHashesStr := &sdkcom.BlockTxHashesStr{} err := json.Unmarshal(data, &blockTxHashesStr)