def on_new_heighest_block(self, event):
     # relay blocks to peers
     for p in self.node.version_service.version_exchanged_nodes:
         if event.height > self.node.version_service.version_statuses[
                 p].version_message.start_height - 2000:
             self.node.send_message(
                 p, InvMessage([Invitem(INV_BLOCK, event.blkhash)]))
Beispiel #2
0
 def on_publish_transaction(self, event):
     if self.node.version_service.version_exchanged_nodes:
         random_peer = random.sample(
             self.node.version_service.version_exchanged_nodes, 1)[0]
         if not self.node.txpool.contains_transaction(event.txhash):
             self.node.txpool.add_tx(event.txhash, event.tx)
         self.node.send_message(random_peer,
                                InvMessage([Invitem(INV_TX, event.txhash)]))
 def on_getblocks(self, event):
     firstfound = first(event.message.block_locator.blockhashlist, 
                        lambda b: self.blockchain.contains_block(b))
     blkhash, i =  firstfound, 0
     inv_to_send = []
     while blkhash and blkhash != event.message.hash_stop and i < 500:
         blkhash = self.blockchain.get_next_in_mainchain(blkhash)
         if blkhash:
             inv_to_send.append(Invitem(INV_BLOCK, blkhash))
         i += 1
     self.node.send_message(event.handler, InvMessage(inv_to_send))
     self.hash_continue = blkhash 
     self.log.info("sending reply to getblocks: %d items" % (len(inv_to_send)))
 def on_getdata(self, event):
     # todo: deserializing 500 block from disk is slow, gui freezes
     for inv in event.message.invitems:
         if inv.type == INV_TX and self.txpool.contains_transaction(inv.hash):
             tx = self.txpool.get_transaction(inv.hash)
             self.node.send_message(event.handler, TxMessage(tx))
         if inv.type == INV_BLOCK and self.blockchain.contains_block(inv.hash):
             block = self.blockchain.get_block(inv.hash)
             self.node.send_message(event.handler, BlockMessage(block))
             self.log.debug("sending block: %s" % (str(hash_block(block))))
                 
             if self.hash_continue and inv.hash == self.hash_continue:
                 hash_best = self.blockchain.database.get_mainchain()
                 self.log.info("sending hashContinue: %s" % (str(hash_best)))
                 self.node.send_message(event.handler, InvMessage([Invitem(INV_BLOCK, hash_best)]))
 def test_deserialize_inv_message(self):
     serialized_inv_message = decodehexstr(
         "f9beb4d9696e76000000000000000000490000008be920f5020100000055b5d3d496e196d624a471b818ba0b1778417ae335a544033536654fdda3eef602000000497b15826573b28c3e83a5d0c5ed30cf48b97dd6bd797849144ea24000000000"
     )
     inv_message, _ = MessageSerializer(MAIN).deserialize(
         serialized_inv_message)
     expected_message = InvMessage([
         Invitem(
             INV_TX,
             Uint256.from_hexstr(
                 "f6eea3dd4f6536350344a535e37a4178170bba18b871a424d696e196d4d3b555"
             )),
         Invitem(
             INV_BLOCK,
             Uint256.from_hexstr(
                 "0000000040a24e14497879bdd67db948cf30edc5d0a5833e8cb2736582157b49"
             ))
     ])
     self.assertEquals(inv_message, expected_message)
Beispiel #6
0
 def deserialize(self, data, cursor=0):
     invitems, cursor = self.INV_ENCODER.deserialize(data, cursor)
     return (InvMessage(invitems), cursor)