def test_account_handler_apply_decode_error(): """ Case: send transaction request, to send tokens to address, to account handler with invalid transaction payload. Expect: invalid transaction error is raised cannot decode transaction payload error message. """ serialized_not_valid_transaction_payload = b'F1120071db7c02f5731d06df194dc95465e9b27' transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_not_valid_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_not_valid_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) mock_context = create_context(account_from_balance=ACCOUNT_FROM_BALANCE, account_to_balance=ACCOUNT_TO_BALANCE) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert 'Cannot decode transaction payload.' == str(error.value)
def test_close_atomic_swap_before_approve(): """ Case: close swap before it is approved. Expect: invalid transaction error is raised with transaction cannot be closed before it is approved error message. """ atomic_swap_close_payload = AtomicSwapClosePayload( swap_id=SWAP_ID, secret_key=SECRET_KEY, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.CLOSE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) existing_swap_info_to_close = AtomicSwapInfo() existing_swap_info_to_close.swap_id = SWAP_ID existing_swap_info_to_close.state = AtomicSwapInfo.OPENED existing_swap_info_to_close.secret_lock = SECRET_LOCK existing_swap_info_to_close.is_initiator = True serialized_existing_swap_info_to_lock = existing_swap_info_to_close.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info_to_lock, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert 'Transaction cannot be closed before it\'s approved.' == str( error.value)
def create_raw_transaction_send_token_payload(pub_key_to, amount=1): client = AccountClient() signer = client._signer address = client.make_address_from_data(pub_key_to) node_address = client.get_user_address() transfer = TransferPayload() transfer.address_to = address transfer.value = amount tr = TransactionPayload() tr.method = AccountMethod.TRANSFER tr.data = transfer.SerializeToString() payload = tr.SerializeToString() header = TransactionHeader( signer_public_key=signer.get_public_key().as_hex(), family_name=client._family_handler.family_name, family_version=client._family_handler.family_versions[-1], inputs=[node_address, address], outputs=[node_address, address], dependencies=[], payload_sha512=hash512(payload), batcher_public_key=signer.get_public_key().as_hex(), nonce=time.time().hex().encode()).SerializeToString() signature = signer.sign(header) transaction = Transaction(header=header, payload=payload, header_signature=signature) return transaction
def test_account_handler_apply(): """ Case: send transaction request, to send tokens to address, to the account handler. Expect: addresses data, stored in state, are changed according to transfer amount. """ expected_account_from_balance = ACCOUNT_FROM_BALANCE - TOKENS_AMOUNT_TO_SEND expected_account_to_balance = ACCOUNT_TO_BALANCE + TOKENS_AMOUNT_TO_SEND account_protobuf = Account() account_protobuf.balance = expected_account_from_balance expected_serialized_account_from_balance = account_protobuf.SerializeToString() account_protobuf.balance = expected_account_to_balance expected_serialized_account_to_balance = account_protobuf.SerializeToString() expected_state = { ACCOUNT_ADDRESS_FROM: expected_serialized_account_from_balance, ACCOUNT_ADDRESS_TO: expected_serialized_account_to_balance, } transfer_payload = TransferPayload() transfer_payload.address_to = ACCOUNT_ADDRESS_TO transfer_payload.value = TOKENS_AMOUNT_TO_SEND transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.TRANSFER transaction_payload.data = transfer_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) mock_context = create_context(account_from_balance=ACCOUNT_FROM_BALANCE, account_to_balance=ACCOUNT_TO_BALANCE) AccountHandler().apply(transaction=transaction_request, context=mock_context) state_as_list = mock_context.get_state(addresses=[ACCOUNT_ADDRESS_TO, ACCOUNT_ADDRESS_FROM]) state_as_dict = {entry.address: entry.data for entry in state_as_list} assert expected_state == state_as_dict
def test_approve_atomic_swap_without_secret_lock_state(): """ Case: approve atomic swap without secret lock state. Expect: invalid transaction error is raised with swap identifier is already closed error message. """ atomic_swap_approve_payload = AtomicSwapApprovePayload(swap_id=SWAP_ID, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.APPROVE transaction_payload.data = atomic_swap_approve_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) swap_info = AtomicSwapInfo() swap_info.swap_id = SWAP_ID swap_info.state = AtomicSwapInfo.OPENED swap_info.amount = TOKENS_AMOUNT_TO_SWAP swap_info.created_at = CURRENT_TIMESTAMP swap_info.sender_address = BOT_ADDRESS swap_info.secret_lock = SECRET_LOCK swap_info.receiver_address = ALICE_ADDRESS swap_info.is_initiator = True serialized_swap_info = swap_info.SerializeToString() mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_swap_info, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Swap identifier {SWAP_ID} is already closed.' == str(error.value)
def test_atomic_swap_init_with_empty_proto(): """ Case: send empty proto for init Expect: invalid transaction error """ inputs = outputs = [ ADDRESS_TO_GET_SWAP_COMMISSION_AMOUNT_BY, BLOCK_INFO_CONFIG_ADDRESS, BLOCK_INFO_ADDRESS, BOT_ADDRESS, ZERO_ADDRESS, ADDRESS_TO_STORE_SWAP_INFO_BY, ADDRESS_TO_GET_GENESIS_MEMBERS_AS_STRING_BY, ] atomic_swap_init_payload = AtomicSwapInitPayload() transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.INIT transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=inputs, outputs=outputs, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=inputs, outputs=outputs, initial_state={}) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( AtomicSwapInitPayload, { 'receiver_address': ['Missed address'], 'sender_address_non_local': ['This field is required.'], 'amount': ['This field is required.'], 'swap_id': ['Missed swap_id'], 'created_at': ['This field is required.'], } ) == str(error.value)
def test_atomic_swap_init_swap_no_account_in_state(): """ Case: initialize swap of bot's Remme node tokens to Alice's ERC20 Remme tokens from non-existent bot address. Expect: invalid transaction error is raised with not enough balance error message. """ atomic_swap_init_payload = AtomicSwapInitPayload( receiver_address=ALICE_ADDRESS, sender_address_non_local=BOT_ETHEREUM_ADDRESS, amount=TOKENS_AMOUNT_TO_SWAP, swap_id=SWAP_ID, secret_lock_by_solicitor=BOT_IT_IS_INITIATOR_MARK, email_address_encrypted_by_initiator=ALICE_EMAIL_ADDRESS_ENCRYPTED_BY_INITIATOR, created_at=CURRENT_TIMESTAMP, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.INIT transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) swap_commission_setting = Setting() swap_commission_setting.entries.add(key=SETTINGS_SWAP_COMMISSION, value=str(SWAP_COMMISSION_AMOUNT)) serialized_swap_commission_setting = swap_commission_setting.SerializeToString() mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ BLOCK_INFO_CONFIG_ADDRESS: SERIALIZED_BLOCK_INFO_CONFIG, BLOCK_INFO_ADDRESS: SERIALIZED_BLOCK_INFO, ADDRESS_TO_GET_SWAP_COMMISSION_AMOUNT_BY: serialized_swap_commission_setting, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) total_amount = TOKENS_AMOUNT_TO_SWAP + SWAP_COMMISSION_AMOUNT assert f'Not enough balance to perform the transaction in the amount (with a commission) {total_amount}.' \ == str(error.value)
def test_set_lock_to_atomic_swap_with_set_lock(): """ Case: set secret lock to atomic swap with already set secret lock. Expect: invalid transaction error is raised with secret lock is already added error message. """ atomic_swap_init_payload = AtomicSwapSetSecretLockPayload( swap_id=SWAP_ID, secret_lock=SECRET_LOCK, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.SET_SECRET_LOCK transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) already_set_lock_swap_info = AtomicSwapInfo() already_set_lock_swap_info.swap_id = SWAP_ID already_set_lock_swap_info.state = AtomicSwapInfo.OPENED already_set_lock_swap_info.secret_lock = SECRET_LOCK serialized_already_set_lock_swap_info = already_set_lock_swap_info.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_already_set_lock_swap_info, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Secret lock is already added for {SWAP_ID}.' == str(error.value)
def test_close_already_closed_atomic_swap(): """ Case: close already closed atomic swap. Expect: invalid transaction error is raised with already operation with closed or expired swap error message. """ atomic_swap_close_payload = AtomicSwapClosePayload( swap_id=SWAP_ID, secret_key=SECRET_KEY, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.CLOSE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) already_closed_swap_info = AtomicSwapInfo() already_closed_swap_info.swap_id = SWAP_ID already_closed_swap_info.state = AtomicSwapInfo.CLOSED serialized_already_closed_swap_info = already_closed_swap_info.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_already_closed_swap_info, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'No operations can be done upon the swap: {SWAP_ID} as it is already closed or expired.' == str( error.value)
def test_atomic_swap_init_swap_receiver_address_invalid_type(): """ Case: initialize swap of bot's Remme node tokens to Alice's ERC20 Remme tokens with invalid Alice node address. Expect: invalid transaction error is raised with atomic swap id has already been taken error message. """ invalid_receiver_address = '112934y*(J#QJ3UH*PD(:9B&TYDB*I0b0a8edc4104ef28093ee30' atomic_swap_init_payload = AtomicSwapInitPayload( receiver_address=invalid_receiver_address, sender_address_non_local=BOT_ETHEREUM_ADDRESS, amount=TOKENS_AMOUNT_TO_SWAP, swap_id=SWAP_ID, secret_lock_by_solicitor=BOT_IT_IS_INITIATOR_MARK, email_address_encrypted_by_initiator=ALICE_EMAIL_ADDRESS_ENCRYPTED_BY_INITIATOR, created_at=CURRENT_TIMESTAMP, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.INIT transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ BLOCK_INFO_CONFIG_ADDRESS: SERIALIZED_BLOCK_INFO_CONFIG, BLOCK_INFO_ADDRESS: SERIALIZED_BLOCK_INFO, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( AtomicSwapInitPayload, {'receiver_address': ['Address is not of a blockchain token type.']} ) == str(error.value)
def test_expire_atomic_swap_by_not_swap_owner(): """ Case: to expire atomic swap by signer address isn't specified in atomic swap sender address. Expect: invalid transaction error is raised with signer is not the one who opened the swap. error message. """ atomic_swap_close_payload = AtomicSwapExpirePayload(swap_id=SWAP_ID, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.EXPIRE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) existing_swap_info_to_expire = AtomicSwapInfo() existing_swap_info_to_expire.swap_id = SWAP_ID existing_swap_info_to_expire.state = AtomicSwapInfo.OPENED existing_swap_info_to_expire.sender_address = RANDOM_ADDRESS serialized_existing_swap_info_to_expire = existing_swap_info_to_expire.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info_to_expire, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Signer is not the one who opened the swap.' == str(error.value)
def generate_header(payload, inputs, outputs, signer_public_key=SENDER_PUBLIC_KEY): return TransactionHeader( signer_public_key=signer_public_key, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=inputs, outputs=outputs, dependencies=[], payload_sha512=hash512(data=payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), )
def get_crt_export_bin_sig_rem_sig(cert, key, client): crt_export = cert.public_bytes(serialization.Encoding.PEM) crt_bin = cert.public_bytes(serialization.Encoding.DER) pub_key = cert.public_key().public_bytes(encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo).decode('utf-8') crt_hash = hash512(crt_bin.hex()) rem_sig = client.sign_text(crt_hash) crt_sig = get_pub_key_signature(key, rem_sig).hex() valid_from = int(cert.not_valid_before.strftime("%s")) valid_to = int(cert.not_valid_after.strftime("%s")) return crt_export, crt_bin, crt_sig, rem_sig, pub_key, valid_from, valid_to
def test_account_handler_genesis_already_initialized(): """ Case: send transaction request, to send tokens from genesis address, to the account handler when genesis was already initialized. Expect: invalid transaction error is raised with genesis is already initialized error message. """ genesis_payload = GenesisPayload() genesis_payload.total_supply = TOKENS_AMOUNT_TO_SUPPLY transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.GENESIS transaction_payload.data = genesis_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=NODE_PRIVATE_KEY).sign(serialized_header), ) genesis_status = GenesisStatus() genesis_status.status = True mock_context = StubContext( inputs=INPUTS, outputs=OUTPUTS, initial_state={GENESIS_ADDRESS: genesis_status.SerializeToString()}) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert f'Genesis is already initialized.' == str(error.value)
def test_atomic_swap_init_swap_no_block_info(): """ Case: initialize swap of bot's Remme node tokens to Alice's ERC20 Remme tokens when no needed block information. Expect: invalid transaction error is raised with nlock config not found error message. """ atomic_swap_init_payload = AtomicSwapInitPayload( receiver_address=ALICE_ADDRESS, sender_address_non_local=BOT_ETHEREUM_ADDRESS, amount=TOKENS_AMOUNT_TO_SWAP, swap_id=SWAP_ID, secret_lock_by_solicitor=BOT_IT_IS_INITIATOR_MARK, email_address_encrypted_by_initiator=ALICE_EMAIL_ADDRESS_ENCRYPTED_BY_INITIATOR, created_at=CURRENT_TIMESTAMP, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.INIT transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ BLOCK_INFO_CONFIG_ADDRESS: SERIALIZED_BLOCK_INFO_CONFIG, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Block {block_info_config.latest_block + 1} not found.' == str(error.value)
def test_close_not_initialized_atomic_swap(): """ Case: close not initialized atomic swap. Expect: invalid transaction error is raised with atomic swap was not initiated error message. """ atomic_swap_close_payload = AtomicSwapClosePayload( swap_id=SWAP_ID, secret_key=SECRET_KEY, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.CLOSE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={}) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Atomic swap was not initiated for identifier {SWAP_ID}!' == str( error.value)
def test_set_lock_with_empty_proto(): """ Case: send empty proto for set lock Expect: invalid transaction error """ atomic_swap_init_payload = AtomicSwapSetSecretLockPayload() transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.SET_SECRET_LOCK transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={}) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( AtomicSwapSetSecretLockPayload, { 'swap_id': ['Missed swap_id'], 'secret_lock': ['This field is required.'], }) == str(error.value)
def test_account_handler_genesis_apply_with_empty_proto(): """ Case: send transaction request of genesis with empty proto Expect: invalid transaction error """ genesis_payload = GenesisPayload() transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.GENESIS transaction_payload.data = genesis_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=NODE_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={}) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( GenesisPayload, {'total_supply': ['This field is required.']}) == str(error.value)
def test_account_transfer_from_address_to_address_not_account_type(): """ Case: transfer tokens from address to address that is not account type. Expect: invalid transaction error is raised with receiver address is not account type error message. """ mock_context = create_context(account_from_balance=ACCOUNT_FROM_BALANCE, account_to_balance=ACCOUNT_TO_BALANCE) transfer_payload = TransferPayload() transfer_payload.address_to = ADDRESS_NOT_ACCOUNT_TYPE transfer_payload.value = TOKENS_AMOUNT_TO_SEND transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.TRANSFER transaction_payload.data = transfer_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( TransferPayload, { 'address_to': ['Address is not of a blockchain token type.'], } ) == str(error.value)
def test_account_transfer_from_address_without_previous_usage(): """ Case: transfer tokens from address to address when them have never been used before. Expect: invalid transaction error is raised with not enough transferable balance error message. """ initial_state = { ACCOUNT_ADDRESS_FROM: None, ACCOUNT_ADDRESS_TO: None, } mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state=initial_state) transfer_payload = TransferPayload() transfer_payload.address_to = ACCOUNT_ADDRESS_TO transfer_payload.value = TOKENS_AMOUNT_TO_SEND transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.TRANSFER transaction_payload.data = transfer_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert f'Not enough transferable balance. Sender\'s current balance: 0.' == str(error.value)
def test_account_handler_with_empty_proto(): """ Case: send transaction request with empty proto Expect: invalid transaction error """ transfer_payload = TransferPayload() transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.TRANSFER transaction_payload.data = transfer_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={}) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert proto_error_msg( TransferPayload, { 'address_to': ['Missed address'], 'value': ['Could not transfer with zero amount.'], } ) == str(error.value)
def test_account_handler_apply_invalid_transfer_method(): """ Case: send transaction request, to send tokens to address, to account handler with invalid transfer method value. Expect: invalid transaction error is raised with invalid account method value error message. """ account_method_impossible_value = 5347 transfer_payload = TransferPayload() transfer_payload.address_to = ACCOUNT_ADDRESS_TO transfer_payload.value = TOKENS_AMOUNT_TO_SEND transaction_payload = TransactionPayload() transaction_payload.method = account_method_impossible_value transaction_payload.data = transfer_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=RANDOM_NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get('family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer(private_key=ACCOUNT_FROM_PRIVATE_KEY).sign(serialized_header), ) mock_context = create_context(account_from_balance=ACCOUNT_FROM_BALANCE, account_to_balance=ACCOUNT_TO_BALANCE) with pytest.raises(InvalidTransaction) as error: AccountHandler().apply(transaction=transaction_request, context=mock_context) assert f'Invalid account method value ({account_method_impossible_value}) has been set.' == str(error.value)
def make_batch_list(self, payload_pb, addresses_input, addresses_output): payload = payload_pb.SerializeToString() signer = self._signer header = TransactionHeader( signer_public_key=signer.get_public_key().as_hex(), family_name=self._family_handler.family_name, family_version=self._family_handler.family_versions[-1], inputs=addresses_input, outputs=addresses_output, dependencies=[], payload_sha512=hash512(payload), batcher_public_key=signer.get_public_key().as_hex(), nonce=time.time().hex().encode()).SerializeToString() signature = signer.sign(header) transaction = Transaction(header=header, payload=payload, header_signature=signature) return self._sign_batch_list(signer, [transaction])
def test_expire_atomic_swap_before_invalid_withdrawal_by_bot(): """ Case: to expire atomic swap by bot if 48 hasn't been passed from atomic swap initialization timestamp. Expect: invalid transaction error is raised with signer is not the one who opened the swap. error message. """ atomic_swap_close_payload = AtomicSwapExpirePayload(swap_id=SWAP_ID, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.EXPIRE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) existing_swap_info_to_expire = AtomicSwapInfo() existing_swap_info_to_expire.swap_id = SWAP_ID existing_swap_info_to_expire.state = AtomicSwapInfo.OPENED existing_swap_info_to_expire.sender_address = BOT_ADDRESS existing_swap_info_to_expire.created_at = CURRENT_TIMESTAMP existing_swap_info_to_expire.is_initiator = True serialized_existing_swap_info_to_expire = existing_swap_info_to_expire.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info_to_expire, BLOCK_INFO_CONFIG_ADDRESS: SERIALIZED_BLOCK_INFO_CONFIG, BLOCK_INFO_ADDRESS: SERIALIZED_BLOCK_INFO, }) with pytest.raises(InvalidTransaction) as error: AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) assert f'Swap initiator needs to wait 24 hours since timestamp {CURRENT_TIMESTAMP} to withdraw.' == \ str(error.value)
def test_approve_atomic_swap(): """ Case: approve atomic swap. Expect: atomic swap state is changed to approved. """ atomic_swap_init_payload = AtomicSwapApprovePayload(swap_id=SWAP_ID, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.APPROVE transaction_payload.data = atomic_swap_init_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) existing_swap_info = AtomicSwapInfo() existing_swap_info.swap_id = SWAP_ID existing_swap_info.state = AtomicSwapInfo.SECRET_LOCK_PROVIDED existing_swap_info.sender_address = BOT_ADDRESS existing_swap_info.secret_lock = SECRET_LOCK existing_swap_info.is_initiator = True serialized_existing_swap_info = existing_swap_info.SerializeToString() mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info, }) expected_swap_info = AtomicSwapInfo() expected_swap_info.swap_id = SWAP_ID expected_swap_info.state = AtomicSwapInfo.APPROVED expected_swap_info.sender_address = BOT_ADDRESS expected_swap_info.secret_lock = SECRET_LOCK expected_swap_info.is_initiator = True serialized_expected_swap_info = expected_swap_info.SerializeToString() expected_state = { ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_expected_swap_info, } AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) state_as_list = mock_context.get_state( addresses=[ADDRESS_TO_STORE_SWAP_INFO_BY]) state_as_dict = {entry.address: entry.data for entry in state_as_list} assert expected_state == state_as_dict
def test_expire_atomic_swap(): """ Case: to expire atomic swap. Expect: increase bot address balance by swap amount. Leave commission on zero address. """ atomic_swap_expire_payload = AtomicSwapExpirePayload(swap_id=SWAP_ID, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.EXPIRE transaction_payload.data = atomic_swap_expire_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) bot_account = Account() bot_account.balance = 4700 serialized_bot_account = bot_account.SerializeToString() genesis_members_setting = Setting() genesis_members_setting.entries.add(key=SETTINGS_KEY_ZERO_ADDRESS_OWNERS, value=f'{BOT_PUBLIC_KEY},') serialized_genesis_members_setting = genesis_members_setting.SerializeToString( ) existing_swap_info = AtomicSwapInfo() existing_swap_info.swap_id = SWAP_ID existing_swap_info.state = AtomicSwapInfo.OPENED existing_swap_info.amount = TOKENS_AMOUNT_TO_SWAP existing_swap_info.created_at = CURRENT_TIMESTAMP // 2 existing_swap_info.sender_address = BOT_ADDRESS existing_swap_info.receiver_address = ALICE_ADDRESS existing_swap_info.is_initiator = True serialized_existing_swap_info = existing_swap_info.SerializeToString() mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ BLOCK_INFO_CONFIG_ADDRESS: SERIALIZED_BLOCK_INFO_CONFIG, BLOCK_INFO_ADDRESS: SERIALIZED_BLOCK_INFO, BOT_ADDRESS: serialized_bot_account, ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info, ADDRESS_TO_GET_GENESIS_MEMBERS_AS_STRING_BY: serialized_genesis_members_setting, }) expected_bot_account = Account() expected_bot_account.balance = 4700 + TOKENS_AMOUNT_TO_SWAP serialized_expected_bot_account = expected_bot_account.SerializeToString() expected_swap_info = AtomicSwapInfo() expected_swap_info.swap_id = SWAP_ID expected_swap_info.state = AtomicSwapInfo.EXPIRED expected_swap_info.amount = TOKENS_AMOUNT_TO_SWAP expected_swap_info.created_at = CURRENT_TIMESTAMP // 2 expected_swap_info.sender_address = BOT_ADDRESS expected_swap_info.receiver_address = ALICE_ADDRESS expected_swap_info.is_initiator = True serialized_expected_swap_info = expected_swap_info.SerializeToString() expected_state = { BOT_ADDRESS: serialized_expected_bot_account, ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_expected_swap_info, } AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) state_as_list = mock_context.get_state(addresses=[ ADDRESS_TO_STORE_SWAP_INFO_BY, BOT_ADDRESS, ]) state_as_dict = {entry.address: entry.data for entry in state_as_list} assert expected_state == state_as_dict
def __init__(self, name, versions): self._family_name = name self._family_versions = versions self._prefix = hash512(self._family_name)[:6]
def make_address_from_data(self, data): appendix = hash512(data)[:64] return self.make_address(appendix)
def test_account_handler_genesis_apply(): """ Case: send transaction request, to send tokens from genesis address, to the account handler. Expect: """ account = Account() account.balance = TOKENS_AMOUNT_TO_SUPPLY expected_serialized_account_to_balance = account.SerializeToString() genesis_payload = GenesisPayload() genesis_payload.total_supply = TOKENS_AMOUNT_TO_SUPPLY transaction_payload = TransactionPayload() transaction_payload.method = AccountMethod.GENESIS transaction_payload.data = genesis_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=NODE_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=NODE_PRIVATE_KEY).sign(serialized_header), ) genesis_status = GenesisStatus() genesis_status.status = True expected_state = { GENESIS_ADDRESS: genesis_status.SerializeToString(), ACCOUNT_ADDRESS_TO: expected_serialized_account_to_balance, } mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={}) AccountHandler().apply(transaction=transaction_request, context=mock_context) state_as_list = mock_context.get_state( addresses=[GENESIS_ADDRESS, ACCOUNT_ADDRESS_TO]) state_as_dict = {entry.address: entry.data for entry in state_as_list} assert expected_state == state_as_dict
def test_close_atomic_swap(): """ Case: close atomic swap. Expect: increase Alice account address by swap amount. """ atomic_swap_close_payload = AtomicSwapClosePayload( swap_id=SWAP_ID, secret_key=SECRET_KEY, ) transaction_payload = TransactionPayload() transaction_payload.method = AtomicSwapMethod.CLOSE transaction_payload.data = atomic_swap_close_payload.SerializeToString() serialized_transaction_payload = transaction_payload.SerializeToString() transaction_header = TransactionHeader( signer_public_key=BOT_PUBLIC_KEY, family_name=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_name'), family_version=TRANSACTION_REQUEST_ACCOUNT_HANDLER_PARAMS.get( 'family_version'), inputs=INPUTS, outputs=OUTPUTS, dependencies=[], payload_sha512=hash512(data=serialized_transaction_payload), batcher_public_key=RANDOM_NODE_PUBLIC_KEY, nonce=time.time().hex().encode(), ) serialized_header = transaction_header.SerializeToString() transaction_request = TpProcessRequest( header=transaction_header, payload=serialized_transaction_payload, signature=create_signer( private_key=BOT_PRIVATE_KEY).sign(serialized_header), ) alice_account = Account() alice_account.balance = 0 serialized_alice_account = alice_account.SerializeToString() existing_swap_info_to_close = AtomicSwapInfo() existing_swap_info_to_close.swap_id = SWAP_ID existing_swap_info_to_close.amount = 200 existing_swap_info_to_close.state = AtomicSwapInfo.APPROVED existing_swap_info_to_close.secret_lock = SECRET_LOCK existing_swap_info_to_close.is_initiator = True existing_swap_info_to_close.sender_address = BOT_ADDRESS existing_swap_info_to_close.receiver_address = ALICE_ADDRESS serialized_existing_swap_info_to_lock = existing_swap_info_to_close.SerializeToString( ) genesis_members_setting = Setting() genesis_members_setting.entries.add(key=SETTINGS_KEY_ZERO_ADDRESS_OWNERS, value=f'{BOT_PUBLIC_KEY},') serialized_genesis_members_setting = genesis_members_setting.SerializeToString( ) mock_context = StubContext(inputs=INPUTS, outputs=OUTPUTS, initial_state={ ADDRESS_TO_GET_GENESIS_MEMBERS_AS_STRING_BY: serialized_genesis_members_setting, ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_existing_swap_info_to_lock, ALICE_ADDRESS: serialized_alice_account, }) expected_alice_account = Account() expected_alice_account.balance = TOKENS_AMOUNT_TO_SWAP serialized_expected_alice_account = expected_alice_account.SerializeToString( ) expected_closed_swap_info = AtomicSwapInfo() expected_closed_swap_info.swap_id = SWAP_ID expected_closed_swap_info.amount = 200 expected_closed_swap_info.state = AtomicSwapInfo.CLOSED expected_closed_swap_info.secret_lock = SECRET_LOCK expected_closed_swap_info.secret_key = SECRET_KEY expected_closed_swap_info.is_initiator = True expected_closed_swap_info.sender_address = BOT_ADDRESS expected_closed_swap_info.receiver_address = ALICE_ADDRESS serialized_expected_closed_swap_info = expected_closed_swap_info.SerializeToString( ) expected_state = { ADDRESS_TO_STORE_SWAP_INFO_BY: serialized_expected_closed_swap_info, ALICE_ADDRESS: serialized_expected_alice_account, } AtomicSwapHandler().apply(transaction=transaction_request, context=mock_context) state_as_list = mock_context.get_state( addresses=[ADDRESS_TO_STORE_SWAP_INFO_BY, ALICE_ADDRESS]) state_as_dict = {entry.address: entry.data for entry in state_as_list} assert expected_state == state_as_dict