コード例 #1
0
    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               config=self.config)
コード例 #2
0
    def run(self, **kwargs):
        client = MongoClient()
        database = MongoDatabaseGateway(client.exploder, config)
        rpc_client = AuthServiceProxy("http://%s:%[email protected]:8332"
                                      % (config.get('syncer', 'rpc_user'), config.get('syncer', 'rpc_password')))

        analizer = BlockchainAnalyzer(database, rpc_client, config)
        # Save hash_rate
        hash_rate = analizer.get_network_hash_rate()
        timestamp = time.time()
        analizer.save_network_hash_rate(hash_rate, timestamp)
        # Save network stats
        supply = analizer.get_supply()
        size = analizer.get_blockchain_size()
        analizer.save_network_stats(supply, size)

        # Update client info
        url = config.get('syncer', 'ipify_url')
        client_ip = get_client_ip(url)
        version = analizer.get_client_version()
        peer_info = analizer.get_peer_info()
        updated_peer_info = analizer.update_peer_location(peer_info)
        analizer.save_client_info(version, client_ip, updated_peer_info)

        bootstrap_dir = config.get('syncer', 'bootstrap_dir')
        generate_bootstrap(
            config.get('syncer', 'datadir_path'),
            bootstrap_dir
        )
コード例 #3
0
    def run(self, **kwargs):
        client = MongoClient()
        database = MongoDatabaseGateway(client.exploder, config)
        blockchain = Blockchain(database, config)
        rpc_client = AuthServiceProxy("http://%s:%[email protected]:8332"
                                      % (config.get('syncer', 'rpc_user'), config.get('syncer', 'rpc_password')))
        syncer = BlockchainSyncer(database, blockchain, rpc_client, config)

        # This is where the real work is done
        syncer.sync_auto()
コード例 #4
0
    def run(self, **kwargs):
        client = MongoClient()
        database = MongoDatabaseGateway(client.exploder, config)
        coinmarketcap_analyzer = CoinmarketcapAnalyzer(database, config)

        # Information from CoinMarketCap
        coinmarketcap_info = coinmarketcap_analyzer.get_coinmarketcap_game_info()

        # Our time that we are inserting our price, this has to be int because of the time calculations
        price_timestamp = int(time.time())

        # Method that saves price info and timestamp
        coinmarketcap_analyzer.save_price_history(
            coinmarketcap_info['price_usd'],
            coinmarketcap_info['price_btc'],
            coinmarketcap_info['market_cap_usd'],
            price_timestamp
        )

        # Gets GAME old price, 24h ago
        old_price = coinmarketcap_analyzer.get_old_btc_price(price_timestamp)
        # If we have information about the price 24h ago
        if old_price:
            try:
                old_price = Decimal(max(old_price))
                # Round old_price number by 8 decimals
                old_price = round(Decimal(old_price), 8)

                new_price = Decimal(coinmarketcap_info['price_btc'])
                # Round new_price number by 8 decimals
                new_price = round(Decimal(new_price), 8)
                
                percent_change_24h_btc = coinmarketcap_analyzer.btc_price_difference_percentage(old_price, new_price)

                database.update_price_stats(
                    float(coinmarketcap_info['price_usd']),
                    float(coinmarketcap_info['price_btc']),
                    float(coinmarketcap_info['percent_change_24h_usd']),
                    float(percent_change_24h_btc),
                    float(coinmarketcap_info['24h_volume_usd'])
                )
            except InvalidOperation as e:
                logging.error('FIVE_MINUTE_TASK_FAILED ERROR: %s' %e)
コード例 #5
0
    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        for tr in self.transactions:
            self.db.vin.insert_many(
                [VinSerializer.to_database(vin, tr.txid) for vin in tr.vin])
            list_of_lists = [
                VoutSerializer.to_database(vout, tr.txid, index)
                for (index, vout) in enumerate(tr.vout)
            ]
            self.db.vout.insert_many(
                [item for sublist in list_of_lists for item in sublist])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               cache=True,
                                               cache_size=5)
