Skip to content

Commit

Permalink
Merge pull request #420 from EdgeApp/sam/seenTxCheckpoint
Browse files Browse the repository at this point in the history
Integrate Seen Tx Checkpoint API
  • Loading branch information
samholmes authored Jan 15, 2025
2 parents 9ae78a6 + 5267e07 commit 29b441f
Show file tree
Hide file tree
Showing 31 changed files with 257 additions and 130 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- added: Implement `updateInfoPayload` for `EdgeCurrencyEngine` to get currency info updates from info-server.
- changed: Implement new Seen Tx Checkpoint API for all currencies.
- fixed: Fixed cleaner failure for getInfo Blockbook request.

## 3.4.5 (2024-12-12)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"base-x": "^4.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"edge-core-js": "^2.7.0",
"edge-core-js": "^2.24.0",
"esbuild-loader": "^4.1.0",
"eslint": "^7.14.0",
"eslint-config-standard-kit": "0.15.1",
Expand Down
38 changes: 31 additions & 7 deletions src/common/plugin/EngineEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
EdgeCurrencyEngineCallbacks,
EdgeTransaction,
EdgeTransactionEvent,
EdgeTxidMap
} from 'edge-core-js/types'
import { EventEmitter } from 'events'
Expand All @@ -9,9 +10,17 @@ import { SubscribeAddressResponse } from '../utxobased/network/blockbookApi'

