'Create new firstbits records for block and addr_vers.  All
ancestor blocks must have their firstbits already recorded.'

def do_vers_firstbits(store, addr_vers, block_id):

    address_version = store.binout(addr_vers)
    pubkeys = {}
    full = {}
    for (pubkey_id, pubkey_hash, oblock_id) in store.selectall('\n                                                SELECT    DISTINCT\n                                                                            pubkey.pubkey_id,\n                                                                            pubkey.pubkey_hash,\n                                                                            fb.block_id\n                                                        FROM    block    b\n                                                        JOIN    block_tx    bt    ON    (b.block_id    =    bt.block_id)\n                                                        JOIN    txout    ON    (bt.tx_id    =    txout.tx_id)\n                                                        JOIN    pubkey    ON    (txout.pubkey_id    =    pubkey.pubkey_id)\n                                                        LEFT    JOIN    abe_firstbits    fb    ON    (\n                                                                                            fb.address_version    =    ?\n                                                                            AND    fb.pubkey_id    =    pubkey.pubkey_id)\n                                                    WHERE    b.block_id    =    ?', (addr_vers, block_id)):
        pubkey_id = int(pubkey_id)
        if ((oblock_id is not None) and store.is_descended_from(block_id, int(oblock_id))):
            full[pubkey_id] = None
        if (pubkey_id in full):
            continue
        full[pubkey_id] = store.firstbits_full(address_version, store.binout(pubkey_hash))
    for (pubkey_id, s) in full.iteritems():
        if (s is None):
            continue
        (longest, longest_id) = (0, None)
        substrs = [s[0:(i + 1)] for i in xrange(len(s))]
        for (ancestor_id, fblen, o_pubkey_id) in store.selectall((('\n                                                                SELECT    block_id,    LENGTH(firstbits),    pubkey_id\n                                                                        FROM    abe_firstbits    fb\n                                                                    WHERE    address_version    =    ?\n                                                                            AND    firstbits    IN    (?' + (',?' * (len(s) - 1))) + '\n                                                                                            )'), tuple(([addr_vers] + substrs))):
            if ((fblen > longest) and store.is_descended_from(block_id, int(ancestor_id))):
                (longest, longest_id) = (fblen, o_pubkey_id)
        if (longest_id is not None):
            (o_hash,) = store.selectrow('SELECT    pubkey_hash    FROM    pubkey    WHERE    pubkey_id    =    ?', (longest_id,))
            o_fb = store.firstbits_full(address_version, store.binout(o_hash))
            max_len = min(len(s), len(o_fb))
            while ((longest < max_len) and (s[longest] == o_fb[longest])):
                longest += 1
        if (longest == len(s)):
            store.cant_do_firstbits(addr_vers, block_id, pubkey_id)
            continue
        fb = s[0:(longest + 1)]
        ids = pubkeys.get(fb)
        if (ids is None):
            ids = set()
            pubkeys[fb] = ids
        ids.add(pubkey_id)
    count = 0
    for (fb, ids) in pubkeys.iteritems():
        count += store.do_firstbits(addr_vers, block_id, fb, ids, full)
    return count
