From 299f98f41d17a4a02472a2c135931e6e807055cc Mon Sep 17 00:00:00 2001 From: laizy Date: Thu, 23 May 2024 14:20:11 +0800 Subject: [PATCH] add method to get raw transaction data from mempool --- http/base/common/common.go | 5 +-- http/base/rest/interfaces.go | 55 ++++++++++++++-------------- http/jsonrpc/interfaces.go | 69 +++++++++++++++++++----------------- 3 files changed, 68 insertions(+), 61 deletions(-) diff --git a/http/base/common/common.go b/http/base/common/common.go index 2d1dbc63d..ab8ddac45 100644 --- a/http/base/common/common.go +++ b/http/base/common/common.go @@ -198,6 +198,7 @@ type TXNAttrInfo struct { type TXNEntryInfo struct { State []TXNAttrInfo // the result from each validator + Raw string } func GetLogEvent(obj *event.LogEventArgs) (map[string]bool, LogEventArgs) { @@ -610,7 +611,7 @@ func GetBlockTransactions(block *types.Block) interface{} { return b } -//NewNativeInvokeTransaction return native contract invoke transaction +// NewNativeInvokeTransaction return native contract invoke transaction func NewNativeInvokeTransaction(gasPirce, gasLimit uint64, contractAddress common.Address, version byte, method string, params []interface{}) (*types.MutableTransaction, error) { invokeCode, err := cutils.BuildNativeInvokeCode(contractAddress, version, method, params) @@ -643,7 +644,7 @@ func NewSmartContractTransaction(gasPrice, gasLimit uint64, invokeCode []byte) ( return tx, nil } -//BuildNeoVMInvokeCode build NeoVM Invoke code for params +// BuildNeoVMInvokeCode build NeoVM Invoke code for params func BuildNeoVMInvokeCode(smartContractAddress common.Address, params []interface{}) ([]byte, error) { builder := neovm.NewParamsBuilder(new(bytes.Buffer)) err := cutils.BuildNeoVMParam(builder, params) diff --git a/http/base/rest/interfaces.go b/http/base/rest/interfaces.go index e238b50cb..b65ba1cde 100644 --- a/http/base/rest/interfaces.go +++ b/http/base/rest/interfaces.go @@ -19,6 +19,7 @@ package rest import ( + "encoding/hex" "strconv" "github.com/ontio/ontology/common" @@ -54,7 +55,7 @@ func GetNetworkId(cmd map[string]interface{}) map[string]interface{} { return resp } -//get connection node count +// get connection node count func GetConnectionCount(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) count := bactor.GetConnectionCnt() @@ -72,7 +73,7 @@ func GetNodeSyncStatus(cmd map[string]interface{}) map[string]interface{} { return resp } -//get block height +// get block height func GetBlockHeight(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) height := bactor.GetCurrentBlockHeight() @@ -80,7 +81,7 @@ func GetBlockHeight(cmd map[string]interface{}) map[string]interface{} { return resp } -//get block hash by height +// get block hash by height func GetBlockHash(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) param, ok := cmd["Height"].(string) @@ -116,7 +117,7 @@ func getBlock(hash common.Uint256, getTxBytes bool) (interface{}, int64) { return bcomn.GetBlockInfo(block), berr.SUCCESS } -//get block by hash +// get block by hash func GetBlockByHash(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str := cmd["Hash"].(string) @@ -136,7 +137,7 @@ func GetBlockByHash(cmd map[string]interface{}) map[string]interface{} { return resp } -//get block height by transaction hash +// get block height by transaction hash func GetBlockHeightByTxHash(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str, ok := cmd["Hash"].(string) @@ -158,7 +159,7 @@ func GetBlockHeightByTxHash(cmd map[string]interface{}) map[string]interface{} { return resp } -//get block transaction hashes by height +// get block transaction hashes by height func GetBlockTxsByHeight(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) @@ -183,7 +184,7 @@ func GetBlockTxsByHeight(cmd map[string]interface{}) map[string]interface{} { return resp } -//get block by height +// get block by height func GetBlockByHeight(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) @@ -215,7 +216,7 @@ func GetBlockByHeight(cmd map[string]interface{}) map[string]interface{} { return resp } -//get transaction by hash +// get transaction by hash func GetTransactionByHash(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) @@ -247,7 +248,7 @@ func GetTransactionByHash(cmd map[string]interface{}) map[string]interface{} { return resp } -//send raw transaction +// send raw transaction func SendRawTransaction(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) @@ -291,7 +292,7 @@ func SendRawTransaction(cmd map[string]interface{}) map[string]interface{} { return resp } -//get smartcontract event by height +// get smartcontract event by height func GetSmartCodeEventTxsByHeight(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) @@ -323,7 +324,7 @@ func GetSmartCodeEventTxsByHeight(cmd map[string]interface{}) map[string]interfa return resp } -//get smartcontract event by transaction hash +// get smartcontract event by transaction hash func GetSmartCodeEventByTxHash(cmd map[string]interface{}) map[string]interface{} { if !config.DefConfig.Common.EnableEventLog { return ResponsePack(berr.INVALID_METHOD) @@ -354,7 +355,7 @@ func GetSmartCodeEventByTxHash(cmd map[string]interface{}) map[string]interface{ return resp } -//get contract state +// get contract state func GetContractState(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str, ok := cmd["Hash"].(string) @@ -382,7 +383,7 @@ func GetContractState(cmd map[string]interface{}) map[string]interface{} { return resp } -//get storage from contract +// get storage from contract func GetStorage(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str, ok := cmd["Hash"].(string) @@ -409,7 +410,7 @@ func GetStorage(cmd map[string]interface{}) map[string]interface{} { return resp } -//get balance of address +// get balance of address func GetBalance(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) addrBase58, ok := cmd["Addr"].(string) @@ -428,7 +429,7 @@ func GetBalance(cmd map[string]interface{}) map[string]interface{} { return resp } -//get balance of address +// get balance of address func GetBalanceV2(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) addrBase58, ok := cmd["Addr"].(string) @@ -447,7 +448,7 @@ func GetBalanceV2(cmd map[string]interface{}) map[string]interface{} { return resp } -//get merkle proof by transaction hash +// get merkle proof by transaction hash func GetMerkleProof(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str, ok := cmd["Hash"].(string) @@ -488,7 +489,7 @@ func GetMerkleProof(cmd map[string]interface{}) map[string]interface{} { return resp } -//get avg gas price in block +// get avg gas price in block func GetGasPrice(cmd map[string]interface{}) map[string]interface{} { gasPrice, height, err := bcomn.GetGasPrice() if err != nil { @@ -500,7 +501,7 @@ func GetGasPrice(cmd map[string]interface{}) map[string]interface{} { return resp } -//get allowance +// get allowance func GetAllowance(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) asset, ok := cmd["Asset"].(string) @@ -531,7 +532,7 @@ func GetAllowance(cmd map[string]interface{}) map[string]interface{} { return resp } -//get allowance +// get allowance func GetAllowanceV2(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) asset, ok := cmd["Asset"].(string) @@ -562,7 +563,7 @@ func GetAllowanceV2(cmd map[string]interface{}) map[string]interface{} { return resp } -//get unbound ong +// get unbound ong func GetUnboundOng(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) toAddrStr, ok := cmd["Addr"].(string) @@ -582,7 +583,7 @@ func GetUnboundOng(cmd map[string]interface{}) map[string]interface{} { return resp } -//get grant ong +// get grant ong func GetGrantOng(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) toAddrStr, ok := cmd["Addr"].(string) @@ -601,7 +602,7 @@ func GetGrantOng(cmd map[string]interface{}) map[string]interface{} { return resp } -//get memory pool transaction count +// get memory pool transaction count func GetMemPoolTxCount(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) count := bactor.GetTxnCount() @@ -609,7 +610,7 @@ func GetMemPoolTxCount(cmd map[string]interface{}) map[string]interface{} { return resp } -//get memory pool transaction hash list +// get memory pool transaction hash list func GetMemPoolTxHashList(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) txHashList := bactor.GetTxnHashList() @@ -617,7 +618,7 @@ func GetMemPoolTxHashList(cmd map[string]interface{}) map[string]interface{} { return resp } -//get memory poll transaction state +// get memory poll transaction state func GetMemPoolTxState(cmd map[string]interface{}) map[string]interface{} { resp := ResponsePack(berr.SUCCESS) str, ok := cmd["Hash"].(string) @@ -632,10 +633,10 @@ func GetMemPoolTxState(cmd map[string]interface{}) map[string]interface{} { if err != nil { return ResponsePack(berr.UNKNOWN_TRANSACTION) } - attrs := []bcomn.TXNAttrInfo{} + var attrs []bcomn.TXNAttrInfo for _, t := range txEntry.Attrs { - attrs = append(attrs, bcomn.TXNAttrInfo{t.Height, int(t.Type), int(t.ErrCode)}) + attrs = append(attrs, bcomn.TXNAttrInfo{Height: t.Height, Type: int(t.Type), ErrCode: int(t.ErrCode)}) } - resp["Result"] = bcomn.TXNEntryInfo{attrs} + resp["Result"] = bcomn.TXNEntryInfo{State: attrs, Raw: hex.EncodeToString(txEntry.Tx.ToArray())} return resp } diff --git a/http/jsonrpc/interfaces.go b/http/jsonrpc/interfaces.go index e2675f369..ab85ab9d4 100644 --- a/http/jsonrpc/interfaces.go +++ b/http/jsonrpc/interfaces.go @@ -35,7 +35,7 @@ import ( "github.com/ontio/ontology/smartcontract/service/native/utils" ) -//get best block hash +// get best block hash func GetBestBlockHash(params []interface{}) map[string]interface{} { hash := bactor.CurrentBlockHash() return rpc.ResponseSuccess(hash.ToHexString()) @@ -43,8 +43,9 @@ func GetBestBlockHash(params []interface{}) map[string]interface{} { // get block by height or hash // Input JSON string examples for getblock method as following: -// {"jsonrpc": "2.0", "method": "getblock", "params": [1], "id": 0} -// {"jsonrpc": "2.0", "method": "getblock", "params": ["aabbcc.."], "id": 0} +// +// {"jsonrpc": "2.0", "method": "getblock", "params": [1], "id": 0} +// {"jsonrpc": "2.0", "method": "getblock", "params": ["aabbcc.."], "id": 0} func GetBlock(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -87,15 +88,16 @@ func GetBlock(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(common.ToHexString(block.ToArray())) } -//get block height +// get block height func GetBlockCount(params []interface{}) map[string]interface{} { height := bactor.GetCurrentBlockHeight() return rpc.ResponseSuccess(height + 1) } -//get block hash +// get block hash // A JSON example for getblockhash method as following: -// {"jsonrpc": "2.0", "method": "getblockhash", "params": [1], "id": 0} +// +// {"jsonrpc": "2.0", "method": "getblockhash", "params": [1], "id": 0} func GetBlockHash(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -113,13 +115,13 @@ func GetBlockHash(params []interface{}) map[string]interface{} { } } -//get node connection count +// get node connection count func GetConnectionCount(params []interface{}) map[string]interface{} { count := bactor.GetConnectionCnt() return rpc.ResponseSuccess(count) } -//get node connection most height +// get node connection most height func GetSyncStatus(params []interface{}) map[string]interface{} { status, err := bcomn.GetSyncStatus() if err != nil { @@ -130,19 +132,19 @@ func GetSyncStatus(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(status) } -//get memory pool transaction count +// get memory pool transaction count func GetMemPoolTxCount(params []interface{}) map[string]interface{} { count := bactor.GetTxnCount() return rpc.ResponseSuccess(count) } -//get memory pool transaction hash +// get memory pool transaction hash func GetMemPoolTxHashList(params []interface{}) map[string]interface{} { txHashList := bactor.GetTxnHashList() return rpc.ResponseSuccess(txHashList) } -//get memory pool transaction state +// get memory pool transaction state func GetMemPoolTxState(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -160,9 +162,9 @@ func GetMemPoolTxState(params []interface{}) map[string]interface{} { } var attrs []bcomn.TXNAttrInfo for _, t := range txEntry.Attrs { - attrs = append(attrs, bcomn.TXNAttrInfo{t.Height, int(t.Type), int(t.ErrCode)}) + attrs = append(attrs, bcomn.TXNAttrInfo{Height: t.Height, Type: int(t.Type), ErrCode: int(t.ErrCode)}) } - info := bcomn.TXNEntryInfo{attrs} + info := bcomn.TXNEntryInfo{State: attrs, Raw: hex.EncodeToString(txEntry.Tx.ToArray())} return rpc.ResponseSuccess(info) default: return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -171,7 +173,8 @@ func GetMemPoolTxState(params []interface{}) map[string]interface{} { // get raw transaction in raw or json // A JSON example for getrawtransaction method as following: -// {"jsonrpc": "2.0", "method": "getrawtransaction", "params": ["transactioin hash in hex"], "id": 0} +// +// {"jsonrpc": "2.0", "method": "getrawtransaction", "params": ["transactioin hash in hex"], "id": 0} func GetRawTransaction(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -212,8 +215,9 @@ func GetRawTransaction(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(common.ToHexString(common.SerializeToBytes(tx))) } -//get storage from contract -// {"jsonrpc": "2.0", "method": "getstorage", "params": ["code hash", "key"], "id": 0} +// get storage from contract +// +// {"jsonrpc": "2.0", "method": "getstorage", "params": ["code hash", "key"], "id": 0} func GetStorage(params []interface{}) map[string]interface{} { if len(params) < 2 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -254,9 +258,10 @@ func GetStorage(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(common.ToHexString(value)) } -//send raw transaction +// send raw transaction // A JSON example for sendrawtransaction method as following: -// {"jsonrpc": "2.0", "method": "sendrawtransaction", "params": ["raw transactioin in hex"], "id": 0} +// +// {"jsonrpc": "2.0", "method": "sendrawtransaction", "params": ["raw transactioin in hex"], "id": 0} func SendRawTransaction(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -299,7 +304,7 @@ func SendRawTransaction(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(hash.ToHexString()) } -//get node version +// get node version func GetNodeVersion(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(config.Version) } @@ -309,7 +314,7 @@ func GetNetworkId(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(config.DefConfig.P2PNode.NetworkId) } -//get contract state +// get contract state func GetContractState(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -346,7 +351,7 @@ func GetContractState(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(common.ToHexString(sink.Bytes())) } -//get smartconstract event +// get smartconstract event func GetSmartCodeEvent(params []interface{}) map[string]interface{} { if !config.DefConfig.Common.EnableEventLog { return rpc.ResponsePack(berr.INVALID_METHOD, "") @@ -394,7 +399,7 @@ func GetSmartCodeEvent(params []interface{}) map[string]interface{} { return rpc.ResponsePack(berr.INVALID_PARAMS, "") } -//get block height by transaction hash +// get block height by transaction hash func GetBlockHeightByTxHash(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -419,7 +424,7 @@ func GetBlockHeightByTxHash(params []interface{}) map[string]interface{} { return rpc.ResponsePack(berr.INVALID_PARAMS, "") } -//get balance of address +// get balance of address func GetBalance(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -439,7 +444,7 @@ func GetBalance(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(rsp) } -//get balance of address +// get balance of address func GetBalanceV2(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -459,7 +464,7 @@ func GetBalanceV2(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(rsp) } -//get balance of address +// get balance of address func GetOep4Balance(params []interface{}) map[string]interface{} { if len(params) < 2 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -496,7 +501,7 @@ func parseAddressParam(params []interface{}) ([]common.Address, error) { return res, nil } -//get allowance +// get allowance func GetAllowance(params []interface{}) map[string]interface{} { if len(params) < 3 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -528,7 +533,7 @@ func GetAllowance(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(rsp) } -//get allowance +// get allowance func GetAllowanceV2(params []interface{}) map[string]interface{} { if len(params) < 3 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -560,7 +565,7 @@ func GetAllowanceV2(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(rsp) } -//get merkle proof by transaction hash +// get merkle proof by transaction hash func GetMerkleProof(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -599,7 +604,7 @@ func GetMerkleProof(params []interface{}) map[string]interface{} { curHeader.BlockRoot.ToHexString(), curHeight, hashes}) } -//get block transactions by height +// get block transactions by height func GetBlockTxsByHeight(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil) @@ -621,7 +626,7 @@ func GetBlockTxsByHeight(params []interface{}) map[string]interface{} { } } -//get gas price in block +// get gas price in block func GetGasPrice(params []interface{}) map[string]interface{} { gasPrice, height, err := bcomn.GetGasPrice() if err != nil { @@ -672,7 +677,7 @@ func GetGrantOng(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(rsp) } -//get cross chain message by height +// get cross chain message by height func GetCrossChainMsg(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, "") @@ -714,7 +719,7 @@ func GetCrossStatesLeafHashes(params []interface{}) map[string]interface{} { return rpc.ResponseSuccess(bcomn.CrossStatesLeafHashes{Height: uint32(height), Hashes: hexHashes}) } -//get cross chain state proof +// get cross chain state proof func GetCrossStatesProof(params []interface{}) map[string]interface{} { if len(params) < 1 { return rpc.ResponsePack(berr.INVALID_PARAMS, nil)