Răsfoiți Sursa

broadcast received unknown transactions

Malte Kraus 8 ani în urmă
părinte
comite
5918f86037
4 a modificat fișierele cu 18 adăugiri și 13 ștergeri
  1. 4 2
      src/chainbuilder.py
  2. 6 1
      src/protocol.py
  3. 2 4
      tests/test_mining.py
  4. 6 6
      tests/test_proto.py

+ 4 - 2
src/chainbuilder.py

@@ -42,8 +42,10 @@ class ChainBuilder:
         """ Event handler that is called by the network layer when a transaction is received. """
         self._assert_thread_safety()
         hash_val = transaction.get_hash()
-        if self.primary_block_chain.get_transaction_by_hash(hash_val) is None:
-           self.unconfirmed_transactions[hash_val] = transaction
+        if self.primary_block_chain.get_transaction_by_hash(hash_val) is None and \
+                hash_val not in self.unconfirmed_transactions:
+            self.unconfirmed_transactions[hash_val] = transaction
+            self.protocol.broadcast_transaction(transaction)
 
     def _new_primary_block_chain(self, chain):
         """

+ 6 - 1
src/protocol.py

@@ -212,13 +212,18 @@ class Protocol:
 
         Thread(target=self._main_thread, daemon=True).start()
 
-    def broadcast_primary_block(self, block):
+    def broadcast_primary_block(self, block: Block):
         """ Notifies all peers and local listeners of a new primary block. """
         self._primary_block = block.to_json_compatible()
         for peer in self.peers:
             peer.send_msg("block", self._primary_block)
         self.received('block', self._primary_block, None, 0)
 
+    def broadcast_transaction(self, trans: Transaction):
+        """ Notifies all peers and local listeners of a new transaction. """
+        for peer in self.peers:
+            peer.send_msg("transaction", trans.to_json_compatible())
+
     def received(self, msg_type, msg_param, peer, prio=1):
         """ Called by a PeerConnection when a new message was received. """
         with self._callback_counter_lock:

+ 2 - 4
tests/test_mining.py

@@ -19,10 +19,8 @@ sleep(5)
 #proto.fake_block_received(GENESIS_BLOCK)
 strans1 = miner2.chainbuilder.primary_block_chain.head.transactions[0]
 strans1 = TransactionInput(strans1.get_hash(), 0)
-strans2 = miner2.chainbuilder.primary_block_chain.head.transactions[0]
-strans2 = TransactionInput(strans2.get_hash(), 0)
-trans = Transaction([strans1, strans2], [])
-trans.sign([reward_key, reward_key])
+trans = Transaction([strans1], [])
+trans.sign([reward_key])
 miner2.chainbuilder.new_transaction_received(trans)
 sleep(5)
 print(len(miner2.chainbuilder.primary_block_chain.blocks))

+ 6 - 6
tests/test_proto.py

@@ -18,13 +18,11 @@ miner1.start_mining()
 
 
 sleep(5)
-#proto.fake_block_received(GENESIS_BLOCK)
-strans1 = miner2.chainbuilder.primary_block_chain.head.transactions[0]
+strans1 = miner2.chainbuilder.primary_block_chain.blocks[20].transactions[0]
 strans1 = TransactionInput(strans1.get_hash(), 0)
-strans2 = miner2.chainbuilder.primary_block_chain.head.transactions[0]
-strans2 = TransactionInput(strans2.get_hash(), 0)
-trans = Transaction([strans1, strans2], [])
-trans.sign([reward_key, reward_key])
+trans = Transaction([strans1], [])
+trans.sign([reward_key])
+print(trans.verify(miner1.chainbuilder.primary_block_chain, set()))
 proto2.received('transaction', trans.to_json_compatible(), None)
 sleep(5)
 print(len(miner1.chainbuilder.primary_block_chain.blocks))
@@ -32,3 +30,5 @@ print(len(miner2.chainbuilder.primary_block_chain.blocks))
 hashes1 = [b.hash for b in miner1.chainbuilder.primary_block_chain.blocks[:70]]
 hashes2 = [b.hash for b in miner2.chainbuilder.primary_block_chain.blocks[:70]]
 print(hashes1 == hashes2)
+
+print(trans.verify(miner1.chainbuilder.primary_block_chain, set()))