Scan the blockchain for PeerAssets decks, returns list of deck objects.
    : provider - provider instance
    : version - deck protocol version (0, 1, 2, ...)
    : test True/False - test or production P2TH
352897
def find_all_valid_decks(provider: Provider, deck_version: int,
                         prod: bool=True) -> Generator:
    '''
    
    '''

    pa_params = param_query(provider.network)

    if prod:
        p2th = pa_params.P2TH_addr
    else:
        p2th = pa_params.test_P2TH_addr

    if isinstance(provider, RpcNode):
        deck_spawns = (provider.getrawtransaction(i, 1)
                       for i in find_deck_spawns(provider))

    else:
        try:
            deck_spawns = (provider.getrawtransaction(i, 1) for i in
                           provider.listtransactions(p2th))
        except TypeError as err:  # it will except if no transactions are found on this P2TH
            raise EmptyP2THDirectory(err)

    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as th:
        for result in th.map(deck_parser, ((provider, rawtx, deck_version, p2th) for rawtx in deck_spawns)):
            if result:
                yield result
363047
def rpc_get_consensus_hashes( self, block_id_list, **con_info ):
        
        if type(block_id_list) != list:
            return {'error': 'Invalid block heights', 'http_status': 400}

        if len(block_id_list) > 32:
            return {'error': 'Too many block heights', 'http_status': 400}

        for bid in block_id_list:
            if not check_block(bid):
                return {'error': 'Invalid block height', 'http_status': 400}

        db = get_db_state(self.working_dir)
        ret = {}
        for block_id in block_id_list:
            ret[block_id] = db.get_consensus_at(block_id)

        db.close()

        return self.success_response( {'consensus_hashes': ret} )
1160278
def GET_blockchain_ops( self, path_info, blockchain_name, blockheight ):
        
        try:
            blockheight = int(blockheight)
            assert check_block(blockheight)
        except:
            return self._reply_json({'error': 'Invalid block'}, status_code=400)

        if blockchain_name != 'bitcoin':
            # not supported
            return self._reply_json({'error': 'Unsupported blockchain'}, status_code=404)
        
        blockstackd_url = get_blockstackd_url()
        nameops = blockstackd_client.get_blockstack_transactions_at(int(blockheight), hostport=blockstackd_url)
        if json_is_error(nameops):
            # error
            status_code = nameops.get('http_status', 502)
            return self._reply_json({'error': nameops['error']}, status_code=status_code)

        self._reply_json(nameops)
        return
1327409
def runListPeers(self, request):
        
        return self.runSearchRequest(
            request,
            protocol.ListPeersRequest,
            protocol.ListPeersResponse,
            self.peersGenerator)
1223890
def verify_docker_image_sha(chain, link):
    
    cot = link.cot
    task = link.task
    errors = []

    if isinstance(task['payload'].get('image'), dict):
        # Using pre-built image from docker-image task
        docker_image_task_id = task['extra']['chainOfTrust']['inputs']['docker-image']
        log.debug("Verifying {} {} against docker-image {}".format(
            link.name, link.task_id, docker_image_task_id
        ))
        if docker_image_task_id != task['payload']['image']['taskId']:
            errors.append("{} {} docker-image taskId isn't consistent!: {} vs {}".format(
                link.name, link.task_id, docker_image_task_id,
                task['payload']['image']['taskId']
            ))
        else:
            path = task['payload']['image']['path']
            # we need change the hash alg everywhere if we change, and recreate
            # the docker images...
            image_hash = cot['environment']['imageArtifactHash']
            alg, sha = image_hash.split(':')
            docker_image_link = chain.get_link(docker_image_task_id)
            upstream_sha = docker_image_link.cot['artifacts'].get(path, {}).get(alg)
            if upstream_sha is None:
                errors.append("{} {} docker-image docker sha {} is missing! {}".format(
                    link.name, link.task_id, alg,
                    docker_image_link.cot['artifacts'][path]
                ))
            elif upstream_sha != sha:
                errors.append("{} {} docker-image docker sha doesn't match! {} {} vs {}".format(
                    link.name, link.task_id, alg, sha, upstream_sha
                ))
            else:
                log.debug("Found matching docker-image sha {}".format(upstream_sha))
    else:
        prebuilt_task_types = chain.context.config['prebuilt_docker_image_task_types']
        if prebuilt_task_types != "any" and link.task_type not in prebuilt_task_types:
            errors.append(
                "Task type {} not allowed to use a prebuilt docker image!".format(
                    link.task_type
                )
            )
    raise_on_errors(errors)