export declare interface EngineEmitter {
emit: ((
event: EngineEvent.TRANSACTIONS_CHANGED,
transactions: EdgeTransaction[]
event: EngineEvent.SEEN_TX_CHECKPOINT,
checkpoint: string
) => boolean) &
((
event: EngineEvent.TRANSACTIONS,
transactionEvents: EdgeTransactionEvent[]
) => boolean) &
((
event: EngineEvent.TRANSACTIONS_CHANGED,
transactions: EdgeTransaction[]
) => boolean) &
((
event: EngineEvent.ADDRESS_BALANCE_CHANGED,
currencyCode: string,
Expand All @@ -36,9 +45,19 @@ export declare interface EngineEmitter {
((event: EngineEvent.TXIDS_CHANGED, txids: EdgeTxidMap) => boolean)

on: ((
event: EngineEvent.TRANSACTIONS_CHANGED,
listener: (transactions: EdgeTransaction[]) => Promise<void> | void
event: EngineEvent.SEEN_TX_CHECKPOINT,
listener: (checkpoint: string) => Promise<void> | void
) => this) &
((
event: EngineEvent.TRANSACTIONS,
listener: (
transactionEvents: EdgeTransactionEvent[]
) => Promise<void> | void
) => boolean) &
((
event: EngineEvent.TRANSACTIONS_CHANGED,
listener: (transactions: EdgeTransaction[]) => Promise<void> | void
) => this) &
((
event: EngineEvent.ADDRESS_BALANCE_CHANGED,
listener: (
Expand Down Expand Up @@ -76,6 +95,9 @@ export declare interface EngineEmitter {
export class EngineEmitter extends EventEmitter {}

export enum EngineEvent {
SEEN_TX_CHECKPOINT = 'seen:tx:checkpoint',
TRANSACTIONS = 'transactions',
/** @deprecated Use TRANSACTIONS */
TRANSACTIONS_CHANGED = 'transactions:changed',
WALLET_BALANCE_CHANGED = 'wallet:balance:changed',
ADDRESS_BALANCE_CHANGED = 'address:balance:changed',
Expand All @@ -93,16 +115,18 @@ export const makeEngineEmitter = (
): EngineEmitter => {
const emitter = new EngineEmitter()

emitter.on(EngineEvent.TRANSACTIONS_CHANGED, callbacks.onTransactionsChanged)
emitter.on(EngineEvent.WALLET_BALANCE_CHANGED, callbacks.onBalanceChanged)
emitter.on(EngineEvent.ADDRESSES_CHECKED, callbacks.onAddressesChecked)
emitter.on(
EngineEvent.BLOCK_HEIGHT_CHANGED,
(_uri: string, height: number) => {
callbacks.onBlockHeightChanged(height)
}
)
emitter.on(EngineEvent.ADDRESSES_CHECKED, callbacks.onAddressesChecked)
emitter.on(EngineEvent.SEEN_TX_CHECKPOINT, callbacks.onSeenTxCheckpoint)
emitter.on(EngineEvent.TRANSACTIONS, callbacks.onTransactions)
emitter.on(EngineEvent.TRANSACTIONS_CHANGED, callbacks.onTransactionsChanged)
emitter.on(EngineEvent.TXIDS_CHANGED, callbacks.onTxidsChanged)
emitter.on(EngineEvent.WALLET_BALANCE_CHANGED, callbacks.onBalanceChanged)

return emitter
}
6 changes: 6 additions & 0 deletions src/common/utxobased/db/Models/TransactionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export const toEdgeTransaction = async (
memos: [],
nativeAmount: tx.ourAmount,
networkFee: tx.fees,
networkFees: [
{
tokenId: null,
nativeAmount: tx.fees
}
],
ourReceiveAddresses,
signedTx: tx.hex,
tokenId: null,
Expand Down
69 changes: 47 additions & 22 deletions src/common/utxobased/engine/UtxoEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ import {
pathToPurposeType,
sumUtxos
} from './utils'
import {
makeUtxoEngineProcessor,
transactionChanged
} from './UtxoEngineProcessor'
import { makeUtxoEngineProcessor } from './UtxoEngineProcessor'
import { makeUtxoWalletTools } from './UtxoWalletTools'

export async function makeUtxoEngine(
Expand Down Expand Up @@ -131,12 +128,36 @@ export async function makeUtxoEngine(
// private keys.
let nonceDataLayer: DataLayer | undefined

/**
* This is a stateful function, which means it is both a setter and a getter.
* The state is the cached seenTxCheckpoint for the engine during runtime.
* It is initialized with the seenTxCheckpoint state from the core via
* `EdgeCurrencyEngineOptions`.
* It is updated by the engine's processor as the wallet syncs.
* Once the wallet fully syncs, the seenTxCheckpoint value is emitted to the
* `onSeenTxCheckpoint` callback so the core can persist this state to disk.
*/
const seenTxCheckpoint = ((state?: string) => (
value?: string
): string | undefined => {
if (value != null) state = value
return state
})()

// Initialize the seenTxCheckpoint with the value from the core.
seenTxCheckpoint(config.engineOptions.seenTxCheckpoint)

emitter.on(EngineEvent.SEEN_TX_CHECKPOINT, checkpoint => {
seenTxCheckpoint(checkpoint)
})

const engineProcessor = makeUtxoEngineProcessor({
...config,
walletTools,
walletInfo,
dataLayer,
pluginState
pluginState,
seenTxCheckpoint,
walletTools,
walletInfo
})

const engine: EdgeCurrencyEngine = {
Expand Down Expand Up @@ -695,6 +716,12 @@ export async function makeUtxoEngine(
memos,
nativeAmount,
networkFee,
networkFees: [
{
tokenId: null,
nativeAmount: networkFee
}
],
otherParams,
ourReceiveAddresses,
signedTx: '',
Expand Down Expand Up @@ -733,26 +760,23 @@ export async function makeUtxoEngine(
})
if (rbfTx != null) {
rbfTx.blockHeight = -1
await transactionChanged({
walletId: walletInfo.id,
tx: rbfTx,
await dataLayer.saveTransaction({
tx: rbfTx
})
const rbfEdgeTx = await toEdgeTransaction({
dataLayer,
pluginInfo,
emitter,
walletTools,
dataLayer
tx: rbfTx,
walletId: walletInfo.id,
walletTools
})
emitter.emit(EngineEvent.TRANSACTIONS, [
{ isNew: false, transaction: rbfEdgeTx }
])
}
}

const tx = fromEdgeTransaction(edgeTx)
await transactionChanged({
walletId: walletInfo.id,
tx,
pluginInfo,
emitter,
walletTools,
dataLayer
})
await dataLayer.saveTransaction({
tx,
scriptPubkeys: edgeTx.otherParams?.ourScriptPubkeys
Expand Down Expand Up @@ -978,6 +1002,7 @@ export async function makeUtxoEngine(

const tmpEngineProcessor = makeUtxoEngineProcessor({
...config,
dataLayer: tmpDataLayer,
emitter: tmpEmitter,
engineOptions: {
...config.engineOptions,
Expand All @@ -992,7 +1017,7 @@ export async function makeUtxoEngine(
gapLimit: 0
}
},
dataLayer: tmpDataLayer,
seenTxCheckpoint: () => '0',
walletTools: tmpWalletTools,
walletInfo: tmpWalletInfo
})
Expand Down
Loading

0 comments on commit 29b441f

Please sign in to comment.