-
Notifications
You must be signed in to change notification settings - Fork 41
/
impl.go
53 lines (42 loc) · 1.22 KB
/
impl.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package impl
import (
"github.com/aergoio/aergo/chain"
"github.com/aergoio/aergo/config"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/consensus/impl/dpos"
"github.com/aergoio/aergo/consensus/impl/sbp"
"github.com/aergoio/aergo/pkg/component"
)
// New returns consensus.Consensus based on the configuration parameters.
func New(cfg *config.Config, hub *component.ComponentHub, cs *chain.ChainService) (consensus.Consensus, error) {
var (
c consensus.Consensus
err error
blockInterval int64
)
if chain.IsPublic() {
blockInterval = 1
} else {
blockInterval = cfg.Consensus.BlockInterval
}
consensus.InitBlockInterval(blockInterval)
if c, err = newConsensus(cfg, hub, cs); err == nil {
// Link mutual references.
cs.SetChainConsensus(c)
}
return c, err
}
func newConsensus(cfg *config.Config, hub *component.ComponentHub,
cs *chain.ChainService) (consensus.Consensus, error) {
cdb := cs.CDB()
sdb := cs.SDB()
impl := map[string]consensus.Constructor{
"dpos": dpos.GetConstructor(cfg, hub, cdb, sdb), // DPoS
"sbp": sbp.GetConstructor(cfg, hub, cdb, sdb), // Simple BP
}
return impl[cdb.GetGenesisInfo().ConsensusType()]()
}