// passing endpoint through here is an ugly hack, which should be removed once network code is refactored. public void onRequestWalletChunk(int chunk_num, RemoteEndpoint endpoint) { // TODO TODO TODO this function really should be done better if (synchronizing == true) { Logging.warn("Neighbor is requesting WalletState chunks, but we are synchronizing!"); return; } lastChunkRequested = DateTime.UtcNow; lock (pendingWsChunks) { if (chunk_num >= 0 && chunk_num < pendingWsChunks.Count) { WalletStateProtocolMessages.sendWalletStateChunk(endpoint, pendingWsChunks[chunk_num]); if (chunk_num + 1 == pendingWsChunks.Count) { outgoingSyncComplete(); } } else { Logging.warn(String.Format("Neighbor requested an invalid WalletState chunk: {0}, but the pending array only has 0-{1}.", chunk_num, pendingWsChunks.Count)); } } }
// Request missing walletstate chunks from network private void requestWalletChunks() { lock (missingWsChunks) { int count = 0; foreach (int c in missingWsChunks) { bool request_sent = WalletStateProtocolMessages.getWalletStateChunkNeighbor(syncNeighbor, c); if (request_sent == false) { Logging.warn(String.Format("Failed to request wallet chunk from {0}. Restarting WalletState synchronization.", syncNeighbor)); startWalletStateSync(); return; } count += 1; if (count > maxBlockRequests) { break; } } if (count > 0) { Logging.info(String.Format("{0} WalletState chunks are missing before WalletState is synchronized...", missingWsChunks.Count)); } Thread.Sleep(2000); } }
private void startWalletStateSync() { HashSet <string> all_neighbors = new HashSet <string>(NetworkClientManager.getConnectedClients(true).Concat(NetworkServer.getConnectedClients(true))); if (all_neighbors.Count < 1) { Logging.info(String.Format("Wallet state synchronization from storage.")); return; } Random r = new Random(); syncNeighbor = all_neighbors.ElementAt(r.Next(all_neighbors.Count)); Logging.info(String.Format("Starting wallet state synchronization from {0}", syncNeighbor)); WalletStateProtocolMessages.syncWalletStateNeighbor(syncNeighbor); }
// Unified protocol message parsing public static void parseProtocolMessage(ProtocolMessageCode code, byte[] data, RemoteEndpoint endpoint) { if (endpoint == null) { Logging.error("Endpoint was null. parseProtocolMessage"); return; } try { switch (code) { case ProtocolMessageCode.hello: handleHello(data, endpoint); break; case ProtocolMessageCode.helloData: handleHelloData(data, endpoint); break; case ProtocolMessageCode.getBlock: BlockProtocolMessages.handleGetBlock(data, endpoint); break; case ProtocolMessageCode.getBalance: WalletStateProtocolMessages.handleGetBalance(data, endpoint); break; case ProtocolMessageCode.getTransaction: TransactionProtocolMessages.handleGetTransaction(data, endpoint); break; case ProtocolMessageCode.getTransaction2: TransactionProtocolMessages.handleGetTransaction2(data, endpoint); break; case ProtocolMessageCode.getTransaction3: TransactionProtocolMessages.handleGetTransaction3(data, endpoint); break; case ProtocolMessageCode.newTransaction: case ProtocolMessageCode.transactionData: TransactionProtocolMessages.handleTransactionData(data, endpoint); break; case ProtocolMessageCode.bye: CoreProtocolMessage.processBye(data, endpoint); break; case ProtocolMessageCode.newBlock: case ProtocolMessageCode.blockData: BlockProtocolMessages.handleBlockData(data, endpoint); break; case ProtocolMessageCode.syncWalletState: WalletStateProtocolMessages.handleSyncWalletState(data, endpoint); break; case ProtocolMessageCode.walletState: WalletStateProtocolMessages.handleWalletState(data, endpoint); break; case ProtocolMessageCode.getWalletStateChunk: WalletStateProtocolMessages.handleGetWalletStateChunk(data, endpoint); break; case ProtocolMessageCode.walletStateChunk: WalletStateProtocolMessages.handleWalletStateChunk(data, endpoint); break; case ProtocolMessageCode.updatePresence: PresenceProtocolMessages.handleUpdatePresence(data, endpoint); break; case ProtocolMessageCode.keepAlivePresence: PresenceProtocolMessages.handleKeepAlivePresence(data, endpoint); break; case ProtocolMessageCode.getPresence: PresenceProtocolMessages.handleGetPresence(data, endpoint); break; case ProtocolMessageCode.getPresence2: PresenceProtocolMessages.handleGetPresence2(data, endpoint); break; case ProtocolMessageCode.getKeepAlives: PresenceProtocolMessages.handleGetKeepAlives(data, endpoint); break; case ProtocolMessageCode.keepAlivesChunk: PresenceProtocolMessages.handleKeepAlivesChunk(data, endpoint); break; // return 10 random presences of the selected type case ProtocolMessageCode.getRandomPresences: PresenceProtocolMessages.handleGetRandomPresences(data, endpoint); break; case ProtocolMessageCode.getUnappliedTransactions: TransactionProtocolMessages.handleGetUnappliedTransactions(data, endpoint); break; case ProtocolMessageCode.blockTransactionsChunk: BlockProtocolMessages.handleBlockTransactionsChunk(data, endpoint); break; case ProtocolMessageCode.attachEvent: NetworkEvents.handleAttachEventMessage(data, endpoint); break; case ProtocolMessageCode.detachEvent: NetworkEvents.handleDetachEventMessage(data, endpoint); break; case ProtocolMessageCode.blockSignature: SignatureProtocolMessages.handleBlockSignature(data, endpoint); break; case ProtocolMessageCode.getBlockSignatures: { using (MemoryStream m = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(m)) { ulong block_num = reader.ReadUInt64(); int checksum_len = reader.ReadInt32(); byte[] checksum = reader.ReadBytes(checksum_len); SignatureProtocolMessages.handleGetBlockSignatures(block_num, checksum, endpoint); } } } break; case ProtocolMessageCode.blockSignatures: SignatureProtocolMessages.handleSigfreezedBlockSignatures(data, endpoint); break; case ProtocolMessageCode.getNextSuperBlock: BlockProtocolMessages.handleGetNextSuperBlock(data, endpoint); break; case ProtocolMessageCode.getBlockHeaders: BlockProtocolMessages.handleGetBlockHeaders(data, endpoint); break; case ProtocolMessageCode.getPIT: BlockProtocolMessages.handleGetPIT(data, endpoint); break; case ProtocolMessageCode.inventory: handleInventory(data, endpoint); break; case ProtocolMessageCode.inventory2: handleInventory2(data, endpoint); break; case ProtocolMessageCode.getSignatures: SignatureProtocolMessages.handleGetSignatures(data, endpoint); break; case ProtocolMessageCode.signaturesChunk: SignatureProtocolMessages.handleSignaturesChunk(data, endpoint); break; case ProtocolMessageCode.getTransactions: TransactionProtocolMessages.handleGetTransactions(data, endpoint); break; case ProtocolMessageCode.getTransactions2: TransactionProtocolMessages.handleGetTransactions2(data, endpoint); break; case ProtocolMessageCode.transactionsChunk: TransactionProtocolMessages.handleTransactionsChunk(data, endpoint); break; case ProtocolMessageCode.transactionsChunk2: TransactionProtocolMessages.handleTransactionsChunk2(data, endpoint); break; case ProtocolMessageCode.getBlockHeaders2: BlockProtocolMessages.handleGetBlockHeaders2(data, endpoint); break; case ProtocolMessageCode.getPIT2: BlockProtocolMessages.handleGetPIT2(data, endpoint); break; case ProtocolMessageCode.getBlock2: BlockProtocolMessages.handleGetBlock2(data, endpoint); break; case ProtocolMessageCode.getBlock3: BlockProtocolMessages.handleGetBlock3(data, endpoint); break; case ProtocolMessageCode.getBalance2: WalletStateProtocolMessages.handleGetBalance2(data, endpoint); break; case ProtocolMessageCode.getBlockSignatures2: { using (MemoryStream m = new MemoryStream(data)) { using (BinaryReader reader = new BinaryReader(m)) { ulong block_num = reader.ReadIxiVarUInt(); int checksum_len = (int)reader.ReadIxiVarUInt(); byte[] checksum = reader.ReadBytes(checksum_len); SignatureProtocolMessages.handleGetBlockSignatures2(block_num, checksum, endpoint); } } } break; case ProtocolMessageCode.blockSignature2: SignatureProtocolMessages.handleBlockSignature2(data, endpoint); break; default: break; } } catch (Exception e) { Logging.error("Error parsing network message. Details: {0}", e.ToString()); } }