Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): idempotency bug in registerBroadcastHandler #3558

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/api/src/app/wallets/register-broadcasted-payout-txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const registerBroadcastedPayout = async ({
const { estimatedProtocolFee } = setTxIdResult
if (estimatedProtocolFee.amount === proportionalFee.amount) return true

const isRecorded = await LedgerFacade.isOnChainFeeReconciliationRecorded(payoutId)
if (isRecorded !== false) return isRecorded
dolcalmi marked this conversation as resolved.
Show resolved Hide resolved

const { metadata } = LedgerFacade.OnChainFeeReconciliationLedgerMetadata({
payoutId,
txHash: txId,
Expand Down
1 change: 1 addition & 0 deletions core/api/src/services/ledger/domain/errors.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type LedgerFacadeError = import("./errors").LedgerFacadeError
1 change: 1 addition & 0 deletions core/api/src/services/ledger/facade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./intraledger"
export * from "./offchain-receive"
export * from "./offchain-send"
export * from "./onchain-receive"
export * from "./onchain-reconcile"
export * from "./onchain-send"
export * from "./reconciliation"
export * from "./static-account-ids"
Expand Down
40 changes: 0 additions & 40 deletions core/api/src/services/ledger/facade/onchain-receive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MainBook } from "../books"

import { EntryBuilder, toLedgerAccountDescriptor } from "../domain"
import { FeeOnlyEntryBuilder } from "../domain/fee-only-entry-builder"
import { persistAndReturnEntry } from "../helpers"

import { staticAccountIds } from "./static-account-ids"
Expand Down Expand Up @@ -48,42 +47,3 @@ export const recordReceiveOnChain = async ({

return persistAndReturnEntry({ entry, ...txMetadata })
}

export const recordReceiveOnChainFeeReconciliation = async ({
estimatedFee,
actualFee,
metadata,
}: {
estimatedFee: BtcPaymentAmount
actualFee: BtcPaymentAmount
metadata: AddOnChainFeeReconciliationLedgerMetadata
}) => {
const accountIds = await staticAccountIds()
if (accountIds instanceof Error) return accountIds

let entry = MainBook.entry("")
if (actualFee.amount > estimatedFee.amount) {
const btcFeeDifference = calc.sub(actualFee, estimatedFee)
const builder = FeeOnlyEntryBuilder({
staticAccountIds: accountIds,
entry,
metadata,
btcFee: btcFeeDifference,
})
entry = builder.debitBankOwner().creditOnChain()
} else {
const btcFeeDifference = calc.sub(estimatedFee, actualFee)
const builder = FeeOnlyEntryBuilder({
staticAccountIds: accountIds,
entry,
metadata,
btcFee: btcFeeDifference,
})
entry = builder.debitOnChain().creditBankOwner()
}

return persistAndReturnEntry({
entry,
hash: metadata.hash,
})
}
76 changes: 76 additions & 0 deletions core/api/src/services/ledger/facade/onchain-reconcile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { MainBook } from "../books"

import { translateToLedgerTx } from ".."
import { getBankOwnerWalletId } from "../caching"
import { UnknownLedgerError } from "../domain/errors"
import { persistAndReturnEntry } from "../helpers"
import { FeeOnlyEntryBuilder } from "../domain/fee-only-entry-builder"

import { staticAccountIds } from "./static-account-ids"

import { LedgerTransactionType, toLiabilitiesWalletId } from "@/domain/ledger"
import { AmountCalculator } from "@/domain/shared"

const calc = AmountCalculator()

export const recordReceiveOnChainFeeReconciliation = async ({
estimatedFee,
actualFee,
metadata,
}: {
estimatedFee: BtcPaymentAmount
actualFee: BtcPaymentAmount
metadata: AddOnChainFeeReconciliationLedgerMetadata
}) => {
const accountIds = await staticAccountIds()
if (accountIds instanceof Error) return accountIds

let entry = MainBook.entry("")
if (actualFee.amount > estimatedFee.amount) {
const btcFeeDifference = calc.sub(actualFee, estimatedFee)
const builder = FeeOnlyEntryBuilder({
staticAccountIds: accountIds,
entry,
metadata,
btcFee: btcFeeDifference,
})
entry = builder.debitBankOwner().creditOnChain()
} else {
const btcFeeDifference = calc.sub(estimatedFee, actualFee)
const builder = FeeOnlyEntryBuilder({
staticAccountIds: accountIds,
entry,
metadata,
btcFee: btcFeeDifference,
})
entry = builder.debitOnChain().creditBankOwner()
}

return persistAndReturnEntry({
entry,
hash: metadata.hash,
})
}

export const isOnChainFeeReconciliationTxn = (
txn: LedgerTransaction<WalletCurrency>,
): boolean =>
txn.type === LedgerTransactionType.OnchainPayment && txn.address === undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure when we began to use non undefinied/null/empty validation (should be !txn.address we already had an issue in prod with this) also.. should not include bankowner wallet id validation?

Copy link
Contributor Author

@vindard vindard Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I was thinking it should specifically check for the absence of the address property (since the OnChainAddress can technically also be falsy/empty even though it should never be). Or, what sort of issue has this thrown in prod?

should not include bankowner wallet id validation

Maybe not necessary since the reconciliation transaction only ever involves Liabilities:bankOwner account and Asset:OnChain accounts and I believe we only bring in Liabilities accounts in our ledger get methods. address absence should be enough for this check?

Sidenotes:
For this check, ideally it should be a different tx type value instead of also being an onchain_payment so we don't even need the check, but that might be a tricky migration to do. And I also considered maybe setting a custom description for these types of transactions but then there was no way to guarantee that someone couldn't set the same description on normal onchain-payment transactions which would break the heuristic. I switched from checking for satsFee presence to address presence since this is more isolated and might less likely eventually be populated on the reconciliation-type transactions that get constructed inside FeeOnlyEntryBuilder.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least with validations in data from external services we must be more "defensive", we cant guarantee the undefined so it is safer just to use !txn.address

FYI: the error was a validation using undefined and a service returning null value (typescript in theory was ok but you know we cant trust TS )

Copy link
Contributor Author

@vindard vindard Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea ok that makes sense.

For here though, address is set explicitly in our translateToLedgerTx function, so there's also less risk of it being something unexpected? We handle the uncertain-falsy-check-from-external-service in the line tx.payee_addresses && tx.payee_addresses.length > 0 inside translateToLedgerTx


export const isOnChainFeeReconciliationRecorded = async (
payoutId: PayoutId,
): Promise<boolean | LedgerFacadeError> => {
try {
const bankOwnerWalletId = await getBankOwnerWalletId()
const { results } = await MainBook.ledger({
payout_id: payoutId,
account: toLiabilitiesWalletId(bankOwnerWalletId),
})
const txns = results.map((tx) => translateToLedgerTx(tx))
vindard marked this conversation as resolved.
Show resolved Hide resolved

const reconciliationTxn = txns.find((txn) => isOnChainFeeReconciliationTxn(txn))
return reconciliationTxn !== undefined
dolcalmi marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
return new UnknownLedgerError(err)
dolcalmi marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 2 additions & 1 deletion core/api/src/services/ledger/facade/onchain-send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TransactionsMetadataRepository } from "../services"

import { translateToLedgerTx } from ".."

import { isOnChainFeeReconciliationTxn } from "./onchain-reconcile"
import { staticAccountIds } from "./static-account-ids"

import {
Expand Down Expand Up @@ -168,7 +169,7 @@ export const setOnChainTxIdByPayoutId = async ({

const bankOwnerWalletId = await getBankOwnerWalletId()
const bankOwnerTxns = txns.filter(
(txn) => txn.satsFee && txn.walletId === bankOwnerWalletId,
(txn) => txn.walletId === bankOwnerWalletId && !isOnChainFeeReconciliationTxn(txn),
)
if (bankOwnerTxns.length !== 1) {
return new InvalidLedgerTransactionStateError(`payoutId: ${payoutId}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,15 @@ describe("Bria Event Handlers", () => {
})
expect(res).toBe(true)

// Idempotency check
const resRerun = await registerBroadcastedPayout({
payoutId,
proportionalFee,
txId,
vout,
})
expect(resRerun).toBe(true)

// Run after-broadcast checks
const txnsAfter = await LedgerFacade.getTransactionsByPayoutId(payoutId)
if (txnsAfter instanceof Error) throw txnsAfter
Expand Down Expand Up @@ -562,6 +571,15 @@ describe("Bria Event Handlers", () => {
})
expect(res).toBe(true)

// Idempotency check
const resRerun = await registerBroadcastedPayout({
payoutId,
proportionalFee,
txId,
vout,
})
expect(resRerun).toBe(true)

// Run after-broadcast checks
const txnsAfter = await LedgerFacade.getTransactionsByPayoutId(payoutId)
if (txnsAfter instanceof Error) throw txnsAfter
Expand Down Expand Up @@ -617,6 +635,15 @@ describe("Bria Event Handlers", () => {
})
expect(res).toBe(true)

// Idempotency check
const resRerun = await registerBroadcastedPayout({
payoutId,
proportionalFee,
txId,
vout,
})
expect(resRerun).toBe(true)

// Run after-broadcast checks
const txnsAfter = await LedgerFacade.getTransactionsByPayoutId(payoutId)
if (txnsAfter instanceof Error) throw txnsAfter
Expand Down Expand Up @@ -677,6 +704,15 @@ describe("Bria Event Handlers", () => {
})
expect(res).toBeInstanceOf(UnknownLedgerError)

// Idempotency check
const resRerun = await registerBroadcastedPayout({
payoutId,
proportionalFee,
txId,
vout,
})
expect(resRerun).toBeInstanceOf(UnknownLedgerError)

spy.mockRestore()

// Run after-broadcast checks
Expand Down
Loading