コード例 #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()
コード例 #2
0
 async def watch_loop(self):
     while True:
         async with websockets.connect(self.ws_server) as ws:
             await ws.send(
                 json.dumps({
                     'jsonrpc': '2.0',
                     'method': 'subscribe',
                     'id': 'newBlock',
                     'params': {
                         'query': "tm.event='NewBlockHeader'"
                     }
                 }))
             await ws.recv()  # eat up subscription response
             while True:
                 try:
                     await ws.recv()
                     # NOTE: Response from subscription does not give the
                     # block_id of the newly added block. We need another
                     # rpc query to get the block_id. Instead, just call
                     # self.play().
                     self.refresh_remote()
                     self.db = dbproxy.connect_db()
                     self.play(0, self.verbose)
                     self.db.close()
                 except websockets.exceptions.ConnectionClosed:
                     break
コード例 #3
0
ファイル: builder.py プロジェクト: amolabs/explorer2
    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()
コード例 #4
0
    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()
コード例 #5
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.')