From 10d1cdbfcd0a8b472bdc2a6c67d0d50cfe29aec0 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 22 Jun 2024 12:41:20 +0800 Subject: [PATCH] 1 --- server/backtask/mt/Contract.go | 92 ++++++++++++++++++++++++++++++++++ server/backtask/mt/export.go | 3 ++ 2 files changed, 95 insertions(+) create mode 100644 server/backtask/mt/Contract.go diff --git a/server/backtask/mt/Contract.go b/server/backtask/mt/Contract.go new file mode 100644 index 00000000..48ea85e1 --- /dev/null +++ b/server/backtask/mt/Contract.go @@ -0,0 +1,92 @@ +package mt + +import ( + "q5" + "fmt" + "os" + "strings" + "encoding/json" + "bufio" +) + +type Contract struct { + name string + address string +} + +type ContractTable struct { + netIdNameHash *q5.ConcurrentMap[string, *Contract] +} + +func (this *Contract) GetName() string { + return this.name +} + +func (this *Contract) GetAddress() string { + return this.address +} + +func (this *ContractTable) IsNoLoad() bool { + return false +} + +func (this *ContractTable) Load() { + this.netIdNameHash = new(q5.ConcurrentMap[string, *Contract]) + nets := []interface{}{} + { + if f, err := os.Open("../config/nets.json"); err == nil { + jsonStr, _ := bufio.NewReader(f).ReadString(0) + if err := json.Unmarshal([]byte(jsonStr), &nets); err != nil { + panic(fmt.Sprintf("load metafile json decode error %s %s", "nets.json", err)) + } + } else { + panic(fmt.Sprintf("load metafile error %s %s", "nets.json", err)) + } + } + { + for _, val := range nets { + netId := q5.SafeToInt32(val) + fileName := fmt.Sprintf("../config/nets/%d/contract.json", netId) + if f, err := os.Open(fileName); err == nil { + jsonStr, _ := bufio.NewReader(f).ReadString(0) + contracts := []struct { + Name string `json:"name"` + Address string `json:"address"` + }{} + if err := json.Unmarshal([]byte(jsonStr), &contracts); err != nil { + panic(fmt.Sprintf("load metafile json decode error %s %s", "contract.json", err)) + } + for _, val2 := range contracts { + p := new(Contract) + p.name = q5.SafeToString(val2.Name) + p.address = strings.ToLower(q5.SafeToString(val2.Address)) + key := fmt.Sprintf("%d_%s", netId, p.name) + this.netIdNameHash.Store(key, p) + } + } else { + panic(fmt.Sprintf("load metafile error %s %s", "contract.json", err)) + } + } + } +} + +func (this *ContractTable) PreInit1() { + +} + +func (this *ContractTable) ElementsInit(int) { + +} + +func (this *ContractTable) PostInit1() { + +} + +func (this *ContractTable) GetByNetIdName(netId int32, name string) *Contract { + key := fmt.Sprintf("%d_%s", netId, name) + if v, ok := this.netIdNameHash.Load(key); ok { + return *v + } else { + return nil + } +} diff --git a/server/backtask/mt/export.go b/server/backtask/mt/export.go index 6a79d216..c035933b 100644 --- a/server/backtask/mt/export.go +++ b/server/backtask/mt/export.go @@ -12,6 +12,7 @@ type table struct { Config *ConfigTable RankSeason *RankSeasonTable HashRateCommon *HashRateCommonTable + Contract *ContractTable } var Table = f5.New(func(this *table) { @@ -49,4 +50,6 @@ var Table = f5.New(func(this *table) { this.FileName = "../res/HashrateCommon@HashrateCommon.json" this.PrimKey = "id" }) + + this.Contract = new(ContractTable) })