Skip to content

Commit

Permalink
feat: support mult network
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhongtao committed Dec 4, 2023
1 parent da65636 commit 1e4dc72
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 140 deletions.
20 changes: 10 additions & 10 deletions blockchain/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import (
)

func NewChain(blockchain int64, log *xlog.XLog) blockchain.ChainConn {
if blockchain == chain.GetChainCode("ETH", log) {
if chain.GetChainCode(blockchain, "ETH", log) {
//eth
return ether.NewChainClient()
} else if blockchain == chain.GetChainCode("TRON", log) {
} else if chain.GetChainCode(blockchain, "TRON", log) {
//tron
return tron.NewChainClient()
} else if blockchain == chain.GetChainCode("POLYGON", log) {
} else if chain.GetChainCode(blockchain, "POLYGON", log) {
//polygon-pos
return polygonpos.NewChainClient()
} else if blockchain == chain.GetChainCode("BSC", log) {
} else if chain.GetChainCode(blockchain, "BSC", log) {
//bnb
return bnb.NewChainClient()
} else if blockchain == chain.GetChainCode("FIL", log) {
} else if chain.GetChainCode(blockchain, "FIL", log) {
//file-coin
return filecoin.NewChainClient()
} else if blockchain == chain.GetChainCode("BTC", log) {
} else if chain.GetChainCode(blockchain, "BTC", log) {
//btc
return btc.NewChainClient()
} else if blockchain == chain.GetChainCode("XRP", log) {
} else if chain.GetChainCode(blockchain, "XRP", log) {
//xrp
return xrp.NewChainClient()
} else {
Expand All @@ -41,12 +41,12 @@ func NewChain(blockchain int64, log *xlog.XLog) blockchain.ChainConn {
}

func NewNFT(blockchain int64, log *xlog.XLog) blockchain.NFT {
if blockchain == chain.GetChainCode("ETH", log) {
if chain.GetChainCode(blockchain, "ETH", log) {
//eth
return ether.NewNFTClient()
} else if blockchain == chain.GetChainCode("POLYGON", log) {
} else if chain.GetChainCode(blockchain, "POLYGON", log) {
return polygonpos.NewNFTClient()
} else if blockchain == chain.GetChainCode("BSC", log) {
} else if chain.GetChainCode(blockchain, "BSC", log) {
return bnb.NewNFTClient()
} else {
return nil
Expand Down
20 changes: 10 additions & 10 deletions blockchain/service/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ import (
)

func NewApi(blockchain int64, cluster []*config.NodeCluster, xlog *xlog.XLog) blockchain.API {
if blockchain == chain.GetChainCode("ETH", xlog) {
if chain.GetChainCode(blockchain, "ETH", xlog) {
return ether.NewEth(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("TRON", xlog) {
} else if chain.GetChainCode(blockchain, "TRON", xlog) {
return tron.NewTron(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("POLYGON", xlog) {
} else if chain.GetChainCode(blockchain, "POLYGON", xlog) {
return polygon.NewPolygonPos(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("BSC", xlog) {
} else if chain.GetChainCode(blockchain, "BSC", xlog) {
return bnb.NewBnb(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("BTC", xlog) {
} else if chain.GetChainCode(blockchain, "BTC", xlog) {
return btc.NewBtc(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("FIL", xlog) {
} else if chain.GetChainCode(blockchain, "FIL", xlog) {
return filecoin.NewFileCoin(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("XRP", xlog) {
} else if chain.GetChainCode(blockchain, "XRP", xlog) {
return xrp.NewXRP(cluster, blockchain, xlog)
}
return nil
}

func NewNftApi(blockchain int64, cluster []*config.NodeCluster, xlog *xlog.XLog) blockchain.NftApi {
if blockchain == chain.GetChainCode("ETH", xlog) {
if chain.GetChainCode(blockchain, "ETH", xlog) {
return ether.NewNftEth(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("POLYGON", xlog) {
} else if chain.GetChainCode(blockchain, "POLYGON", xlog) {
return polygon.NewNftPolygonPos(cluster, blockchain, xlog)
} else if blockchain == chain.GetChainCode("BSC", xlog) {
} else if chain.GetChainCode(blockchain, "BSC", xlog) {
return bnb.NewNftBnb(cluster, blockchain, xlog)
}
return nil
Expand Down
50 changes: 25 additions & 25 deletions collect/service/cmd/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ func GetBlockchain(blockchain int, c *config.Chain, store collect.StoreTaskInter
code := int64(blockchain)
x := xlog.NewXLogger().BuildOutType(xlog.FILE).BuildFormatter(xlog.FORMAT_JSON).BuildLevel(xlog.Level(logConfig.LogLevel)).BuildFile(fmt.Sprintf("%v/chain_info", logConfig.Path), 24*time.Hour)
var srv collect.BlockChainInterface
if code == chainCode.GetChainCode("ETH", x) {
if chainCode.GetChainCode(code, "ETH", x) {
srv = ether.NewService(c, x, store, nodeId, collect.EthTopic, collect.EthNftTransferSingleTopic)
} else if code == chainCode.GetChainCode("TRON", x) {
} else if chainCode.GetChainCode(code, "TRON", x) {
srv = tron2.NewService(c, x, store, nodeId, collect.TronTopic)
} else if code == chainCode.GetChainCode("POLYGON", x) {
} else if chainCode.GetChainCode(code, "POLYGON", x) {
srv = polygonpos.NewService(c, x, store, nodeId, collect.PolygonTopic, collect.EthNftTransferSingleTopic)
} else if code == chainCode.GetChainCode("BSC", x) {
} else if chainCode.GetChainCode(code, "BSC", x) {
srv = bnb.NewService(c, x, store, nodeId, collect.EthTopic, collect.EthNftTransferSingleTopic)
} else if code == chainCode.GetChainCode("FIL", x) {
} else if chainCode.GetChainCode(code, "FIL", x) {
srv = filecoin.NewService(c, x, store, nodeId, "")
} else if code == chainCode.GetChainCode("XRP", x) {
} else if chainCode.GetChainCode(code, "XRP", x) {
srv = xrp.NewService(c, x, store, nodeId, "")
} else if code == chainCode.GetChainCode("BTC", x) {
} else if chainCode.GetChainCode(code, "BTC", x) {
srv = btc.NewService(c, x, store, nodeId)
}

Expand All @@ -48,18 +48,18 @@ func GetTxHashFromKafka(blockchain int, txMsg []byte) string {
code := int64(blockchain)
r := gjson.ParseBytes(txMsg)
var txHash string
if code == chainCode.GetChainCode("ETH", nil) {
if chainCode.GetChainCode(code, "ETH", nil) {
txHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("TRON", nil) {
} else if chainCode.GetChainCode(code, "TRON", nil) {
tx := r.Get("tx").String()
txHash = gjson.Parse(tx).Get("txID").String()
} else if code == chainCode.GetChainCode("POLYGON", nil) {
} else if chainCode.GetChainCode(code, "POLYGON", nil) {
txHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("BSC", nil) {
} else if chainCode.GetChainCode(code, "BSC", nil) {
txHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("FIL", nil) {
} else if chainCode.GetChainCode(code, "FIL", nil) {
txHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("XRP", nil) {
} else if chainCode.GetChainCode(code, "XRP", nil) {
txHash = r.Get("hash").String()
}

Expand All @@ -70,17 +70,17 @@ func GetBlockHashFromKafka(blockchain int, blockMsg []byte) string {
code := int64(blockchain)
r := gjson.ParseBytes(blockMsg)
var blockHash string
if code == chainCode.GetChainCode("ETH", nil) {
if chainCode.GetChainCode(code, "ETH", nil) {
blockHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("TRON", nil) {
} else if chainCode.GetChainCode(code, "TRON", nil) {
blockHash = r.Get("blockID").String()
} else if code == chainCode.GetChainCode("POLYGON", nil) {
} else if chainCode.GetChainCode(code, "POLYGON", nil) {
blockHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("BSC", nil) {
} else if chainCode.GetChainCode(code, "BSC", nil) {
blockHash = r.Get("hash").String()
} else if code == chainCode.GetChainCode("FIL", nil) {
} else if chainCode.GetChainCode(code, "FIL", nil) {
blockHash = r.Get("blockHash").String()
} else if code == chainCode.GetChainCode("XRP", nil) {
} else if chainCode.GetChainCode(code, "XRP", nil) {
blockHash = r.Get("ledger_hash").String()
}
return blockHash
Expand All @@ -90,17 +90,17 @@ func GetReceiptHashFromKafka(blockchain int, receiptMsg []byte) string {
code := int64(blockchain)
r := gjson.ParseBytes(receiptMsg)
var txHash string
if code == chainCode.GetChainCode("ETH", nil) {
if chainCode.GetChainCode(code, "ETH", nil) {
txHash = r.Get("transactionHash").String()
} else if code == chainCode.GetChainCode("TRON", nil) {
} else if chainCode.GetChainCode(code, "TRON", nil) {
txHash = r.Get("id").String()
} else if code == chainCode.GetChainCode("POLYGON", nil) {
} else if chainCode.GetChainCode(code, "POLYGON", nil) {
txHash = r.Get("transactionHash").String()
} else if code == chainCode.GetChainCode("BSC", nil) {
} else if chainCode.GetChainCode(code, "BSC", nil) {
txHash = r.Get("transactionHash").String()
} else if code == chainCode.GetChainCode("FIL", nil) {
} else if chainCode.GetChainCode(code, "FIL", nil) {
txHash = r.Get("transactionHash").String()
} else if code == chainCode.GetChainCode("XRP", nil) {
} else if chainCode.GetChainCode(code, "XRP", nil) {
txHash = r.Get("hash").String()
}
return txHash
Expand Down
97 changes: 66 additions & 31 deletions common/chain/chain.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
package chain

import (
"encoding/json"
"io"
"os"

"github.com/sunjiangjun/xlog"
"github.com/tidwall/gjson"
)

var defaultChain = `
{
"ETH": 200,
"POLYGON": 201,
"BSC": 202,
"TRON": 205,
"BTC": 300,
"FIL": 301,
"XRP": 310
}
`
//var defaultChain = `
//{
// "ETH": [
// 200,
// 2001
// ],
// "POLYGON": [
// 201,
// 2011
// ],
// "BSC": [
// 202,
// 2021
// ],
// "TRON": [
// 205,
// 2051
// ],
// "BTC": [
// 300,
// 3001
// ],
// "FIL": [
// 301,
// 3011
// ],
// "XRP": [
// 310,
// 3101
// ]
//}
//`

var defaultChainCode = map[string]map[int64]int{"ETH": {200: 1, 2001: 1}, "POLYGON": {201: 1, 2011: 1}, "BSC": {202: 1}, "TRON": {205: 1}, "BTC": {300: 1}, "FIL": {301: 1}, "XRP": {310: 1}}

func LoadConfig(path string) (string, error) {
f, err := os.OpenFile(path, os.O_RDONLY, os.ModeAppend)
Expand All @@ -35,44 +58,56 @@ func LoadConfig(path string) (string, error) {
return string(b), nil
}

func LoadChainCodeFile(file string) map[string]int64 {
//default
body := defaultChain
func LoadChainCodeFile(file string) map[string]map[int64]int {

mp := make(map[string]map[int64]int)

//set customer config
if len(file) > 1 {
f, _ := LoadConfig(file)
if len(f) > 1 {
body = f
body, _ := LoadConfig(file)
if len(body) > 1 {
gjson.Parse(body).ForEach(func(key, v gjson.Result) bool {
k := key.String()
list := v.Array()
m := make(map[int64]int)
for _, v := range list {
code := v.Int()
m[code] = 1
}
mp[k] = m
return true
})
}
}

if len(body) < 1 {
return nil
}

mp := make(map[string]int64)
err := json.Unmarshal([]byte(body), &mp)
if err != nil {
return nil
if len(mp) < 1 {
// default
mp = defaultChainCode
}

return mp
}

func GetChainCode(chainName string, log *xlog.XLog) int64 {
func GetChainCode(chainCode int64, chainName string, log *xlog.XLog) bool {
if log == nil {
log = xlog.NewXLogger()
}
mp := LoadChainCodeFile("./chain.json")
if mp == nil {
log.Errorf("unknown all chainCode,this is a fatal error")
return 0
return false
}

if code, ok := mp[chainName]; ok {
return code
if m, ok := mp[chainName]; ok {
if _, ok := m[chainCode]; ok {
return true
} else {
log.Errorf("unknown chainCode:%v,please check whether the system supports this chain", chainCode)
return false
}
} else {
log.Errorf("unknown chainCode:%v,please check whether the system supports this chain", code)
return 0
log.Errorf("unknown chainCode:%v,please check whether the system supports this chain", chainCode)
return false
}
}
35 changes: 28 additions & 7 deletions common/chain/chain.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
{
"ETH": 200,
"POLYGON": 201,
"BSC": 202,
"TRON": 205,
"BTC": 300,
"FIL": 301,
"XRP": 310
"ETH": [
200,
2001
],
"POLYGON": [
201,
2011
],
"BSC": [
202,
2021
],
"TRON": [
205,
2051
],
"BTC": [
300,
3001
],
"FIL": [
301,
3011
],
"XRP": [
310,
3101
]
}
2 changes: 1 addition & 1 deletion common/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
)

func TestGetChainCode(t *testing.T) {
code := GetChainCode("ETH", xlog.NewXLogger())
code := GetChainCode(200, "ETH", xlog.NewXLogger())
log.Println(code)
}
Loading

0 comments on commit 1e4dc72

Please sign in to comment.