Beispiel #1
0
def tx_retract(tx, cursor):
    payload = json.loads(tx.payload)
    payload['amount'] = int(payload['amount'])

    sender = models.Account(tx.chain_id, tx.sender, cursor)
    asset_stat = stats.Asset(tx.chain_id, cursor)

    sender.delegate -= payload['amount']
    sender.balance += payload['amount']
    del_addr = sender.del_addr
    if sender.delegate == 0:
        sender.del_addr = None
    sender.save(cursor)
    rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index,
                              cursor)
    rel.amount += payload['amount']
    rel.save(cursor)

    delegatee = models.Account(tx.chain_id, del_addr, cursor)
    delegatee.eff_stake -= payload['amount']
    delegatee.save(cursor)

    asset_stat.active_coins += payload['amount']
    asset_stat.delegates -= payload['amount']
    asset_stat.save(cursor)
def tx_withdraw(tx, cursor):
    payload = json.loads(tx.payload)
    payload['amount'] = int(payload['amount'])

    sender = models.Account(tx.chain_id, tx.sender, cursor)
    sender.stake -= payload['amount']
    sender.eff_stake -= payload['amount']
    sender.balance += payload['amount']
    if sender.stake == 0:
        sender.val_addr = None
    sender.save(cursor)

    asset_stat = stats.Asset(tx.chain_id, cursor)
    asset_stat.active_coins += payload['amount']
    asset_stat.stakes -= payload['amount']
    asset_stat.save(cursor)
def tx_delegate(tx, cursor):
    payload = json.loads(tx.payload)
    payload['amount'] = int(payload['amount'])

    sender = models.Account(tx.chain_id, tx.sender, cursor)
    sender.delegate += payload['amount']
    sender.balance -= payload['amount']
    sender.del_addr = payload['to']
    sender.save(cursor)

    delegatee = models.Account(tx.chain_id, sender.del_addr, cursor)
    delegatee.eff_stake += payload['amount']
    delegatee.save(cursor)

    asset_stat = stats.Asset(tx.chain_id, cursor)
    asset_stat.active_coins -= payload['amount']
    asset_stat.delegates += payload['amount']
    asset_stat.save(cursor)
def tx_stake(tx, cursor):
    payload = json.loads(tx.payload)
    payload['amount'] = int(payload['amount'])

    sender = models.Account(tx.chain_id, tx.sender, cursor)
    sender.stake += payload['amount']
    sender.stake_locked += payload['amount']
    sender.eff_stake += payload['amount']
    sender.balance -= payload['amount']
    sender.val_pubkey = payload['validator']
    b = bytearray.fromhex(sender.val_pubkey)
    sender.val_addr = sha256(b).hexdigest()[:40].upper()
    sender.save(cursor)

    asset_stat = stats.Asset(tx.chain_id, cursor)
    asset_stat.active_coins -= payload['amount']
    asset_stat.stakes += payload['amount']
    asset_stat.save(cursor)
