Ejemplo n.º 1
0
    def setUp(self):
        self.db = MagicMock()

        self.example_block = BlockFactory.from_mongo(EXAMPLE_MONGO_BLOCK)
        self.db.blocks.find_one = MagicMock(return_value=EXAMPLE_MONGO_BLOCK)
        self.chain = ExploderSyncer(
            database=self.db,
            blocks_dir="/home/vagrant/.gamecredits/blocks/",
            rpc_client=MagicMock()
        )
        self.db.blocks.create_index = MagicMock()
        self.SYNC_LIMIT = 100
Ejemplo n.º 2
0
class StartRpcSyncAfterStreamSync(unittest.TestCase):
    """
    Stream sync reached the limit, RPC sync should start
    """
    def setUp(self):
        self.db = MagicMock()

        self.example_block = BlockFactory.from_mongo(EXAMPLE_MONGO_BLOCK)
        self.db.blocks.find_one = MagicMock(return_value=EXAMPLE_MONGO_BLOCK)
        self.chain = ExploderSyncer(
            database=self.db,
            blocks_dir="/home/vagrant/.gamecredits/blocks/",
            rpc_client=MagicMock()
        )
        self.db.blocks.create_index = MagicMock()
        self.SYNC_LIMIT = 100

    def test_rpc_sync_no_reconverge(self):
        self.chain.db.highest_block = PropertyMock(return_value=self.example_block)
        self.chain._get_rpc_block_by_hash = MagicMock(return_value=self.example_block)
        self.chain.db.flush_cache = MagicMock()
        self.chain._follow_chain_and_insert = MagicMock()
        self.chain._find_reconverge_point = MagicMock(return_value=(self.example_block, self.example_block))
        self.chain._follow_chain_and_delete = MagicMock()
        self.chain._sync_rpc(550, 50)

        self.assertTrue(self.chain._follow_chain_and_insert.called)

        # There should be no reconverge
        self.assertFalse(self.chain._find_reconverge_point.called)
        # No blocks should be deleted
        self.assertFalse(self.chain._follow_chain_and_delete.called)

        self.assertTrue(self.chain.db.flush_cache.called)

    def test_rpc_sync_reconverge(self):
        self.chain.highest_block = PropertyMock(return_value=self.example_block)
        block2 = copy.deepcopy(self.example_block)
        block2.previousblockhash = "somethingsomething"
        self.chain._get_rpc_block_by_hash = MagicMock(return_value=block2)

        reconverge_mock = MagicMock(return_value=(self.example_block, self.example_block))

        self.chain._find_reconverge_point = reconverge_mock
        self.chain._follow_chain_and_delete = MagicMock()
        self.chain._follow_chain_and_insert = MagicMock()

        self.chain._sync_rpc(550, 50)

        self.assertTrue(self.chain._find_reconverge_point.called)
Ejemplo n.º 3
0
    def setUp(self):
        self.fake_mongo = MagicMock()
        self.fake_mongo.blocks.find_one.return_value = None

        self.fake_rpc = MagicMock()
        self.fake_rpc.getblockcount = MagicMock(return_value=10000)

        self.db = DatabaseGateway(database=self.fake_mongo, cache_size=1000)
        self.db.highest_block = PropertyMock(return_value=None)

        self.chain = ExploderSyncer(
            database=self.db,
            blocks_dir="/home/vagrant/.gamecredits/blocks/",
            rpc_client=self.fake_rpc
        )
Ejemplo n.º 4
0
def listen(path):
    mongo = MongoClient()
    rpc = AuthServiceProxy("http://%s:%[email protected]:8332" %
                           (RPC_USER, RPC_PASSWORD))
    db = DatabaseGateway(database=mongo.exploder)
    syncer = ExploderSyncer(database=db,
                            blocks_dir=path,
                            rpc_client=rpc,
                            rpc_sync_percent=98)

    while True:
        time.sleep(5)
        if db.highest_block is None or rpc.getblockcount(
        ) > db.highest_block.height:
            syncer.sync()
            sys.stdout.flush()
