Skip to content

Commit

Permalink
perf: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhongtao committed Jan 9, 2024
1 parent 7baa819 commit 4eb1c94
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 162 deletions.
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
59 changes: 17 additions & 42 deletions blockchain/service/bnb/bnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions blockchain/service/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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")
Expand Down
Loading

0 comments on commit 4eb1c94

Please sign in to comment.