def get_tx_done_block(ask_amount, bid_amount, traded_amount, ask_total_traded, bid_total_traded): ask_pair = AssetPair(AssetAmount(ask_amount, 'BTC'), AssetAmount(ask_amount, 'MB')) bid_pair = AssetPair(AssetAmount(bid_amount, 'BTC'), AssetAmount(bid_amount, 'MB')) ask = Order(OrderId(TraderId(b'0' * 20), OrderNumber(1)), ask_pair, Timeout(3600), Timestamp.now(), True) ask._traded_quantity = ask_total_traded bid = Order(OrderId(TraderId(b'1' * 20), OrderNumber(1)), bid_pair, Timeout(3600), Timestamp.now(), False) bid._traded_quantity = bid_total_traded tx = Transaction( TransactionId(TraderId(b'0' * 20), TransactionNumber(1)), AssetPair(AssetAmount(traded_amount, 'BTC'), AssetAmount(traded_amount, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) tx.transferred_assets.first += AssetAmount(traded_amount, 'BTC') tx.transferred_assets.second += AssetAmount(traded_amount, 'MB') tx_done_block = MarketBlock() tx_done_block.type = b'tx_done' tx_done_block.transaction = { 'ask': ask.to_status_dictionary(), 'bid': bid.to_status_dictionary(), 'tx': tx.to_dictionary(), 'version': MarketCommunity.PROTOCOL_VERSION } tx_done_block.transaction['ask']['address'], tx_done_block.transaction[ 'ask']['port'] = "1.1.1.1", 1234 tx_done_block.transaction['bid']['address'], tx_done_block.transaction[ 'bid']['port'] = "1.1.1.1", 1234 return tx_done_block
def test_insert_or_update_transaction(db, transaction): """ Test the conditional insertion or update of a transaction in the database """ # Test insertion db.insert_or_update_transaction(transaction) transactions = db.get_all_transactions() assert len(transactions) == 1 # Test try to update with older timestamp before_trans1 = Transaction(transaction.transaction_id, transaction.assets, transaction.order_id, transaction.partner_order_id, Timestamp(int(transaction.timestamp) - 1000)) db.insert_or_update_transaction(before_trans1) transaction = db.get_transaction(transaction.transaction_id) assert int(transaction.timestamp) == int(transaction.timestamp) # Test update with newer timestamp after_trans1 = Transaction(transaction.transaction_id, transaction.assets, transaction.order_id, transaction.partner_order_id, Timestamp(int(transaction.timestamp) + 1000)) db.insert_or_update_transaction(after_trans1) transaction = db.get_transaction(transaction.transaction_id) assert int(transaction.timestamp) == int(after_trans1.timestamp)
async def setUp(self): super(TestDatabase, self).setUp() path = os.path.join(self.getStateDir(), 'sqlite') if not os.path.exists(path): os.makedirs(path) self.database = MarketDB(self.getStateDir(), 'market') self.order_id1 = OrderId(TraderId(b'3' * 20), OrderNumber(4)) self.order_id2 = OrderId(TraderId(b'4' * 20), OrderNumber(5)) self.order1 = Order( self.order_id1, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), True) self.order2 = Order( self.order_id2, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), False) self.order2.reserve_quantity_for_tick( OrderId(TraderId(b'3' * 20), OrderNumber(4)), 3) self.transaction_id1 = TransactionId(b'a' * 32) self.transaction1 = Transaction( self.transaction_id1, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(2)), Timestamp(20000)) self.payment1 = Payment(TraderId(b'0' * 20), self.transaction_id1, AssetAmount(5, 'BTC'), WalletAddress('abc'), WalletAddress('def'), PaymentId("abc"), Timestamp(20000)) self.transaction1.add_payment(self.payment1)
def test_insert_or_update_transaction(self): """ Test the conditional insertion or update of a transaction in the database """ # Test insertion self.database.insert_or_update_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) # Test try to update with older timestamp before_trans1 = Transaction( self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(int(self.transaction1.timestamp) - 1000)) self.database.insert_or_update_transaction(before_trans1) transaction = self.database.get_transaction( self.transaction1.transaction_id) self.assertEqual(int(transaction.timestamp), int(self.transaction1.timestamp)) # Test update with newer timestamp after_trans1 = Transaction( self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(int(self.transaction1.timestamp) + 1000)) self.database.insert_or_update_transaction(after_trans1) transaction = self.database.get_transaction( self.transaction1.transaction_id) self.assertEqual(int(transaction.timestamp), int(after_trans1.timestamp))
def transaction(payment): transaction_id = TransactionId(b'a' * 32) transaction = Transaction( transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(2)), Timestamp(20000)) transaction.add_payment(payment) return transaction
def setUp(self): BaseTestCase.setUp(self) self.ask = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), True) self.bid = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), False) self.transaction = Transaction( TransactionId(TraderId(b'0' * 20), TransactionNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) ask_tx = self.ask.to_block_dict() bid_tx = self.bid.to_block_dict() self.tick_block = MarketBlock() self.tick_block.type = b'ask' self.tick_block.transaction = {'tick': ask_tx} self.cancel_block = MarketBlock() self.cancel_block.type = b'cancel_order' self.cancel_block.transaction = { 'trader_id': 'a' * 20, 'order_number': 1 } self.tx_block = MarketBlock() self.tx_block.type = b'tx_init' self.tx_block.transaction = { 'ask': ask_tx, 'bid': bid_tx, 'tx': self.transaction.to_dictionary() } payment = { 'trader_id': 'a' * 40, 'transaction_number': 3, 'transferred': { 'amount': 3, 'type': 'BTC' }, 'payment_id': 'a', 'address_from': 'a', 'address_to': 'b', 'timestamp': 1234, 'success': True } self.payment_block = MarketBlock() self.payment_block.type = b'tx_payment' self.payment_block.transaction = {'payment': payment}
def setUp(self): # Object creation self.transaction_id = TransactionId(b'a' * 32) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) self.proposed_trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MC')), Timestamp(0)) self.tick = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(5, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(00), True) self.tick2 = Tick( OrderId(TraderId(b'0' * 20), OrderNumber(2)), AssetPair(AssetAmount(500, 'BTC'), AssetAmount(5, 'MC')), Timeout(0), Timestamp(0), True) self.order_timestamp = Timestamp.now() self.order = Order( OrderId(TraderId(b'0' * 20), OrderNumber(3)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(40, 'MC')), Timeout(5000), self.order_timestamp, False) self.order.set_verified() self.order2 = Order( OrderId(TraderId(b'0' * 20), OrderNumber(4)), AssetPair(AssetAmount(50, 'BTC'), AssetAmount(10, 'MC')), Timeout(5), Timestamp(int(time.time() * 1000) - 1000 * 1000), True) self.order2.set_verified()
def test_from_proposed_trade(self): """ Test creating a transaction from a proposed trade """ transaction = Transaction.from_proposed_trade(self.proposed_trade, self.transaction_id) self.assertEqual(transaction.assets, self.transaction.assets)
def get_all_transactions(self): """ Return all transactions in the database. """ db_result = self.execute(u"SELECT * FROM transactions") return [Transaction.from_database(db_item, self.get_payments(TransactionId(TraderId(bytes(db_item[0])), TransactionNumber(db_item[1])))) for db_item in db_result]
def setUp(self): # Object creation self.transaction_id = TransactionId(TraderId(b'0' * 20), TransactionNumber(1)) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), OrderId(TraderId(b'3' * 20), OrderNumber(2)), OrderId(TraderId(b'2' * 20), OrderNumber(1)), Timestamp(0)) self.proposed_trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), Timestamp(0)) self.payment = Payment( TraderId(b'0' * 20), TransactionId(TraderId(b'2' * 20), TransactionNumber(2)), AssetAmount(3, 'MB'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4), True)
def tx_done_block(ask, bid, accepted_trade): tx_done_block = MarketBlock() tx_done_block.type = b'tx_done' transaction = Transaction.from_accepted_trade(accepted_trade, TransactionId(b'a' * 32)) tx_done_block.transaction = { 'ask': ask.to_block_dict(), 'bid': bid.to_block_dict(), 'tx': transaction.to_block_dictionary() }
def setUp(self): # Object creation self.memory_transaction_repository = MemoryTransactionRepository(b'0' * 20) self.transaction_id = TransactionId(b'a' * 32) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(10, 'BTC'), AssetAmount(10, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'2' * 20), OrderNumber(2)), Timestamp(0))
def create_from_proposed_trade(self, proposed_trade): """ :type proposed_trade: ProposedTrade :rtype: Transaction """ transaction = Transaction.from_proposed_trade(proposed_trade, self.transaction_repository.next_identity()) self.transaction_repository.add(transaction) self._logger.info("Transaction created with id: %s, asset pair %s", transaction.transaction_id, transaction.assets) return transaction
def get_transaction(self, transaction_id): """ Return a transaction with a specific id. """ try: db_result = next(self.execute(u"SELECT * FROM transactions WHERE trader_id = ? AND transaction_number = ?", (database_blob(bytes(transaction_id.trader_id)), text_type(transaction_id.transaction_number)))) except StopIteration: return None return Transaction.from_database(db_result, self.get_payments(transaction_id))
def create_from_start_transaction(self, start_transaction): """ :type start_transaction: StartTransaction :rtype: Transaction """ transaction = Transaction(start_transaction.transaction_id, start_transaction.assets, start_transaction.recipient_order_id, start_transaction.order_id, Timestamp.now()) self.transaction_repository.add(transaction) self._logger.info("Transaction created with id: %s, asset pair %s", transaction.transaction_id, transaction.assets) return transaction
def add_transaction_and_payment(self): """ Add a transaction and a payment to the market """ self.accepted_trade = AcceptedTrade( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), 1234, AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timestamp(0)) transaction = Transaction.from_accepted_trade(self.accepted_trade, TransactionId(b'a' * 32)) payment = Payment(TraderId(b'0' * 20), transaction.transaction_id, AssetAmount(20, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4000)) transaction.add_payment(payment) self.nodes[ 0].overlay.transaction_manager.transaction_repository.update( transaction) return transaction
class TestMarketBlock(BaseTestCase): """ This class contains tests for a TrustChain block as used in the market. """ def setUp(self): BaseTestCase.setUp(self) self.ask = Ask( OrderId(TraderId(b'0' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), True) self.bid = Ask( OrderId(TraderId(b'1' * 20), OrderNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), Timeout(30), Timestamp(0), False) self.transaction = Transaction( TransactionId(TraderId(b'0' * 20), TransactionNumber(1)), AssetPair(AssetAmount(30, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(1)), Timestamp(0)) ask_tx = self.ask.to_block_dict() bid_tx = self.bid.to_block_dict() self.tick_block = MarketBlock() self.tick_block.type = b'ask' self.tick_block.transaction = {'tick': ask_tx} self.cancel_block = MarketBlock() self.cancel_block.type = b'cancel_order' self.cancel_block.transaction = { 'trader_id': 'a' * 20, 'order_number': 1 } self.tx_block = MarketBlock() self.tx_block.type = b'tx_init' self.tx_block.transaction = { 'ask': ask_tx, 'bid': bid_tx, 'tx': self.transaction.to_dictionary() } payment = { 'trader_id': 'a' * 40, 'transaction_number': 3, 'transferred': { 'amount': 3, 'type': 'BTC' }, 'payment_id': 'a', 'address_from': 'a', 'address_to': 'b', 'timestamp': 1234, 'success': True } self.payment_block = MarketBlock() self.payment_block.type = b'tx_payment' self.payment_block.transaction = {'payment': payment} def test_tick_block(self): """ Test whether a tick block can be correctly verified """ self.assertTrue(self.tick_block.is_valid_tick_block()) self.tick_block.transaction['tick']['timeout'] = -1 self.assertFalse(self.tick_block.is_valid_tick_block()) self.tick_block.transaction['tick']['timeout'] = 3600 self.tick_block.type = b'test' self.assertFalse(self.tick_block.is_valid_tick_block()) self.tick_block.type = b'ask' self.tick_block.transaction['test'] = self.tick_block.transaction.pop( 'tick') self.assertFalse(self.tick_block.is_valid_tick_block()) self.tick_block.transaction['tick'] = self.tick_block.transaction.pop( 'test') self.tick_block.transaction['tick'].pop('timeout') self.assertFalse(self.tick_block.is_valid_tick_block()) self.tick_block.transaction['tick']['timeout'] = "300" self.assertFalse(self.tick_block.is_valid_tick_block()) self.tick_block.transaction['tick']['timeout'] = 300 self.tick_block.transaction['tick']['trader_id'] = 'g' * 21 self.assertFalse(self.tick_block.is_valid_tick_block()) # Make the asset pair invalid assets = self.tick_block.transaction['tick']['assets'] self.tick_block.transaction['tick']['trader_id'] = 'a' * 40 assets['test'] = assets.pop('first') self.assertFalse(self.tick_block.is_valid_tick_block()) assets['first'] = assets.pop('test') assets['first']['test'] = assets['first'].pop('amount') self.assertFalse(self.tick_block.is_valid_tick_block()) assets['first']['amount'] = assets['first']['test'] assets['second']['test'] = assets['second'].pop('amount') self.assertFalse(self.tick_block.is_valid_tick_block()) assets['second']['amount'] = assets['second']['test'] assets['first']['amount'] = 3.4 self.assertFalse(self.tick_block.is_valid_tick_block()) assets['first']['amount'] = 2**64 self.assertFalse(self.tick_block.is_valid_tick_block()) assets['first']['amount'] = 3 assets['second']['type'] = 4 self.assertFalse(self.tick_block.is_valid_tick_block()) def test_cancel_block(self): """ Test whether a cancel block can be correctly verified """ self.assertTrue(self.cancel_block.is_valid_cancel_block()) self.cancel_block.type = b'cancel' self.assertFalse(self.cancel_block.is_valid_cancel_block()) self.cancel_block.type = b'cancel_order' self.cancel_block.transaction.pop('trader_id') self.assertFalse(self.cancel_block.is_valid_cancel_block()) self.cancel_block.transaction['trader_id'] = 3 self.assertFalse(self.cancel_block.is_valid_cancel_block()) def test_tx_init_done_block(self): """ Test whether a tx_init/tx_done block can be correctly verified """ self.assertTrue(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.type = b'test' self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.type = b'tx_init' self.tx_block.transaction['test'] = self.tx_block.transaction.pop( 'ask') self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['ask'] = self.tx_block.transaction.pop( 'test') self.tx_block.transaction['ask']['timeout'] = 3.44 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['ask']['timeout'] = 3 self.tx_block.transaction['bid']['timeout'] = 3.44 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['bid']['timeout'] = 3 self.tx_block.transaction['tx'].pop('trader_id') self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['tx']['trader_id'] = 'a' * 40 self.tx_block.transaction['tx']['test'] = 3 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['tx'].pop('test') self.tx_block.transaction['tx']['trader_id'] = 'a' self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['tx']['trader_id'] = 'a' * 40 self.tx_block.transaction['tx']['assets']['first']['amount'] = 3.4 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['tx']['assets']['first']['amount'] = 3 self.tx_block.transaction['tx']['transferred']['first']['amount'] = 3.4 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) self.tx_block.transaction['tx']['transferred']['first']['amount'] = 3 self.tx_block.transaction['tx']['transaction_number'] = 3.4 self.assertFalse(self.tx_block.is_valid_tx_init_done_block()) def test_tx_payment_block(self): """ Test whether a tx_payment block can be correctly verified """ self.assertTrue(self.payment_block.is_valid_tx_payment_block()) self.payment_block.type = b'test' self.assertFalse(self.payment_block.is_valid_tx_payment_block()) self.payment_block.type = b'tx_payment' self.payment_block.transaction[ 'test'] = self.payment_block.transaction.pop('payment') self.assertFalse(self.payment_block.is_valid_tx_payment_block()) self.payment_block.transaction[ 'payment'] = self.payment_block.transaction.pop('test') self.payment_block.transaction['payment'].pop('address_to') self.assertFalse(self.payment_block.is_valid_tx_payment_block()) self.payment_block.transaction['payment']['address_to'] = 'a' self.payment_block.transaction['payment']['test'] = 'a' self.assertFalse(self.payment_block.is_valid_tx_payment_block()) self.payment_block.transaction['payment'].pop('test') self.payment_block.transaction['payment']['address_to'] = 3 self.assertFalse(self.payment_block.is_valid_tx_payment_block()) self.payment_block.transaction['payment']['address_to'] = 'a' self.payment_block.transaction['payment']['trader_id'] = 'a' * 39 self.assertFalse(self.payment_block.is_valid_tx_payment_block()) def test_is_valid_asset_pair(self): """ Test the method to verify whether an asset pair is valid """ self.assertFalse(MarketBlock.is_valid_asset_pair({'a': 'b'})) self.assertFalse( MarketBlock.is_valid_asset_pair({ 'first': { 'amount': 3, 'type': 'DUM1' }, 'second': { 'amount': 3 } })) self.assertFalse( MarketBlock.is_valid_asset_pair({ 'first': { 'type': 'DUM1' }, 'second': { 'amount': 3, 'type': 'DUM2' } })) self.assertFalse( MarketBlock.is_valid_asset_pair({ 'first': { 'amount': "4", 'type': 'DUM1' }, 'second': { 'amount': 3, 'type': 'DUM2' } })) self.assertFalse( MarketBlock.is_valid_asset_pair({ 'first': { 'amount': 4, 'type': 'DUM1' }, 'second': { 'amount': "3", 'type': 'DUM2' } })) self.assertFalse( MarketBlock.is_valid_asset_pair({ 'first': { 'amount': -4, 'type': 'DUM1' }, 'second': { 'amount': 3, 'type': 'DUM2' } }))
class TransactionTestSuite(unittest.TestCase): """Transaction test cases.""" def setUp(self): # Object creation self.maxDiff = None self.transaction_id = TransactionId(b'a' * 32) self.transaction = Transaction( self.transaction_id, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), OrderId(TraderId(b'3' * 20), OrderNumber(2)), OrderId(TraderId(b'2' * 20), OrderNumber(1)), Timestamp(0)) self.proposed_trade = Trade.propose( TraderId(b'0' * 20), OrderId(TraderId(b'0' * 20), OrderNumber(2)), OrderId(TraderId(b'1' * 20), OrderNumber(3)), AssetPair(AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')), Timestamp(0)) self.payment = Payment(TraderId(b'0' * 20), TransactionId(b'a' * 32), AssetAmount(100, 'MB'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4)) self.payment2 = Payment(TraderId(b'0' * 20), TransactionId(b'a' * 32), AssetAmount(100, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('aaa'), Timestamp(4)) def test_add_payment(self): """ Test the addition of a payment to a transaction """ self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.transferred_assets.first.amount, 0) self.assertEqual(self.transaction.transferred_assets.second.amount, 100) self.assertTrue(self.transaction.payments) def test_next_payment(self): """ Test the process of determining the next payment details during a transaction """ self.assertEqual(self.transaction.next_payment(True), AssetAmount(100, 'BTC')) self.assertEqual(self.transaction.next_payment(False), AssetAmount(100, 'MB')) def test_is_payment_complete(self): """ Test whether a payment is correctly marked as complete """ self.assertFalse(self.transaction.is_payment_complete()) self.transaction.add_payment(self.payment) self.assertFalse(self.transaction.is_payment_complete()) self.transaction._transferred_assets = AssetPair( AssetAmount(100, 'BTC'), AssetAmount(100, 'MB')) self.assertTrue(self.transaction.is_payment_complete()) def test_to_dictionary(self): """ Test the to dictionary method of a transaction """ self.assertDictEqual( self.transaction.to_block_dictionary(), { 'trader_id': "33" * 20, 'transaction_id': "61" * 32, 'order_number': 2, 'partner_trader_id': "32" * 20, 'partner_order_number': 1, 'assets': { 'first': { 'amount': 100, 'type': 'BTC', }, 'second': { 'amount': 100, 'type': 'MB' } }, 'transferred': { 'first': { 'amount': 0, 'type': 'BTC', }, 'second': { 'amount': 0, 'type': 'MB' } }, 'timestamp': 0, }) def test_status(self): """ Test the status of a transaction """ self.assertEqual(self.transaction.status, 'pending') self.transaction.add_payment(self.payment) self.transaction.add_payment(self.payment2) self.assertEqual(self.transaction.status, 'completed')
class TestDatabase(AbstractServer): async def setUp(self): super(TestDatabase, self).setUp() path = os.path.join(self.getStateDir(), 'sqlite') if not os.path.exists(path): os.makedirs(path) self.database = MarketDB(self.getStateDir(), 'market') self.order_id1 = OrderId(TraderId(b'3' * 20), OrderNumber(4)) self.order_id2 = OrderId(TraderId(b'4' * 20), OrderNumber(5)) self.order1 = Order( self.order_id1, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), True) self.order2 = Order( self.order_id2, AssetPair(AssetAmount(5, 'BTC'), AssetAmount(6, 'EUR')), Timeout(3600), Timestamp.now(), False) self.order2.reserve_quantity_for_tick( OrderId(TraderId(b'3' * 20), OrderNumber(4)), 3) self.transaction_id1 = TransactionId(b'a' * 32) self.transaction1 = Transaction( self.transaction_id1, AssetPair(AssetAmount(100, 'BTC'), AssetAmount(30, 'MB')), OrderId(TraderId(b'0' * 20), OrderNumber(1)), OrderId(TraderId(b'1' * 20), OrderNumber(2)), Timestamp(20000)) self.payment1 = Payment(TraderId(b'0' * 20), self.transaction_id1, AssetAmount(5, 'BTC'), WalletAddress('abc'), WalletAddress('def'), PaymentId("abc"), Timestamp(20000)) self.transaction1.add_payment(self.payment1) async def tearDown(self): self.database.close() await super(TestDatabase, self).tearDown() def test_add_get_order(self): """ Test the insertion and retrieval of an order in the database """ self.database.add_order(self.order1) self.database.add_order(self.order2) orders = self.database.get_all_orders() self.assertEqual(len(orders), 2) def test_get_specific_order(self): """ Test the retrieval of a specific order """ order_id = OrderId(TraderId(b'3' * 20), OrderNumber(4)) self.assertIsNone(self.database.get_order(order_id)) self.database.add_order(self.order1) self.assertIsNotNone(self.database.get_order(order_id)) def test_delete_order(self): """ Test the deletion of an order from the database """ self.database.add_order(self.order1) self.assertEqual(len(self.database.get_all_orders()), 1) self.database.delete_order(self.order_id1) self.assertEqual(len(self.database.get_all_orders()), 0) def test_get_next_order_number(self): """ Test the retrieval of the next order number from the database """ self.assertEqual(self.database.get_next_order_number(), 1) self.database.add_order(self.order1) self.assertEqual(self.database.get_next_order_number(), 5) def test_add_delete_reserved_ticks(self): """ Test the retrieval, addition and deletion of reserved ticks in the database """ self.database.add_reserved_tick(self.order_id1, self.order_id2, self.order1.total_quantity) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 1) self.database.delete_reserved_ticks(self.order_id1) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 0) def test_add_get_transaction(self): """ Test the insertion and retrieval of a transaction in the database """ self.database.add_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) self.assertEqual( len(self.database.get_payments(self.transaction1.transaction_id)), 1) def test_insert_or_update_transaction(self): """ Test the conditional insertion or update of a transaction in the database """ # Test insertion self.database.insert_or_update_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) # Test try to update with older timestamp before_trans1 = Transaction( self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(int(self.transaction1.timestamp) - 1000)) self.database.insert_or_update_transaction(before_trans1) transaction = self.database.get_transaction( self.transaction1.transaction_id) self.assertEqual(int(transaction.timestamp), int(self.transaction1.timestamp)) # Test update with newer timestamp after_trans1 = Transaction( self.transaction1.transaction_id, self.transaction1.assets, self.transaction1.order_id, self.transaction1.partner_order_id, Timestamp(int(self.transaction1.timestamp) + 1000)) self.database.insert_or_update_transaction(after_trans1) transaction = self.database.get_transaction( self.transaction1.transaction_id) self.assertEqual(int(transaction.timestamp), int(after_trans1.timestamp)) def test_get_specific_transaction(self): """ Test the retrieval of a specific transaction """ transaction_id = TransactionId(b'a' * 32) self.assertIsNone(self.database.get_transaction(transaction_id)) self.database.add_transaction(self.transaction1) self.assertIsNotNone(self.database.get_transaction(transaction_id)) def test_delete_transaction(self): """ Test the deletion of a transaction from the database """ self.database.add_transaction(self.transaction1) self.assertEqual(len(self.database.get_all_transactions()), 1) self.database.delete_transaction(self.transaction_id1) self.assertEqual(len(self.database.get_all_transactions()), 0) def test_add_get_payment(self): """ Test the insertion and retrieval of a payment in the database """ self.database.add_payment(self.payment1) payments = self.database.get_payments(self.transaction_id1) self.assertEqual(len(payments), 1) def test_add_remove_tick(self): """ Test addition, retrieval and deletion of ticks in the database """ ask = Tick.from_order(self.order1) self.database.add_tick(ask) bid = Tick.from_order(self.order2) self.database.add_tick(bid) self.assertEqual(len(self.database.get_ticks()), 2) self.database.delete_all_ticks() self.assertEqual(len(self.database.get_ticks()), 0) def test_check_database(self): """ Test the check of the database """ self.assertEqual( self.database.check_database(b"%d" % LATEST_DB_VERSION), LATEST_DB_VERSION) def test_get_upgrade_script(self): """ Test fetching the upgrade script of the database """ self.assertTrue(self.database.get_upgrade_script(1)) def test_db_upgrade(self): self.database.execute(u"DROP TABLE orders;") self.database.execute(u"DROP TABLE ticks;") self.database.execute( u"CREATE TABLE orders(x INTEGER PRIMARY KEY ASC);") self.database.execute( u"CREATE TABLE ticks(x INTEGER PRIMARY KEY ASC);") self.assertEqual(self.database.check_database(b"1"), 5)
def test_from_block(self): """ Test the from block method of a transaction """ block = { 'tx': { 'payment_complete': False, 'status': 'pending', 'partner_trader_id': '1111111111111111111111111111111111111111', 'trader_id': '0000000000000000000000000000000000000000', 'timestamp': 0, 'transferred': { 'second': { 'amount': 0, 'type': 'MB' }, 'first': { 'amount': 0, 'type': 'BTC' } }, 'partner_order_number': 1, 'order_number': 1, 'transaction_number': 1, 'assets': { 'second': { 'amount': 30, 'type': 'MB' }, 'first': { 'amount': 30, 'type': 'BTC' } } } } transaction = Transaction.from_block(block) self.assertEqual(block['tx']['trader_id'], transaction.transaction_id.trader_id.as_hex()) self.assertEqual(block['tx']['transaction_number'], int(transaction.transaction_id.transaction_number)) self.assertEqual(block['tx']['partner_trader_id'], transaction.partner_order_id.trader_id.as_hex()) self.assertEqual(block['tx']['partner_order_number'], int(transaction.partner_order_id.order_number)) self.assertEqual(block['tx']['timestamp'], int(transaction.timestamp)) self.assertEqual(block['tx']['assets']['first']['amount'], transaction.assets.first.amount) self.assertEqual(block['tx']['assets']['first']['type'], transaction.assets.first.asset_id) self.assertEqual(block['tx']['assets']['second']['amount'], transaction.assets.second.amount) self.assertEqual(block['tx']['assets']['second']['type'], transaction.assets.second.asset_id) self.assertEqual(block['tx']['transferred']['first']['amount'], transaction.transferred_assets.first.amount) self.assertEqual(block['tx']['transferred']['first']['type'], transaction.transferred_assets.first.asset_id) self.assertEqual(block['tx']['transferred']['second']['amount'], transaction.transferred_assets.second.amount) self.assertEqual(block['tx']['transferred']['second']['type'], transaction.transferred_assets.second.asset_id)