-
Notifications
You must be signed in to change notification settings - Fork 48
/
manager.go
91 lines (79 loc) · 2.33 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package parachain
import (
"fmt"
"strconv"
"github.com/spf13/viper"
"github.com/xuperchain/xupercore/kernel/contract"
"github.com/xuperchain/xupercore/kernel/engines/xuperos/common"
)
const (
ConfigName = "engine.yaml"
)
// Deprecated
// use Config instead
type ParaChainConfig = Config
type Config struct {
MinNewChainAmount string
NewChainWhiteList map[string]bool `yaml:"newChainWhiteList,omitempty"` //能创建链的address白名单
}
// Manager
type Manager struct {
Ctx *Ctx
}
// NewParaChainManager create instance of ParaChain
func NewParaChainManager(ctx *Ctx) (*Manager, error) {
if ctx == nil || ctx.Contract == nil || ctx.BcName == "" {
return nil, fmt.Errorf("parachain ctx set error")
}
conf, err := loadConfig(ctx.ChainCtx.EngCtx.EnvCfg.GenConfFilePath(ConfigName))
if err != nil {
return nil, err
}
minNewChainAmount, err := strconv.ParseInt(conf.MinNewChainAmount, 10, 64)
if err != nil {
return nil, err
}
t := NewParaChainContract(ctx.BcName, minNewChainAmount, conf.NewChainWhiteList, ctx.ChainCtx)
register := ctx.Contract.GetKernRegistry()
// 注册合约方法
kMethods := map[string]contract.KernMethod{
"createChain": t.createChain,
"editGroup": t.editGroup,
"getGroup": t.getGroup,
"stopChain": t.stopChain,
}
for method, f := range kMethods {
if _, err := register.GetKernMethod(ParaChainKernelContract, method); err != nil {
register.RegisterKernMethod(ParaChainKernelContract, method, f)
}
}
// 仅主链绑定handleCreateChain 从链上下文中获取链绑定的异步任务worker
asyncTask := map[string]common.TaskHandler{
"CreateBlockChain": t.handleCreateChain,
"StopBlockChain": t.handleStopChain,
"RefreshBlockChain": t.handleRefreshChain,
}
for task, f := range asyncTask {
ctx.ChainCtx.Asyncworker.RegisterHandler(ParaChainKernelContract, task, f)
}
mg := &Manager{
Ctx: ctx,
}
return mg, nil
}
func loadConfig(file string) (*Config, error) {
viperObj := viper.New()
viperObj.SetConfigFile(file)
err := viperObj.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("read config failed.path:%s,err:%v", file, err)
}
cfg := &Config{
MinNewChainAmount: "100",
NewChainWhiteList: map[string]bool{},
}
if err = viperObj.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("unmatshal config failed.path:%s,err:%v", file, err)
}
return cfg, nil
}