forked from zhengq1/DNA-GYNSH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (101 loc) · 2.72 KB
/
main.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
package main
import (
"DNA/account"
"DNA/common/config"
"DNA/common/log"
"DNA/consensus/dbft"
"DNA/core/ledger"
"DNA/core/store/ChainStore"
"DNA/core/transaction"
"DNA/crypto"
"DNA/net"
"DNA/net/httpjsonrpc"
"DNA/net/httprestful"
"DNA/net/httpwebsocket"
"DNA/net/protocol"
"os"
"runtime"
"time"
)
const (
DefaultMultiCoreNum = 4
)
func init() {
log.Init(log.Path, log.Stdout)
var coreNum int
if config.Parameters.MultiCoreNum > DefaultMultiCoreNum {
coreNum = int(config.Parameters.MultiCoreNum)
} else {
coreNum = DefaultMultiCoreNum
}
log.Debug("The Core number is ", coreNum)
runtime.GOMAXPROCS(coreNum)
}
func main() {
var acct *account.Account
var blockChain *ledger.Blockchain
var err error
var noder protocol.Noder
log.Trace("Node version: ", config.Version)
log.Info("0. Loading the Ledger")
ledger.DefaultLedger = new(ledger.Ledger)
ledger.DefaultLedger.Store, err = ChainStore.NewLedgerStore()
defer ledger.DefaultLedger.Store.Close()
if err != nil {
log.Fatal("open LedgerStore err:", err)
os.Exit(1)
}
ledger.DefaultLedger.Store.InitLedgerStore(ledger.DefaultLedger)
transaction.TxStore = ledger.DefaultLedger.Store
crypto.SetAlg(config.Parameters.EncryptAlg)
log.Info("1. Open the account")
client := account.GetClient()
if client == nil {
log.Error("Can't get local account.")
goto ERROR
}
acct, err = client.GetDefaultAccount()
if err != nil {
log.Error(err)
goto ERROR
}
log.Debug("The Node's PublicKey ", acct.PublicKey)
ledger.StandbyBookKeepers = account.GetBookKeepers()
log.Info("3. BlockChain init")
blockChain, err = ledger.NewBlockchainWithGenesisBlock(ledger.StandbyBookKeepers, account.GetStateUpdater())
if err != nil {
log.Error(err, " BlockChain generate failed")
goto ERROR
}
ledger.DefaultLedger.Blockchain = blockChain
log.Info("4. Start the P2P networks")
// Don't need two return value.
noder = net.StartProtocol(acct.PublicKey)
httpjsonrpc.RegistRpcNode(noder)
time.Sleep(20 * time.Second)
noder.SyncNodeHeight()
noder.WaitForFourPeersStart()
if protocol.SERVICENODENAME != config.Parameters.NodeType {
log.Info("5. Start DBFT Services")
dbftServices := dbft.NewDbftService(client, "logdbft", noder)
httpjsonrpc.RegistDbftService(dbftServices)
go dbftServices.Start()
time.Sleep(5 * time.Second)
}
log.Info("--Start the RPC interface")
go httpjsonrpc.StartRPCServer()
go httpjsonrpc.StartLocalServer()
go httprestful.StartServer(noder)
go httpwebsocket.StartServer(noder)
for {
time.Sleep(dbft.GenBlockTime)
log.Trace("BlockHeight = ", ledger.DefaultLedger.Blockchain.BlockHeight)
isNeedNewFile := log.CheckIfNeedNewFile()
if isNeedNewFile == true {
log.ClosePrintLog()
log.Init(log.Path, os.Stdout)
}
}
ERROR:
os.Exit(1)
}