Beispiel #5
0
def tx_withdraw(tx, cursor):
    payload = json.loads(tx.payload)
    payload['amount'] = int(payload['amount'])

    sender = models.Account(tx.chain_id, tx.sender, cursor)
    sender.stake -= payload['amount']
    sender.eff_stake -= payload['amount']
    sender.balance += payload['amount']
    if sender.stake == 0:
        sender.val_addr = None
        sender.val_pubkey = None
        sender.val_power = 0
    sender.save(cursor)
    rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height, tx.index,
                              cursor)
    rel.amount += payload['amount']
    rel.save(cursor)

    asset_stat = stats.Asset(tx.chain_id, cursor)
    asset_stat.active_coins += payload['amount']
    asset_stat.stakes -= payload['amount']
    asset_stat.save(cursor)
    def play_genesis(self):
        cur = self.db.cursor()
        cur.execute(
            """
            SELECT `genesis` FROM `c_genesis`
            WHERE (`chain_id` = %(chain_id)s)
            """, self._vars())
        row = cur.fetchone()
        if row:
            genesis = json.loads(row[0])['app_state']
            # genesis balances
            asset_stat = stats.Asset(self.chain_id, cur)
            for item in genesis['balances']:
                acc = models.Account(self.chain_id, item['owner'], cur)
                acc.balance = int(item['amount'])
                asset_stat.active_coins += int(item['amount'])
                acc.save(cur)
            asset_stat.save(cur)
        else:
            return False  # TODO: return error

        self.db.commit()
        cur.close()
 def play_events_end(self, cursor):
     # events
     cursor.execute(
         """
         SELECT events_end FROM `c_blocks`
         WHERE (`chain_id` = %(chain_id)s AND `height` = %(height)s)
         """, self._vars())
     rows = cursor.fetchall()
     # cols = cursor.column_names
     for row in rows:
         (raw, ) = row
         events = json.loads(raw)
         asset_stat = stats.Asset(self.chain_id, cursor)
         for ev in events:
             ev = util.parse_event(ev)
             # TODO: refactor
             if ev['type'] == 'draft':
                 draft = models.Draft(self.chain_id, int(ev['attr']['id']),
                                      None, cursor)
                 close_count_old = draft.close_count
                 util.from_dict(draft, json.loads(ev['attr']['draft']))
                 draft.deposit = int(draft.deposit)
                 draft.tally_quorum = int(draft.tally_quorum)
                 draft.tally_approve = int(draft.tally_approve)
                 draft.tally_reject = int(draft.tally_reject)
                 if close_count_old > 0 and draft.close_count == 0:
                     draft.closed_at = self.height
                     cursor.execute(
                         """
                         UPDATE `s_votes` v LEFT JOIN `s_accounts` a
                             ON v.voter = a.address
                         SET v.`tally` = a.`eff_stake`
                         WHERE (v.`chain_id` = %(chain_id)s
                             AND v.`draft_id` = %(draft_id)s)
                         """, {
                             'chain_id': self.chain_id,
                             'draft_id': draft.draft_id
                         })
                 draft.save(cursor)
             if ev['type'] == 'stake_unlock':
                 recp = models.Account(self.chain_id,
                                       ev['attr']['address'].strip('"'),
                                       cursor)
                 recp.stake_locked -= int(ev['attr']['amount'].strip('"'))
                 recp.save(cursor)
             if ev['type'] == 'config':
                 cursor.execute(
                     """
                     UPDATE `s_drafts` SET `applied_at` = %(height)s
                     WHERE `chain_id` = %(chain_id)s
                     ORDER BY `draft_id` DESC LIMIT 1
                     """, self._vars())
             if ev['type'] == 'incentive':
                 vs = self._vars()
                 vs['address'] = ev['attr']['address'].strip('"')
                 vs['amount'] = ev['attr']['amount'].strip('"')
                 cursor.execute(
                     """
                     INSERT INTO `s_incentives`
                         (`chain_id`, `height`, `address`, `amount`)
                     VALUES
                         (%(chain_id)s, %(height)s, %(address)s, %(amount)s)
                     """, vs)
                 recp = models.Account(self.chain_id,
                                       ev['attr']['address'].strip('"'),
                                       cursor)
                 recp.balance += int(ev['attr']['amount'].strip('"'))
                 asset_stat.active_coins += int(
                     ev['attr']['amount'].strip('"'))
                 recp.save(cursor)
             if ev['type'] == 'penalty':
                 vs = self._vars()
                 vs['address'] = ev['attr']['address'].strip('"')
                 vs['amount'] = ev['attr']['amount'].strip('"')
                 cursor.execute(
                     """
                     INSERT INTO `s_penalties`
                         (`chain_id`, `height`, `address`, `amount`)
                     VALUES
                         (%(chain_id)s, %(height)s, %(address)s, %(amount)s)
                     """, vs)
                 recp = models.Account(self.chain_id,
                                       ev['attr']['address'].strip('"'),
                                       cursor)
                 if recp.stake > 0:  # staker
                     recp.stake -= int(ev['attr']['amount'].strip('"'))
                     recp.eff_stake -= int(ev['attr']['amount'].strip('"'))
                     asset_stat.stakes -= int(
                         ev['attr']['amount'].strip('"'))
                     recp.save(cursor)
                 elif recp.delegate > 0:  # delegator
                     recp.delegate -= int(ev['attr']['amount'].strip('"'))
                     staker = models.Account(self.chain_id, recp.del_addr,
                                             cursor)
                     staker.eff_stake -= int(
                         ev['attr']['amount'].strip('"'))
                     asset_stat.delegates -= int(
                         ev['attr']['amount'].strip('"'))
                     recp.save(cursor)
                     staker.save(cursor)
             if ev['type'] == 'draft_deposit':
                 recp = models.Account(self.chain_id,
                                       ev['attr']['address'].strip('"'),
                                       cursor)
                 recp.balance += int(ev['attr']['amount'].strip('"'))
                 recp.save(cursor)
             asset_stat.save(cursor)
