def create_token_commitment_for_eon(token: Token, eon_number): logger.info('Creating Token Commitment for {} at {}'.format( token.address, eon_number)) last_eon_number = eon_number - 1 with transaction.atomic(): wallets = Wallet.objects\ .filter( token=token, registration_operator_authorization__isnull=False, trail_identifier__isnull=False)\ .order_by('trail_identifier') new_balances = [] left, right = 0, 0 for wallet in wallets: with wallet.lock(auto_renewal=True): wallet_transfer_context = WalletTransferContext( wallet=wallet, transfer=None) last_transfer, last_transfer_is_outgoing = wallet_transfer_context.last_appended_active_transfer( eon_number=last_eon_number) last_transfer_active_state = None if last_transfer is not None and last_transfer.is_open_swap(): last_transfer.retire_swap() if last_transfer is not None: last_transfer_active_state = WalletTransferContext.appropriate_transfer_active_state( transfer=last_transfer, is_outgoing=last_transfer_is_outgoing) available_funds = wallet_transfer_context.available_funds_at_eon( eon_number=last_eon_number, only_appended=True) right = left + available_funds assert right >= left, 'Wallet {} Token {} Balance {}'.format( wallet.address, token.address, available_funds) passive_checksum, passive_amount, passive_marker = wallet_transfer_context.get_passive_values( eon_number=last_eon_number) new_balances.append({ 'contract': settings.HUB_LQD_CONTRACT_ADDRESS, 'token': token.address, 'wallet': wallet.address, 'left': left, 'right': right, 'active_state_checksum': last_transfer_active_state.checksum() if last_transfer_active_state is not None else b'\0'*32, 'active_state': last_transfer_active_state, 'passive_checksum': passive_checksum, 'passive_amount': passive_amount, 'passive_marker': passive_marker, }) left = right last_incoming_passive_transfer = wallet_transfer_context.last_appended_incoming_passive_transfer( eon_number=last_eon_number) if last_incoming_passive_transfer: wallet_transfer_context = WalletTransferContext( wallet=wallet, transfer=last_incoming_passive_transfer) passive_eon_transfers_list = wallet_transfer_context.incoming_passive_transfers_list( only_appended=True, force_append=False) passive_transfers_merkle_tree = wallet_transfer_context.incoming_passive_transfers_tree( only_appended=True, force_append=False) for index, incoming_passive_transfer in enumerate(passive_eon_transfers_list): final_transfer_index = index final_transfer_membership_proof = passive_transfers_merkle_tree.proof( final_transfer_index) final_transfer_membership_proof_chain = final_transfer_membership_proof.get( "chain") final_transfer_membership_proof_values = final_transfer_membership_proof.get( "values") assert incoming_passive_transfer.final_receipt_hashes is None assert incoming_passive_transfer.final_receipt_index is None assert incoming_passive_transfer.final_receipt_values is None incoming_passive_transfer.final_receipt_hashes = final_transfer_membership_proof_chain incoming_passive_transfer.final_receipt_index = final_transfer_index incoming_passive_transfer.final_receipt_values = final_transfer_membership_proof_values incoming_passive_transfer.save() if last_transfer_active_state is None: continue wallet_transfer_context = WalletTransferContext( wallet=wallet, transfer=last_transfer) starting_balance = int( wallet_transfer_context.starting_balance_in_eon(last_eon_number)) # if last active transfer is a multi eon swap # starting balance included in every tx checksum should be set to the cached starting balance # this way checkpoint state will match signed active state if last_transfer.is_swap() and not last_transfer.cancelled: if Transfer.objects.filter(eon_number=last_transfer.eon_number-1, tx_id=last_transfer.tx_id).exists(): matched_out, matched_in = last_transfer.matched_amounts( all_eons=True) current_matched_out, current_matched_in = last_transfer.matched_amounts( all_eons=False) if last_transfer_is_outgoing: sender_starting_balance = last_transfer.sender_starting_balance # current eon's starting balance should be equal to # cached starting balance - committed matched out amount in past rounds assert(starting_balance == sender_starting_balance - matched_out + current_matched_out) starting_balance = sender_starting_balance else: recipient_starting_balance = last_transfer.recipient_starting_balance # current eon's starting balance should be equal to # cached starting balance + committed matched in amount in past rounds assert( starting_balance == recipient_starting_balance + matched_in - current_matched_in) starting_balance = recipient_starting_balance confirmed_eon_transfers_list = wallet_transfer_context.authorized_transfers_list( only_appended=True, force_append=False) confirmed_eon_transfers_list_shorthand = wallet_transfer_context.authorized_transfers_list_shorthand( only_appended=True, force_append=False, last_transfer_is_finalized=False, starting_balance=starting_balance) transaction_merkle_tree = TransactionMerkleTree( confirmed_eon_transfers_list_shorthand) transaction_merkle_tree_root = hex_value( transaction_merkle_tree.root_hash()) assert transaction_merkle_tree_root == last_transfer_active_state.tx_set_hash,\ '{}/{}'.format(transaction_merkle_tree_root, last_transfer_active_state.tx_set_hash) for confirmed_incoming_transfer in confirmed_eon_transfers_list: if confirmed_incoming_transfer.recipient != wallet: continue final_transfer_index = transaction_merkle_tree.merkle_tree_nonce_map.get( confirmed_incoming_transfer.nonce) final_transfer_membership_proof_chain = transaction_merkle_tree.proof( final_transfer_index) assert confirmed_incoming_transfer.final_receipt_hashes is None assert confirmed_incoming_transfer.final_receipt_index is None confirmed_incoming_transfer.final_receipt_hashes = final_transfer_membership_proof_chain confirmed_incoming_transfer.final_receipt_index = final_transfer_index confirmed_incoming_transfer.save() managed_funds = 0 if eon_number > 1: last_eon = LocalViewInterface.confirmed(eon_number=last_eon_number) pending_withdrawals_until_last_eon = \ WithdrawalRequest.objects\ .filter(wallet__token=token, eon_number__lte=last_eon_number, slashed=False)\ .filter(Q(withdrawal__isnull=True) | Q(withdrawal__block__gt=last_eon.block)) if not pending_withdrawals_until_last_eon.exists(): last_eon_pending_withdrawals = 0 else: last_eon_pending_withdrawals = pending_withdrawals_until_last_eon\ .aggregate(Sum('amount')) \ .get('amount__sum') total_token_balance = last_eon.contractledgerstate_set.get( token=token).total_balance managed_funds = total_token_balance - last_eon_pending_withdrawals if right < managed_funds: logger.warning('UNCLAIMED FUNDS: {} in {}'.format( managed_funds - right, token.address)) send_admin_email( subject='Soft TokenCommitment Warning: Extra funds', content='There are some additional funds in the balance pool that belong to no one: {} of {}' .format(managed_funds - right, token.address)) altered_balances = new_balances + [{ 'contract': settings.HUB_LQD_CONTRACT_ADDRESS, 'token': token.address, 'wallet': settings.HUB_OWNER_ACCOUNT_ADDRESS, 'left': left, 'right': managed_funds, 'active_state_checksum': b'\0'*32, 'active_state': None, 'passive_checksum': b'\0'*32, 'passive_amount': 0, 'passive_marker': 0, }] new_merkle_tree = MerkleTree(altered_balances, managed_funds) right = managed_funds else: if right > managed_funds: logger.error('OVERCLAIMING FUNDS!! {} > {} in {}'.format( right, managed_funds, token.address)) send_admin_email( subject='HARD Checkpoint Error: OVERCLAIMING!', content='OVERCLAIMING FUNDS!! {} > {} in {}'.format(right, managed_funds, token.address)) new_merkle_tree = MerkleTree(new_balances, right) bulk_manager = BulkCreateManager(chunk_size=500) for index, balance in enumerate(new_balances): if not balance.get('wallet') or balance.get('wallet') == '0x0000000000000000000000000000000000000000': continue merkle_proof = new_merkle_tree.proof(index) wallet = Wallet.objects.get( token=token, address=remove_0x_prefix(balance.get('wallet'))) # TODO verify validity through RPC prior to insertion assert(wallet.trail_identifier == index) # create records in batches bulk_manager.add( ExclusiveBalanceAllotment( wallet=wallet, eon_number=eon_number, left=balance.get('left'), right=balance.get('right'), merkle_proof_hashes=merkle_proof.get('chain'), merkle_proof_values=merkle_proof.get('values'), merkle_proof_trail=index, active_state=balance.get('active_state') ) ) # make sure remaining batch is added bulk_manager.done() token_commitment = TokenCommitment.objects.create( token=token, merkle_root=hex_value(new_merkle_tree.root_hash()), upper_bound=right) return token_commitment
def should_void_transfer(transfer, wallet_view_context: WalletTransferContext, recipient_view_context: WalletTransferContext, operator_eon_number, is_checkpoint_created): if transfer.eon_number != operator_eon_number and is_checkpoint_created: logger.error('Transfer {} eon mismatch ({}, {})'.format( transfer.id, transfer.eon_number, operator_eon_number)) return True if transfer.amount < 0: logger.error('Transfer {} has negative amount'.format(transfer.id)) return True # Unauthorized transfer if transfer.sender_active_state is None: logger.error('Transfer {} no authorization'.format(transfer.id)) return True # Invalid signature by sender if not transfer.sender_active_state.wallet_signature.is_valid(): logger.error('Transfer {} invalid sender signature.'.format( transfer.id)) return True # Ensure sender log consistency can_append_to_sender_log = wallet_view_context.can_append_transfer() if can_append_to_sender_log is not True: logger.error('Sender: {}'.format(can_append_to_sender_log)) return True # Ensure recipient log consistency can_append_to_recipient_log = recipient_view_context.can_append_transfer() if can_append_to_recipient_log is not True: logger.error('Recipient: {}'.format(can_append_to_recipient_log)) return True # Ensure transfer consistency can_spend, currently_available_funds = wallet_view_context.can_send_transfer( current_eon_number=operator_eon_number, using_only_appended_funds=True) if can_spend is not True: passively_received = wallet_view_context.off_chain_passively_received_amount( eon_number=operator_eon_number, only_appended=True) logger.error(can_spend) logger.info(passively_received) return True last_active_transfer, last_active_transfer_is_outgoing = wallet_view_context.last_appended_active_transfer( operator_eon_number) last_active_state = WalletTransferContext.appropriate_transfer_active_state( transfer=last_active_transfer, is_outgoing=last_active_transfer_is_outgoing) previous_spendings = last_active_state.updated_spendings if last_active_transfer else 0 updated_spendings = transfer.sender_active_state.updated_spendings # Incorrect updated spendings if last_active_transfer: if updated_spendings != previous_spendings + transfer.amount: logger.error( 'Transfer {} invalid updated spendings. Expected {}, found {}.' .format(transfer.id, previous_spendings + transfer.amount, updated_spendings)) return True elif updated_spendings != transfer.amount: logger.error( 'Transfer {} invalid initial spendings. Expected {}, found {}.'. format(transfer.id, transfer.amount, updated_spendings)) return True # Incorrect transfer position last_passively_received = recipient_view_context.last_appended_incoming_passive_transfer( operator_eon_number) if last_passively_received: if transfer.position != last_passively_received.position + last_passively_received.amount: logger.error( 'Transfer {} invalid offset. Expected {}, found {}.'.format( transfer.id, last_passively_received.position + last_passively_received.amount, transfer.position)) return True elif transfer.position != 0: logger.error( 'Transfer {} invalid offset. Expected {}, found {}.'.format( transfer.id, 0, transfer.position)) return True if transfer.sender_balance_marker.amount > currently_available_funds - transfer.amount: logger.error('Transfer {} invalid concise marker balance.'.format( transfer.id)) return True concise_balance_marker = MinimumAvailableBalanceMarker( wallet=transfer.wallet, eon_number=transfer.eon_number, amount=transfer.sender_balance_marker.amount) concise_balance_marker_checksum = hex_value( concise_balance_marker.checksum()) if transfer.sender_balance_marker.signature.checksum != concise_balance_marker_checksum: logger.error( 'Transfer {} invalid concise marker checksum worth: {}'.format( transfer.id, currently_available_funds - transfer.amount)) passively_received = wallet_view_context.off_chain_passively_received_amount( eon_number=operator_eon_number, only_appended=True) logger.info(passively_received) return True return False