Пример #1
0
 def update_data(self):
     old_keys = set(self.db.keys())
     new_keys = set()
     for candidate in CurrentVote.get_candidates():
         try:
             alias = Address.select().where(Address.address == candidate.address).first().alias
         except AttributeError:
             alias = 'unknown'
         if candidate.start_block == 0 and candidate.end_block == MAX_END_BLOCK:
             vote_count = CurrentVote.select().where(
                 CurrentVote.address == candidate.address,
                 CurrentVote.start_block == 0,
                 CurrentVote.end_block == MAX_END_BLOCK).count()
             already_voted = CurrentVote.select().where(
                 CurrentVote.address == candidate.address,
                 CurrentVote.start_block == 0,
                 CurrentVote.end_block == MAX_END_BLOCK,
                 CurrentVote.given_from == Profile.get_active().address).count() > 0
             if candidate.perm_type == Permission.ADMIN:
                 required = math.ceil(Permission.num_guardians() * ADMIN_CONSENUS_ADMIN)
             else:
                 required = math.ceil(Permission.num_guardians() * ADMIN_CONSENUS_MINE)
             key = (candidate.address, candidate.perm_type)
             new_keys.add(key)
             self.db[key] = [alias, candidate.address.address, candidate.perm_type,
                             "{} of {}".format(vote_count, required), already_voted]
     deleted_keys = old_keys - new_keys
     for key in deleted_keys:
         del self.db[key]
Пример #2
0
    def __init__(self, parent):
        super().__init__(parent)
        self.setupUi(self)

        self.edit_amount.textChanged.connect(self.on_amount_edit)

        self.edit_address.setValidator(AddressValidator())
        self.edit_address.textChanged.connect(self.on_address_edit)
        self.edit_address.textChanged.connect(self.check_state)

        self.edit_description.setStyleSheet('QLineEdit:focus {background-color: #fff79a}')

        self.btn_send_cancel.clicked.connect(self.on_cancel_clicked)
        self.btn_send_send.setDisabled(True)
        self.amount_valid = False
        self.address_valid = False
        self.btn_send_send.clicked.connect(self.on_send_clicked)

        address_list =[]
        for address in Address.select().order_by(Address.address.desc()):
            if address.alias is not None:
                address_list.append("{} ({})".format(address.alias, address.address))
            address_list.append(address.address)
        completer = QCompleter(address_list, self.edit_address)
        completer_delegate = QStyledItemDelegate(completer)
        completer.setCaseSensitivity(Qt.CaseInsensitive)
        completer.popup().setItemDelegate(completer_delegate)
        completer.popup().setStyleSheet(
            """
            QAbstractItemView {
                font: 10pt "Roboto Light";
                border: 1px solid #41ADFF;
                border-top: 0px;
                background-color: #FFF79A;
                border-radius: 2px;
            }
            QAbstractItemView::item  {
                margin-top: 3px;
            }           
            """
        )
        self.edit_address.setCompleter(completer)
Пример #3
0
def liststreamitems_alias():
    """
    Sample stream item (none verbose):
    {
        'blocktime': 1505905511,
        'confirmations': 28948,
        'data': '4d696e65722031',
        'key': 'Miner_1',
        'publishers': ['1899xJpqZN3kMQdpvTxESWqykxgFJwRddCE4Tr'],
        'txid': 'caa1155e719803b9f39096860519a5e08e78214245ae9822beeea2b37a656178'
    }
    """

    client = get_active_rpc_client()

    # TODO read and process only fresh stream data by storing a state cursor between runs
    # TODO read stream items 100 at a time
    stream_items = client.liststreamitems(Stream.alias.name, count=100000)
    if not stream_items['result']:
        log.debug('got no items from stream alias')
        return 0

    by_addr = {}  # address -> alias
    reseved = set()  # reserved aliases

    # aggregate the final state of address to alias mappings from stream
    for item in stream_items['result']:
        confirmations = item['confirmations']
        alias = item['key']
        address = item['publishers'][0]
        data = item['data']
        num_publishers = len(item['publishers'])

        # Sanity checks
        if confirmations < 1:
            log.debug('ignore alias - 0 confirmations for %s -> %s' %
                      (address, alias))
            continue
        if data:
            log.debug('ignore alias - alias item "%s" with data "%s..."' %
                      (alias, data[:8]))
            continue
        if not is_valid_username(alias):
            log.debug('ignore alias - alias does not match our regex: %s' %
                      alias)
            continue
        if num_publishers != 1:
            log.debug('ignore alias - alias has multiple publishers: %s' %
                      alias)
            continue
        if alias in reseved:
            log.debug('ignore alias - alias "%s" already reserved by "%s"' %
                      (alias, address))
            continue

        is_new_address = address not in by_addr

        if is_new_address:
            by_addr[address] = alias
            reseved.add(alias)
            continue

        is_alias_change = by_addr[address] != alias

        if is_alias_change:
            log.debug('change alias of %s from %s to %s' %
                      (address, by_addr[address], alias))
            # reserve new name
            reseved.add(alias)
            # release old name
            reseved.remove(by_addr[address])
            # set new name
            by_addr[address] = alias
            continue

    # update database
    profile = Profile.get_active()
    new_main_alias = by_addr.get(profile.address)
    if new_main_alias and profile.alias != new_main_alias:
        log.debug('sync found new alias. profile.alias from %s to %s' %
                  (profile.alias, new_main_alias))
        profile.alias = new_main_alias
        profile.save()

    with data_db.atomic():
        old_addrs = set(
            Address.select(Address.address, Address.alias).tuples())
        new_addrs = set(by_addr.items())
        # set of elements that are only in new_addr but not in old_addr
        changed_addrs = new_addrs - old_addrs
        new_rows = [dict(address=i[0], alias=i[1]) for i in changed_addrs]
        if new_rows:
            log.debug('adding new aliases %s' % changed_addrs)
            # insert rows 100 at a time.
            for idx in range(0, len(new_rows), 100):
                Address.insert_many(new_rows[idx:idx +
                                             100]).upsert(True).execute()

    return len(changed_addrs)