Skip to content

Commit

Permalink
Merge pull request #92 from 0xcregis/internal-transaction-data
Browse files Browse the repository at this point in the history
feat: Internal transaction data
  • Loading branch information
sunjiangjun committed Jan 9, 2024
2 parents 226baf6 + 4eb1c94 commit 0120158
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 136 deletions.
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ 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)
}

type ChainCluster interface {
BalanceCluster() *config.NodeCluster
BalanceCluster(trace bool) *config.NodeCluster
}
1 change: 1 addition & 0 deletions blockchain/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type NodeCluster struct {
Weight int64 `json:"Weight"`
ErrorCount int64 `json:"ErrorCount"`
Utxo string `json:"Utxo"`
Trace bool `json:"Trace"`
}
1 change: 1 addition & 0 deletions blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type API interface {

type ExApi interface {
GasPrice(chainCode int64) (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)
Expand Down
75 changes: 47 additions & 28 deletions blockchain/service/bnb/bnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down 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 @@ -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.SendReq(chainCode, req, true)
}

func (e *Bnb) Balance(chainCode int64, address string, tag string) (string, error) {
start := time.Now()
defer func() {
Expand All @@ -364,15 +378,15 @@ 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) {
start := time.Now()
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")
Expand All @@ -398,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 @@ -409,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 @@ -469,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 @@ -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(trace)
if cluster == nil {
//不存在节点
return "", errors.New("blockchain node has not found")
Expand All @@ -502,25 +516,27 @@ func (e *Bnb) SendReq(blockChain int64, reqBody string) (resp string, err error)
return resp, err
}

func (e *Bnb) BalanceCluster() *config.NodeCluster {
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 {
if v.Weight == 0 {
//如果没有设置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))
Expand All @@ -536,6 +552,9 @@ func (e *Bnb) BalanceCluster() *config.NodeCluster {
} else if l == 1 {
//如果 仅有一个节点,则只能使用该节点
resultCluster = e.nodeCluster[0]
if trace && !resultCluster.Trace {
return nil
}
} else {
return nil
}
Expand Down
18 changes: 9 additions & 9 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()
cluster := e.BalanceCluster(trace)
if cluster == nil {
//不存在节点
return "", errors.New("blockchain node has not found")
Expand All @@ -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)

Expand Down
Loading

0 comments on commit 0120158

Please sign in to comment.