コード例 #6
0
    def run(self, **kwargs):
        client = MongoClient()
        database = MongoDatabaseGateway(client.exploder, config)
        rpc_client = AuthServiceProxy("http://%s:%[email protected]:8332"
                                      % (config.get('syncer', 'rpc_user'), config.get('syncer', 'rpc_password')))

        analizer = BlockchainAnalyzer(database, rpc_client, config)
        # Calculate and save sync progress
        progress = analizer.calculate_sync_progress()
        analizer.update_sync_progress(progress)
        # Save GameCredits usd price
        price = analizer.get_game_price()
        analizer.save_game_price(price)
コード例 #7
0
class MongoDbGatewayTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.client = get_mongo_connection()
        cls.db = cls.client.test_database
        blocks = generate_test_data(50)
        transactions = []
        for block in blocks:
            transactions += block.tx

        cls.blocks = blocks[:45]
        cls.blocks_to_insert = blocks[45:]
        cls.transactions = transactions[:45]
        cls.transactions_to_insert = transactions[45:]

    @classmethod
    def tearDownClass(cls):
        cls.client.drop_database('test_database')

    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        for tr in self.transactions:
            self.db.vin.insert_many(
                [VinSerializer.to_database(vin, tr.txid) for vin in tr.vin])
            list_of_lists = [
                VoutSerializer.to_database(vout, tr.txid, index)
                for (index, vout) in enumerate(tr.vout)
            ]
            self.db.vout.insert_many(
                [item for sublist in list_of_lists for item in sublist])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               cache=True,
                                               cache_size=5)

    def tearDown(self):
        self.db.blocks.drop()
        self.db.transactions.drop()
        self.db.vin.drop()
        self.db.vout.drop()

    def test_get_highest_block(self):
        highest_block = max(self.blocks, key=lambda block: block.height)
        self.assertEqual(highest_block, self.db_gateway.get_highest_block())

    def test_get_block_by_hash(self):
        some_block = self.blocks[13]
        fetched_block = self.db_gateway.get_block_by_hash(some_block.hash)
        self.assertEqual(some_block, fetched_block)

    def test_get_block_by_height(self):
        some_block = self.blocks[14]
        fetched_block = self.db_gateway.get_block_by_height(some_block.height)
        self.assertEqual(some_block, fetched_block)

    def test_get_latest_blocks_without_offset(self):
        blocks = sorted(self.blocks,
                        key=lambda block: block.height,
                        reverse=True)[:5]
        fetched_blocks = self.db_gateway.get_latest_blocks(limit=5, offset=0)

        self.assertEqual(len(blocks), len(fetched_blocks))

        for i in range(len(fetched_blocks)):
            self.assertEqual(blocks[i], fetched_blocks[i])

    def test_get_latest_blocks_with_offset(self):
        offset = 5
        blocks = sorted(self.blocks,
                        key=lambda block: block.height,
                        reverse=True)[offset:offset + 5]
        fetched_blocks = self.db_gateway.get_latest_blocks(limit=5, offset=5)

        self.assertEqual(len(blocks), len(fetched_blocks))

        for i in range(len(fetched_blocks)):
            self.assertEqual(blocks[i], fetched_blocks[i])

    def test_put_block(self):
        block = self.blocks_to_insert[0]

        # Check that this block doesn't exist in the DB
        with self.assertRaises(KeyError):
            self.db_gateway.get_block_by_hash(block.hash)

        # Insert it and then check that it's there
        self.db_gateway.put_block(block)
        self.assertEqual(self.db_gateway.get_block_by_hash(block.hash), block)

    def test_delete_block(self):
        block = self.blocks[0]

        # Check that this block exists in the DB
        self.assertEqual(block, self.db_gateway.get_block_by_hash(block.hash))

        # Delete it and then check that it isn't there any more
        self.db_gateway.delete_block(block.hash)

        with self.assertRaises(KeyError):
            self.db_gateway.get_block_by_hash(block.hash)

    def test_update_block(self):
        block = self.blocks[0]

        # Check that this block exists in the DB
        self.assertEqual(block, self.db_gateway.get_block_by_hash(block.hash))

        # Delete it and then check that it isn't there any more
        self.db_gateway.update_block(block.hash, {"height": 666})

        fetched_block = self.db_gateway.get_block_by_hash(block.hash)
        self.assertEqual(fetched_block.height, 666)

    def test_get_transaction_by_txid(self):
        transaction = self.transactions[0]

        self.assertEqual(
            transaction,
            self.db_gateway.get_transaction_by_txid(transaction.txid))

    def test_get_transactions_by_blockhash(self):
        blockhash = self.transactions[0].blockhash
        transactions = [
            tr for tr in self.transactions if tr.blockhash == blockhash
        ]

        fetched_transactions = self.db_gateway.get_transactions_by_blockhash(
            blockhash)

        self.assertEqual(len(fetched_transactions), len(transactions))

        for i in range(len(transactions)):
            self.assertEqual(transactions[i], fetched_transactions[i])

    def test_get_transactions_by_address(self):
        address = self.transactions[0].vout[0].addresses[0]
        fetched = self.db_gateway.get_transactions_by_address(address)
        self.assertGreater(len(fetched), 0)

    def test_put_transaction(self):
        transaction = self.transactions_to_insert[0]

        # Check that this block doesn't exist in the DB
        with self.assertRaises(KeyError):
            self.db_gateway.get_transaction_by_txid(transaction.txid)

        # Insert it and then check that it's there
        self.db_gateway.put_transaction(transaction)
        self.assertEqual(
            self.db_gateway.get_transaction_by_txid(transaction.txid),
            transaction)

    def test_get_vouts_by_address(self):
        address = self.transactions[5].vout[0].addresses[0]
        self.assertGreater(len(self.db_gateway.get_vouts_by_address(address)),
                           0)

    def test_get_vin_by_vout(self):
        vout = self.transactions[5].vout[0]
        vout.index = 0
        vout.txid = self.transactions[5].txid
        fake_vin = Vin(txid=self.transactions[7].txid,
                       prev_txid=vout.txid,
                       vout_index=vout.index,
                       hex="xexexeex",
                       sequence=1)
        # First we create a vin that references an existing vout
        self.db_gateway.put_vin(fake_vin, fake_vin.txid)

        self.assertEqual(fake_vin, self.db_gateway.get_vin_by_vout(vout))
