113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package mt
|
|
|
|
import (
|
|
"encoding/json"
|
|
"f5"
|
|
"fmt"
|
|
"q5"
|
|
"strings"
|
|
)
|
|
|
|
type Contract struct {
|
|
name string
|
|
address string
|
|
}
|
|
|
|
type ContractTable struct {
|
|
netIdNameHash *q5.ConcurrentMap[string, *Contract]
|
|
netIdAddressHash *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])
|
|
this.netIdAddressHash = new(q5.ConcurrentMap[string, *Contract])
|
|
nets := []interface{}{}
|
|
{
|
|
if jsonStr, err := f5.ReadJsonFile("../config/nets.json"); err == nil {
|
|
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 jsonStr, err := f5.ReadJsonFile(fileName); err == nil {
|
|
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)
|
|
}
|
|
{
|
|
key := fmt.Sprintf("%d_%s", netId, p.address)
|
|
this.netIdAddressHash.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
|
|
}
|
|
}
|
|
|
|
func (this *ContractTable) GetByNetIdAddress(netId int32, address string) *Contract {
|
|
key := fmt.Sprintf("%d_%s", netId, address)
|
|
if v, ok := this.netIdAddressHash.Load(key); ok {
|
|
return *v
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *ContractTable) Traverse(cb func(*Contract) bool) {
|
|
this.netIdNameHash.Range(func(k string, v *Contract) bool {
|
|
return cb(v)
|
|
})
|
|
}
|