/
config.go
146 lines (132 loc) · 4.22 KB
/
config.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package amana
import (
"fmt"
"github.com/Qitmeer/qng/config"
"github.com/Qitmeer/qng/core/protocol"
mconsensus "github.com/Qitmeer/qng/meerevm/amana/consensus"
mcommon "github.com/Qitmeer/qng/meerevm/common"
"github.com/Qitmeer/qng/meerevm/eth"
mparams "github.com/Qitmeer/qng/meerevm/params"
qparams "github.com/Qitmeer/qng/params"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/params"
"net"
"path/filepath"
)
var (
ClientIdentifier = mconsensus.Identifier
)
func MakeConfig(datadir string) (*eth.Config, error) {
genesis := Genesis()
econfig := ethconfig.Defaults
econfig.NetworkId = genesis.Config.ChainID.Uint64()
econfig.Genesis = genesis
econfig.ConsensusEngine = createConsensusEngine
nodeConf := node.DefaultConfig
nodeConf.DataDir = datadir
nodeConf.Name = ClientIdentifier
nodeConf.Version = params.VersionWithMeta
nodeConf.HTTPModules = append(nodeConf.HTTPModules, "eth")
nodeConf.WSModules = append(nodeConf.WSModules, "eth")
nodeConf.IPCPath = ClientIdentifier + ".ipc"
nodeConf.KeyStoreDir = filepath.Join(datadir, "keystore")
var p2pPort int
nodeConf.HTTPPort, nodeConf.WSPort, nodeConf.AuthPort, p2pPort = getDefaultPort()
nodeConf.P2P.ListenAddr = fmt.Sprintf(":%d", p2pPort)
nodeConf.P2P.BootstrapNodes = getBootstrapNodes(p2pPort)
cfg := ð.Config{
Eth: econfig,
Node: nodeConf,
Metrics: metrics.DefaultConfig,
}
eth.SetDNSDiscoveryDefaults(cfg)
return cfg, nil
}
func MakeParams(cfg *config.Config) (*eth.Config, []string, error) {
ecfg, err := MakeConfig(cfg.DataDir)
if err != nil {
return ecfg, nil, err
}
args, err := mcommon.ProcessEnv(cfg.AmanaEnv, ecfg.Node.Name, nil)
return ecfg, args, err
}
func getDefaultPort() (int, int, int, int) {
switch qparams.ActiveNetParams.Net {
case protocol.MainNet:
return 8525, 8526, 8527, 8528
case protocol.TestNet:
return 18525, 18526, 18527, 18528
case protocol.MixNet:
return 28525, 28526, 28527, 28528
default:
return 38525, 38526, 38527, 38528
}
}
func createConsensusEngine(stack *node.Node, ethashConfig *ethash.Config, cliqueConfig *params.CliqueConfig, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
engine := mconsensus.New(ChainConfig().Clique, db)
return engine
}
func getBootstrapNodes(port int) []*enode.Node {
db, _ := enode.OpenDB("")
key, _ := crypto.GenerateKey()
ln := enode.NewLocalNode(db, key)
ln.SetFallbackIP(net.IP{127, 0, 0, 1})
ln.SetFallbackUDP(port)
urls := []string{}
switch qparams.ActiveNetParams.Net {
case protocol.MainNet:
urls = append(urls, ln.Node().String())
case protocol.TestNet:
urls = append(urls, "enr:-KO4QMjjjBd9NKfXaf2LCEmEVcl7q2m9ArnKOJxGaIvkC5L3Fn6zqJxi1ON4nFkH3Go4fE5oDa7uUxLWeBgsSHbWb3SGAYdv5iIng2V0aMfGhE5aFCGAgmlkgnY0gmlwhC0gC3eJc2VjcDI1NmsxoQK6ou0UT10hfVO8-b-2zWawj1_l8xflj764RASTmM8f64RzbmFwwIN0Y3CCSGCDdWRwgkhg")
case protocol.MixNet:
urls = append(urls, ln.Node().String())
default:
urls = append(urls, ln.Node().String())
}
bootstrapNodes := []*enode.Node{}
for _, url := range urls {
if url != "" {
node, err := enode.Parse(enode.ValidSchemes, url)
if err != nil {
log.Crit("Bootstrap URL invalid", "enode", url, "err", err)
continue
}
bootstrapNodes = append(bootstrapNodes, node)
}
}
return bootstrapNodes
}
func ChainConfig() *params.ChainConfig {
switch qparams.ActiveNetParams.Net {
case protocol.MainNet:
return mparams.AmanaChainConfig
case protocol.TestNet:
return mparams.AmanaTestnetChainConfig
case protocol.MixNet:
return mparams.AmanaMixnetChainConfig
case protocol.PrivNet:
return mparams.AmanaPrivnetChainConfig
}
return nil
}
func Genesis() *core.Genesis {
switch qparams.ActiveNetParams.Net {
case protocol.MainNet:
return AmanaGenesis()
case protocol.TestNet:
return AmanaTestnetGenesis()
case protocol.MixNet:
return AmanaMixnetGenesis()
case protocol.PrivNet:
return AmanaPrivnetGenesis()
}
return nil
}