From db8c3d5604d05c8109b3a4c2ecfb5cb12e72623e Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Mon, 8 Jan 2024 11:19:23 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat=EF=BC=9Aadd=20trace=5Ftx=20for=20evm?= =?UTF-8?q?=20to=20get=20inter=5Ftx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blockchain/blockchain.go | 2 +- blockchain/config/config.go | 1 + blockchain/service.go | 1 + blockchain/service/bnb/bnb.go | 70 ++++++++++++++++++---- blockchain/service/btc/btc.go | 4 +- blockchain/service/ether/ether.go | 72 ++++++++++++++++++----- blockchain/service/ether/ether_test.go | 16 +++++ blockchain/service/filecoin/filecoin.go | 8 +-- blockchain/service/polygon/polygon_pos.go | 72 ++++++++++++++++++----- blockchain/service/tron/tron.go | 12 ++-- blockchain/service/xrp/xrp.go | 6 +- 11 files changed, 209 insertions(+), 55 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 0602dd9..2040f5d 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -26,5 +26,5 @@ type ChainNet interface { } type ChainCluster interface { - BalanceCluster() *config.NodeCluster + BalanceCluster(trace bool) *config.NodeCluster } diff --git a/blockchain/config/config.go b/blockchain/config/config.go index 1ecc72f..ad2dc9d 100644 --- a/blockchain/config/config.go +++ b/blockchain/config/config.go @@ -22,4 +22,5 @@ type NodeCluster struct { Weight int64 `json:"Weight"` ErrorCount int64 `json:"ErrorCount"` Utxo string `json:"Utxo"` + Trace bool `json:"Trace"` } diff --git a/blockchain/service.go b/blockchain/service.go index bd31768..1408acc 100644 --- a/blockchain/service.go +++ b/blockchain/service.go @@ -40,6 +40,7 @@ type API interface { type ExApi interface { GasPrice(chainCode int64) (string, error) + TraceTransaction(chainCode int64, address string) (string, error) EstimateGas(chainCode int64, from, to, data string) (string, error) EstimateGasForTron(chainCode int64, from, to, functionSelector, parameter string) (string, error) GetAccountResourceForTron(chainCode int64, address string) (string, error) diff --git a/blockchain/service/bnb/bnb.go b/blockchain/service/bnb/bnb.go index ae45708..76eac0e 100644 --- a/blockchain/service/bnb/bnb.go +++ b/blockchain/service/bnb/bnb.go @@ -24,7 +24,7 @@ type Bnb struct { } func (e *Bnb) TokenURI(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -38,7 +38,7 @@ func (e *Bnb) TokenURI(chainCode int64, contractAddress string, tokenId string, } func (e *Bnb) BalanceOf(chainCode int64, contractAddress string, address string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -52,7 +52,7 @@ func (e *Bnb) BalanceOf(chainCode int64, contractAddress string, address string, } func (e *Bnb) OwnerOf(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -66,7 +66,7 @@ func (e *Bnb) OwnerOf(chainCode int64, contractAddress string, tokenId string, e } func (e *Bnb) TotalSupply(chainCode int64, contractAddress string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -80,7 +80,7 @@ func (e *Bnb) TotalSupply(chainCode int64, contractAddress string, eip int64) (s } func (e *Bnb) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -345,6 +345,20 @@ func (e *Bnb) MonitorCluster() any { return e.nodeCluster } +func (e *Bnb) TraceTransaction(chainCode int64, address string) (string, error) { + req := `{ + "id": 1, + "jsonrpc": "2.0", + "params": [ + "%v" + ], + "method": "trace_transaction" + }` + req = fmt.Sprintf(req, address) + + return e.SendReq2(chainCode, req) +} + func (e *Bnb) Balance(chainCode int64, address string, tag string) (string, error) { start := time.Now() defer func() { @@ -372,7 +386,7 @@ func (e *Bnb) TokenBalance(chainCode int64, address string, contractAddr string, defer func() { e.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -487,7 +501,7 @@ func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error) e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -502,14 +516,39 @@ func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error) return resp, err } -func (e *Bnb) BalanceCluster() *config.NodeCluster { +func (e *Bnb) SendReq2(blockChain int64, reqBody string) (resp string, err error) { + reqBody = strings.Replace(reqBody, "\t", "", -1) + reqBody = strings.Replace(reqBody, "\n", "", -1) + var uri string + defer func() { + if err != nil { + e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) + } else { + e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") + } + }() + cluster := e.BalanceCluster(true) + if cluster == nil { + //不存在节点 + return "", errors.New("blockchain node has not found") + } + uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) + + resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) + if err != nil { + cluster.ErrorCount += 1 + } + return resp, err +} + +func (e *Bnb) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) if l > 1 { //如果有多个节点,则根据权重计算 mp := make(map[string][]int64, 0) - originCluster := make(map[string]*config.NodeCluster, 0) + originCluster := make(map[string]*config.NodeCluster, 1) var sum int64 for _, v := range e.nodeCluster { @@ -517,10 +556,12 @@ func (e *Bnb) BalanceCluster() *config.NodeCluster { //如果没有设置weight,则默认设定5 v.Weight = 5 } - sum += v.Weight - key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) - mp[key] = []int64{v.Weight, sum} - originCluster[key] = v + if !trace || trace && v.Trace { + sum += v.Weight + key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) + mp[key] = []int64{v.Weight, sum} + originCluster[key] = v + } } f := math.Mod(float64(time.Now().Unix()), float64(sum)) @@ -536,6 +577,9 @@ func (e *Bnb) BalanceCluster() *config.NodeCluster { } else if l == 1 { //如果 仅有一个节点,则只能使用该节点 resultCluster = e.nodeCluster[0] + if trace && !resultCluster.Trace { + return nil + } } else { return nil } diff --git a/blockchain/service/btc/btc.go b/blockchain/service/btc/btc.go index ad4b382..3f9d3ba 100644 --- a/blockchain/service/btc/btc.go +++ b/blockchain/service/btc/btc.go @@ -213,7 +213,7 @@ func (e *Btc) SendReq(blockChain int64, reqBody string) (resp string, err error) } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -228,7 +228,7 @@ func (e *Btc) SendReq(blockChain int64, reqBody string) (resp string, err error) return resp, err } -func (e *Btc) BalanceCluster() *config.NodeCluster { +func (e *Btc) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) diff --git a/blockchain/service/ether/ether.go b/blockchain/service/ether/ether.go index 897acc9..d89ad3b 100644 --- a/blockchain/service/ether/ether.go +++ b/blockchain/service/ether/ether.go @@ -27,7 +27,7 @@ type Ether struct { } func (e *Ether) TokenURI(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -41,7 +41,7 @@ func (e *Ether) TokenURI(chainCode int64, contractAddress string, tokenId string } func (e *Ether) BalanceOf(chainCode int64, contractAddress string, address string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -55,7 +55,7 @@ func (e *Ether) BalanceOf(chainCode int64, contractAddress string, address strin } func (e *Ether) OwnerOf(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -69,7 +69,7 @@ func (e *Ether) OwnerOf(chainCode int64, contractAddress string, tokenId string, } func (e *Ether) TotalSupply(chainCode int64, contractAddress string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -83,7 +83,7 @@ func (e *Ether) TotalSupply(chainCode int64, contractAddress string, eip int64) } func (e *Ether) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -392,7 +392,7 @@ func (e *Ether) TokenBalance(chainCode int64, address string, contractAddr strin defer func() { e.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -492,8 +492,22 @@ func (e *Ether) GasPrice(chainCode int64) (string, error) { return res, nil } +func (e *Ether) TraceTransaction(chainCode int64, address string) (string, error) { + req := `{ + "id": 1, + "jsonrpc": "2.0", + "params": [ + "%v" + ], + "method": "trace_transaction" + }` + req = fmt.Sprintf(req, address) + + return e.SendReq2(chainCode, req) +} + func (e *Ether) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -608,7 +622,7 @@ func (e *Ether) SendReq(blockChain int64, reqBody string) (resp string, err erro e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -623,14 +637,39 @@ func (e *Ether) SendReq(blockChain int64, reqBody string) (resp string, err erro return resp, err } -func (e *Ether) BalanceCluster() *config.NodeCluster { +func (e *Ether) SendReq2(blockChain int64, reqBody string) (resp string, err error) { + reqBody = strings.Replace(reqBody, "\t", "", -1) + reqBody = strings.Replace(reqBody, "\n", "", -1) + var uri string + defer func() { + if err != nil { + e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) + } else { + e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") + } + }() + cluster := e.BalanceCluster(true) + if cluster == nil { + //不存在节点 + return "", errors.New("blockchain node has not found") + } + uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) + + resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) + if err != nil { + cluster.ErrorCount += 1 + } + return resp, err +} + +func (e *Ether) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) if l > 1 { //如果有多个节点,则根据权重计算 mp := make(map[string][]int64, 0) - originCluster := make(map[string]*config.NodeCluster, 0) + originCluster := make(map[string]*config.NodeCluster, 1) var sum int64 for _, v := range e.nodeCluster { @@ -638,10 +677,12 @@ func (e *Ether) BalanceCluster() *config.NodeCluster { //如果没有设置weight,则默认设定5 v.Weight = 5 } - sum += v.Weight - key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) - mp[key] = []int64{v.Weight, sum} - originCluster[key] = v + if !trace || trace && v.Trace { + sum += v.Weight + key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) + mp[key] = []int64{v.Weight, sum} + originCluster[key] = v + } } f := math.Mod(float64(time.Now().Unix()), float64(sum)) @@ -657,6 +698,9 @@ func (e *Ether) BalanceCluster() *config.NodeCluster { } else if l == 1 { //如果 仅有一个节点,则只能使用该节点 resultCluster = e.nodeCluster[0] + if trace && !resultCluster.Trace { + return nil + } } else { return nil } diff --git a/blockchain/service/ether/ether_test.go b/blockchain/service/ether/ether_test.go index 31768f1..022e919 100644 --- a/blockchain/service/ether/ether_test.go +++ b/blockchain/service/ether/ether_test.go @@ -19,6 +19,11 @@ func Init2() blockchain.NftApi { return NewNftEth(cfg.Cluster[200], 200, xlog.NewXLogger()) } +func Init3() blockchain.ExApi { + cfg := config.LoadConfig("./../../../cmd/blockchain/config_ether.json") + return NewEth2(cfg.Cluster[200], 200, xlog.NewXLogger()) +} + func TestEther_Token(t *testing.T) { s := Init() resp, err := s.Token(200, "0x4577fcfB0642afD21b5f2502753ED6D497B830E9", "", "721") @@ -139,3 +144,14 @@ func TestEther_GetTransactionReceiptByHash(t *testing.T) { t.Log(resp) } } + +func TestEther_TraceTransaction(t *testing.T) { + ex := Init3() + resp, err := ex.TraceTransaction(200, "0x999cabe1fcca80148290827a8c655734531615cb22d30faa222ec7a67928587b") + if err != nil { + t.Error(err) + } else { + t.Log(resp) + } + +} diff --git a/blockchain/service/filecoin/filecoin.go b/blockchain/service/filecoin/filecoin.go index fb06bbf..e5e90b4 100644 --- a/blockchain/service/filecoin/filecoin.go +++ b/blockchain/service/filecoin/filecoin.go @@ -378,7 +378,7 @@ func (e *FileCoin) TokenBalance(chainCode int64, address string, contractAddr st defer func() { e.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -392,7 +392,7 @@ func (e *FileCoin) TokenBalance(chainCode int64, address string, contractAddr st } func (e *FileCoin) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -412,7 +412,7 @@ func (e *FileCoin) SendReq(blockChain int64, reqBody string) (resp string, err e e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -428,7 +428,7 @@ func (e *FileCoin) SendReq(blockChain int64, reqBody string) (resp string, err e //return "", errors.New("blockChainCode is error") } -func (e *FileCoin) BalanceCluster() *config.NodeCluster { +func (e *FileCoin) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) diff --git a/blockchain/service/polygon/polygon_pos.go b/blockchain/service/polygon/polygon_pos.go index 3da29b8..42e00cf 100644 --- a/blockchain/service/polygon/polygon_pos.go +++ b/blockchain/service/polygon/polygon_pos.go @@ -27,7 +27,7 @@ type PolygonPos struct { } func (e *PolygonPos) TokenURI(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -41,7 +41,7 @@ func (e *PolygonPos) TokenURI(chainCode int64, contractAddress string, tokenId s } func (e *PolygonPos) BalanceOf(chainCode int64, contractAddress string, address string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -55,7 +55,7 @@ func (e *PolygonPos) BalanceOf(chainCode int64, contractAddress string, address } func (e *PolygonPos) OwnerOf(chainCode int64, contractAddress string, tokenId string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -69,7 +69,7 @@ func (e *PolygonPos) OwnerOf(chainCode int64, contractAddress string, tokenId st } func (e *PolygonPos) TotalSupply(chainCode int64, contractAddress string, eip int64) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -83,7 +83,7 @@ func (e *PolygonPos) TotalSupply(chainCode int64, contractAddress string, eip in } func (e *PolygonPos) Token(chainCode int64, contractAddr string, abi string, eip string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -391,7 +391,7 @@ func (e *PolygonPos) TokenBalance(chainCode int64, address string, contractAddr defer func() { e.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -491,8 +491,22 @@ func (e *PolygonPos) SendRawTransaction(chainCode int64, signedTx string) (strin return e.SendReq(chainCode, req) } +func (e *PolygonPos) TraceTransaction(chainCode int64, address string) (string, error) { + req := `{ + "id": 1, + "jsonrpc": "2.0", + "params": [ + "%v" + ], + "method": "trace_transaction" + }` + req = fmt.Sprintf(req, address) + + return e.SendReq2(chainCode, req) +} + func (e *PolygonPos) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -607,7 +621,7 @@ func (e *PolygonPos) SendReq(blockChain int64, reqBody string) (resp string, err e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -625,14 +639,39 @@ func (e *PolygonPos) SendReq(blockChain int64, reqBody string) (resp string, err //return "", errors.New("blockChainCode is error") } -func (e *PolygonPos) BalanceCluster() *config.NodeCluster { +func (e *PolygonPos) SendReq2(blockChain int64, reqBody string) (resp string, err error) { + reqBody = strings.Replace(reqBody, "\t", "", -1) + reqBody = strings.Replace(reqBody, "\n", "", -1) + var uri string + defer func() { + if err != nil { + e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) + } else { + e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") + } + }() + cluster := e.BalanceCluster(true) + if cluster == nil { + //不存在节点 + return "", errors.New("blockchain node has not found") + } + uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) + + resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) + if err != nil { + cluster.ErrorCount += 1 + } + return resp, err +} + +func (e *PolygonPos) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) if l > 1 { //如果有多个节点,则根据权重计算 mp := make(map[string][]int64, 0) - originCluster := make(map[string]*config.NodeCluster, 0) + originCluster := make(map[string]*config.NodeCluster, 1) var sum int64 for _, v := range e.nodeCluster { @@ -640,10 +679,12 @@ func (e *PolygonPos) BalanceCluster() *config.NodeCluster { //如果没有设置weight,则默认设定5 v.Weight = 5 } - sum += v.Weight - key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) - mp[key] = []int64{v.Weight, sum} - originCluster[key] = v + if !trace || trace && v.Trace { + sum += v.Weight + key := fmt.Sprintf("%v/%v", v.NodeUrl, v.NodeToken) + mp[key] = []int64{v.Weight, sum} + originCluster[key] = v + } } f := math.Mod(float64(time.Now().Unix()), float64(sum)) @@ -659,6 +700,9 @@ func (e *PolygonPos) BalanceCluster() *config.NodeCluster { } else if l == 1 { //如果 仅有一个节点,则只能使用该节点 resultCluster = e.nodeCluster[0] + if trace && !resultCluster.Trace { + return nil + } } else { return nil } diff --git a/blockchain/service/tron/tron.go b/blockchain/service/tron/tron.go index 8addc60..cd554d2 100644 --- a/blockchain/service/tron/tron.go +++ b/blockchain/service/tron/tron.go @@ -274,7 +274,7 @@ func (t *Tron) GetTxByHash(chainCode int64, hash string) (string, error) { } func (t *Tron) SendJsonRpc(chainCode int64, req string) (string, error) { - cluster := t.BalanceCluster() + cluster := t.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -311,7 +311,7 @@ func (t *Tron) TokenBalance(chainCode int64, address string, contractAddr string defer func() { t.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := t.BalanceCluster() + cluster := t.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -354,6 +354,10 @@ func (t *Tron) SendRawTransaction(chainCode int64, signedTx string) (string, err return t.SendReq(chainCode, req, "wallet/broadcasthex") } +func (e *Tron) TraceTransaction(chainCode int64, address string) (string, error) { + return "", nil +} + func (t *Tron) SendReq(blockChain int64, reqBody string, url string) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) @@ -364,7 +368,7 @@ func (t *Tron) SendReq(blockChain int64, reqBody string, url string) (resp strin t.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := t.BalanceCluster() + cluster := t.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -378,7 +382,7 @@ func (t *Tron) SendReq(blockChain int64, reqBody string, url string) (resp strin return resp, err } -func (t *Tron) BalanceCluster() *config.NodeCluster { +func (t *Tron) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(t.nodeCluster) diff --git a/blockchain/service/xrp/xrp.go b/blockchain/service/xrp/xrp.go index 57564a6..0f8c0ab 100644 --- a/blockchain/service/xrp/xrp.go +++ b/blockchain/service/xrp/xrp.go @@ -305,7 +305,7 @@ func (e *XRP) TokenBalance(chainCode int64, address string, contractAddr string, defer func() { e.log.Printf("TokenBalance,Duration=%v", time.Since(start)) }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -360,7 +360,7 @@ func (e *XRP) SendReq(blockChain int64, reqBody string) (resp string, err error) e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster() + cluster := e.BalanceCluster(false) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -375,7 +375,7 @@ func (e *XRP) SendReq(blockChain int64, reqBody string) (resp string, err error) return resp, err } -func (e *XRP) BalanceCluster() *config.NodeCluster { +func (e *XRP) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) From 7baa819e6d0d1b388f00b63ef1b460a7f44b03f4 Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Mon, 8 Jan 2024 15:00:31 +0800 Subject: [PATCH 2/3] feat: add api for trace tx --- blockchain/service.go | 2 +- blockchain/service/http_handler.go | 39 ++++++++++++++++++++++++ blockchain/type.go | 49 ++++++++++++++++++++++++++++++ cmd/blockchain/README.md | 45 +++++++++++++++++++++++++++ cmd/blockchain/app.go | 3 +- cmd/blockchain/config_ether.json | 5 +-- cmd/easynode/app.go | 1 + 7 files changed, 140 insertions(+), 4 deletions(-) diff --git a/blockchain/service.go b/blockchain/service.go index 1408acc..a00e187 100644 --- a/blockchain/service.go +++ b/blockchain/service.go @@ -40,7 +40,7 @@ type API interface { type ExApi interface { GasPrice(chainCode int64) (string, error) - TraceTransaction(chainCode int64, address string) (string, error) + TraceTransaction(chainCode int64, hash string) (string, error) EstimateGas(chainCode int64, from, to, data string) (string, error) EstimateGasForTron(chainCode int64, from, to, functionSelector, parameter string) (string, error) GetAccountResourceForTron(chainCode int64, address string) (string, error) diff --git a/blockchain/service/http_handler.go b/blockchain/service/http_handler.go index 88bd0b5..959de93 100644 --- a/blockchain/service/http_handler.go +++ b/blockchain/service/http_handler.go @@ -198,6 +198,45 @@ func (h *HttpHandler) GetBlockByNumber(ctx *gin.Context) { h.Success(ctx, string(b), res, ctx.Request.RequestURI) } +func (h *HttpHandler) GetTraceTransaction(ctx *gin.Context) { + b, err := io.ReadAll(ctx.Request.Body) + if err != nil { + h.Error(ctx, "", ctx.Request.RequestURI, err.Error()) + return + } + blockChainCode := gjson.ParseBytes(b).Get("chain").Int() + hash := gjson.ParseBytes(b).Get("hash").String() + if _, ok := h.exBlockChainClients[blockChainCode]; !ok { + h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode)) + return + } + res, err := h.exBlockChainClients[blockChainCode].TraceTransaction(blockChainCode, hash) + + if err != nil { + h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error()) + return + } + + r := make([]*blockchain.InterTx, 0, 10) + if chain.GetChainCode(blockChainCode, "ETH", nil) || chain.GetChainCode(blockChainCode, "BSC", nil) || chain.GetChainCode(blockChainCode, "POLYGON", nil) { + list := gjson.Parse(res).Get("result").Array() + for _, v := range list { + value := v.Get("action.value").String() + if value != "0x0" { + var t blockchain.InterTx + _ = json.Unmarshal([]byte(v.String()), &t) + hexValue, _ := util.HexToInt(t.Action.Value) + t.Action.Value = util.Div(hexValue, 18) + t.Action.Gas, _ = util.HexToInt(t.Action.Gas) + r = append(r, &t) + } + } + + } + + h.Success(ctx, string(b), r, ctx.Request.RequestURI) +} + func (h *HttpHandler) GetTxByHash(ctx *gin.Context) { b, err := io.ReadAll(ctx.Request.Body) if err != nil { diff --git a/blockchain/type.go b/blockchain/type.go index 10fca70..d8e64cd 100644 --- a/blockchain/type.go +++ b/blockchain/type.go @@ -14,3 +14,52 @@ type WsRespMessage struct { Params map[string]string Resp interface{} } + +/** + { + "action": { + "from": "0x4e68ccd3e89f51c3074ca5072bbac773960dfa36", + "callType": "staticcall", + "gas": "0x26e23", + "input": "0x70a082310000000000000000000000004e68ccd3e89f51c3074ca5072bbac773960dfa36", + "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + "value": "0x0" + }, + "blockHash": "0x6b3f8639a57d744dcbbbe0d84bca25d7ea776282d6f53f329db3cc9408416115", + "blockNumber": 18938364, + "result": { + "gasUsed": "0x9e6", + "output": "0x00000000000000000000000000000000000000000000036e79c74a878ae9c2cd" + }, + "subtraces": 0, + "traceAddress": [ + 1, + 1 + ], + "transactionHash": "0x999cabe1fcca80148290827a8c655734531615cb22d30faa222ec7a67928587b", + "transactionPosition": 201, + "type": "call" + } +*/ + +type InterTx struct { + Result struct { + Output string `json:"output" gorm:"column:output"` + GasUsed string `json:"gasUsed" gorm:"column:gasUsed"` + } `json:"-" gorm:"column:result"` + BlockHash string `json:"blockHash" gorm:"column:blockHash"` + TransactionPosition int `json:"-" gorm:"column:transactionPosition"` + BlockNumber int `json:"blockNumber" gorm:"column:blockNumber"` + TraceAddress []int `json:"-" gorm:"column:traceAddress"` + Action struct { + Input string `json:"-" gorm:"column:input"` + Gas string `json:"gas" gorm:"column:gas"` + From string `json:"from" gorm:"column:from"` + To string `json:"to" gorm:"column:to"` + Value string `json:"value" gorm:"column:value"` + CallType string `json:"callType" gorm:"column:callType"` + } `json:"action" gorm:"column:action"` + Type string `json:"-" gorm:"column:type"` + Subtraces int `json:"-" gorm:"column:subtraces"` + TransactionHash string `json:"transactionHash" gorm:"column:transactionHash"` +} diff --git a/cmd/blockchain/README.md b/cmd/blockchain/README.md index 5e1888e..95496c9 100644 --- a/cmd/blockchain/README.md +++ b/cmd/blockchain/README.md @@ -226,6 +226,51 @@ curl -X POST \ "message": "ok" } + +//Query trace transaction by txHash for only evm chain +curl -X POST \ + http://127.0.0.1:9002/api/chain/origin/tx/trace \ + -H 'Content-Type: application/json' \ + -H 'Postman-Token: 72b4bc45-c235-4361-b88b-2ccffd42a384' \ + -H 'cache-control: no-cache' \ + -d '{ + "chain":200, + "hash":"0x999cabe1fcca80148290827a8c655734531615cb22d30faa222ec7a67928587b" + }' + + response: + { + "code": 0, + "data": [ + { + "blockHash": "0x6b3f8639a57d744dcbbbe0d84bca25d7ea776282d6f53f329db3cc9408416115", + "blockNumber": 18938364, + "action": { + "gas": "281806", + "from": "0xae185e9cd79631c5fc19f0fd0454adca8a94976f", + "to": "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", + "value": "0.309648405815618", + "callType": "call" + }, + "transactionHash": "0x999cabe1fcca80148290827a8c655734531615cb22d30faa222ec7a67928587b" + }, + { + "blockHash": "0x6b3f8639a57d744dcbbbe0d84bca25d7ea776282d6f53f329db3cc9408416115", + "blockNumber": 18938364, + "action": { + "gas": "261730", + "from": "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", + "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", + "value": "0.309648405815618", + "callType": "call" + }, + "transactionHash": "0x999cabe1fcca80148290827a8c655734531615cb22d30faa222ec7a67928587b" + } + ], + "message": "ok" + } + + //Query receipt by txHash curl -X POST \ http://127.0.0.1:9002/api/chain/origin/receipts/hash \ diff --git a/cmd/blockchain/app.go b/cmd/blockchain/app.go index 95ee59e..ce3f0f9 100644 --- a/cmd/blockchain/app.go +++ b/cmd/blockchain/app.go @@ -15,7 +15,7 @@ import ( func main() { var configPath string - flag.StringVar(&configPath, "blockchain", "./cmd/blockchain/blockchain_config.json", "The system file of config") + flag.StringVar(&configPath, "blockchain", "./cmd/blockchain/config_ether.json", "The system file of config") flag.Parse() if len(configPath) < 1 { panic("can not find config file") @@ -50,6 +50,7 @@ func main() { origin.POST("/block/hash", srv.GetBlockByHash) origin.POST("/block/number", srv.GetBlockByNumber) origin.POST("/tx/hash", srv.GetTxByHash) + origin.POST("/tx/trace", srv.GetTraceTransaction) origin.POST("/receipts/hash", srv.GetTxReceiptByHash) origin.POST("/account/balance", srv.GetBalance) origin.POST("/account/tokenBalance", srv.GetTokenBalance) diff --git a/cmd/blockchain/config_ether.json b/cmd/blockchain/config_ether.json index 33afa2e..4f5e85c 100644 --- a/cmd/blockchain/config_ether.json +++ b/cmd/blockchain/config_ether.json @@ -10,8 +10,9 @@ "Nodes": [ { "BlockChain": 200, - "NodeUrl": "https://ethereum.publicnode.com", - "NodeToken": "" + "NodeUrl": "https://docs-demo.quiknode.pro", + "NodeToken": "", + "Trace": true } ] } diff --git a/cmd/easynode/app.go b/cmd/easynode/app.go index d3876c9..4cd3f02 100644 --- a/cmd/easynode/app.go +++ b/cmd/easynode/app.go @@ -190,6 +190,7 @@ func startBlockchain(configPath string, ctx context.Context) { origin.POST("/block/hash", srv.GetBlockByHash) origin.POST("/block/number", srv.GetBlockByNumber) origin.POST("/tx/hash", srv.GetTxByHash) + origin.POST("/tx/trace", srv.GetTraceTransaction) origin.POST("/receipts/hash", srv.GetTxReceiptByHash) origin.POST("/account/balance", srv.GetBalance) origin.POST("/account/tokenBalance", srv.GetTokenBalance) From 4eb1c94b2e7e381f7cf6878f4cbbb99964e0108c Mon Sep 17 00:00:00 2001 From: sunhongtao Date: Tue, 9 Jan 2024 11:14:35 +0800 Subject: [PATCH 3/3] perf: improve code --- blockchain/blockchain.go | 2 +- blockchain/service/bnb/bnb.go | 59 +++++++---------------- blockchain/service/btc/btc.go | 16 +++--- blockchain/service/ether/ether.go | 59 +++++++---------------- blockchain/service/filecoin/filecoin.go | 30 ++++++------ blockchain/service/polygon/polygon_pos.go | 59 +++++++---------------- blockchain/service/xrp/xrp.go | 24 ++++----- 7 files changed, 87 insertions(+), 162 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 2040f5d..859353c 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -21,7 +21,7 @@ type NFT interface { } type ChainNet interface { - SendReq(blockChain int64, reqBody string) (string, error) + SendReq(blockChain int64, reqBody string, trace bool) (string, error) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) } diff --git a/blockchain/service/bnb/bnb.go b/blockchain/service/bnb/bnb.go index 76eac0e..036d286 100644 --- a/blockchain/service/bnb/bnb.go +++ b/blockchain/service/bnb/bnb.go @@ -117,7 +117,7 @@ func (e *Bnb) GetCode(chainCode int64, address string) (string, error) { ] }` query = fmt.Sprintf(query, address) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Bnb) GetAddressType(chainCode int64, address string) (string, error) { @@ -135,7 +135,7 @@ func (e *Bnb) GetAddressType(chainCode int64, address string) (string, error) { ] }` query = fmt.Sprintf(query, address) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -166,7 +166,7 @@ func (e *Bnb) UnSubscribe(chainCode int64, subId string) (string, error) { query := `{"id": 1, "method": "eth_unsubscribe", "params": ["%v"]}` query = fmt.Sprintf(query, subId) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -195,7 +195,7 @@ func (e *Bnb) GetBlockReceiptByBlockNumber(chainCode int64, number string) (stri ] }` query = fmt.Sprintf(query, number) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Bnb) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) { @@ -209,7 +209,7 @@ func (e *Bnb) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Bnb) GetTransactionReceiptByHash(chainCode int64, hash string) (string, error) { @@ -226,7 +226,7 @@ func (e *Bnb) GetTransactionReceiptByHash(chainCode int64, hash string) (string, ] }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Bnb) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) { @@ -242,7 +242,7 @@ func (e *Bnb) GetBlockByHash(chainCode int64, hash string, flag bool) (string, e }` req = fmt.Sprintf(req, hash, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) GetBlockByNumber(chainCode int64, number string, flag bool) (string, error) { @@ -259,7 +259,7 @@ func (e *Bnb) GetBlockByNumber(chainCode int64, number string, flag bool) (strin ` number, _ = util.Int2Hex(number) req = fmt.Sprintf(req, number, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) GetTxByHash(chainCode int64, hash string) (string, error) { @@ -278,11 +278,11 @@ func (e *Bnb) GetTxByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func NewNftBnb(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.NftApi { @@ -356,7 +356,7 @@ func (e *Bnb) TraceTransaction(chainCode int64, address string) (string, error) }` req = fmt.Sprintf(req, address) - return e.SendReq2(chainCode, req) + return e.SendReq(chainCode, req, true) } func (e *Bnb) Balance(chainCode int64, address string, tag string) (string, error) { @@ -378,7 +378,7 @@ func (e *Bnb) Balance(chainCode int64, address string, tag string) (string, erro }` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) TokenBalance(chainCode int64, address string, contractAddr string, abi string) (string, error) { @@ -412,7 +412,7 @@ func (e *Bnb) Nonce(chainCode int64, address string, tag string) (string, error) } ` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) LatestBlock(chainCode int64) (string, error) { @@ -423,7 +423,7 @@ func (e *Bnb) LatestBlock(chainCode int64) (string, error) { "method": "eth_blockNumber" } ` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) GetAccountResourceForTron(chainCode int64, address string) (string, error) { @@ -483,14 +483,14 @@ func (e *Bnb) SendRawTransaction(chainCode int64, signedTx string) (string, erro "method": "eth_sendRawTransaction" }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Bnb) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { return "", nil } -func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *Bnb) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -501,7 +501,7 @@ func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error) e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -516,31 +516,6 @@ func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error) return resp, err } -func (e *Bnb) SendReq2(blockChain int64, reqBody string) (resp string, err error) { - reqBody = strings.Replace(reqBody, "\t", "", -1) - reqBody = strings.Replace(reqBody, "\n", "", -1) - var uri string - defer func() { - if err != nil { - e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) - } else { - e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") - } - }() - cluster := e.BalanceCluster(true) - if cluster == nil { - //不存在节点 - return "", errors.New("blockchain node has not found") - } - uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) - - resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) - if err != nil { - cluster.ErrorCount += 1 - } - return resp, err -} - func (e *Bnb) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) diff --git a/blockchain/service/btc/btc.go b/blockchain/service/btc/btc.go index 3f9d3ba..7827e50 100644 --- a/blockchain/service/btc/btc.go +++ b/blockchain/service/btc/btc.go @@ -75,7 +75,7 @@ func (e *Btc) GetBlockByHash(chainCode int64, hash string, flag bool) (string, e } req = fmt.Sprintf(req, hash, hasTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Btc) GetBlockByNumber(chainCode int64, number string, flag bool) (string, error) { @@ -88,7 +88,7 @@ func (e *Btc) GetBlockByNumber(chainCode int64, number string, flag bool) (strin } ` req = fmt.Sprintf(req, number) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return resp, err @@ -118,11 +118,11 @@ func (e *Btc) GetTxByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Btc) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func NewBtc(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API { @@ -183,7 +183,7 @@ func (e *Btc) LatestBlock(chainCode int64) (string, error) { "method": "getblockcount" } ` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Btc) SendRawTransaction(chainCode int64, signedTx string) (string, error) { @@ -194,14 +194,14 @@ func (e *Btc) SendRawTransaction(chainCode int64, signedTx string) (string, erro ] }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Btc) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { return "", nil } -func (e *Btc) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *Btc) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -213,7 +213,7 @@ func (e *Btc) SendReq(blockChain int64, reqBody string) (resp string, err error) } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") diff --git a/blockchain/service/ether/ether.go b/blockchain/service/ether/ether.go index d89ad3b..80da9b8 100644 --- a/blockchain/service/ether/ether.go +++ b/blockchain/service/ether/ether.go @@ -120,7 +120,7 @@ func (e *Ether) GetCode(chainCode int64, address string) (string, error) { ] }` query = fmt.Sprintf(query, address) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Ether) GetAddressType(chainCode int64, address string) (string, error) { @@ -138,7 +138,7 @@ func (e *Ether) GetAddressType(chainCode int64, address string) (string, error) ] }` query = fmt.Sprintf(query, address) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -185,7 +185,7 @@ func (e *Ether) UnSubscribe(chainCode int64, subId string) (string, error) { query := `{"id": 1, "method": "eth_unsubscribe", "params": ["%v"]}` query = fmt.Sprintf(query, subId) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -214,7 +214,7 @@ func (e *Ether) GetBlockReceiptByBlockNumber(chainCode int64, number string) (st ] }` query = fmt.Sprintf(query, number) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Ether) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) { @@ -228,7 +228,7 @@ func (e *Ether) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Ether) GetTransactionReceiptByHash(chainCode int64, hash string) (string, error) { @@ -245,7 +245,7 @@ func (e *Ether) GetTransactionReceiptByHash(chainCode int64, hash string) (strin ] }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *Ether) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) { @@ -261,7 +261,7 @@ func (e *Ether) GetBlockByHash(chainCode int64, hash string, flag bool) (string, }` req = fmt.Sprintf(req, hash, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) GetBlockByNumber(chainCode int64, number string, flag bool) (string, error) { @@ -279,7 +279,7 @@ func (e *Ether) GetBlockByNumber(chainCode int64, number string, flag bool) (str number, _ = util.Int2Hex(number) req = fmt.Sprintf(req, number, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) GetTxByHash(chainCode int64, hash string) (string, error) { @@ -298,11 +298,11 @@ func (e *Ether) GetTxByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func NewNftEth(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.NftApi { @@ -384,7 +384,7 @@ func (e *Ether) Balance(chainCode int64, address string, tag string) (string, er }` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) TokenBalance(chainCode int64, address string, contractAddr string, abi string) (string, error) { @@ -418,7 +418,7 @@ func (e *Ether) Nonce(chainCode int64, address string, tag string) (string, erro } ` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) LatestBlock(chainCode int64) (string, error) { @@ -429,7 +429,7 @@ func (e *Ether) LatestBlock(chainCode int64) (string, error) { "method": "eth_blockNumber" } ` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) SendRawTransaction(chainCode int64, signedTx string) (string, error) { @@ -442,7 +442,7 @@ func (e *Ether) SendRawTransaction(chainCode int64, signedTx string) (string, er "method": "eth_sendRawTransaction" }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *Ether) GetAccountResourceForTron(chainCode int64, address string) (string, error) { @@ -503,7 +503,7 @@ func (e *Ether) TraceTransaction(chainCode int64, address string) (string, error }` req = fmt.Sprintf(req, address) - return e.SendReq2(chainCode, req) + return e.SendReq(chainCode, req, true) } func (e *Ether) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { @@ -611,7 +611,7 @@ func (e *Ether) SendReqByWs(blockChain int64, receiverCh chan string, sendCh cha return "", nil } -func (e *Ether) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *Ether) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -622,7 +622,7 @@ func (e *Ether) SendReq(blockChain int64, reqBody string) (resp string, err erro e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -637,31 +637,6 @@ func (e *Ether) SendReq(blockChain int64, reqBody string) (resp string, err erro return resp, err } -func (e *Ether) SendReq2(blockChain int64, reqBody string) (resp string, err error) { - reqBody = strings.Replace(reqBody, "\t", "", -1) - reqBody = strings.Replace(reqBody, "\n", "", -1) - var uri string - defer func() { - if err != nil { - e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) - } else { - e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") - } - }() - cluster := e.BalanceCluster(true) - if cluster == nil { - //不存在节点 - return "", errors.New("blockchain node has not found") - } - uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) - - resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) - if err != nil { - cluster.ErrorCount += 1 - } - return resp, err -} - func (e *Ether) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) diff --git a/blockchain/service/filecoin/filecoin.go b/blockchain/service/filecoin/filecoin.go index e5e90b4..184dbbe 100644 --- a/blockchain/service/filecoin/filecoin.go +++ b/blockchain/service/filecoin/filecoin.go @@ -54,7 +54,7 @@ func (e *FileCoin) GetCode(chainCode int64, address string) (string, error) { ] }` query = fmt.Sprintf(query, address) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *FileCoin) GetAddressType(chainCode int64, address string) (string, error) { @@ -72,7 +72,7 @@ func (e *FileCoin) GetAddressType(chainCode int64, address string) (string, erro ] }` query = fmt.Sprintf(query, address) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -119,7 +119,7 @@ func (e *FileCoin) UnSubscribe(chainCode int64, subId string) (string, error) { query := `{"id": 1, "method": "eth_unsubscribe", "params": ["%v"]}` query = fmt.Sprintf(query, subId) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -166,7 +166,7 @@ func (e *FileCoin) GetTransactionReceiptByHash(chainCode int64, hash string) (st ] }` query = fmt.Sprintf(query, eth.String()) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *FileCoin) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) { @@ -183,7 +183,7 @@ func (e *FileCoin) GetBlockByHash(chainCode int64, hash string, flag bool) (stri }` req = fmt.Sprintf(req, hash) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -219,7 +219,7 @@ func (e *FileCoin) GetBlockByNumber(chainCode int64, number string, flag bool) ( } ` req = fmt.Sprintf(req, number) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -269,7 +269,7 @@ func (e *FileCoin) GetTxsByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) GetTxByHash(chainCode int64, hash string) (string, error) { @@ -290,7 +290,7 @@ func (e *FileCoin) GetTxByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) Nonce(chainCode int64, address string, tag string) (string, error) { @@ -305,7 +305,7 @@ func (e *FileCoin) Nonce(chainCode int64, address string, tag string) (string, e } ` req = fmt.Sprintf(req, address) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) LatestBlock(chainCode int64) (string, error) { @@ -316,7 +316,7 @@ func (e *FileCoin) LatestBlock(chainCode int64) (string, error) { "method": "eth_blockNumber" } ` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) SendRawTransaction(chainCode int64, signedTx string) (string, error) { @@ -329,11 +329,11 @@ func (e *FileCoin) SendRawTransaction(chainCode int64, signedTx string) (string, "method": "Filecoin.MpoolPush" }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) StartWDT() { @@ -370,7 +370,7 @@ func (e *FileCoin) Balance(chainCode int64, address string, tag string) (string, }` req = fmt.Sprintf(req, address) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *FileCoin) TokenBalance(chainCode int64, address string, contractAddr string, abi string) (string, error) { @@ -401,7 +401,7 @@ func (e *FileCoin) SendReqByWs(blockChain int64, receiverCh chan string, sendCh return "", errors.New("blockChainCode is error") } -func (e *FileCoin) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *FileCoin) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -412,7 +412,7 @@ func (e *FileCoin) SendReq(blockChain int64, reqBody string) (resp string, err e e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") diff --git a/blockchain/service/polygon/polygon_pos.go b/blockchain/service/polygon/polygon_pos.go index 42e00cf..e9b5985 100644 --- a/blockchain/service/polygon/polygon_pos.go +++ b/blockchain/service/polygon/polygon_pos.go @@ -120,7 +120,7 @@ func (e *PolygonPos) GetCode(chainCode int64, address string) (string, error) { ] }` query = fmt.Sprintf(query, address) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *PolygonPos) GetAddressType(chainCode int64, address string) (string, error) { @@ -138,7 +138,7 @@ func (e *PolygonPos) GetAddressType(chainCode int64, address string) (string, er ] }` query = fmt.Sprintf(query, address) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -185,7 +185,7 @@ func (e *PolygonPos) UnSubscribe(chainCode int64, subId string) (string, error) query := `{"id": 1, "method": "eth_unsubscribe", "params": ["%v"]}` query = fmt.Sprintf(query, subId) - resp, err := e.SendReq(chainCode, query) + resp, err := e.SendReq(chainCode, query, false) if err != nil { return "", err } @@ -214,7 +214,7 @@ func (e *PolygonPos) GetBlockReceiptByBlockNumber(chainCode int64, number string ] }` query = fmt.Sprintf(query, number) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *PolygonPos) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, error) { @@ -228,7 +228,7 @@ func (e *PolygonPos) GetBlockReceiptByBlockHash(chainCode int64, hash string) (s }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *PolygonPos) GetTransactionReceiptByHash(chainCode int64, hash string) (string, error) { @@ -245,7 +245,7 @@ func (e *PolygonPos) GetTransactionReceiptByHash(chainCode int64, hash string) ( ] }` query = fmt.Sprintf(query, hash) - return e.SendReq(chainCode, query) + return e.SendReq(chainCode, query, false) } func (e *PolygonPos) GetBlockByHash(chainCode int64, hash string, flag bool) (string, error) { @@ -261,7 +261,7 @@ func (e *PolygonPos) GetBlockByHash(chainCode int64, hash string, flag bool) (st }` req = fmt.Sprintf(req, hash, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) GetBlockByNumber(chainCode int64, number string, flag bool) (string, error) { @@ -278,7 +278,7 @@ func (e *PolygonPos) GetBlockByNumber(chainCode int64, number string, flag bool) ` number, _ = util.Int2Hex(number) req = fmt.Sprintf(req, number, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) GetTxByHash(chainCode int64, hash string) (string, error) { @@ -297,11 +297,11 @@ func (e *PolygonPos) GetTxByHash(chainCode int64, hash string) (string, error) { } ` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func NewPolygonPos(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API { @@ -383,7 +383,7 @@ func (e *PolygonPos) Balance(chainCode int64, address string, tag string) (strin }` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) TokenBalance(chainCode int64, address string, contractAddr string, abi string) (string, error) { @@ -417,7 +417,7 @@ func (e *PolygonPos) Nonce(chainCode int64, address string, tag string) (string, } ` req = fmt.Sprintf(req, address, tag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) LatestBlock(chainCode int64) (string, error) { @@ -428,7 +428,7 @@ func (e *PolygonPos) LatestBlock(chainCode int64) (string, error) { "method": "eth_blockNumber" } ` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) GetAccountResourceForTron(chainCode int64, address string) (string, error) { @@ -488,7 +488,7 @@ func (e *PolygonPos) SendRawTransaction(chainCode int64, signedTx string) (strin "method": "eth_sendRawTransaction" }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *PolygonPos) TraceTransaction(chainCode int64, address string) (string, error) { @@ -502,7 +502,7 @@ func (e *PolygonPos) TraceTransaction(chainCode int64, address string) (string, }` req = fmt.Sprintf(req, address) - return e.SendReq2(chainCode, req) + return e.SendReq(chainCode, req, true) } func (e *PolygonPos) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { @@ -610,7 +610,7 @@ func (e *PolygonPos) SendReqByWs(blockChain int64, receiverCh chan string, sendC return "", nil } -func (e *PolygonPos) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *PolygonPos) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -621,7 +621,7 @@ func (e *PolygonPos) SendReq(blockChain int64, reqBody string) (resp string, err e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found") @@ -639,31 +639,6 @@ func (e *PolygonPos) SendReq(blockChain int64, reqBody string) (resp string, err //return "", errors.New("blockChainCode is error") } -func (e *PolygonPos) SendReq2(blockChain int64, reqBody string) (resp string, err error) { - reqBody = strings.Replace(reqBody, "\t", "", -1) - reqBody = strings.Replace(reqBody, "\n", "", -1) - var uri string - defer func() { - if err != nil { - e.log.Errorf("method:%v,blockChain:%v,req:%v,err:%v,uri:%v", "SendReq", blockChain, reqBody, err, uri) - } else { - e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") - } - }() - cluster := e.BalanceCluster(true) - if cluster == nil { - //不存在节点 - return "", errors.New("blockchain node has not found") - } - uri = fmt.Sprintf("%v/%v", cluster.NodeUrl, cluster.NodeToken) - - resp, err = e.blockChainClient.SendRequestToChain(cluster.NodeUrl, cluster.NodeToken, reqBody) - if err != nil { - cluster.ErrorCount += 1 - } - return resp, err -} - func (e *PolygonPos) BalanceCluster(trace bool) *config.NodeCluster { var resultCluster *config.NodeCluster l := len(e.nodeCluster) diff --git a/blockchain/service/xrp/xrp.go b/blockchain/service/xrp/xrp.go index 0f8c0ab..885e481 100644 --- a/blockchain/service/xrp/xrp.go +++ b/blockchain/service/xrp/xrp.go @@ -62,7 +62,7 @@ func (e *XRP) GetBlockReceiptByBlockNumber(chainCode int64, number string) (stri ] }` req = fmt.Sprintf(req, number, true) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -110,7 +110,7 @@ func (e *XRP) GetBlockReceiptByBlockHash(chainCode int64, hash string) (string, ] }` req = fmt.Sprintf(req, hash, true) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -156,7 +156,7 @@ func (e *XRP) GetTransactionReceiptByHash(chainCode int64, hash string) (string, ] }` req = fmt.Sprintf(req, hash) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -198,7 +198,7 @@ func (e *XRP) GetBlockByHash(chainCode int64, hash string, flag bool) (string, e ] }` req = fmt.Sprintf(req, hash, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } // GetBlockByNumber number: ledger version or shortcut string @@ -217,7 +217,7 @@ func (e *XRP) GetBlockByNumber(chainCode int64, number string, flag bool) (strin ] }` req = fmt.Sprintf(req, number, flag) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *XRP) GetTxByHash(chainCode int64, hash string) (string, error) { @@ -234,11 +234,11 @@ func (e *XRP) GetTxByHash(chainCode int64, hash string) (string, error) { ] }` req = fmt.Sprintf(req, hash) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *XRP) SendJsonRpc(chainCode int64, req string) (string, error) { - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func NewXRP(cluster []*config.NodeCluster, blockchain int64, xlog *xlog.XLog) blockchain.API { @@ -292,7 +292,7 @@ func (e *XRP) Balance(chainCode int64, address string, tag string) (string, erro }` req = fmt.Sprintf(req, address, tag) - resp, err := e.SendReq(chainCode, req) + resp, err := e.SendReq(chainCode, req, false) if err != nil { return "", err } @@ -329,7 +329,7 @@ func (e *XRP) LatestBlock(chainCode int64) (string, error) { {} ] }` - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *XRP) SendRawTransaction(chainCode int64, signedTx string) (string, error) { @@ -342,14 +342,14 @@ func (e *XRP) SendRawTransaction(chainCode int64, signedTx string) (string, erro ] }` req = fmt.Sprintf(req, signedTx) - return e.SendReq(chainCode, req) + return e.SendReq(chainCode, req, false) } func (e *XRP) SendReqByWs(blockChain int64, receiverCh chan string, sendCh chan string) (string, error) { return "", fmt.Errorf("blockchain:%v not implement the method", blockChain) } -func (e *XRP) SendReq(blockChain int64, reqBody string) (resp string, err error) { +func (e *XRP) SendReq(blockChain int64, reqBody string, trace bool) (resp string, err error) { reqBody = strings.Replace(reqBody, "\t", "", -1) reqBody = strings.Replace(reqBody, "\n", "", -1) var uri string @@ -360,7 +360,7 @@ func (e *XRP) SendReq(blockChain int64, reqBody string) (resp string, err error) e.log.Printf("method:%v,blockChain:%v,req:%v,resp:%v", "SendReq", blockChain, reqBody, "ok") } }() - cluster := e.BalanceCluster(false) + cluster := e.BalanceCluster(trace) if cluster == nil { //不存在节点 return "", errors.New("blockchain node has not found")