def setUp(self): initialise_database('test_database_file') self.private_key = SigningKey.generate() self.public_key = PublicKey.from_signing_key(self.private_key) # self.uuids = [uuid4() for _ in range(3)] self.operations = [[ Operation(OperationRev(),'http://example0.com/v0/', [self.public_key]), Operation(OperationRev(),'http://example1.com/v0/', [self.public_key]) ]] for op in self.operations[0]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[0][0]), 'http://example0.com/v1/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[0][1]), 'http://example1.com/v1/', [self.public_key]), Operation(OperationRev(), 'http://example2.com/v0/', [self.public_key]) ]) for op in self.operations[1]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[1][0]),'http://example0.com/v2/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[1][1]), 'http://example1.com/v2/', [self.public_key]) ]) for op in self.operations[2]: sign_object(self.public_key, self.private_key, op) self.operations.append([ Operation(OperationRev.from_obj(self.operations[1][1]), 'http://alternative1.com/', [self.public_key]), Operation(OperationRev.from_obj(self.operations[1][2]), 'http://alternative2.com/', [self.public_key]) ]) for op in self.operations[3]: sign_object(self.public_key, self.private_key, op) timestamp = int(time.time()) - 100 self.blocks = [Block.from_operations_list(BlockRev(), timestamp, self.operations[0])] self.blocks[0].mine() sign_object(self.public_key, self.private_key, self.blocks[0]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[0]), timestamp + 20, self.operations[1])) self.blocks[1].mine() sign_object(self.public_key, self.private_key, self.blocks[1]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[1]), timestamp + 40, self.operations[2])) self.blocks[2].mine() sign_object(self.public_key, self.private_key, self.blocks[2]) self.blocks.append( Block.from_operations_list(BlockRev.from_obj(self.blocks[1]), timestamp + 60, self.operations[3])) self.blocks[3].mine() sign_object(self.public_key, self.private_key, self.blocks[3])
def test_4_wrong_previous_block(self): self.blocks[0].previous_block_rev = BlockRev.from_id(double_sha(b'something')) self.blocks[0].mine() sign_object(self.public_key, self.private_key, self.blocks[0]) with self.assertRaisesRegex(Block.ChainError, "previous_block_rev does not exist"): self.blocks[0].verify()
def add_blocks(self, ops): """ create blocks: blocks[0] -> blocks[1] -> blocks[2] -> blocks[3] -> blocks[5] \-> blocks[4] -> blocks[6] """ start_time = 42 blocks = [Block.from_operations_list(BlockRev(), start_time, [ops[0], ops[1]])] blocks[0].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[0]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[0]), start_time + 10, [ops[2], ops[6]])) blocks[1].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[1]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[1]), start_time + 20, [ops[3], ops[5]])) blocks[2].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[2]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[2]), start_time + 30, [ops[8], ops[4]])) blocks[3].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[3]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[2]), start_time + 40, [ops[4], ops[7]])) blocks[4].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[4]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[3]), start_time + 50, [ops[7], ops[9]])) blocks[5].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[5]) blocks.append(Block.from_operations_list(BlockRev.from_obj(blocks[4]), start_time + 60, [ops[8], ops[9]])) blocks[6].mine() sign_object(self.public_keys[0], self.private_keys[0], blocks[6]) return blocks
def mint_block(): if len(ops) >= MIN_OPS_IN_BLOCK: print("Preparing block with {} operations.".format(len(ops))) blockchain = pmpi.core.get_blockchain() rev = BlockRev.from_id(blockchain.head) if blockchain.max_depth > 0 else BlockRev() block = Block.from_operations_list(rev, int(time.time()), ops) block.difficulty = 10 # TODO difficulty! block.mine() self.user.sign_object(block) block.verify() print("Block minted. Sending.") self.transport.write(b'BL' + block.raw_with_operations()) else: print("There are not enough operations to mint a block") for op in ops: asyncio.ensure_future(operation_queue.put(op))
def test(): try: os.remove('test_database_file') except OSError: pass initialise_database('test_database_file') obj_private_key = SigningKey.generate() obj_public_key = PublicKey.from_signing_key(obj_private_key) obj_uuids = [uuid4() for _ in range(3)] obj_operations = [[ Operation(OperationRev(), 'http://example0.com/v0/', [obj_public_key]), Operation(OperationRev(), 'http://example1.com/v0/', [obj_public_key]) ]] for op in obj_operations[0]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[0][0]), 'http://example0.com/v1/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[0][1]), 'http://example1.com/v1/', [obj_public_key]), Operation(OperationRev(), 'http://example2.com/v0/', [obj_public_key]) ]) for op in obj_operations[1]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][0]), 'http://example0.com/v2/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://example1.com/v2/', [obj_public_key]) ]) for op in obj_operations[2]: sign_object(obj_public_key, obj_private_key, op) obj_operations.append([ Operation(OperationRev.from_obj(obj_operations[1][1]), 'http://alternative1.com/', [obj_public_key]), Operation(OperationRev.from_obj(obj_operations[1][2]), 'http://alternative2.com/', [obj_public_key]) ]) for op in obj_operations[3]: sign_object(obj_public_key, obj_private_key, op) timestamp = int(time.time()) - 100 obj_blocks = [Block.from_operations_list(BlockRev(), timestamp, obj_operations[0])] obj_blocks[0].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[0]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[0]), timestamp + 20, obj_operations[1])) obj_blocks[1].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[1]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 40, obj_operations[2])) obj_blocks[2].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[2]) obj_blocks.append( Block.from_operations_list(BlockRev.from_obj(obj_blocks[1]), timestamp + 60, obj_operations[3])) obj_blocks[3].mine() sign_object(obj_public_key, obj_private_key, obj_blocks[3]) for block in obj_blocks: block.put() for block in obj_blocks: new_block = Block.get(block.id) assert new_block.id == block.id for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass assert sorted(Block.get_ids_list()) == sorted([block.id for block in obj_blocks]) obj_blocks[2].remove() for block in obj_blocks[:2]: try: block.remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[3].remove() try: obj_blocks[0].remove() raise AssertionError except Block.ChainOperationBlockedError: pass obj_blocks[1].remove() obj_blocks[0].remove() for block in obj_blocks: try: block.remove() raise AssertionError except Block.DoesNotExist: pass assert Block.get_ids_list() == [] close_database() os.remove('test_database_file')