Skip to content

Commit

Permalink
perf: load chainCode map
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhongtao committed Dec 4, 2023
1 parent 1e4dc72 commit 9df74fc
Showing 1 changed file with 17 additions and 45 deletions.
62 changes: 17 additions & 45 deletions common/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,15 @@ import (
"github.com/tidwall/gjson"
)

//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}}
var defaultChainCode = map[string]map[int64]int8{
"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 @@ -58,18 +33,16 @@ func LoadConfig(path string) (string, error) {
return string(b), nil
}

func LoadChainCodeFile(file string) map[string]map[int64]int {

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

func LoadChainCodeFile(file string) map[string]map[int64]int8 {
//set customer config
mp := make(map[string]map[int64]int8)
if len(file) > 1 {
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)
m := make(map[int64]int8)
for _, v := range list {
code := v.Int()
m[code] = 1
Expand All @@ -81,19 +54,18 @@ func LoadChainCodeFile(file string) map[string]map[int64]int {

}

if len(mp) < 1 {
// default
mp = defaultChainCode
}

return mp
}

func GetChainCode(chainCode int64, chainName string, log *xlog.XLog) bool {
if log == nil {
log = xlog.NewXLogger()
}
mp := LoadChainCodeFile("./chain.json")
mp := defaultChainCode

//todo load chainCode if it is necessary, but it is not efficient because It loads configuration files very frequently
//mp = LoadChainCodeFile("./chain.json")

if mp == nil {
log.Errorf("unknown all chainCode,this is a fatal error")
return false
Expand Down

0 comments on commit 9df74fc

Please sign in to comment.