Beispiel #8
0
def tx_transfer(tx, cursor):
    payload = json.loads(tx.payload)

    # NOTE: This is line is for creating buyer account in s_accounts table. It
    # is necessary to satisfy the foreign key constraint. Even if we create a
    # row for each tx sender, it is not the case for the recipient for
    # `request` or `grant` tx. So, we need to explicitly create a grantee or
    # recipient account in s_accounts table.
    recp = models.Account(tx.chain_id, payload['to'], cursor)
    recp.save(cursor)

    if payload.get('parcel'):
        owner = models.Account(tx.chain_id, tx.sender, cursor)
        parcel = models.Parcel(tx.chain_id, payload['target'], owner.address,
                               cursor)
        recp = models.Account(tx.chain_id, payload['to'], cursor)

        parcel.owner = recp.address
        parcel.save(cursor)
        if tx.sender not in yappers:
            rel = models.RelParcelTx(tx.chain_id, parcel.parcel_id, tx.height,
                                     tx.index)
            rel.save(cursor)

    else:
        amount = int(payload['amount'])
        udc = payload.get('udc')

        if udc is None:
            sender = models.Account(tx.chain_id, tx.sender, cursor)
            sender.balance -= amount
            sender.save(cursor)
            rel = models.RelAccountTx(tx.chain_id, tx.sender, tx.height,
                                      tx.index, cursor)
            rel.amount -= amount
            rel.save(cursor)

            recp = models.Account(tx.chain_id, payload['to'], cursor)
            recp.balance += amount
            recp.save(cursor)
            rel = models.RelAccountTx(tx.chain_id, payload['to'], tx.height,
                                      tx.index, cursor)
            rel.amount += amount
            rel.save(cursor)

            if recp.address == '000000000000000000000000000000000000DEAD':
                # This account is used for ad-hoc burning process, the balance
                # of this account is considered out of normal operation from
                # now. So let's decrease the amount of active coins
                # permanently.
                asset_stat = stats.Asset(tx.chain_id, cursor)
                asset_stat.active_coins -= amount
                asset_stat.save(cursor)
        else:
            sender = models.UDCBalance(tx.chain_id, udc, tx.sender, cursor)
            sender.balance -= amount
            sender.save(cursor)
            rel = models.RelBalanceTx(tx.chain_id, udc, tx.sender, tx.height,
                                      tx.index, cursor)
            rel.amount -= amount
            rel.save(cursor)

            recp = models.UDCBalance(tx.chain_id, udc, payload['to'], cursor)
            recp.balance += amount
            recp.save(cursor)
            rel = models.RelBalanceTx(tx.chain_id, udc, payload['to'],
                                      tx.height, tx.index, cursor)
            rel.amount += amount
            rel.save(cursor)