250178
def list_assets(self):
        
        response = self.requests_session.get(self._base_url).content
        if not response:
            return {}

        try:
            asset_list = json.loads(response)
        except TypeError:
            asset_list = None
        except ValueError:
            raise ValueError(response.decode('UTF-8'))

        if not asset_list:
            return []

        if 'ids' in asset_list:
            return asset_list['ids']
        return []
1282877
function formatDeckAsShortCards(deck) {
    var newDeck = {
        _id: deck._id,
        name: deck.name,
        username: deck.username,
        lastUpdated: deck.lastUpdated,
        faction: { name: deck.faction.name, value: deck.faction.value }
    };

    if (deck.agenda) {
        newDeck.agenda = { code: deck.agenda.code };
    }

    newDeck.bannerCards = (deck.bannerCards || []).map(function (card) {
        return { code: card.code };
    });
    newDeck.drawCards = formatCards(deck.drawCards || []);
    newDeck.plotCards = formatCards(deck.plotCards || []);
    newDeck.rookeryCards = formatCards(deck.rookeryCards || []);

    return newDeck;
}
497151
async def fetch_peers(self, request):
        

        response = await self._query_validator(
            Message.CLIENT_PEERS_GET_REQUEST,
            client_peers_pb2.ClientPeersGetResponse,
            client_peers_pb2.ClientPeersGetRequest())

        return self._wrap_response(
            request,
            data=response['peers'],
            metadata=self._get_metadata(request, response))