コード例 #8
0
class MongoDbGatewayTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        CONFIG_FILE = os.environ['EXPLODER_CONFIG']
        cls.config = ConfigParser.RawConfigParser()
        cls.config.read(CONFIG_FILE)

        cls.client = get_mongo_connection()
        cls.db = cls.client.test_database
        blocks = generate_test_data(50, cls.config)

        for block in blocks:
            block.chain = cls.config.get('syncer', 'main_chain')
        transactions = []
        for block in blocks:
            transactions += block.tx

        cls.blocks = blocks[:45]
        cls.blocks_to_insert = blocks[45:]
        cls.transactions = transactions[:45]
        cls.transactions_to_insert = transactions[45:]

    @classmethod
    def tearDownClass(cls):
        cls.client.drop_database('test_database')

    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               config=self.config)

    def tearDown(self):
        self.db.blocks.drop()
        self.db.transactions.drop()

    def test_get_highest_block(self):
        highest_block = max(self.blocks, key=lambda block: block.height)
        self.assertEqual(highest_block, self.db_gateway.get_highest_block())

    def test_get_block_by_hash(self):
        some_block = self.blocks[13]
        fetched_block = self.db_gateway.get_block_by_hash(some_block.hash)
        self.assertEqual(some_block, fetched_block)

    def test_get_block_by_height(self):
        some_block = self.blocks[14]
        fetched_block = self.db_gateway.get_block_by_height(some_block.height)
        self.assertEqual(some_block, fetched_block)

    def test_put_block(self):
        block = self.blocks_to_insert[0]

        # Check that this block doesn't exist in the DB
        with self.assertRaises(KeyError):
            self.db_gateway.get_block_by_hash(block.hash)

        # Insert it and then check that it's there
        self.db_gateway.put_block(block)
        self.assertEqual(self.db_gateway.get_block_by_hash(block.hash), block)

    def test_delete_block(self):
        block = self.blocks[0]

        # Check that this block exists in the DB
        self.assertEqual(block, self.db_gateway.get_block_by_hash(block.hash))

        # Delete it and then check that it isn't there any more
        self.db_gateway.delete_block(block.hash)

        with self.assertRaises(KeyError):
            self.db_gateway.get_block_by_hash(block.hash)

    def test_update_block(self):
        block = self.blocks[0]

        # Check that this block exists in the DB
        self.assertEqual(block, self.db_gateway.get_block_by_hash(block.hash))

        # Delete it and then check that it isn't there any more
        self.db_gateway.update_block(block.hash, {"height": 666})

        fetched_block = self.db_gateway.get_block_by_hash(block.hash)
        self.assertEqual(fetched_block.height, 666)

    def test_get_transaction_by_txid(self):
        transaction = self.transactions[0]

        self.assertEqual(
            transaction,
            self.db_gateway.get_transaction_by_txid(transaction.txid))

    def test_get_transactions_by_blockhash(self):
        blockhash = self.transactions[0].blockhash
        transactions = [
            tr for tr in self.transactions if tr.blockhash == blockhash
        ]

        fetched_transactions = self.db_gateway.get_transactions_by_blockhash(
            blockhash)

        self.assertEqual(len(fetched_transactions), len(transactions))

        for i in range(len(transactions)):
            self.assertEqual(transactions[i], fetched_transactions[i])

    def test_get_transactions_by_address(self):
        address = self.transactions[0].vout[0].addresses[0]
        fetched = self.db_gateway.get_transactions_by_address(address)
        self.assertGreater(len(fetched), 0)

    def test_put_transaction(self):
        transaction = self.transactions_to_insert[0]

        # Check that this block doesn't exist in the DB
        with self.assertRaises(KeyError):
            self.db_gateway.get_transaction_by_txid(transaction.txid)

        # Insert it and then check that it's there
        self.db_gateway.put_transaction(transaction)
        self.assertEqual(
            self.db_gateway.get_transaction_by_txid(transaction.txid),
            transaction)
