github.com/Unheilbar/quorum@v1.0.0/eth/handler_qlight_client.go (about) 1 package eth 2 3 import ( 4 "errors" 5 "fmt" 6 "sync/atomic" 7 "time" 8 9 "github.com/ethereum/go-ethereum/common" 10 "github.com/ethereum/go-ethereum/core" 11 "github.com/ethereum/go-ethereum/core/forkid" 12 "github.com/ethereum/go-ethereum/core/types" 13 "github.com/ethereum/go-ethereum/eth/downloader" 14 "github.com/ethereum/go-ethereum/eth/fetcher" 15 "github.com/ethereum/go-ethereum/eth/protocols/eth" 16 qlightproto "github.com/ethereum/go-ethereum/eth/protocols/qlight" 17 "github.com/ethereum/go-ethereum/event" 18 "github.com/ethereum/go-ethereum/log" 19 "github.com/ethereum/go-ethereum/p2p" 20 "github.com/ethereum/go-ethereum/p2p/enode" 21 "github.com/ethereum/go-ethereum/params" 22 "github.com/ethereum/go-ethereum/trie" 23 ) 24 25 type qlightClientHandler ethHandler 26 27 func (h *qlightClientHandler) Chain() *core.BlockChain { return h.chain } 28 func (h *qlightClientHandler) StateBloom() *trie.SyncBloom { return h.stateBloom } 29 func (h *qlightClientHandler) TxPool() eth.TxPool { return h.txpool } 30 31 func (h *qlightClientHandler) RunPeer(peer *eth.Peer, handler eth.Handler) error { 32 return nil 33 } 34 func (h *qlightClientHandler) Handle(peer *eth.Peer, packet eth.Packet) error { 35 return (*ethHandler)(h).Handle(peer, packet) 36 } 37 38 func (h *qlightClientHandler) RunQPeer(peer *qlightproto.Peer, hand qlightproto.Handler) error { 39 return (*handler)(h).runQLightClientPeer(peer, hand) 40 } 41 42 // PeerInfo retrieves all known `eth` information about a peer. 43 func (h *qlightClientHandler) PeerInfo(id enode.ID) interface{} { 44 if p := h.peers.peer(id.String()); p != nil { 45 return p.info() 46 } 47 return nil 48 } 49 50 // AcceptTxs retrieves whether transaction processing is enabled on the node 51 // or if inbound transactions should simply be dropped. 52 func (h *qlightClientHandler) AcceptTxs() bool { 53 return atomic.LoadUint32(&h.acceptTxs) == 1 54 } 55 56 // newHandler returns a handler for all Ethereum chain management protocol. 57 func newQLightClientHandler(config *handlerConfig) (*handler, error) { 58 // Create the protocol manager with the base fields 59 if config.EventMux == nil { 60 config.EventMux = new(event.TypeMux) // Nicety initialization for tests 61 } 62 h := &handler{ 63 networkID: config.Network, 64 forkFilter: forkid.NewFilter(config.Chain), 65 eventMux: config.EventMux, 66 database: config.Database, 67 txpool: config.TxPool, 68 chain: config.Chain, 69 peers: newPeerSet(), 70 authorizationList: config.AuthorizationList, 71 txsyncCh: make(chan *txsync), 72 quitSync: make(chan struct{}), 73 raftMode: config.RaftMode, 74 engine: config.Engine, 75 psi: config.psi, 76 privateClientCache: config.privateClientCache, 77 tokenHolder: config.tokenHolder, 78 } 79 80 if config.Sync == downloader.FullSync { 81 // The database seems empty as the current block is the genesis. Yet the fast 82 // block is ahead, so fast sync was enabled for this node at a certain point. 83 // The scenarios where this can happen is 84 // * if the user manually (or via a bad block) rolled back a fast sync node 85 // below the sync point. 86 // * the last fast sync is not finished while user specifies a full sync this 87 // time. But we don't have any recent state for full sync. 88 // In these cases however it's safe to reenable fast sync. 89 fullBlock, fastBlock := h.chain.CurrentBlock(), h.chain.CurrentFastBlock() 90 if fullBlock.NumberU64() == 0 && fastBlock.NumberU64() > 0 { 91 h.fastSync = uint32(1) 92 log.Warn("Switch sync mode from full sync to fast sync") 93 } 94 } else { 95 if h.chain.CurrentBlock().NumberU64() > 0 { 96 // Print warning log if database is not empty to run fast sync. 97 log.Warn("Switch sync mode from fast sync to full sync") 98 } else { 99 // If fast sync was requested and our database is empty, grant it 100 h.fastSync = uint32(1) 101 if config.Sync == downloader.SnapSync { 102 h.snapSync = uint32(1) 103 } 104 } 105 } 106 // If we have trusted checkpoints, enforce them on the chain 107 if config.Checkpoint != nil { 108 h.checkpointNumber = (config.Checkpoint.SectionIndex+1)*params.CHTFrequency - 1 109 h.checkpointHash = config.Checkpoint.SectionHead 110 } 111 // Construct the downloader (long sync) and its backing state bloom if fast 112 // sync is requested. The downloader is responsible for deallocating the state 113 // bloom when it's done. 114 if atomic.LoadUint32(&h.fastSync) == 1 { 115 h.stateBloom = trie.NewSyncBloom(config.BloomCache, config.Database) 116 } 117 h.downloader = downloader.New(h.checkpointNumber, config.Database, h.stateBloom, h.eventMux, h.chain, nil, h.removePeer) 118 119 // Construct the fetcher (short sync) 120 validator := func(header *types.Header) error { 121 return h.chain.Engine().VerifyHeader(h.chain, header, true) 122 } 123 heighter := func() uint64 { 124 return h.chain.CurrentBlock().NumberU64() 125 } 126 inserter := func(blocks types.Blocks) (int, error) { 127 // If sync hasn't reached the checkpoint yet, deny importing weird blocks. 128 // 129 // Ideally we would also compare the head block's timestamp and similarly reject 130 // the propagated block if the head is too old. Unfortunately there is a corner 131 // case when starting new networks, where the genesis might be ancient (0 unix) 132 // which would prevent full nodes from accepting it. 133 if h.chain.CurrentBlock().NumberU64() < h.checkpointNumber { 134 log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash()) 135 return 0, nil 136 } 137 // If fast sync is running, deny importing weird blocks. This is a problematic 138 // clause when starting up a new network, because fast-syncing miners might not 139 // accept each others' blocks until a restart. Unfortunately we haven't figured 140 // out a way yet where nodes can decide unilaterally whether the network is new 141 // or not. This should be fixed if we figure out a solution. 142 if atomic.LoadUint32(&h.fastSync) == 1 { 143 log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash()) 144 return 0, nil 145 } 146 n, err := h.chain.InsertChain(blocks) 147 if err == nil { 148 atomic.StoreUint32(&h.acceptTxs, 1) // Mark initial sync done on any fetcher import 149 } 150 return n, err 151 } 152 h.blockFetcher = fetcher.NewBlockFetcher(false, nil, h.chain.GetBlockByHash, validator, h.BroadcastBlockQLightClient, heighter, nil, inserter, h.removePeer) 153 154 fetchTx := func(peer string, hashes []common.Hash) error { 155 p := h.peers.peer(peer) 156 if p == nil { 157 return errors.New("unknown peer") 158 } 159 return p.RequestTxs(hashes) 160 } 161 h.txFetcher = fetcher.NewTxFetcher(h.txpool.Has, h.txpool.AddRemotes, fetchTx) 162 h.chainSync = newChainSyncer(h) 163 return h, nil 164 } 165 166 // runEthPeer registers an eth peer into the joint eth/snap peerset, adds it to 167 // various subsistems and starts handling messages. 168 func (h *handler) runQLightClientPeer(peer *qlightproto.Peer, handler qlightproto.Handler) error { 169 // If the peer has a `snap` extension, wait for it to connect so we can have 170 // a uniform initialization/teardown mechanism 171 snap, err := h.peers.waitSnapExtension(peer.EthPeer) 172 if err != nil { 173 peer.Log().Error("Snapshot extension barrier failed", "err", err) 174 return err 175 } 176 // TODO(karalabe): Not sure why this is needed 177 if !h.chainSync.handlePeerEvent(peer.EthPeer) { 178 return p2p.DiscQuitting 179 } 180 h.peerWG.Add(1) 181 defer h.peerWG.Done() 182 183 // Execute the Ethereum handshake 184 var ( 185 genesis = h.chain.Genesis() 186 head = h.chain.CurrentHeader() 187 hash = head.Hash() 188 number = head.Number.Uint64() 189 td = h.chain.GetTd(hash, number) 190 ) 191 forkID := forkid.NewID(h.chain.Config(), h.chain.Genesis().Hash(), h.chain.CurrentHeader().Number.Uint64()) 192 if err := peer.EthPeer.Handshake(h.networkID, td, hash, genesis.Hash(), forkID, h.forkFilter); err != nil { 193 peer.Log().Debug("Ethereum handshake failed", "err", err) 194 195 // Quorum 196 // When the Handshake() returns an error, the Run method corresponding to `eth` protocol returns with the error, causing the peer to drop, signal subprotocol as well to exit the `Run` method 197 peer.EthPeerDisconnected <- struct{}{} 198 // End Quorum 199 return err 200 } 201 202 log.Info("QLight attempting handshake") 203 if err := peer.QLightHandshake(false, h.psi, h.tokenHolder.CurrentToken()); err != nil { 204 peer.Log().Debug("QLight handshake failed", "err", err) 205 log.Info("QLight handshake failed", "err", err) 206 207 // Quorum 208 // When the Handshake() returns an error, the Run method corresponding to `eth` protocol returns with the error, causing the peer to drop, signal subprotocol as well to exit the `Run` method 209 peer.EthPeerDisconnected <- struct{}{} 210 // End Quorum 211 return err 212 } 213 214 peer.Log().Debug("QLight handshake result for peer", "peer", peer.ID(), "server", peer.QLightServer(), "psi", peer.QLightPSI(), "token", peer.QLightToken()) 215 log.Info("QLight handshake result for peer", "peer", peer.ID(), "server", peer.QLightServer(), "psi", peer.QLightPSI(), "token", peer.QLightToken()) 216 // if we're not connected to a qlight server - disconnect the peer 217 if !peer.QLightServer() { 218 peer.Log().Debug("QLight connected to a non server peer. Disconnecting.") 219 220 // Quorum 221 // When the Handshake() returns an error, the Run method corresponding to `eth` protocol returns with the error, causing the peer to drop, signal subprotocol as well to exit the `Run` method 222 peer.EthPeerDisconnected <- struct{}{} 223 // End Quorum 224 return fmt.Errorf("connected to a non server peer") 225 } 226 227 reject := false // reserved peer slots 228 if atomic.LoadUint32(&h.snapSync) == 1 { 229 if snap == nil { 230 // If we are running snap-sync, we want to reserve roughly half the peer 231 // slots for peers supporting the snap protocol. 232 // The logic here is; we only allow up to 5 more non-snap peers than snap-peers. 233 if all, snp := h.peers.len(), h.peers.snapLen(); all-snp > snp+5 { 234 reject = true 235 } 236 } 237 } 238 // Ignore maxPeers if this is a trusted peer 239 if !peer.Peer.Info().Network.Trusted { 240 if reject || h.peers.len() >= h.maxPeers { 241 return p2p.DiscTooManyPeers 242 } 243 } 244 peer.Log().Debug("Ethereum peer connected", "name", peer.Name()) 245 246 // Register the peer locally 247 if err := h.peers.registerQPeer(peer); err != nil { 248 peer.Log().Error("Ethereum peer registration failed", "err", err) 249 250 // Quorum 251 // When the Register() returns an error, the Run method corresponding to `eth` protocol returns with the error, causing the peer to drop, signal subprotocol as well to exit the `Run` method 252 peer.EthPeerDisconnected <- struct{}{} 253 // End Quorum 254 255 return err 256 } 257 defer h.removePeer(peer.ID()) 258 259 p := h.peers.peer(peer.ID()) 260 if p == nil { 261 return errors.New("peer dropped during handling") 262 } 263 // Register the peer in the downloader. If the downloader considers it banned, we disconnect 264 if err := h.downloader.RegisterPeer(peer.ID(), peer.Version(), peer.EthPeer); err != nil { 265 peer.Log().Error("Failed to register peer in eth syncer", "err", err) 266 return err 267 } 268 if snap != nil { 269 if err := h.downloader.SnapSyncer.Register(snap); err != nil { 270 peer.Log().Error("Failed to register peer in snap syncer", "err", err) 271 return err 272 } 273 } 274 h.chainSync.handlePeerEvent(peer.EthPeer) 275 276 // Propagate existing transactions. new transactions appearing 277 // after this will be sent via broadcasts. 278 h.syncTransactions(peer.EthPeer) 279 280 // If we have a trusted CHT, reject all peers below that (avoid fast sync eclipse) 281 if h.checkpointHash != (common.Hash{}) { 282 // Request the peer's checkpoint header for chain height/weight validation 283 if err := peer.EthPeer.RequestHeadersByNumber(h.checkpointNumber, 1, 0, false); err != nil { 284 return err 285 } 286 // Start a timer to disconnect if the peer doesn't reply in time 287 p.syncDrop = time.AfterFunc(syncChallengeTimeout, func() { 288 peer.Log().Warn("Checkpoint challenge timed out, dropping", "addr", peer.RemoteAddr(), "type", peer.Name()) 289 h.removePeer(peer.ID()) 290 }) 291 // Make sure it's cleaned up if the peer dies off 292 defer func() { 293 if p.syncDrop != nil { 294 p.syncDrop.Stop() 295 p.syncDrop = nil 296 } 297 }() 298 } 299 // If we have any explicit authorized block hashes, request them 300 for number := range h.authorizationList { 301 if err := peer.EthPeer.RequestHeadersByNumber(number, 1, 0, false); err != nil { 302 return err 303 } 304 } 305 306 // Quorum notify other subprotocols that the eth peer is ready, and has been added to the peerset. 307 p.EthPeerRegistered <- struct{}{} 308 // Quorum 309 310 // Handle incoming messages until the connection is torn down 311 return handler(peer) 312 } 313 314 func (h *handler) StartQLightClient() { 315 h.maxPeers = 1 316 // Quorum 317 if h.raftMode { 318 // We set this immediately in raft mode to make sure the miner never drops 319 // incoming txes. Raft mode doesn't use the fetcher or downloader, and so 320 // this would never be set otherwise. 321 atomic.StoreUint32(&h.acceptTxs, 1) 322 } 323 // End Quorum 324 325 // start sync handlers 326 h.wg.Add(1) 327 go h.chainSync.loop() 328 } 329 330 func (h *handler) StopQLightClient() { 331 if h == nil { 332 return 333 } 334 // Quit chainSync and txsync64. 335 // After this is done, no new peers will be accepted. 336 close(h.quitSync) 337 h.wg.Wait() 338 339 // Disconnect existing sessions. 340 // This also closes the gate for any new registrations on the peer set. 341 // sessions which are already established but not added to h.peers yet 342 // will exit when they try to register. 343 h.peers.close() 344 h.peerWG.Wait() 345 log.Info("QLight client protocol stopped") 346 } 347 348 // BroadcastBlock will either propagate a block to a subset of its peers, or 349 // will only announce its availability (depending what's requested). 350 func (h *handler) BroadcastBlockQLightClient(block *types.Block, propagate bool) { 351 } 352 353 // Handle is invoked from a peer's message handler when it receives a new remote 354 // message that the handler couldn't consume and serve itself. 355 func (h *qlightClientHandler) QHandle(peer *qlightproto.Peer, packet eth.Packet) error { 356 // Consume any broadcasts and announces, forwarding the rest to the downloader 357 switch packet := packet.(type) { 358 case *eth.BlockHeadersPacket: 359 return (*ethHandler)(h).Handle(peer.EthPeer, packet) 360 361 case *eth.BlockBodiesPacket: 362 txset, uncleset := packet.Unpack() 363 h.handleBodiesQLight(txset) 364 return (*ethHandler)(h).handleBodies(peer.EthPeer, txset, uncleset) 365 366 case *eth.NewBlockHashesPacket: 367 return (*ethHandler)(h).Handle(peer.EthPeer, packet) 368 369 case *eth.NewBlockPacket: 370 h.updateCacheWithNonPartyTxData(packet.Block.Transactions()) 371 return (*ethHandler)(h).handleBlockBroadcast(peer.EthPeer, packet.Block, packet.TD) 372 case *qlightproto.BlockPrivateDataPacket: 373 return h.handleBlockPrivateData(packet) 374 case *eth.NewPooledTransactionHashesPacket: 375 return (*ethHandler)(h).Handle(peer.EthPeer, packet) 376 case *eth.TransactionsPacket: 377 h.updateCacheWithNonPartyTxData(*packet) 378 return (*ethHandler)(h).Handle(peer.EthPeer, packet) 379 case *eth.PooledTransactionsPacket: 380 return (*ethHandler)(h).Handle(peer.EthPeer, packet) 381 382 default: 383 return fmt.Errorf("unexpected eth packet type: %T", packet) 384 } 385 } 386 387 // handleBodies is invoked from a peer's message handler when it transmits a batch 388 // of block bodies for the local node to process. 389 func (h *qlightClientHandler) handleBodiesQLight(txs [][]*types.Transaction) { 390 for _, txArray := range txs { 391 h.updateCacheWithNonPartyTxData(txArray) 392 } 393 } 394 395 func (h *qlightClientHandler) updateCacheWithNonPartyTxData(transactions []*types.Transaction) { 396 for _, tx := range transactions { 397 if tx.IsPrivate() || tx.IsPrivacyMarker() { 398 txHash := common.BytesToEncryptedPayloadHash(tx.Data()) 399 h.privateClientCache.CheckAndAddEmptyEntry(txHash) 400 } 401 } 402 } 403 404 func (h *qlightClientHandler) handleBlockPrivateData(blockPrivateData *qlightproto.BlockPrivateDataPacket) error { 405 for _, b := range *blockPrivateData { 406 if err := h.privateClientCache.AddPrivateBlock(b); err != nil { 407 return fmt.Errorf("Unable to handle private block data: %v", err) 408 } 409 } 410 return nil 411 }