Ejemplo n.º 5
0
class SyncStreamTestCase(unittest.TestCase):
    def setUp(self):
        self.fake_mongo = MagicMock()
        self.fake_mongo.blocks.find_one.return_value = None

        self.fake_rpc = MagicMock()
        self.fake_rpc.getblockcount = MagicMock(return_value=10000)

        self.db = DatabaseGateway(database=self.fake_mongo, cache_size=1000)
        self.db.highest_block = PropertyMock(return_value=None)

        self.chain = ExploderSyncer(
            database=self.db,
            blocks_dir="/home/vagrant/.gamecredits/blocks/",
            rpc_client=self.fake_rpc
        )

        self.example_block = BlockFactory.from_mongo(EXAMPLE_MONGO_BLOCK)

    def test_block_files_get_skipped(self):
        files_before = self.chain.blk_files
        self.chain._sync_stream(highest_known=self.example_block, client_height=10000, limit=100)
        self.assertNotEqual(files_before, self.chain.blk_files)

    @patch('interactors.open')
    def test_stream_seek_called(self, mock_open):
        mock_stream = MagicMock()
        mock_open.return_value = mock_stream

        # has_length will raise type error because
        # MagicMock is not a file
        with self.assertRaises(TypeError):
            self.chain.sync(limit=3)
            self.assertTrue(mock_open.called)
            self.assertTrue(mock_stream.seek.called)

    def test_parse_100_blocks_from_zero_height(self):
        self.chain.handle_stream_block = MagicMock()
        self.chain.db.put_transactions = MagicMock()
        self.chain._update_progress = MagicMock()

        parsed = self.chain._sync_stream(highest_known=None, client_height=1000, limit=100)

        self.assertEqual(parsed, 100)
        self.assertEqual(self.chain.handle_stream_block.call_count, 100)
        self.assertEqual(self.chain.db.put_transactions.call_count, 100)
        self.assertEqual(self.chain._update_progress.call_count, 100)
Ejemplo n.º 6
0
class InitialSyncTestCase(unittest.TestCase):
    """
    Sync is ran for the first time, should be read from the stream (.dat files).
    """
    def setUp(self):
        self.fake_mongo = MagicMock()
        self.fake_mongo.blocks.find_one.return_value = None

        self.fake_rpc = MagicMock()
        self.fake_rpc.getblockcount = MagicMock(return_value=10000)

        self.db = DatabaseGateway(database=self.fake_mongo, cache_size=1000)
        self.db.highest_block = PropertyMock(return_value=None)

        self.chain = ExploderSyncer(
            database=self.db,
            blocks_dir="/home/vagrant/.gamecredits/blocks/",
            rpc_client=self.fake_rpc
        )

    def test_database_initialized_correctly(self):
        # caches are initialized and empty
        self.assertEqual(self.db.block_cache, {})
        self.assertEqual(self.db.tr_cache, [])
        self.assertEqual(self.db.vin_cache, [])
        self.assertEqual(self.db.vout_cache, [])

        # Indexes are created
        self.assertTrue(self.db.blocks.create_index.called)

    def test_blockchain_initialized_correctly(self):
        # First iter is true
        self.assertTrue(self.chain.first_iter)

        # chain peak is None
        self.assertIsNone(self.chain.chain_peak)

        # blockfiles are found
        self.assertGreater(len(self.chain.blk_files), 0)

    def test_no_blk_files_skipped(self):
        """
        No block files should be skipped on initial sync
        """
        files_before = self.chain.blk_files
        self.chain.sync(limit=3)
        self.assertEqual(files_before, self.chain.blk_files)

    @patch('interactors.open')
    def test_stream_seek_not_called(self, mock_open):
        """
        No seek should be called on initial sync
        """
        mock_stream = MagicMock()
        mock_open.return_value = mock_stream

        # has_length will raise type error because
        # MagicMock is not a file
        with self.assertRaises(TypeError):
            self.chain.sync(limit=3)
            self.assertTrue(mock_open.called)
            self.assertFalse(mock_stream.seek.called)

    def test_rpc_sync_not_called(self):
        self.chain._sync_rpc = MagicMock()
        self.assertFalse(self.chain._sync_rpc.called)