def getheaders(self, message): msg = msg_getheaders() msg.nVersion = PROTO_VERSION msg.locator.vHave = self.chaindb.getlocator() # msg.vHave = [bytearray.fromhex('2ada80bf415a89358d697569c96eb98cdbf4c3b8878ac5722c01284492e27228')] # msg.hashstop = bytearray.fromhex('2ada80bf415a89358d697569c96eb98cdbf4c3b8878ac5722c01284492e27228') self.send_message(msg)
def send_getheaders(self): our_height = self.chaindb.getheight() if our_height < 0: tophash = self.params.GENESIS_BLOCK.GetHash() elif our_height < self.remote_height: tophash = self.chaindb.gettophash() else: return gb = messages.msg_getheaders(protover=self.ver_send) if our_height >= 0: gb.locator.vHave.append(tophash) self.send_message(gb)
def test_getheaders_message_no_blocks_to_return(self, mock): message = messages.msg_getheaders() message.locator.vHave = ['hash1'] self.chain.tips = {'some_hash': 'some_block'} mock.return_value = [] self.networking.getheaders_message(self.private_connection, message) self.assertTrue(mock.called) self.assertEqual(mock.call_args[0][0], self.chain.tips) self.assertEqual(mock.call_args[0][1], BlockOrigin.private) self.assertEqual(mock.call_args[0][2], message.locator.vHave) self.assertTrue(self.private_connection.send.called) self.assertEqual(self.private_connection.send.call_args[0][0], 'headers') self.assertEqual(self.private_connection.send.call_args[0][1].headers, [])
def on_version(self, connection, unused_message): if connection.incoming: self.send_version(connection) self.send_verack(connection) else: self.send_verack(connection) msg = messages.msg_getheaders() msg.locator = messages.CBlockLocator() headers = chainutil.request_get_headers(self.chain.tips, BlockOrigin.public) msg.locator.vHave = headers connection.send('getheaders', msg) logging.info( 'requested getheaders with starting hash={} from new connection={}' .format(core.b2lx(headers[0]), connection.host))
def test_getheaders_message_no_block_found(self, mock): message = messages.msg_getheaders() block1 = Block('cblock_header1', BlockOrigin.private) block1.cblock = CBlock(nNonce=1) block2 = Block('cblock_header2', BlockOrigin.private) block2.cblock = CBlock(nNonce=2) mock.return_value = [block1, block2] self.networking.getheaders_message(self.private_connection, message) self.assertTrue(self.private_connection.send.called) self.assertEqual( len(self.private_connection.send.call_args[0][1].headers), 2) self.assertEqual( self.private_connection.send.call_args[0][1].headers[0], block1.cblock) self.assertEqual( self.private_connection.send.call_args[0][1].headers[1], block2.cblock)
def inv_message(self, connection, message): self.sync.lock.acquire() try: getdata_inv = [] for inv in message.inv: try: if net.CInv.typemap[inv.type] == "Block": hash_ = inv.hash logging.info("received block inv {} from {}".format(core.b2lx(inv.hash), self.repr_connection(connection))) if hash_ not in self.chain.blocks: if hash_ not in self.blocks_in_flight: getdata_inv.append(hash_) get_headers = messages.msg_getheaders() get_headers.locator = messages.CBlockLocator() if connection.host[0] == self.private_ip: headers = chainutil.request_get_headers(self.chain.tips, BlockOrigin.private) else: headers = chainutil.request_get_headers(self.chain.tips, BlockOrigin.public) get_headers.locator.vHave = headers connection.send('getheaders', get_headers) logging.info('requested new headers with {} headers and starting hash={} from {}' .format(len(headers), core.b2lx(headers[0]), self.repr_connection(connection))) else: logging.info('block inv {} already in local chain'.format(core.b2lx(hash_))) elif net.CInv.typemap[inv.type] == "Error": logging.warn("received an error inv from {}".format(self.repr_connection(connection))) else: pass except KeyError: logging.warn("unknown inv type={}") self.request_blocks(connection, getdata_inv) finally: self.sync.lock.release()
def send_getheaders(self, block_hash_string): locator = CBlockLocator() locator.vHave = [lx(block_hash_string)] msg = msg_getheaders() msg.locator = locator self.send_message(msg)