Esempio n. 1
0
 def get_tick_block(return_ask, pair):
     tick_cls = Ask if return_ask else Bid
     ask = tick_cls(OrderId(TraderId(b'0' * 20), OrderNumber(1)), pair, Timeout(3600), Timestamp.now(), return_ask)
     ask_tx = ask.to_block_dict()
     ask_tx["address"], ask_tx["port"] = "127.0.0.1", 1337
     tick_block = MarketBlock()
     tick_block.type = b'ask' if return_ask else b'bid'
     tick_block.transaction = {'tick': ask_tx, 'version': MarketCommunity.PROTOCOL_VERSION}
     return tick_block
Esempio n. 2
0
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()
    }
Esempio n. 3
0
def payment_block():
    payment = {
        'trader_id': 'a' * 40,
        'transaction_id': 'a' * 64,
        'transferred': {
            'amount': 3,
            'type': 'BTC'
        },
        'payment_id': 'a',
        'address_from': 'a',
        'address_to': 'b',
        'timestamp': 1234,
    }
    payment_block = MarketBlock()
    payment_block.type = b'tx_payment'
    payment_block.transaction = {'payment': payment}
    return payment_block
Esempio n. 4
0
    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}
Esempio n. 5
0
 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
Esempio n. 6
0
 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'
             }
         }))
Esempio n. 7
0
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'
                }
            }))
Esempio n. 8
0
def tx_init_block(accepted_trade):
    tx_init_block = MarketBlock()
    tx_init_block.type = b'tx_init'
    tx_init_block.transaction = {'tx': accepted_trade.to_block_dictionary()}
    return tx_init_block
Esempio n. 9
0
def cancel_block():
    cancel_block = MarketBlock()
    cancel_block.type = b'cancel_order'
    cancel_block.transaction = {'trader_id': 'a' * 20, 'order_number': 1}
    return cancel_block
Esempio n. 10
0
def tick_block(ask):
    tick_block = MarketBlock()
    tick_block.type = b'ask'
    tick_block.transaction = {'tick': ask.to_block_dict()}
    return tick_block