コード例 #9
0
class InsertBlockTestCaseWithTestData(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.client = get_mongo_connection()
        cls.db = cls.client.test_database
        blocks = generate_test_data(50)
        transactions = []
        for block in blocks:
            transactions += block.tx

        cls.blocks = blocks[:45]
        for block in cls.blocks:
            block.chain = MAIN_CHAIN

        cls.blocks_to_insert = blocks[45:]
        cls.transactions = transactions[:45]
        cls.transactions_to_insert = transactions[45:]

    @classmethod
    def tearDownClass(cls):
        cls.client.drop_database('test_database')

    def setUp(self):
        self.db.blocks.insert_many(
            [BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([
            TransactionSerializer.to_database(tr) for tr in self.transactions
        ])

        for tr in self.transactions:
            self.db.vin.insert_many(
                [VinSerializer.to_database(vin, tr.txid) for vin in tr.vin])
            list_of_lists = [
                VoutSerializer.to_database(vout, tr.txid, index)
                for (index, vout) in enumerate(tr.vout)
            ]
            self.db.vout.insert_many(
                [item for sublist in list_of_lists for item in sublist])

        self.db_gateway = MongoDatabaseGateway(database=self.db,
                                               cache=True,
                                               cache_size=5)

        self.chain = Blockchain(self.db_gateway)

    def tearDown(self):
        self.db.blocks.drop()
        self.db.transactions.drop()
        self.db.vin.drop()
        self.db.vout.drop()

    def test_fork_no_reconverge(self):
        to_add = self.blocks_to_insert[0]
        fork_point = self.blocks[10]
        to_add.previousblockhash = fork_point.hash

        added = self.chain.insert_block(to_add)

        self.assertTrue(added['fork'])
        self.assertFalse(added['reconverge'])

        self.assertEqual(added['block'].height, fork_point.height + 1)
        self.assertNotEqual(added['block'].chain, MAIN_CHAIN)

        to_add2 = self.blocks_to_insert[1]
        to_add2.previousblockhash = to_add.hash

        added2 = self.chain.insert_block(to_add2)

        self.assertTrue(added2['fork'])
        self.assertFalse(added2['reconverge'])

        self.assertEqual(added2['block'].height, added['block'].height + 1)
        self.assertEqual(added2['block'].chain, added['block'].chain)

    def test_fork_reconverge(self):
        to_add = self.blocks_to_insert[0]
        fork_point = self.blocks[10]
        to_add.previousblockhash = fork_point.hash
        # Force reconverge
        to_add.work = 10000000000

        some_block_on_main_chain = self.db_gateway.get_block_by_hash(
            self.blocks[11].hash)
        self.assertEqual(some_block_on_main_chain.chain, MAIN_CHAIN)
        added = self.chain.insert_block(to_add)

        self.assertTrue(added['fork'])
        self.assertTrue(added['reconverge'])

        some_block_on_main_chain = self.db_gateway.get_block_by_hash(
            self.blocks[11].hash)
        self.assertNotEqual(some_block_on_main_chain.chain, MAIN_CHAIN)
コード例 #10
0
class InsertBlockTestCaseWithTestData(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        CONFIG_FILE = os.environ['EXPLODER_CONFIG']
        cls.config = ConfigParser.RawConfigParser()
        cls.config.read(CONFIG_FILE)
        cls.config.set('syncer', 'unspent_tracking', "False")

        cls.client = get_mongo_connection()
        cls.db = cls.client.test_database
        blocks = generate_test_data(50, cls.config)
        transactions = []
        for block in blocks:
            transactions += block.tx

        cls.blocks = blocks[:45]
        for block in cls.blocks:
            block.chain = cls.config.get('syncer', 'main_chain')

        cls.blocks_to_insert = blocks[45:]
        cls.transactions = transactions[:45]
        cls.transactions_to_insert = transactions[45:]

    @classmethod
    def tearDownClass(cls):
        cls.client.drop_database('test_database')

    def setUp(self):
        self.db.blocks.insert_many([BlockSerializer.to_database(block) for block in self.blocks])
        self.db.transactions.insert_many([TransactionSerializer.to_database(tr) for tr in self.transactions])

        self.db_gateway = MongoDatabaseGateway(
            database=self.db,
            config=self.config
        )

        self.chain = Blockchain(self.db_gateway, self.config)

    def tearDown(self):
        self.db.blocks.drop()
        self.db.transactions.drop()
        self.db.vin.drop()
        self.db.vout.drop()

    def test_fork_no_reconverge(self):
        to_add = self.blocks_to_insert[0]
        fork_point = self.blocks[10]
        to_add.previousblockhash = fork_point.hash

        added = self.chain.insert_block(to_add)

        self.assertTrue(added['fork'])
        self.assertFalse(added['reconverge'])

        self.assertEqual(added['block'].height, fork_point.height + 1)
        self.assertNotEqual(added['block'].chain, self.config.get('syncer', 'main_chain'))

        to_add2 = self.blocks_to_insert[1]
        to_add2.previousblockhash = to_add.hash

        added2 = self.chain.insert_block(to_add2)

        self.assertTrue(added2['fork'])
        self.assertFalse(added2['reconverge'])

        self.assertEqual(added2['block'].height, added['block'].height + 1)
        self.assertEqual(added2['block'].chain, added['block'].chain)

    def test_fork_reconverge(self):
        to_add = self.blocks_to_insert[0]
        fork_point = self.blocks[10]
        to_add.previousblockhash = fork_point.hash
        # Force reconverge
        to_add.work = 10000000000
        some_block_on_main_chain = self.db_gateway.get_block_by_hash(self.blocks[11].hash)
        self.assertEqual(some_block_on_main_chain.chain, self.config.get('syncer', 'main_chain'))
        added = self.chain.insert_block(to_add)

        self.assertTrue(added['fork'])
        self.assertTrue(added['reconverge'])

        some_block_on_main_chain = self.db_gateway.get_block_by_hash(self.blocks[11].hash)
        self.assertNotEqual(some_block_on_main_chain.chain, self.config.get('syncer', 'main_chain'))