forked from klaytn/klaytn
/
mainbridge.go
543 lines (471 loc) · 15.6 KB
/
mainbridge.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// Modifications Copyright 2019 The klaytn Authors
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from eth/backend.go (2018/06/04).
// Modified and improved for the klaytn development.
package sc
import (
"errors"
"fmt"
"github.com/klaytn/klaytn/accounts"
"github.com/klaytn/klaytn/api"
"github.com/klaytn/klaytn/blockchain"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/crypto"
"github.com/klaytn/klaytn/event"
"github.com/klaytn/klaytn/networks/p2p"
"github.com/klaytn/klaytn/networks/p2p/discover"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/klaytn/klaytn/node"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/storage/database"
"io"
"math/big"
"net"
"sync"
"time"
)
const (
// chainHeadChanSize is the size of channel listening to ChainHeadEvent.
chainHeadChanSize = 10000
chainLogChanSize = 10000
transactionChanSize = 10000
rpcBufferSize = 1024
)
// MainBridgeInfo represents a short summary of the Klaytn sub-protocol metadata
// known about the host peer.
type MainBridgeInfo struct {
Network uint64 `json:"network"` // Klaytn network ID
BlockScore *big.Int `json:"blockscore"` // Total blockscore of the host's blockchain
Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
}
// MainBridge implements the main bridge of service chain.
type MainBridge struct {
config *SCConfig
// DB interfaces
chainDB database.DBManager // Blockchain database
eventMux *event.TypeMux
accountManager *accounts.Manager
gasPrice *big.Int
networkId uint64
netRPCService *api.PublicNetAPI
lock sync.RWMutex // Protects the variadic fields (e.g. gas price)
bridgeServer p2p.Server
ctx *node.ServiceContext
maxPeers int
APIBackend *MainBridgeAPI
// channels for fetcher, syncer, txsyncLoop
newPeerCh chan BridgePeer
quitSync chan struct{}
noMorePeers chan struct{}
// wait group is used for graceful shutdowns during downloading
// and processing
wg sync.WaitGroup
pmwg sync.WaitGroup
blockchain *blockchain.BlockChain
txPool *blockchain.TxPool
chainHeadCh chan blockchain.ChainHeadEvent
chainHeadSub event.Subscription
logsCh chan []*types.Log
logsSub event.Subscription
txCh chan blockchain.NewTxsEvent
txSub event.Subscription
peers *bridgePeerSet
handler *MainBridgeHandler
eventhandler *MainChainEventHandler
rpcServer *rpc.Server
rpcConn net.Conn
rpcResponseCh chan []byte
}
// NewMainBridge creates a new MainBridge object (including the
// initialisation of the common MainBridge object)
func NewMainBridge(ctx *node.ServiceContext, config *SCConfig) (*MainBridge, error) {
chainDB := CreateDB(ctx, config, "scchaindata")
mb := &MainBridge{
config: config,
chainDB: chainDB,
peers: newBridgePeerSet(),
newPeerCh: make(chan BridgePeer),
noMorePeers: make(chan struct{}),
eventMux: ctx.EventMux,
accountManager: ctx.AccountManager,
networkId: config.NetworkId,
ctx: ctx,
chainHeadCh: make(chan blockchain.ChainHeadEvent, chainHeadChanSize),
logsCh: make(chan []*types.Log, chainLogChanSize),
txCh: make(chan blockchain.NewTxsEvent, transactionChanSize),
quitSync: make(chan struct{}),
maxPeers: config.MaxPeer,
rpcResponseCh: make(chan []byte),
}
logger.Info("Initialising Klaytn-Bridge protocol", "network", config.NetworkId)
mb.APIBackend = &MainBridgeAPI{mb}
var err error
mb.handler, err = NewMainBridgeHandler(mb.config, mb)
if err != nil {
return nil, err
}
mb.eventhandler, err = NewMainChainEventHandler(mb, mb.handler)
if err != nil {
return nil, err
}
mb.rpcServer = rpc.NewServer()
p1, p2 := net.Pipe()
mb.rpcConn = p1
go mb.rpcServer.ServeCodec(rpc.NewJSONCodec(p2), rpc.OptionMethodInvocation|rpc.OptionSubscriptions)
go func() {
for {
data := make([]byte, rpcBufferSize)
rlen, err := mb.rpcConn.Read(data)
if err != nil {
if err == io.EOF {
logger.Trace("EOF from the rpc server pipe")
time.Sleep(100 * time.Millisecond)
continue
} else {
// If no one closes the pipe, this situation should not happen.
logger.Error("failed to read from the rpc pipe", "err", err, "rlen", rlen)
return
}
}
logger.Trace("mainbridge message from rpc server pipe", "rlen", rlen)
err = mb.SendRPCResponseData(data[:rlen])
if err != nil {
logger.Error("failed to send response data from RPC server pipe", err)
}
}
}()
return mb, nil
}
// CreateDB creates the chain database.
func CreateDB(ctx *node.ServiceContext, config *SCConfig, name string) database.DBManager {
// OpenFilesLimit and LevelDBCacheSize are used by minimum value.
dbc := &database.DBConfig{Dir: name, DBType: database.LevelDB}
return ctx.OpenDatabase(dbc)
}
// BridgePeerSet implements PeerSetManager
func (mb *MainBridge) BridgePeerSet() *bridgePeerSet {
return mb.peers
}
// APIs returns the collection of RPC services the Klaytn sc package offers.
// NOTE, some of these services probably need to be moved to somewhere else.
func (mb *MainBridge) APIs() []rpc.API {
// Append all the local APIs and return
return []rpc.API{
{
Namespace: "mainbridge",
Version: "1.0",
Service: mb.APIBackend,
Public: true,
},
{
Namespace: "mainbridge",
Version: "1.0",
Service: mb.netRPCService,
Public: true,
},
}
}
func (mb *MainBridge) AccountManager() *accounts.Manager { return mb.accountManager }
func (mb *MainBridge) EventMux() *event.TypeMux { return mb.eventMux }
func (mb *MainBridge) ChainDB() database.DBManager { return mb.chainDB }
func (mb *MainBridge) IsListening() bool { return true } // Always listening
func (mb *MainBridge) ProtocolVersion() int { return int(mb.SCProtocol().Versions[0]) }
func (mb *MainBridge) NetVersion() uint64 { return mb.networkId }
func (mb *MainBridge) Components() []interface{} {
return nil
}
func (mb *MainBridge) SetComponents(components []interface{}) {
for _, component := range components {
switch v := component.(type) {
case *blockchain.BlockChain:
mb.blockchain = v
// event from core-service
mb.chainHeadSub = mb.blockchain.SubscribeChainHeadEvent(mb.chainHeadCh)
mb.logsSub = mb.blockchain.SubscribeLogsEvent(mb.logsCh)
case *blockchain.TxPool:
mb.txPool = v
// event from core-service
mb.txSub = mb.txPool.SubscribeNewTxsEvent(mb.txCh)
case []rpc.API:
logger.Debug("p2p rpc registered", "len(v)", len(v))
for _, api := range v {
if api.Public && api.Namespace == "klay" {
logger.Error("p2p rpc registered", "namespace", api.Namespace)
if err := mb.rpcServer.RegisterName(api.Namespace, api.Service); err != nil {
logger.Error("pRPC failed to register", "namespace", api.Namespace)
}
}
}
}
}
mb.pmwg.Add(1)
go mb.loop()
}
// Protocols implements node.Service, returning all the currently configured
// network protocols to start.
func (mb *MainBridge) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
func (mb *MainBridge) SCProtocol() SCProtocol {
return SCProtocol{
Name: SCProtocolName,
Versions: SCProtocolVersion,
Lengths: SCProtocolLength,
}
}
// NodeInfo retrieves some protocol metadata about the running host node.
func (mb *MainBridge) NodeInfo() *MainBridgeInfo {
currentBlock := mb.blockchain.CurrentBlock()
return &MainBridgeInfo{
Network: mb.networkId,
BlockScore: mb.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
Genesis: mb.blockchain.Genesis().Hash(),
Config: mb.blockchain.Config(),
Head: currentBlock.Hash(),
}
}
// getChainID returns the current chain id.
func (mb *MainBridge) getChainID() *big.Int {
return mb.blockchain.Config().ChainID
}
// Start implements node.Service, starting all internal goroutines needed by the
// Klaytn protocol implementation.
func (mb *MainBridge) Start(srvr p2p.Server) error {
serverConfig := p2p.Config{}
serverConfig.PrivateKey = mb.ctx.NodeKey()
serverConfig.Name = mb.ctx.NodeType().String()
serverConfig.Logger = logger
serverConfig.ListenAddr = mb.config.MainBridgePort
serverConfig.MaxPhysicalConnections = mb.maxPeers
serverConfig.NoDiscovery = true
serverConfig.EnableMultiChannelServer = false
serverConfig.NoDial = true
scprotocols := make([]p2p.Protocol, 0, len(mb.SCProtocol().Versions))
for i, version := range mb.SCProtocol().Versions {
// Compatible; initialise the sub-protocol
version := version
scprotocols = append(scprotocols, p2p.Protocol{
Name: mb.SCProtocol().Name,
Version: version,
Length: mb.SCProtocol().Lengths[i],
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
peer := mb.newPeer(int(version), p, rw)
pubKey, _ := p.ID().Pubkey()
addr := crypto.PubkeyToAddress(*pubKey)
peer.SetAddr(addr)
select {
case mb.newPeerCh <- peer:
mb.wg.Add(1)
defer mb.wg.Done()
return mb.handle(peer)
case <-mb.quitSync:
return p2p.DiscQuitting
}
},
NodeInfo: func() interface{} {
return mb.NodeInfo()
},
PeerInfo: func(id discover.NodeID) interface{} {
if p := mb.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
return p.Info()
}
return nil
},
})
}
mb.bridgeServer = p2p.NewServer(serverConfig)
mb.bridgeServer.AddProtocols(scprotocols)
if err := mb.bridgeServer.Start(); err != nil {
return errors.New("fail to bridgeserver start")
}
// Start the RPC service
mb.netRPCService = api.NewPublicNetAPI(mb.bridgeServer, mb.NetVersion())
// Figure out a max peers count based on the server limits
//s.maxPeers = s.bridgeServer.MaxPhysicalConnections()
go mb.syncer()
return nil
}
func (mb *MainBridge) newPeer(pv int, p *p2p.Peer, rw p2p.MsgReadWriter) BridgePeer {
return newBridgePeer(pv, p, newMeteredMsgWriter(rw))
}
func (mb *MainBridge) handle(p BridgePeer) error {
// Ignore maxPeers if this is a trusted peer
if mb.peers.Len() >= mb.maxPeers && !p.GetP2PPeer().Info().Networks[p2p.ConnDefault].Trusted {
return p2p.DiscTooManyPeers
}
p.GetP2PPeer().Log().Debug("Klaytn peer connected", "name", p.GetP2PPeer().Name())
// Execute the handshake
var (
head = mb.blockchain.CurrentHeader()
hash = head.Hash()
number = head.Number.Uint64()
td = mb.blockchain.GetTd(hash, number)
)
err := p.Handshake(mb.networkId, mb.getChainID(), td, hash)
if err != nil {
p.GetP2PPeer().Log().Debug("Klaytn peer handshake failed", "err", err)
fmt.Println(err)
return err
}
// Register the peer locally
if err := mb.peers.Register(p); err != nil {
// if starting node with unlock account, can't register peer until finish unlock
p.GetP2PPeer().Log().Info("Klaytn peer registration failed", "err", err)
fmt.Println(err)
return err
}
defer mb.removePeer(p.GetID())
p.GetP2PPeer().Log().Info("Added a P2P Peer", "peerID", p.GetP2PPeerID())
// main loop. handle incoming messages.
for {
if err := mb.handleMsg(p); err != nil {
p.GetP2PPeer().Log().Debug("Klaytn message handling failed", "err", err)
return err
}
}
}
func (mb *MainBridge) SendRPCResponseData(data []byte) error {
peers := mb.BridgePeerSet().peers
logger.Trace("mainbridge send rpc response data to peers", "data len", len(data), "peers", len(peers))
for _, peer := range peers {
err := peer.SendResponseRPC(data)
if err != nil {
logger.Error("failed to send rpc response to the peer", "err", err)
}
return err
}
return nil
}
func (mb *MainBridge) loop() {
defer mb.pmwg.Done()
report := time.NewTicker(1 * time.Second)
defer report.Stop()
// Keep waiting for and reacting to the various events
for {
select {
// Handle ChainHeadEvent
case ev := <-mb.chainHeadCh:
if ev.Block != nil {
mb.eventhandler.HandleChainHeadEvent(ev.Block)
} else {
logger.Error("mainbridge block event is nil")
}
// Handle NewTxsEvent
case ev := <-mb.txCh:
if ev.Txs != nil {
mb.eventhandler.HandleTxsEvent(ev.Txs)
} else {
logger.Error("mainbridge tx event is nil")
}
// Handle ChainLogsEvent
case logs := <-mb.logsCh:
mb.eventhandler.HandleLogsEvent(logs)
case <-report.C:
// report status
case err := <-mb.chainHeadSub.Err():
if err != nil {
logger.Error("mainbridge block subscription ", "err", err)
}
return
case err := <-mb.txSub.Err():
if err != nil {
logger.Error("mainbridge tx subscription ", "err", err)
}
return
case err := <-mb.logsSub.Err():
if err != nil {
logger.Error("mainbridge log subscription ", "err", err)
}
return
}
}
}
func (mb *MainBridge) removePeer(id string) {
// Short circuit if the peer was already removed
peer := mb.peers.Peer(id)
if peer == nil {
return
}
logger.Debug("Removing Klaytn peer", "peer", id)
if err := mb.peers.Unregister(id); err != nil {
logger.Error("Peer removal failed", "peer", id, "err", err)
}
// Hard disconnect at the networking layer
if peer != nil {
peer.GetP2PPeer().Disconnect(p2p.DiscUselessPeer)
}
}
// handleMsg is invoked whenever an inbound message is received from a remote
// peer. The remote connection is torn down upon returning any error.
func (mb *MainBridge) handleMsg(p BridgePeer) error {
//Below message size checking is done by handle().
//Read the next message from the remote peer, and ensure it's fully consumed
msg, err := p.GetRW().ReadMsg()
if err != nil {
p.GetP2PPeer().Log().Warn("ProtocolManager failed to read msg", "err", err)
return err
}
if msg.Size > ProtocolMaxMsgSize {
err := errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
p.GetP2PPeer().Log().Warn("ProtocolManager over max msg size", "err", err)
return err
}
defer msg.Discard()
return mb.handler.HandleSubMsg(p, msg)
}
func (mb *MainBridge) syncer() {
// Start and ensure cleanup of sync mechanisms
//pm.fetcher.Start()
//defer pm.fetcher.Stop()
//defer pm.downloader.Terminate()
// Wait for different events to fire synchronisation operations
forceSync := time.NewTicker(forceSyncCycle)
defer forceSync.Stop()
for {
select {
case peer := <-mb.newPeerCh:
go mb.synchronise(peer)
case <-forceSync.C:
// Force a sync even if not enough peers are present
go mb.synchronise(mb.peers.BestPeer())
case <-mb.noMorePeers:
return
}
}
}
func (mb *MainBridge) synchronise(peer BridgePeer) {
// @TODO Klaytn ServiceChain Sync
}
// Stop implements node.Service, terminating all internal goroutines used by the
// Klaytn protocol.
func (mb *MainBridge) Stop() error {
close(mb.quitSync)
mb.chainHeadSub.Unsubscribe()
mb.txSub.Unsubscribe()
mb.logsSub.Unsubscribe()
mb.eventMux.Stop()
mb.chainDB.Close()
mb.bridgeServer.Stop()
return nil
}
func errResp(code errCode, format string, v ...interface{}) error {
return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
}