def setUpClass(cls):

        Blockchain.DeregisterBlockchain()

        super(BlockchainFixtureTestCase, cls).setUpClass()

        if not os.path.exists(cls.FIXTURE_FILENAME):
            logzero.logger.info(
                "downloading fixture block database from %s. this may take a while"
                % cls.FIXTURE_REMOTE_LOC)

            response = requests.get(cls.FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            os.makedirs(os.path.dirname(cls.FIXTURE_FILENAME), exist_ok=True)
            with open(cls.FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.FIXTURE_FILENAME)
            tar.extractall(path=settings.DATA_DIR_PATH)
            tar.close()
        except Exception as e:
            raise Exception(
                "Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this."
                % (e, cls.FIXTURE_FILENAME))

        if not os.path.exists(cls.leveldb_testpath()):
            raise Exception("Error downloading fixtures at %s" %
                            cls.leveldb_testpath())

        cls._blockchain = TestLevelDBBlockchain(path=cls.leveldb_testpath(),
                                                skip_version_check=True)
        Blockchain.RegisterBlockchain(cls._blockchain)
    def setUpClass(cls):

        Blockchain.DeregisterBlockchain()

        super(BlockchainFixtureTestCase, cls).setUpClass()

        if not os.path.exists(cls.FIXTURE_FILENAME):

            print("downloading fixture block database. this may take a while")

            response = requests.get(cls.FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            with open(cls.FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.FIXTURE_FILENAME)
            tar.extractall()
            tar.close()
        except Exception as e:
            raise Exception("Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this." % (e, cls.FIXTURE_FILENAME))

        if not os.path.exists(cls.leveldb_testpath()):
            raise Exception("Error downloading fixtures")

        cls._blockchain = TestLevelDBBlockchain(path=cls.leveldb_testpath())
        Blockchain.RegisterBlockchain(cls._blockchain)
示例#3
0
 def setUpClass(cls):
     settings.setup_unittest_net()
     Blockchain.DeregisterBlockchain()
     cls._blockchain = LevelDBBlockchain(path=cls.LEVELDB_TESTPATH,
                                         skip_version_check=True)
     Blockchain.RegisterBlockchain(cls._blockchain)
     cls._genesis = Blockchain.GenesisBlock()
示例#4
0
 def Dispose(self):
     Blockchain.DeregisterBlockchain()
     self._header_index = []
     self._db.close()
     closed = self._db.closed
     self._db = None
     return closed
示例#5
0
    def setUpClass(cls):

        Blockchain.DeregisterBlockchain()

        os.makedirs(cls.LEVELDB_TESTPATH, exist_ok=True)
        cls._blockchain = Blockchain(getBlockchainDB(cls.LEVELDB_TESTPATH), skip_version_check=True)
        Blockchain.RegisterBlockchain(cls._blockchain)
示例#6
0
    def setUpClass(self):

        Blockchain.DeregisterBlockchain()

        os.makedirs(self.LEVELDB_TESTPATH, exist_ok=True)
        self._blockchain = LevelDBBlockchain(path=self.LEVELDB_TESTPATH,
                                             skip_version_check=True)
        Blockchain.RegisterBlockchain(self._blockchain)
    def setUpClass(cls):

        Blockchain.DeregisterBlockchain()

        super(BlockchainFixtureTestCase, cls).setUpClass()

        # for some reason during testing asyncio.get_event_loop() fails and does not create a new one if needed. This is the workaround
        try:
            loop = asyncio.get_running_loop()
        except RuntimeError:
            loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        nodemgr = NodeManager()
        nodemgr.reset_for_test()

        # setup Blockchain DB
        if not os.path.exists(cls.FIXTURE_FILENAME):
            logger.info(
                "downloading fixture block database from %s. this may take a while"
                % cls.FIXTURE_REMOTE_LOC)

            response = requests.get(cls.FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            os.makedirs(os.path.dirname(cls.FIXTURE_FILENAME), exist_ok=True)
            with open(cls.FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.FIXTURE_FILENAME)
            tar.extractall(path=settings.DATA_DIR_PATH)
            tar.close()
        except Exception as e:
            raise Exception(
                "Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this."
                % (e, cls.FIXTURE_FILENAME))

        if not os.path.exists(cls.leveldb_testpath()):
            raise Exception("Error downloading fixtures at %s" %
                            cls.leveldb_testpath())

        settings.setup_unittest_net()

        cls._blockchain = Blockchain(
            getBlockchainDB(path=cls.leveldb_testpath()),
            skip_version_check=True)

        cls._blockchain.UT = True
        Blockchain.RegisterBlockchain(cls._blockchain)

        # setup Notification DB
        if not os.path.exists(cls.N_FIXTURE_FILENAME):
            logger.info(
                "downloading fixture notification database from %s. this may take a while"
                % cls.N_FIXTURE_REMOTE_LOC)

            response = requests.get(cls.N_FIXTURE_REMOTE_LOC, stream=True)

            response.raise_for_status()
            with open(cls.N_FIXTURE_FILENAME, 'wb+') as handle:
                for block in response.iter_content(1024):
                    handle.write(block)

        try:
            tar = tarfile.open(cls.N_FIXTURE_FILENAME)
            tar.extractall(path=settings.DATA_DIR_PATH)
            tar.close()

        except Exception as e:
            raise Exception(
                "Could not extract tar file - %s. You may want need to remove the fixtures file %s manually to fix this."
                % (e, cls.N_FIXTURE_FILENAME))
        if not os.path.exists(cls.N_NOTIFICATION_DB_NAME):
            raise Exception("Error downloading fixtures at %s" %
                            cls.N_NOTIFICATION_DB_NAME)

        settings.NOTIFICATION_DB_PATH = cls.N_NOTIFICATION_DB_NAME
        ndb = NotificationDB.instance()
        ndb.start()
示例#8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-m",
                        "--mainnet",
                        action="store_true",
                        default=False,
                        help="use MainNet instead of the default TestNet")
    parser.add_argument("-c",
                        "--config",
                        action="store",
                        help="Use a specific config file")

    # Where to store stuff
    parser.add_argument("--datadir",
                        action="store",
                        help="Absolute path to use for database directories")

    parser.add_argument("-o", "--output", help="Where to save output file")

    parser.add_argument("-t",
                        "--totalblocks",
                        help="Total blocks to export",
                        type=int)

    args = parser.parse_args()

    if args.mainnet and args.config:
        print(
            "Cannot use both --config and --mainnet parameters, please use only one."
        )
        exit(1)

    # Setting the datadir must come before setting the network, else the wrong path is checked at net setup.
    if args.datadir:
        settings.set_data_dir(args.datadir)

    # Setup depending on command line arguments. By default, the testnet settings are already loaded.
    if args.config:
        settings.setup(args.config)
    elif args.mainnet:
        settings.setup_mainnet()

    if not args.output:
        raise Exception("Please specify an output path")

    file_path = args.output

    # Instantiate the blockchain and subscribe to notifications
    blockchain = Blockchain(getBlockchainDB(settings.chain_leveldb_path))
    Blockchain.DeregisterBlockchain()
    Blockchain.RegisterBlockchain(blockchain)

    chain = Blockchain.Default()

    with open(file_path, 'wb') as file_out:

        total = Blockchain.Default().Height - 1

        if args.totalblocks:
            total = args.totalblocks

        total_block_output = total.to_bytes(4, 'little')

        print("Using network %s " % settings.net_name)
        print("Will export %s blocks to %s " % (total, file_path))

        file_out.write(total_block_output)

        for index in trange(total, desc='Exporting blocks:', unit=' Block'):

            block = chain.GetBlockByHeight(index)
            block.LoadTransactions()

            # make sure this block has transactions
            # otherwise we will have a bad import
            if len(block.Transactions) < 1:
                raise Exception("Block %s has no transactions %s " %
                                block.Index)

            output = Helper.ToStream(block)

            output_length = len(output).to_bytes(4, 'little')
            file_out.write(output_length)
            file_out.write(output)

    print("Exported %s blocks to %s " % (total, file_path))