-
Notifications
You must be signed in to change notification settings - Fork 9
/
master.go
95 lines (78 loc) · 2.23 KB
/
master.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
92
93
94
95
package master
import (
"path/filepath"
"strings"
fcom "github.com/meshplus/hyperbench-common/common"
"github.com/meshplus/hyperbench/plugins/blockchain"
"github.com/meshplus/hyperbench/vm"
"github.com/meshplus/hyperbench/vm/base"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// Master is the interface of mater node
type Master interface {
// Prepare is used to prepare
Prepare() error
// GetContext generate the context, which will be passed to Worker
GetContext() ([]byte, error)
// Statistic query the remote statistic data from chain
Statistic(from, to int64) (*fcom.RemoteStatistic, error)
// LogStatus records blockheight and time
LogStatus() (int64, error)
}
// LocalMaster is the implement of master in local
type LocalMaster struct {
masterVM vm.VM
params []string
}
// Prepare is used to prepare
func (m *LocalMaster) Prepare() (err error) {
// call user hook
err = m.masterVM.BeforeDeploy()
if err != nil {
return errors.Wrap(err, "can not call user hook `BeforeDeploy`")
}
// prepare contract
err = m.masterVM.DeployContract()
if err != nil {
return errors.Wrap(err, "can not deploy contract")
}
return nil
}
// GetContext generate the context, which will be passed to Worker
func (m *LocalMaster) GetContext() ([]byte, error) {
err := m.masterVM.BeforeGet()
if err != nil {
return nil, err
}
return m.masterVM.GetContext()
}
// Statistic query the remote statistic data from chain
func (m *LocalMaster) Statistic(from, to int64) (*fcom.RemoteStatistic, error) {
return m.masterVM.Statistic(from, to)
}
// LogStatus records blockheight and time
func (m *LocalMaster) LogStatus() (end int64, err error) {
return m.masterVM.LogStatus()
}
// NewLocalMaster create LocalMaster.
func NewLocalMaster() (*LocalMaster, error) {
blockchain.InitPlugin()
params := viper.GetStringSlice(fcom.ClientContractArgsPath)
scriptPath := viper.GetString(fcom.ClientScriptPath)
vmType := strings.TrimPrefix(filepath.Ext(scriptPath), ".")
masterVM, err := vm.NewVM(vmType, base.ConfigBase{
Path: scriptPath,
Ctx: fcom.VMContext{
WorkerIdx: -1,
VMIdx: -1,
},
})
if err != nil {
return nil, errors.Wrap(err, "can not create master")
}
return &LocalMaster{
masterVM: masterVM,
params: params,
}, nil
}