def play_events_begin(self, cursor): # events cursor.execute( """ SELECT events_begin 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) for ev in events: ev = util.parse_event(ev) if ev['type'] == 'protocol_upgrade': # do something or nothing vs = self._vars() vs['protocol_version'] = ev['attr']['version'] cursor.execute( """ INSERT INTO `s_protocol` (`chain_id`, `height`, `version`) VALUES (%(chain_id)s, %(height)s, %(protocol_version)s) """, vs)
def play_events(self, cursor): for ev in self.events: ev = util.parse_event(ev) # TODO: refactor if ev['type'] == 'draft': draft = models.Draft(self.chain_id, int(ev['attr']['id']), None, cursor) 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) draft.save(cursor) # TODO: use another events regarding balance change proposer = models.Account(self.chain_id, draft.proposer, cursor) proposer.balance -= draft.deposit proposer.save(cursor)
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)