forked from seeleteam/go-seele
/
factory.go
58 lines (49 loc) · 1.76 KB
/
factory.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
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package factory
import (
"crypto/ecdsa"
"fmt"
"path/filepath"
"github.com/seeleteam/go-seele/common"
"github.com/seeleteam/go-seele/common/errors"
"github.com/seeleteam/go-seele/consensus"
"github.com/seeleteam/go-seele/consensus/ethash"
"github.com/seeleteam/go-seele/consensus/istanbul"
"github.com/seeleteam/go-seele/consensus/istanbul/backend"
"github.com/seeleteam/go-seele/consensus/pow"
"github.com/seeleteam/go-seele/consensus/spow"
"github.com/seeleteam/go-seele/database/leveldb"
)
// GetConsensusEngine get consensus engine according to miner algorithm name
// WARNING: engine may be a heavy instance. we should have as less as possible in our process.
func GetConsensusEngine(minerAlgorithm string, folder string, percentage int) (consensus.Engine, error) {
var minerEngine consensus.Engine
if minerAlgorithm == common.EthashAlgorithm {
minerEngine = ethash.New(ethash.GetDefaultConfig(), nil, false)
} else if minerAlgorithm == common.Sha256Algorithm {
minerEngine = pow.NewEngine(1)
} else if minerAlgorithm == common.SpowAlgorithm {
minerEngine = spow.NewSpowEngine(1, folder, percentage)
} else {
return nil, fmt.Errorf("unknown miner algorithm")
}
return minerEngine, nil
}
func GetBFTEngine(privateKey *ecdsa.PrivateKey, folder string) (consensus.Engine, error) {
path := filepath.Join(folder, common.BFTDataFolder)
db, err := leveldb.NewLevelDB(path)
if err != nil {
return nil, errors.NewStackedError(err, "create bft folder failed")
}
return backend.New(istanbul.DefaultConfig, privateKey, db), nil
}
func MustGetConsensusEngine(minerAlgorithm string) consensus.Engine {
engine, err := GetConsensusEngine(minerAlgorithm, "temp", 10)
if err != nil {
panic(err)
}
return engine
}