Skip to content

Commit

Permalink
Fix: Improve audit trail to provide better info on transaction issues…
Browse files Browse the repository at this point in the history
… (outputs already signed err)
  • Loading branch information
minibits-cash committed Nov 14, 2024
1 parent 874f2bd commit 773cdd8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minibits_wallet",
"version": "0.1.9-beta.32",
"version": "0.1.9-beta.33",
"private": true,
"scripts": {
"android:clean": "cd android && ./gradlew clean",
Expand Down
32 changes: 18 additions & 14 deletions src/models/Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const MintProofsCounterModel = types.model('MintProofsCounter', {
self.counter = inFlightTo // temp increase of main counter value
self.inFlightTid = inFlightTid

log.trace('[setInFlight]', 'Lock and inflight indexes were set', self)
log.trace('[setInFlight]', 'Lock and inflight indexes were set', {inFlightTid})
},
resetInFlight(inFlightTid: number) {
self.inFlightFrom = undefined
Expand All @@ -66,7 +66,7 @@ export const MintProofsCounterModel = types.model('MintProofsCounter', {
increaseProofsCounter(numberOfProofs: number) {
if(isNaN(self.counter)) self.counter = 0
self.counter += numberOfProofs
log.trace('[increaseProofsCounter]', 'Increased proofsCounter', {numberOfProofs, counter: self.counter})
log.info('[increaseProofsCounter]', 'Increased proofsCounter', {numberOfProofs, counter: self.counter})
},
decreaseProofsCounter(numberOfProofs: number) {
self.counter -= numberOfProofs
Expand Down Expand Up @@ -167,7 +167,6 @@ export const MintModel = types
self.proofsCounters.push(counter)
self.proofsCounters = cast(self.proofsCounters)
}


},
removeProofsCounter(counter: MintProofsCounter) {
Expand Down Expand Up @@ -198,15 +197,20 @@ export const MintModel = types
}))
.actions(self => ({
createProofsCounter(keyset: CashuMintKeyset) {
const existing = self.getProofsCounter(keyset.id)

if(!existing) {
const newCounter = MintProofsCounterModel.create({
keyset: keyset.id,
unit: keyset.unit as MintUnit,
counter: 0,
})

self.addProofsCounter(newCounter)
return newCounter
}

const newCounter = MintProofsCounterModel.create({
keyset: keyset.id,
unit: keyset.unit as MintUnit,
counter: 0,
})

self.addProofsCounter(newCounter)
return self.getProofsCounter(keyset.id)
return existing
}
}))
.actions(self => ({
Expand Down Expand Up @@ -292,10 +296,10 @@ export const MintModel = types
const counter = self.proofsCounters.find(p => p.keyset === keysetId)
if(!counter) {
const keyset = self.keysets.find(k => k.id === keysetId)
if(keyset) {
return self.createProofsCounter(keyset)
if(!keyset) {
throw new AppError(Err.VALIDATION_ERROR, 'Missing keyset.')
}
throw new AppError(Err.VALIDATION_ERROR, 'Missing keyset.')
return self.createProofsCounter(keyset)
}

return counter
Expand Down
18 changes: 10 additions & 8 deletions src/models/ProofsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export const ProofsStoreModel = types
const mintsStore = getRootStore(self).mintsStore
const mintInstance = mintsStore.findByUrl(newProofs[0].mintUrl as string)

if(!mintInstance) {
throw new AppError(Err.STORAGE_ERROR, 'Could not find mint', {mintUrl: newProofs[0].mintUrl})
}

for (const [keysetId, keysetProofs] of proofsByKeysetId.entries()) {

for (const proof of keysetProofs) {
Expand All @@ -165,14 +169,12 @@ export const ProofsStoreModel = types
}

// Find the corresponding counter for this keysetId
const proofsCounter = mintInstance?.getProofsCounterByKeysetId(keysetId)
if (proofsCounter) {
// Increment the counter by the number of proofs to insert
proofsCounter.increaseProofsCounter(keysetProofs.length)
}
}

log.debug('[addProofs]', `Added new ${addedProofs.length}${isPending ? ' pending' : ''} proofs to the ProofsStore`)
const proofsCounter = mintInstance.getProofsCounterByKeysetId(keysetId)
// Increment the counter by the number of proofs to insert
proofsCounter.increaseProofsCounter(keysetProofs.length)
}

log.info('[addProofs]', `Added new ${addedProofs.length}${isPending ? ' pending' : ''} proofs to the ProofsStore`)

if (addedProofs.length > 0) {
Database.addOrUpdateProofs(addedProofs, isPending) // isSpent = false
Expand Down
19 changes: 12 additions & 7 deletions src/services/wallet/transferTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const transferTask = async function (
amountToTransfer,
unit,
meltQuote,
encodedInvoice,
encodedInvoice,
isNwc: nwcEvent ? true : false,
createdAt: new Date(),
}
]
Expand Down Expand Up @@ -127,18 +128,21 @@ export const transferTask = async function (
proofs: proofsToPay,
mintFeePaid,
mintFeeReserve,
isSwapNeeded
isSwapNeeded,
counter
} = swapResult

proofsToPayAmount = CashuUtils.getProofsAmount(proofsToPay)

// TODO in case of swap from inactive keysets, different meltFees might apply than above calculated meltFeeReserve
// In such case, we might need to add / substract the fee difference to / from proofsToPay

log.debug('[transfer]', 'Prepared poofsToPay proofs', {
log.info('[transfer]', 'Prepared poofsToPay proofs', {
proofsToPayAmount,
unit,
transactionId
transactionId,
isSwapNeeded,
counter
})

// Update transaction status
Expand All @@ -148,6 +152,7 @@ export const transferTask = async function (
mintFeePaid,
proofsToPayAmount,
isSwapNeeded,
counter,
createdAt: new Date(),
})

Expand Down Expand Up @@ -194,8 +199,8 @@ export const transferTask = async function (

lockedProofsCounter.decreaseProofsCounter(countOfInFlightProofs)
// release lock
lockedProofsCounter.resetInFlight(transaction.id)

lockedProofsCounter.resetInFlight(transactionId)
if (state === MeltQuoteState.PAID) {

log.debug('[transfer] Invoice PAID', {
Expand Down Expand Up @@ -277,7 +282,7 @@ export const transferTask = async function (
nwcEvent
} as TransactionTaskResult

} else if(state === MeltQuoteState.PENDING) {
} else if(state === MeltQuoteState.PENDING) {

log.debug('[transfer] Invoice PENDING', {
state,
Expand Down
11 changes: 4 additions & 7 deletions src/services/wallet/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ const lockAndSetInFlight = async function (
const walletInstance = await walletStore.getWallet(mint.mintUrl, unit, {withSeed: true})
const currentCounter = mint.getProofsCounterByKeysetId!(walletInstance.keys.id)

log.info('[lockAndSetInFlight] Before lock', {transactionId, counter: currentCounter.counter})

if(!currentCounter) {
throw new AppError(Err.VALIDATION_ERROR, 'Missing ProofsCounter.')
}

// log.trace('[lockAndSetInFlight] proofsCounter before lock', {currentCounter})

if(!retryCount) {
retryCount = 10
}

// deprecated, should not be necessary anymore with serial task queue processing
if(currentCounter && currentCounter.inFlightTid && currentCounter.inFlightTid !== transactionId) {
Expand All @@ -49,8 +46,8 @@ const lockAndSetInFlight = async function (

await delay(1000)

if (retryCount < 50) {
// retry to acquire lock, increment the count of retries up to 50 seconds
if (retryCount < 10) {
// retry to acquire lock, increment the count of retries up to 10 seconds
return lockAndSetInFlight(
mint,
unit,
Expand Down
5 changes: 4 additions & 1 deletion src/services/walletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,7 @@ const _handleClaimTask = async function (params: {
if(result && result.transaction) {
const transaction = transactionsStore.findById(result.transaction.id!)
const {zapSenderProfile, zapRequest} = claimedToken
let message: string = ''

if(transaction) {
if (zapSenderProfile) {
Expand All @@ -1432,10 +1433,12 @@ const _handleClaimTask = async function (params: {
return {
mintUrl: decoded.token[0].mint,
taskFunction: '_handleClaimTask',
message: 'Ecash sent to your lightning address has been received.',
message: result.error ? result.error.message : 'Ecash sent to your lightning address has been received.',
error: result.error || undefined,
proofsCount: decoded.token[0].proofs.length,
proofsAmount: result.transaction?.amount,
} as WalletTaskResult

} catch (e: any) {
log.error(e.name, e.message)

Expand Down

0 comments on commit 773cdd8

Please sign in to comment.