453686
def snv_get_blockstack_ops_at(current_block_id, current_consensus_hash, block_id, consensus_hash, proxy=None):
    
    import blockstack

    log.debug('verify {}-{} to {}-{}'.format(
        current_block_id, current_consensus_hash, block_id, consensus_hash
    ))

    proxy = get_default_proxy() if proxy is None else proxy

    # work backwards in time, using a Merkle skip-list constructed
    # by blockstackd over the set of consensus hashes.
    next_block_id = current_block_id

    prev_blockstack_ops_hashes = {}
    prev_consensus_hashes = {
        next_block_id: current_consensus_hash
    }

    # print 'next_block_id = {}, block_id = {}'.format(next_block_id, block_id)
    while next_block_id >= block_id:
        # get blockstack_ops_at[ next_block_id ], and all consensus_hash[ next_block_id - 2^i ]
        # such that block_id - 2*i > block_id (start at i = 1)
        i = 0
        blockstack_ops_hash = None

        if next_block_id in prev_blockstack_ops_hashes:
            blockstack_ops_hash = prev_blockstack_ops_hashes[next_block_id]
        else:
            blockstack_ops_resp = get_blockstack_ops_hash_at(next_block_id, proxy=proxy)

            if 'error' in blockstack_ops_resp:
                log.error('get_blockstack_ops_hash_at: {}'.format(blockstack_ops_resp['error']))
                return {'error': 'Failed to get nameops: {}'.format(blockstack_ops_resp['error'])}

            blockstack_ops_hash = str(blockstack_ops_resp)
            prev_blockstack_ops_hashes[next_block_id] = blockstack_ops_hash

        log.debug('nameops hash at {}: {}'.format(next_block_id, blockstack_ops_hash))

        # find out which consensus hashes we'll need
        to_fetch = []
        ch_block_ids = []
        while next_block_id - (2 ** (i + 1) - 1) >= FIRST_BLOCK_MAINNET:
            i += 1
            prev_block_id = next_block_id - (2 ** i - 1)
            ch_block_ids.append(prev_block_id)

            if prev_block_id not in prev_consensus_hashes:
                to_fetch.append(prev_block_id)

        # get the consensus hashes
        chs = {}
        if to_fetch:
            chs = get_consensus_hashes(to_fetch, proxy=proxy)
            if 'error' in chs:
                msg = 'Failed to get consensus hashes for {}: {}'
                log.error(msg.format(to_fetch, chs['error']))
                return {'error': 'Failed to get consensus hashes'}

        prev_consensus_block_ids = []
        for b in ch_block_ids:
            # NOTE: we process to_fetch *in decreasing order* so we know when we're missing data
            if b not in chs and b not in prev_consensus_hashes:
                msg = 'Missing consensus hash response for {} (chs={}, prev_chs={})'
                log.error(msg.format(b, chs, prev_consensus_hashes))
                return {'error': 'Server did not reply valid data'}

            prev_consensus_block_ids.append(b)
            if b in prev_consensus_hashes:
                # already got this one
                continue

            ch = chs[b]
            if ch is not None:
                prev_consensus_hashes[b] = str(ch)
            else:
                # no consensus hash for this block and all future blocks
                prev_consensus_block_ids.pop()
                break

        # prev_consensus_hashes_list = [ prev_consensus_hashes[b] for b in ch_block_ids ]
        prev_consensus_hashes_list = [
            prev_consensus_hashes[b] for b in prev_consensus_block_ids
        ]

        # calculate the snapshot, and see if it matches
        ch = virtualchain.StateEngine.make_snapshot_from_ops_hash(
            blockstack_ops_hash, prev_consensus_hashes_list
        )

        expected_ch = prev_consensus_hashes[next_block_id]
        if ch != expected_ch:
            msg = 'Consensus hash mismatch at {}: expected {}, got {} (from {}, {})'
            log.error(msg.format(next_block_id, expected_ch, ch, blockstack_ops_hash, prev_consensus_hashes))
            return {'error': 'Consensus hash mismatch'}

        # advance!
        # find the smallest known consensus hash whose block is greater than block_id
        current_candidate = next_block_id
        found_any = False
        for candidate_block_id in prev_consensus_hashes:
            if candidate_block_id < block_id:
                continue

            if candidate_block_id < current_candidate:
                current_candidate = candidate_block_id
                found_any = True

        if not found_any:
            break

        next_block_id = current_candidate

    # get the final nameops
    historic_nameops = get_blockstack_transactions_at(block_id, proxy=proxy)
    if isinstance(historic_nameops, dict) and 'error' in historic_nameops:
        log.error('Failed to get nameops at {}: {}'.format(block_id, historic_nameops['error']))
        return {'error': 'BUG: no nameops found'}

    log.debug("nameops at {}\n{}".format(block_id, json.dumps(historic_nameops, sort_keys=True)))

    # sanity check...
    for historic_op in historic_nameops:
        if 'opcode' not in historic_op:
            return {'error': 'Invalid/corrupt name operations detected'}

        # recover binary op string
        if 'op' not in historic_op:
            historic_op['op'] = NAME_OPCODES[str(historic_op['opcode'])]

    # check integrity
    opfields = blockstack.lib.virtualchain_hooks.get_opfields()
    serialized_historic_nameops = [
        virtualchain.StateEngine.serialize_op(
            str(op['op'][0]), op, opfields, verbose=True
        ) for op in historic_nameops
    ]

    historic_blockstack_ops_hash = virtualchain.StateEngine.make_ops_snapshot(serialized_historic_nameops)

    if block_id not in prev_blockstack_ops_hashes:
        return {'error': 'Previous block/consensus hash is unreachable from trusted block/consensus hash'}

    if historic_blockstack_ops_hash != prev_blockstack_ops_hashes[block_id]:
        log.error("historic nameops hash at {}: {}".format(block_id, historic_blockstack_ops_hash))
        log.error("prev_blockstack_ops_hashes:\n{}".format(json.dumps(prev_blockstack_ops_hashes, indent=4, sort_keys=True)))
        return {
            'error': 'Hash mismatch: failed to get operations at {}-{} from {}-{} ({} != {})'.format(
                block_id, consensus_hash, current_block_id, current_consensus_hash, historic_blockstack_ops_hash, prev_blockstack_ops_hashes[block_id]
            )
        }

    log.debug('{} nameops at {}'.format(len(historic_nameops), block_id))

    # strip history
    for hn in historic_nameops:
        if 'history' in hn.keys():
            del hn['history']

    return historic_nameops
