async def __init_peer_auth(self): try: node_key: bytes = await StubCollection().peer_stub.async_task().get_node_key() self.__peer_auth = Signer.from_prikey(node_key) ChannelProperty().peer_auth = self.__peer_auth except Exception as e: utils.exit_and_msg(f"peer auth init fail cause : {e}")
def test_valid_timestamp(self): """Test for timestamp buffer in block verifier""" def block_maker(timestamp: int, height: int = 0, prev_hash=None): """Make dummy block""" tx_versioner = TransactionVersioner() dummy_receipts = {} block_builder = BlockBuilder.new("0.1a", tx_versioner) for i in range(1000): tx_builder = TransactionBuilder.new("0x3", None, tx_versioner) tx_builder.signer = test_signer tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx = tx_builder.build() tx_serializer = TransactionSerializer.new(tx.version, tx.type(), tx_versioner) block_builder.transactions[tx.hash] = tx dummy_receipts[tx.hash.hex()] = { "dummy_receipt": "dummy", "tx_dumped": tx_serializer.to_full_data(tx) } block_builder.signer = test_signer block_builder.prev_hash = prev_hash block_builder.height = height block_builder.state_hash = Hash32(bytes(Hash32.size)) block_builder.receipts = dummy_receipts block_builder.reps = [ExternalAddress.fromhex_address(test_signer.address)] block_builder.peer_id = ExternalAddress.fromhex(test_signer.address) block_builder.next_leader = ExternalAddress.fromhex(test_signer.address) block_builder.fixed_timestamp = timestamp b = block_builder.build() assert b.header.timestamp == timestamp return b test_signer = Signer.from_prikey(os.urandom(32)) first_block = block_maker(height=0, timestamp=utils.get_time_stamp()) second_block = block_maker(height=1, timestamp=utils.get_time_stamp() + 5, prev_hash=first_block.header.hash) third_block_from_far_future = block_maker(height=2, prev_hash=second_block.header.hash, timestamp=utils.get_time_stamp() + conf.TIMESTAMP_BUFFER_IN_VERIFIER + 5_000_000) block_verifier = BlockVerifier.new("0.1a", TransactionVersioner()) leader = first_block.header.peer_id reps = [ExternalAddress.fromhex_address(test_signer.address)] print("*---Normal time range") block_verifier.verify(block=second_block, prev_block=first_block, blockchain=None, generator=leader, reps=reps) print("*---Abnormal time range") with self.assertRaises(Exception): block_verifier.verify(block=third_block_from_far_future, prev_block=second_block, blockchain=None, generator=leader, reps=reps)
def setUp(self): test_util.print_testname(self._testMethodName) self.peer_auth = Signer.from_prikey(os.urandom(32)) set_mock(self) # BlockChain 을 만듬 self.test_store = test_util.make_key_value_store('blockchain_db') self.assertIsNotNone(self.test_store, "DB생성 불가") self.chain = BlockChain(self.test_store)
def set_mock(test): peer_auth = Signer.from_prikey(os.urandom(32)) test.peer_auth = peer_auth peer_service_mock = PeerServiceMock() peer_service_mock.peer_manager = PeerManagerMock(peer_auth) peer_service_mock.channel_service = ChannelServiceMock(conf.LOOPCHAIN_DEFAULT_CHANNEL) ObjectManager().peer_service = peer_service_mock ObjectManager().channel_service = peer_service_mock.channel_service
def setUp(self): test_util.print_testname(self._testMethodName) self.peer_auth = Signer.from_prikey(os.urandom(32)) set_mock(self) # BlockChain 을 만듬 test_db = test_util.make_level_db(self.db_name) self.assertIsNotNone(test_db, "DB생성 불가") self.chain = BlockChain(test_db)
async def __init_peer_auth(self): try: node_key: bytes = await StubCollection().peer_stub.async_task().get_node_key(ChannelProperty().name) self.__peer_auth = Signer.from_prikey(node_key) except KeyError: self.__peer_auth = Signer.from_channel(ChannelProperty().name) except Exception as e: logging.exception(f"peer auth init fail cause : {e}") util.exit_and_msg(f"peer auth init fail cause : {e}")
def _tx_item(tx_versioner: TransactionVersioner) -> Transaction: test_signer = Signer.from_prikey(os.urandom(32)) tx_builder = TransactionBuilder.new("0x3", "", tx_versioner) tx_builder.signer = test_signer tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx: Transaction = tx_builder.build() return tx
def pytest_configure(): signers = [Signer.from_prikey(os.urandom(32)) for _ in range(100)] reps = [ExternalAddress.fromhex_address(signer.address) for signer in signers] pytest.SIGNERS: List[Signer] = signers pytest.REPS: List[ExternalAddress] = reps
def setUp(self): conf.Configure().init_configure() test_util.print_testname(self._testMethodName) self.peer_auth = Signer.from_prikey(os.urandom(32)) set_mock(self)
def test_block_v0_4(self): block_version = "0.4" test_signer = Signer.from_prikey(os.urandom(32)) tx_versioner = TransactionVersioner() dummy_receipts = {} block_builder = BlockBuilder.new(block_version, tx_versioner) for i in range(5): tx_builder = TransactionBuilder.new("0x3", None, tx_versioner) tx_builder.signer = test_signer tx_builder.to_address = ExternalAddress.new() tx_builder.step_limit = random.randint(0, 10000) tx_builder.value = random.randint(0, 10000) tx_builder.nid = 2 tx = tx_builder.build() tx_serializer = TransactionSerializer.new(tx.version, tx.type(), tx_versioner) block_builder.transactions[tx.hash] = tx dummy_receipts[tx.hash.hex()] = { "dummy_receipt": "dummy", "tx_dumped": tx_serializer.to_full_data(tx) } next_leader = ExternalAddress.fromhex( "hx00112233445566778899aabbccddeeff00112233") block_builder.signer = test_signer block_builder.height = 0 block_builder.prev_hash = Hash32(bytes(Hash32.size)) block_builder.state_hash = Hash32(bytes(Hash32.size)) block_builder.receipts = dummy_receipts block_builder.reps = [ ExternalAddress.fromhex_address(test_signer.address) ] block_builder.next_leader = next_leader block_builder.next_reps = [] vote = BlockVote.new(test_signer, utils.get_time_stamp(), block_builder.height - 1, 0, block_builder.prev_hash) votes = BlockVotes(block_builder.reps, conf.VOTING_RATIO, block_builder.height - 1, 0, block_builder.prev_hash) votes.add_vote(vote) block_builder.prev_votes = votes.votes block = block_builder.build() block_verifier = BlockVerifier.new(block_version, tx_versioner) block_verifier.invoke_func = lambda b, prev_b: (block, dummy_receipts) reps_getter = lambda _: block_builder.reps generator = ExternalAddress.fromhex_address(test_signer.address) block_verifier.verify(block, None, None, generator=generator, reps_getter=reps_getter) block_serializer = BlockSerializer.new(block_version, tx_versioner) block_serialized = block_serializer.serialize(block) logging.info(json.dumps(block_serialized, indent=4)) block_deserialized = block_serializer.deserialize(block_serialized) logging.info( json.dumps(block_serializer.serialize(block_deserialized), indent=4)) assert block.header == block_deserialized.header assert block.body == block_deserialized.body tx_hashes = list(block.body.transactions) tx_index = random.randrange(0, len(tx_hashes)) block_prover = BlockProver.new(block.header.version, tx_hashes, BlockProverType.Transaction) tx_proof = block_prover.get_proof(tx_index) assert block_prover.prove(tx_hashes[tx_index], block.header.transactions_hash, tx_proof) block_prover = BlockProver.new(block.header.version, block_builder.receipts, BlockProverType.Receipt) receipt_proof = block_prover.get_proof(tx_index) receipts_hash = block_prover.to_hash32( block_builder.receipts[tx_index]) assert block_prover.prove(receipts_hash, block.header.receipts_hash, receipt_proof)
def setUpClass(cls): cls.signers = [Signer.from_prikey(os.urandom(32)) for _ in range(100)] cls.reps = [ ExternalAddress.fromhex_address(signer.address) for signer in cls.signers ]
def setUpClass(cls) -> None: cls.temp_dir = tempfile.TemporaryDirectory() # Private Key cls.private_key = ec.generate_private_key(ec.SECP256K1, default_backend()) cls.private_der_path = os.path.join(cls.temp_dir.name, "private.der") with open(cls.private_der_path, "wb") as private_der_file: private_der_file.write( cls.private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.BestAvailableEncryption(b"TEST") ) ) cls.private_pem_path = os.path.join(cls.temp_dir.name, "private.pem") with open(cls.private_pem_path, "wb") as private_pem_file: private_pem_file.write( cls.private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.BestAvailableEncryption(b"TEST") ) ) key_info = keys.PrivateKeyInfo.load(cls.private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() )) cls.private_key_bytes = long_to_bytes(key_info['private_key'].native['private_key']) # Public Key cls.public_key = cls.private_key.public_key() cls.public_der_path = os.path.join(cls.temp_dir.name, "public.der") with open(cls.public_der_path, "wb") as public_der_file: public_der_file.write( cls.public_key.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) cls.public_pem_path = os.path.join(cls.temp_dir.name, "public.pem") with open(cls.public_pem_path, "wb") as public_pem_file: public_pem_file.write( cls.public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) key_info = keys.PublicKeyInfo.load( cls.public_key.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) cls.public_key_bytes = key_info['public_key'].native cls.signer_private_key_bytes = Signer.from_prikey(cls.private_key_bytes) cls.signer_private_key_der = Signer.from_prikey_file(cls.private_der_path, b"TEST") cls.signer_private_key_pem = Signer.from_prikey_file(cls.private_pem_path, b"TEST") cls.sign_verifier_private_key_bytes = SignVerifier.from_prikey(cls.private_key_bytes) cls.sign_verifier_private_key_der = SignVerifier.from_prikey_file(cls.private_der_path, b"TEST") cls.sign_verifier_private_key_pem = SignVerifier.from_prikey_file(cls.private_pem_path, b"TEST") cls.sign_verifier_public_key_bytes = SignVerifier.from_pubkey(cls.public_key_bytes) cls.sign_verifier_public_key_der = SignVerifier.from_pubkey_file(cls.public_der_path) cls.sign_verifier_public_key_pem = SignVerifier.from_pubkey_file(cls.public_pem_path)
def setUp(self): self.channel_name = conf.LOOPCHAIN_DEFAULT_CHANNEL test_util.print_testname(self._testMethodName) self.peer_auth = Signer.from_prikey(os.urandom(32)) set_mock(self)
def setUp(self): test_util.print_testname(self._testMethodName) self.signer = Signer.from_prikey(os.urandom(32)) self.tx_versioner = TransactionVersioner() self.tx_versioner.hash_generator_versions["0x2"] = 0
def setUp(self): test_util.print_testname(self._testMethodName) self.peer_auth = Signer.from_prikey(os.urandom(32)) set_mock(self)