Эх сурвалжийг харах

fix JSON (de)-serialization and add simple wip protocol test

Malte Kraus 8 жил өмнө
parent
commit
15f0049fdd

+ 0 - 1
requirements.txt

@@ -4,7 +4,6 @@ docutils==0.13.1
 imagesize==0.7.1
 Jinja2==2.9.5
 MarkupSafe==0.23
-pkg-resources==0.0.0
 pycrypto==2.6.1
 Pygments==2.2.0
 pytz==2016.10

+ 5 - 4
src/block.py

@@ -26,9 +26,9 @@ class Block:
 
     def to_json_compatible(self):
         val = {}
-        val['hash'] = hexlify(self.hash).encode()
-        val['prev_block_hash'] = hexlify(self.prev_block_hash).encode()
-        val['merkle_root_hash'] = hexlify(self.merkle_root_hash).encode()
+        val['hash'] = hexlify(self.hash).decode()
+        val['prev_block_hash'] = hexlify(self.prev_block_hash).decode()
+        val['merkle_root_hash'] = hexlify(self.merkle_root_hash).decode()
         val['time'] = self.time.timestamp()
         val['nonce'] = self.nonce
         val['height'] = self.height
@@ -37,7 +37,8 @@ class Block:
         return val
 
     @classmethod
-    def from_json_compatible(cls):
+    def from_json_compatible(cls, val):
+        from .transaction import Transaction
         return cls(unhexlify(val['hash']),
                    unhexlify(val['prev_block_hash']),
                    datetime.fromtimestamp(float(val['time'])),

+ 15 - 15
src/transaction.py

@@ -34,37 +34,37 @@ class Transaction:
         val['inputs'] = []
         for inp in self.inputs:
             val['inputs'].append({
-                'recipient_pk': hexlify(inp.recipient_pk.to_bytes()).decode(),
-                'amount': inp.amount,
+                'transaction_hash': hexlify(inp.transaction_hash).decode(),
+                'output_idx': inp.output_idx,
             })
         val['targets'] = []
         for targ in self.targets:
             val['targets'].append({
-                'transaction_hash': hexlify(targ.transaction_hash).decode(),
-                'output_idx': targ.output_idx,
+                'recipient_pk': hexlify(targ.recipient_pk.as_bytes()).decode(),
+                'amount': targ.amount,
             })
         val['signatures'] = []
         for sig in self.signatures:
-            val['signatures'].append(sig)
-        val['iv'] = hexlify(self.iv)
+            val['signatures'].append(hexlify(sig).decode())
+        if self.iv is not None:
+            val['iv'] = hexlify(self.iv).decode()
         return val
 
     @classmethod
     def from_json_compatible(cls, obj: dict):
         inputs = []
         for inp in obj['inputs']:
-            inputs.append(TransactionInput(Signing(unhexlify(inp['recipient_pk'])),
-                                           int(inp['amount'])))
+            inputs.append(TransactionInput(unhexlify(inp['transaction_hash']),
+                                           int(inp['output_idx'])))
         targets = []
         for targ in obj['targets']:
-            targets.append(TransactionTarget(unhexlify(inp['transaction_hash']),
-                                           int(inp['output_idx'])))
-        signatures = obj['signatures']
-        for sig in signatures:
-            if not isinstance(sig, str):
-                raise ValueError()
+            targets.append(TransactionTarget(Signing(unhexlify(targ['recipient_pk'])),
+                                           int(targ['amount'])))
+        signatures = []
+        for sig in obj['signatures']:
+            signatures.append(unhexlify(sig))
 
-        iv = unhexlify(obj['iv'])
+        iv = unhexlify(obj['iv']) if 'iv' in obj else None
         return cls(inputs, targets, signatures, iv)
 
 

+ 32 - 0
tests/test_proto.py

@@ -0,0 +1,32 @@
+from src.protocol import Protocol
+from src.mining import Miner
+from src.block import GENESIS_BLOCK
+from src.crypto import Signing
+from src.transaction import Transaction, TransactionInput, TransactionTarget
+
+from time import sleep
+
+reward_key = Signing.generatePrivateKey()
+
+proto1 = Protocol(("127.0.0.1", 1337), GENESIS_BLOCK, 1337)
+proto2 = Protocol(("127.0.0.1", 1337), GENESIS_BLOCK, 1338)
+miner1 = Miner(proto1, reward_key)
+miner2 = Miner(proto2, reward_key)
+miner2.chain_changed()
+miner1.chain_changed()
+
+
+
+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])
+miner2.chainbuilder.new_transaction_received(trans)
+sleep(5)
+print(len(miner1.chainbuilder.primary_block_chain.blocks))
+print(len(miner2.chainbuilder.primary_block_chain.blocks))
+print(miner1.chainbuilder.primary_block_chain.blocks[1].hash == miner2.chainbuilder.primary_block_chain.blocks[1].hash)