Exemple #1
0
    def __init__(self, node, db=None):
        if node is None:
            raise ArgError('no node is given.')
        self.node = node

        if db is None:
            db = dbproxy.connect_db()
        self.db = db
        cur = self.db.cursor()

        self.refresh_remote()

        lock = FileLock(f'/var/tmp/collector-{self.chain_id}.lock')
        try:
            lock.acquire(timeout=1)
        except Timeout:
            self.print_log('another instance is running. exiting.')
            sys.exit(-1)
        else:
            self.lock = lock

        # get current explorer state
        cur.execute(
            """SELECT `height` FROM `c_blocks`
            WHERE (`chain_id` = %(chain_id)s)
            ORDER BY `height` DESC LIMIT 1""", self._vars())
        row = cur.fetchone()
        if row:
            b = dict(zip(cur.column_names, row))
            self.height = int(b['height'])
        else:
            self.height = 0
        cur.close()
Exemple #2
0
    def __init__(self, chain_id,
                 db: Union[MySQLConnection, CMySQLConnection] = None):
        # read config
        config_dir = os.path.dirname(os.path.abspath(__file__)) + '/..'
        configfile = config_dir + '/config.json'
        config = None
        try:
            f = open(configfile, "r")
            config = json.load(f)
        except OSError as e:
            print(f'Unable to read config({configfile}):', e)
            config = {}
        else:
            f.close()
        yappers = config.get('yappers')
        if yappers is not None:
            tx.set_yappers(yappers)
            print('yappers are set to:')
            for y in tx.yappers:
                print(f'  {y}')

        if chain_id is None:
            raise ArgError('no chain_id is given.')
        self.chain_id = chain_id

        if db is None:
            db = dbproxy.connect_db()
        self.db = db
        self.cursor = self.db.cursor()

        self.refresh_roof()

        lock = FileLock(f'/var/tmp/builder-{self.chain_id}.lock')
        try:
            lock.acquire(timeout=1)
        except Timeout:
            self.print_log('another instance is running. exiting.')
            sys.exit(-1)
        else:
            self.lock = lock

        cur = self.cursor
        # get current explorer state
        cur.execute(
            """
            SELECT * FROM `play_stat` WHERE (`chain_id` = %(chain_id)s)
            """, self._vars())
        row = cur.fetchone()
        if row:
            d = dict(zip(cur.column_names, row))
            self.height = d['height']
        else:
            self.height = 0
            cur.execute(
                """
                INSERT INTO `play_stat` (`chain_id`, `height`)
                VALUES (%(chain_id)s, %(height)s)
                """, self._vars())
            self.db.commit()
    def __init__(self, chain_id, db=None):
        if chain_id is None:
            raise ArgError('no chain_id is given.')
        self.chain_id = chain_id

        if db is None:
            db = dbproxy.connect_db()
        self.db = db
        self.cursor = self.db.cursor()

        self.refresh_roof()

        lock = FileLock(f'/var/tmp/builder-{self.chain_id}.lock')
        try:
            lock.acquire(timeout=1)
        except Timeout:
            self.print_log('another instance is running. exiting.')
            sys.exit(-1)
        else:
            self.lock = lock

        cur = self.cursor
        # get current explorer state
        cur.execute(
            """
            SELECT * FROM `play_stat` WHERE (`chain_id` = %(chain_id)s)
            """, self._vars())
        row = cur.fetchone()
        if row:
            d = dict(zip(cur.column_names, row))
            self.height = d['height']
        else:
            self.height = 0
            cur.execute(
                """
                INSERT INTO `play_stat` (`chain_id`, `height`)
                VALUES (%(chain_id)s, %(height)s)
                """, self._vars())
            self.db.commit()
Exemple #4
0
    def __init__(self, chain_id, targets, db=None, verbose=False, dry=False):
        self.chain_id = ""
        self.targets = []
        self.known = {}
        self.nodes = {}

        self.db = None
        self.lock = None
        self.verbose = verbose
        self.dry = dry

        self.loop = None
        self.futures = []

        if chain_id is not None:
            self.chain_id = chain_id

        if targets is not None:
            self.targets = targets

        if db is None and not self.dry:
            db = dbproxy.connect_db()
        self.db = db

        lock = FileLock(f'/var/tmp/nodes-{self.chain_id}.lock')
        try:
            lock.acquire(timeout=1)
        except Timeout:
            print('another instance is running. exiting.')
            sys.exit(-1)
        else:
            self.lock = lock

        if not self.dry:
            self.known = self._query_nodes()

        if len(self.known) == 0 and len(self.targets) == 0:
            raise ArgError('no targets are given.')