forked from DE-labtory/it-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
214 lines (159 loc) · 5.39 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"github.com/it-chain/it-chain-Engine/legacy/service"
"github.com/it-chain/it-chain-Engine/legacy/network/comm"
"github.com/it-chain/it-chain-Engine/legacy/auth"
"github.com/spf13/viper"
"github.com/it-chain/it-chain-Engine/common"
"strings"
"github.com/it-chain/it-chain-Engine/legacy/domain"
"time"
"encoding/base64"
"crypto/sha1"
"sync"
"fmt"
"net"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc"
pb "github.com/it-chain/it-chain-Engine/legacy/network/protos"
"golang.org/x/net/context"
"github.com/it-chain/it-chain-Engine/legacy/service/webhook"
"strconv"
)
type Node struct {
identity *domain.Peer
blockService service.BlockService
peerService service.PeerService
consensusService service.ConsensusService
smartContractService service.SmartContractService
transactionService service.TransactionService
webHookService webhook.WebhookService
connectionManager comm.ConnectionManager
crypto auth.Crypto
}
//todo msg publisher를 통한 receive Message분배 로직 추가
func NewNode(ip string) *Node{
//ip format (xxx.xxx.xxx.xxx:pppp)
node := &Node{}
////set baisc Info
node.identity = &domain.Peer{}
node.identity.IpAddress = strings.Split(ip,":")[0]
node.identity.Port = strings.Split(ip,":")[1]
//// crpyto
crpyto, err := auth.NewCryptoImpl(viper.GetString("key.defaultPath"),&auth.RSAKeyGenOpts{})
if err != nil{
common.Log.Errorln("crypto create error")
return nil
}
node.crypto = crpyto
node.identity.PeerID = node.GenerateID()
///// comm
connectionManager := comm.NewConnectionManagerImpl(crpyto)
node.connectionManager = connectionManager
//// peerService
_, pub, _ := node.crypto.GetKey()
node.identity.PubKey = pub.SKI()
node.identity.HeartBeat = 0
node.identity.TimeStamp = time.Now()
peerTable ,err := domain.NewPeerTable(node.identity)
if err != nil{
common.Log.Errorln("peerTable create error")
return nil
}
peerService := service.NewPeerServiceImpl(peerTable,connectionManager)
node.peerService = peerService
///// TransactionService
transactionService := service.NewTransactionService(viper.GetString("txDatabase.defaultPath"),connectionManager,peerService)
node.transactionService = transactionService
///// blockService
blockService := service.NewLedger(viper.GetString("ledger.defaultPath"))
node.blockService = blockService
///// smartContractService
smartContractService := service.NewSmartContractService(viper.GetString("smartcontract.defaultPath"),viper.GetString("smartContract.githubID"))
node.smartContractService = smartContractService
//// webHookService
webHookService,err := webhook.NewWebhookService()
node.webHookService = webHookService
p , _ :=strconv.Atoi(viper.GetString("webhook.port"))
go webHookService.Serve(p)
///// consensusService
consensusService := service.NewPBFTConsensusService(node.connectionManager,node.webHookService, node.peerService,node.blockService,node.identity,smartContractService,transactionService)
node.consensusService = consensusService
return node
}
func (n *Node) GenerateID() string{
pri, _, _ := n.crypto.GetKey()
h := sha1.New()
h.Write(pri.SKI())
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
func (n *Node) SearchBootNode(bootip string){
common.Log.Println("Searching peer", bootip)
peer, err := n.peerService.RequestPeer(bootip)
if err!=nil{
n.peerService.SetLeader(n.identity)
return
}
n.peerService.SetLeader(peer)
n.peerService.AddPeer(peer)
}
func (n *Node) GetPeer(context.Context, *pb.Empty) (*pb.Peer, error){
pp := domain.ToProtoPeer(*n.identity)
return pp,nil
}
func (n *Node) PostTransaction(context context.Context,ptxData *pb.TxData) (*pb.Transaction, error){
common.Log.Println("Request PostTransaction")
if ptxData.Params == nil{
// error 처리
}
if ptxData.ContractID == ""{
// error
}
txData := domain.FromProtoTxData(*ptxData)
transaction,err := n.transactionService.CreateTransaction(txData)
if err !=nil{
// error
}
t := domain.ToProtoTransaction(*transaction)
return t, nil
}
func (n* Node) Run() {
common.Log.Println("Run it-chain")
lis, err := net.Listen("tcp", n.identity.GetEndPoint())
if err != nil {
common.Log.Fatalf("failed to listen: %v", err)
}
server := grpc.NewServer()
pb.RegisterStreamServiceServer(server,n.connectionManager)
pb.RegisterPeerServiceServer(server,n)
pb.RegisterTransactionServiceServer(server,n)
reflection.Register(server)
common.Log.Println("It-chain peer is on:",n.identity.Port)
if err := server.Serve(lis); err != nil {
common.Log.Fatalf("failed to serve: %v", err)
server.Stop()
lis.Close()
}
}
func PrintLogo(){
fmt.Println(`
___ _________ ________ ___ ___ ________ ___ ________
|\ \|\___ ___\ |\ ____\|\ \|\ \|\ __ \|\ \|\ ___ \
\ \ \|___ \ \_|____________\ \ \___|\ \ \\\ \ \ \|\ \ \ \ \ \\ \ \
\ \ \ \ \ \|\____________\ \ \ \ \ __ \ \ __ \ \ \ \ \\ \ \
\ \ \ \ \ \|____________|\ \ \____\ \ \ \ \ \ \ \ \ \ \ \ \\ \ \
\ \__\ \ \__\ \ \_______\ \__\ \__\ \__\ \__\ \__\ \__\\ \__\
\|__| \|__| \|_______|\|__|\|__|\|__|\|__|\|__|\|__| \|__|
`)
}
func main() {
PrintLogo()
ip := viper.GetString("node.ip")
bootIp := viper.GetString("bootNode.ip")
n := NewNode(ip)
n.SearchBootNode(bootIp)
wg := sync.WaitGroup{}
wg.Add(1)
go n.Run()
wg.Wait()
}