コード例 #1
0
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--full_shard_id",
                        type=int,
                        help="full shard id to operate")
    parser.add_argument(
        "--all_shards",
        action="store_true",
        default=False,
        help="query balances in all shards",
    )
    parser.add_argument("--recipient",
                        default=None,
                        type=str,
                        help="query a specific recipient")
    parser.add_argument(
        "--minor_block_height",
        default=None,
        type=int,
        help="query balance at specific minor block height",
    )
    ClusterConfig.attach_arguments(parser)
    args = parser.parse_args()

    env = DEFAULT_ENV.copy()
    env.cluster_config = ClusterConfig.create_from_args(args)

    # initialize database
    if not env.cluster_config.use_mem_db():
        env.db = PersistentDb(
            "{path}/master.db".format(path=env.cluster_config.DB_PATH_ROOT),
            clean=env.cluster_config.CLEAN,
        )

    return env, args
コード例 #2
0
    def __init_shard_db(self):
        """
        Create a PersistentDB or use the env.db if DB_PATH_ROOT is not specified in the ClusterConfig.
        """
        if self.env.cluster_config.use_mem_db():
            return InMemoryDb()

        db_path = "{path}/shard-{shard_id}.db".format(
            path=self.env.cluster_config.DB_PATH_ROOT, shard_id=self.shard_id)
        return PersistentDb(db_path, clean=self.env.cluster_config.CLEAN)
コード例 #3
0
def main():
    args = parse_args()
    config = ClusterConfig.create_from_args(args)
    db = RootDb(PersistentDb(args.db), config.QUARKCHAIN, 0)
    header = db.get_tip_header()
    if not header:
        raise RuntimeError("Not a valid RootDb")
    from_height = header.height if args.root_height <= 0 else args.root_height
    tip_header = None
    block = db.get_root_block_by_hash(header.get_hash(), False)
    shard_to_address_count = dict()  # shard -> (recipient -> count)
    while block.header.height > 0:
        if block.header.height > from_height:
            block = db.get_root_block_by_hash(block.header.hash_prev_block, False)
            continue
        if block.header.height == from_height:
            tip_header = block.header
        for minor_header in block.minor_block_header_list:
            shard = minor_header.branch.get_full_shard_id()
            address_hex = minor_header.coinbase_address.recipient.hex()
            address_to_count = shard_to_address_count.setdefault(shard, dict())
            current = address_to_count.setdefault(address_hex, 0)
            address_to_count[address_hex] = current + 1
        block = db.get_root_block_by_hash(block.header.hash_prev_block, False)

    algo_to_address_count = dict()  # algorithm -> (recipient -> count)
    for shard_id, address_to_count in shard_to_address_count.items():
        algo = shard_id_to_algorithm(shard_id)
        addr_to_count = algo_to_address_count.setdefault(algo, dict())
        for address, count in address_to_count.items():
            current = addr_to_count.setdefault(address, 0)
            addr_to_count[address] = current + count

    print(
        "Counting shard blocks from root block {} {}".format(
            tip_header.height, tip_header.get_hash().hex()
        )
    )

    for algo, address_count in algo_to_address_count.items():
        total = sum(address_count.values())

        print()
        print("{} has {} blocks".format(algo, total))
        sorted_by_count = sorted(
            address_count.items(), key=operator.itemgetter(1), reverse=True
        )
        for address, count in sorted_by_count:
            print("{} {} {:.2f}%".format(address, count, count / total * 100))
コード例 #4
0
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("action", type=str, help="action to take")
    parser.add_argument("--height", type=int, help="block height to operate")
    parser.add_argument("--hash", type=str, help="block hash to operate")
    ClusterConfig.attach_arguments(parser)
    args = parser.parse_args()

    env = DEFAULT_ENV.copy()
    env.cluster_config = ClusterConfig.create_from_args(args)

    # initialize database
    if not env.cluster_config.use_mem_db():
        env.db = PersistentDb(
            "{path}/master.db".format(path=env.cluster_config.DB_PATH_ROOT),
            clean=env.cluster_config.CLEAN,
        )

    return env, args
コード例 #5
0
            self.root_node,
            bin_to_nibbles(key),
            value)
        self._update_root_hash()

    def root_hash_valid(self):
        if self.root_hash == BLANK_ROOT:
            return True
        return self.root_hash in self.db


if __name__ == "__main__":
    import sys
    from quarkchain.db import PersistentDb

    _db = PersistentDb(path=sys.argv[2])

    def encode_node(nd):
        if isinstance(nd, bytes):
            return nd.hex()
        else:
            return rlp_encode(nd).hex()

    if len(sys.argv) >= 2:
        if sys.argv[1] == 'insert':
            t = Trie(_db, bytes.fromhex(sys.argv[3]))
            t.update(bytes(sys.argv[4], "ascii"), bytes(sys.argv[5], "ascii"))
            print(encode_node(t.root_hash))
        elif sys.argv[1] == 'get':
            t = Trie(_db, bytes.fromhex(sys.argv[3]))
            print(t.get(bytes(sys.argv[4], "ascii")))