def test_load_chain(self): chain = Chain( blocks=[Block( timestamp=datetime(2017, 8, 9, 10, 11, 12, tzinfo=pytz.UTC), prev_hash='xxyy', data={'foo': 'bar'} )]) json.dump(chain.to_dict(), open(os.path.join(FileBackend.FILE_DIR, 'n2.json'), 'w')) c2 = self.backend.load_chain('n2') self.assertEqual(c2.to_dict(), chain.to_dict())
def test_save_chain(self): chain = Chain( blocks=[Block( timestamp=datetime(2017, 8, 9, 10, 11, 12, tzinfo=pytz.UTC), prev_hash='abcd', data={'foo': 'bar'} )]) self.backend.save_chain(chain, 'n1') d = json.load(open(os.path.join(FileBackend.FILE_DIR, 'n1.json'))) self.assertEqual(d, chain.to_dict())
def test_share_chain_rejected(self): try: blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': { 'foo': 'bar' } } blk2 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': { 'woo': 'hoo' } } dct = {'blocks': [blk, blk2]} chain = Chain.from_dict(dct) node_id = 'blah' backend = MockFileBackend(chains={node_id: chain}) n1 = Node.start(node_id=node_id, backend=backend) p1 = n1.proxy() short_chain = Chain.from_dict({'blocks': [blk]}) blk3 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': { 'p': 'b' } } dct2 = {'blocks': [blk, blk3]} chain2 = Chain.from_dict(dct2) n2 = Node.start(node_id='2', backend=MockFileBackend({'2': short_chain})) n3 = Node.start(node_id='3', backend=MockFileBackend({'3': chain2})) n4 = Node.start(node_id='4', backend=MockFileBackend({'4': chain2})) p2 = n2.proxy() p3 = n3.proxy() p4 = n4.proxy() p1.register_peer(p2) p1.register_peer(p3) p1.register_peer(p4) p1.share_chain().get() self.assertEqual(p1.chain.get(), short_chain) self.assertEqual(p2.chain.get(), short_chain) self.assertEqual(p3.chain.get(), chain2) self.assertEqual(p4.chain.get(), chain2) finally: n1.stop() n2.stop() n3.stop() n4.stop()
def test_add_block(self): blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': {'foo': 'bar'} } dct = { 'blocks': [blk] } chain = Chain.from_dict(dct) b2 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': {'on': 'point'} } chain.add_block(Block.from_dict(b2)) self.assertEqual( chain.to_dict(), {'blocks': [Block.from_dict(blk).to_dict(), Block.from_dict(b2).to_dict()]} ) # prev_hash doesn't match last hash - should fail b3 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': {'some': 'thing'} } self.assertRaises(ValueError, chain.add_block, Block.from_dict(b3))
def test_receive_chain_accept(self): n1 = None try: blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': { 'foo': 'bar' } } dct = {'blocks': [blk]} chain = Chain.from_dict(dct) node_id = 'blah' backend = MockFileBackend(chains={node_id: chain}) n1 = Node.start(node_id=node_id, backend=backend) p1 = n1.proxy() chain2 = copy.deepcopy(chain) blk2 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': { 'a': 'b' } } chain2.add_block(Block.from_dict(blk2)) res = p1.receive_chain(chain2).get() self.assertEqual(res, ACCEPTED) self.assertEqual(p1.chain.get(), chain2) finally: n1.stop()
def test_latest_hash(self): block_dicts = [ { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': {'foo': 'bar'} }, { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '5678', 'data': {'on': 'point'} } ] chain = Chain.from_dict( {'blocks': block_dicts} ) self.assertEqual(chain.latest_hash(), Block.from_dict(block_dicts[-1]).hash) chain2 = Chain(blocks=[]) self.assertIsNone(chain2.latest_hash())
def load_chain(self, node_id): """ :type node_id: str :rtype: blokka.entities.Chain """ try: dct = json.load(open(self.__file_path(node_id), 'r')) return Chain.from_dict(dct) except IOError: return None
def test_from_dict_to_dict(self): blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': {'foo': 'bar'} } dct = { 'blocks': [blk] } chain = Chain.from_dict(dct) dct['blocks'][-1]['hash'] = chain.blocks[-1].hash self.assertEqual(chain.to_dict(), dct)
def test_receive_chain_reject(self): n1 = None try: blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': { 'foo': 'bar' } } blk2 = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': Block.from_dict(blk).hash, 'data': { 'woo': 'hoo' } } dct = {'blocks': [blk, blk2]} chain = Chain.from_dict(dct) node_id = 'blah' backend = MockFileBackend(chains={node_id: chain}) n1 = Node.start(node_id=node_id, backend=backend) p1 = n1.proxy() dct2 = { 'blocks': [{ 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': 'abc', 'data': { 'x': 'y' } }] } chain2 = Chain.from_dict(dct2) res = p1.receive_chain(chain2).get() self.assertEqual(res, REJECTED) self.assertEqual(p1.chain.get(), chain) finally: n1.stop()
def test_chain(self): node = None try: node_id = 'aaa' blk = { 'timestamp': '2017-01-02T03:04:05.000123Z', 'prev_hash': '1234', 'data': { 'foo': 'bar' } } dct = {'blocks': [blk]} chain = Chain.from_dict(dct) backend = MockFileBackend(chains={node_id: chain}) node = Node.start(node_id=node_id, backend=backend) proxy = node.proxy() self.assertEqual(proxy.chain.get(), chain) finally: node.stop()