649260
def deck_issue_mode(proto: DeckSpawnProto) -> Iterable[str]:
    ''''''

    if proto.issue_mode == 0:
        yield "NONE"
        return

    for mode, value in proto.MODE.items():
        if value > proto.issue_mode:
            continue
        if value & proto.issue_mode:
            yield mode
1098920
def atlas_zonefile_find_push_peers( zonefile_hash, peer_table=None, zonefile_bits=None, con=None, path=None ):
    

    if zonefile_bits is None:
        zonefile_bits = atlasdb_get_zonefile_bits( zonefile_hash, path=path, con=con )
        if len(zonefile_bits) == 0:
            # we don't even know about it
            return []

    push_peers = []
    
    with AtlasPeerTableLocked(peer_table) as ptbl:
        for peer_hostport in ptbl.keys():
            zonefile_inv = atlas_peer_get_zonefile_inventory( peer_hostport, peer_table=ptbl )
            res = atlas_inventory_test_zonefile_bits( zonefile_inv, zonefile_bits )
            if res:
                push_peers.append( peer_hostport )

    return push_peers
47432
def get_all_boards(*args, **kwargs):
    
    # Use https based on how the Board class instances are to be instantiated
    https = kwargs.get('https', args[1] if len(args) > 1 else False)

    # Dummy URL generator, only used to generate the board list which doesn't
    # require a valid board name
    url_generator = Url(None, https)
    _fetch_boards_metadata(url_generator)
    return get_boards(_metadata.keys(), *args, **kwargs)
669880
def versions(self):
        
        if not self.__versions:
            self.__versions = Versions(self.__connection)
        return self.__versions
1101113
def get_pb_ids(self) -> List[str]:
        
        values = DB.get_hash_value(self._key, 'processing_block_ids')
        return ast.literal_eval(values)
394440
def _load_version(cls, unpickler, version):
        
        obj = unpickler.load()
        return TransformerChain(obj._state["steps"])
1124364
def list_pkgs(versions_as_list=False, **kwargs):
    '''
    
    '''
    # not yet implemented or not applicable
    if any([salt.utils.data.is_true(kwargs.get(x))
        for x in ('removed', 'purge_desired')]):
        return {}

    if 'pkg.list_pkgs' in __context__:
        if versions_as_list:
            return __context__['pkg.list_pkgs']
        else:
            ret = copy.deepcopy(__context__['pkg.list_pkgs'])
            __salt__['pkg_resource.stringify'](ret)
            return ret

    ret = {}
    cmd = '/bin/pkg list -Hv'
    lines = __salt__['cmd.run_stdout'](cmd).splitlines()
    # column 1 is full FMRI name in form pkg://publisher/class/name@version
    for line in lines:
        name = _ips_get_pkgname(line)
        version = _ips_get_pkgversion(line)
        __salt__['pkg_resource.add_pkg'](ret, name, version)

    __salt__['pkg_resource.sort_pkglist'](ret)
    __context__['pkg.list_pkgs'] = copy.deepcopy(ret)
    if not versions_as_list:
        __salt__['pkg_resource.stringify'](ret)
    return ret
719143
def validate_deckspawn_p2th(provider: Provider, rawtx: dict, p2th: str) -> bool:
    '''
    '''

    try:
        vout = rawtx["vout"][0]["scriptPubKey"].get("addresses")[0]
    except TypeError:
        '''TypeError: 'NoneType' object is not subscriptable error on some of the deck spawns.'''
        raise InvalidDeckSpawn("Invalid Deck P2TH.")

    if not vout == p2th:
        raise InvalidDeckSpawn("InvalidDeck P2TH.")

    return True