/
engine.go
77 lines (59 loc) · 1.79 KB
/
engine.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
package engine
import (
"math/big"
"github.com/ethereum/go-ethereum/common/math"
"github.com/mosaicnetworks/evm-lite/src/config"
"github.com/mosaicnetworks/evm-lite/src/consensus"
"github.com/mosaicnetworks/evm-lite/src/currency"
"github.com/mosaicnetworks/evm-lite/src/service"
"github.com/mosaicnetworks/evm-lite/src/state"
)
// Engine is the actor that coordinates State, Service and Consensus
type Engine struct {
state *state.State
service *service.Service
consensus consensus.Consensus
}
// NewEngine instantiates a new Engine with coupled State, Service, and Consensus
func NewEngine(config config.Config, consensus consensus.Consensus) (*Engine, error) {
logger := config.Logger()
submitCh := make(chan []byte)
state, err := state.NewState(
config.DbFile,
config.Cache,
config.Genesis,
logger.WithField("component", "state"))
if err != nil {
logger.WithError(err).Error("engine.go:NewEngine() state.NewState")
return nil, err
}
minGasPrice, ok := math.ParseBig256(currency.ExpandCurrencyString(config.MinGasPrice))
if !ok {
logger.WithField("min-gas-price", config.MinGasPrice).Debug("Could not parse min-gas-price")
minGasPrice = big.NewInt(0)
}
service := service.NewService(
config.EthAPIAddr,
state,
submitCh,
minGasPrice,
logger.WithField("component", "service"))
if err := consensus.Init(state, service); err != nil {
logger.WithError(err).Error("engine.go:NewEngine() Consensus.Init")
return nil, err
}
service.SetInfoCallback(consensus.Info)
engine := &Engine{
state: state,
service: service,
consensus: consensus,
}
return engine, nil
}
// Run starts the engine's Service asynchronously and starts the Consensus
// system synchronously
func (e *Engine) Run() error {
go e.service.Run()
e.consensus.Run()
return nil
}