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

Renames for readability #409

Merged
merged 6 commits into from
Jul 25, 2024
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
16 changes: 8 additions & 8 deletions src/common/plugin/PluginState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EdgeIo, EdgeLog } from 'edge-core-js/types'
import { makeMemlet } from 'memlet'

import { UtxoUserSettings } from '../utxobased/engine/types'
import { UtxoEngineState } from '../utxobased/engine/UtxoEngineState'
import { UtxoEngineProcessor } from '../utxobased/engine/UtxoEngineProcessor'
import {
asServerCache,
ServerCache,
Expand Down Expand Up @@ -42,8 +42,8 @@ export interface PluginStateSettings {
}

export interface PluginState {
addEngine: (engineState: UtxoEngineState) => void
removeEngine: (engineState: UtxoEngineState) => void
addEngine: (engineProcessor: UtxoEngineProcessor) => void
removeEngine: (engineProcessor: UtxoEngineProcessor) => void
dumpData: () => JsonObject
load: () => Promise<PluginState>
serverScoreDown: (uri: string) => void
Expand All @@ -67,7 +67,7 @@ export function makePluginState(settings: PluginStateSettings): PluginState {
log
} = settings

let engines: UtxoEngineState[] = []
let engines: UtxoEngineProcessor[] = []
const memlet = makeMemlet(pluginDisklet)

let serverCache: ServerCache = {
Expand Down Expand Up @@ -142,15 +142,15 @@ export function makePluginState(settings: PluginStateSettings): PluginState {
/**
* Begins notifying the engine of state changes. Used at connection time.
*/
addEngine(engineState: UtxoEngineState): void {
engines.push(engineState)
addEngine(engineProcessor: UtxoEngineProcessor): void {
engines.push(engineProcessor)
},

/**
* Stops notifying the engine of state changes. Used at disconnection time.
*/
removeEngine(engineState: UtxoEngineState): void {
engines = engines.filter(engine => engine !== engineState)
removeEngine(engineProcessor: UtxoEngineProcessor): void {
engines = engines.filter(engine => engine !== engineProcessor)
},

dumpData(): JsonObject {
Expand Down
12 changes: 6 additions & 6 deletions src/common/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from 'edge-core-js/types'
import * as wif from 'wif'

import { asIUTXO, IProcessorTransaction, IUTXO } from '../utxobased/db/types'
import { asUtxoData, TransactionData, UtxoData } from '../utxobased/db/types'
import { UtxoInitOptions } from '../utxobased/engine/types'
import { ScriptTemplates } from '../utxobased/info/scriptTemplates/types'
import { UtxoPicker } from '../utxobased/keymanager/utxopicker'
Expand All @@ -45,12 +45,12 @@ export interface AddressPath {
}

export interface TxOptions {
utxos?: IUTXO[]
utxos?: UtxoData[]
subtractFee?: boolean
CPFP?: string
}
export const asTxOptions = asObject<TxOptions>({
utxos: asOptional(asArray(asIUTXO)),
utxos: asOptional(asArray(asUtxoData)),
subtractFee: asOptional(asBoolean),
CPFP: asOptional(asString)
})
Expand Down Expand Up @@ -148,13 +148,13 @@ export interface EngineInfo {
* Some currencies require an additional blockbook payload
* 'getTransactionSpecific' in order to provide all relevant transaction
* data. If this function is defined, it should merge the unknown `specialTx`
* data from the Blockbook endpoint with the `IProcessorTransaction` object
* data from the Blockbook endpoint with the `TransactionData` object
* which is stored to disk.
**/
txSpecificHandling?: (
tx: IProcessorTransaction,
tx: TransactionData,
specialTx: unknown
) => IProcessorTransaction
) => TransactionData
}

export interface ServerConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import { AddressPath, ChangePath } from '../../plugin/types'
import { makeBaselets } from './Baselets'
import { addressPathToPrefix, TxIdByDate } from './Models/baselet'
import {
IAddress,
IProcessorTransaction,
ITransactionInput,
ITransactionOutput,
IUTXO
AddressData,
TransactionData,
TransactionDataInput,
TransactionDataOutput,
UtxoData
} from './types'

const BASELET_DIR = 'tables'

interface ProcessorConfig {
interface DataLayerConfig {
disklet: Disklet
}

/* Transaction table interfaces */

interface SaveTransactionArgs {
tx: IProcessorTransaction
tx: TransactionData
scriptPubkeys?: string[]
}

Expand All @@ -51,7 +51,13 @@ interface DumpDataReturn {
data: unknown
}

export interface Processor {
/**
* The Data Access Layer for the UTXO-based wallet engine.
*
* It provides all the methods necessary to interact with underlying
* database/storage mechanism.
*/
export interface DataLayer {
clearAll: () => Promise<void>
dumpData: () => Promise<DumpDataReturn[]>

Expand All @@ -64,13 +70,13 @@ export interface Processor {
Used to store UTXOs. Needs to be able to track UTXOs that are already used in
a transaction, but not confirmed yet */

saveUtxo: (utxo: IUTXO) => Promise<void>
saveUtxo: (utxo: UtxoData) => Promise<void>
// remove either all UTXOs if the array is empty, or as selected from an array
// of UTXO ids
removeUtxos: (utxoIds: string[]) => Promise<void>
// fetch either all UTXOs if the array is empty or as selected from an array
// of UTXO ids
fetchUtxos: (args: FetchUtxosArgs) => Promise<Array<IUTXO | undefined>>
fetchUtxos: (args: FetchUtxosArgs) => Promise<Array<UtxoData | undefined>>

/* Transaction processing
**********************
Expand All @@ -87,12 +93,12 @@ export interface Processor {
Used to store transactions. Needs to be updated for confirmation
heights, and detecting used script pubkeys */

saveTransaction: (args: SaveTransactionArgs) => Promise<IProcessorTransaction>
saveTransaction: (args: SaveTransactionArgs) => Promise<TransactionData>
numTransactions: () => number
removeTransaction: (txId: string) => Promise<void>
fetchTransactions: (
args: FetchTransactionArgs
) => Promise<Array<IProcessorTransaction | undefined>>
) => Promise<Array<TransactionData | undefined>>

/* Address processing
*********************
Expand All @@ -105,17 +111,17 @@ export interface Processor {
Used to store script pubkeys / addresses. Needs to be updated for 'used' flag
and path */

saveAddress: (args: IAddress) => Promise<void>
saveAddress: (args: AddressData) => Promise<void>
// used to calculate total number of addresses
numAddressesByFormatPath: (path: ChangePath) => number
// get the last used address index for a specific format
lastUsedIndexByFormatPath: (path: ChangePath) => Promise<number>
fetchAddress: (args: AddressPath | string) => Promise<IAddress | undefined>
fetchAddress: (args: AddressPath | string) => Promise<AddressData | undefined>
}

export async function makeProcessor(
config: ProcessorConfig
): Promise<Processor> {
export async function makeDataLayer(
config: DataLayerConfig
): Promise<DataLayer> {
const disklet = navigateDisklet(config.disklet, BASELET_DIR)
let memlet = makeMemlet(disklet)
let baselets = await makeBaselets({ storage: memlet }).catch(async error => {
Expand All @@ -126,11 +132,11 @@ export async function makeProcessor(
/**
* Calculates the transaction value supplied (negative) or received (positive). In order to calculate
* a value, the `ourIns` and `ourOuts` of the object must be populated with indices.
* @param tx {IProcessorTransaction} A transaction object with `ourIns` and `ourOuts` populated
* @param tx {TransactionData} A transaction object with `ourIns` and `ourOuts` populated
*/
const calculateTxAmount = (tx: IProcessorTransaction): string => {
const calculateTxAmount = (tx: TransactionData): string => {
interface TxIndexMap {
[index: string]: ITransactionInput | ITransactionOutput
[index: string]: TransactionDataInput | TransactionDataOutput
}
let ourAmount = '0'
let txIndexMap: TxIndexMap = {}
Expand All @@ -152,7 +158,7 @@ export async function makeProcessor(
return ourAmount
}

const processor: Processor = {
const dataLayer: DataLayer = {
async clearAll(): Promise<void> {
await memlet.onFlush.next().value
await clearMemletCache()
Expand All @@ -176,7 +182,7 @@ export async function makeProcessor(
)
},

async saveUtxo(utxo: IUTXO): Promise<void> {
async saveUtxo(utxo: UtxoData): Promise<void> {
return await baselets.utxo(async tables => {
const [utxoIds = []] = await tables.utxoIdsByScriptPubkey.query('', [
utxo.scriptPubkey
Expand All @@ -203,7 +209,7 @@ export async function makeProcessor(
// 2. Use the map to remove utxoIds from the utxoIdsByScriptPubkey table
const utxos = (await tables.utxoById.query('', utxoIds)).filter(
utxo => utxo != null
) as IUTXO[]
) as UtxoData[]
const utxoIdsMap: { [scriptPubkey: string]: string[] } = {}
for (const utxo of utxos) {
utxoIdsMap[utxo.scriptPubkey] = [
Expand Down Expand Up @@ -234,7 +240,7 @@ export async function makeProcessor(
})
},

async fetchUtxos(args): Promise<Array<IUTXO | undefined>> {
async fetchUtxos(args): Promise<Array<UtxoData | undefined>> {
const { scriptPubkey, utxoIds = [] } = args
return await baselets.utxo(async tables => {
if (scriptPubkey != null) {
Expand All @@ -261,19 +267,17 @@ export async function makeProcessor(
})
},

async saveTransaction(
args: SaveTransactionArgs
): Promise<IProcessorTransaction> {
async saveTransaction(args: SaveTransactionArgs): Promise<TransactionData> {
const { scriptPubkeys = [], tx } = args
return await baselets.tx(async tables => {
// Check if the transaction already exists
const processorTx = await tables.txById
const transactionData = await tables.txById
.query('', [tx.txid])
.then(transactions => transactions[0])
.catch(_ => undefined)

// Use the existing transaction if it does exist.
const transaction = processorTx ?? tx
const transaction = transactionData ?? tx

// Mark the used inputs with the provided script pubkey
for (const scriptPubkey of scriptPubkeys) {
Expand Down Expand Up @@ -325,7 +329,7 @@ export async function makeProcessor(
await tables.txById.insert('', transaction.txid, transaction)

// Save index entry only for first transaction insert
if (processorTx == null) {
if (transactionData == null) {
await tables.txIdsByDate.insert('', {
txid: tx.txid,
date: tx.date
Expand All @@ -346,10 +350,10 @@ export async function makeProcessor(

async fetchTransactions(
args: FetchTransactionArgs
): Promise<Array<IProcessorTransaction | undefined>> {
): Promise<Array<TransactionData | undefined>> {
const { blockHeightMax, txId, options } = args
let { blockHeight } = args
const txs: Array<IProcessorTransaction | undefined> = []
const txs: Array<TransactionData | undefined> = []
await baselets.tx(async tables => {
// Fetch transactions by id
if (txId != null) {
Expand Down Expand Up @@ -407,7 +411,7 @@ export async function makeProcessor(
return txs
},

async saveAddress(address: IAddress): Promise<void> {
async saveAddress(address: AddressData): Promise<void> {
await baselets.address(async tables => {
// This variable is used to update the scriptPubkeyByPath table.
// The path table must be written after the address table because the
Expand Down Expand Up @@ -564,7 +568,7 @@ export async function makeProcessor(

async fetchAddress(
fetchAddressArg: AddressPath | string
): Promise<IAddress | undefined> {
): Promise<AddressData | undefined> {
return await baselets.address(async tables => {
if (typeof fetchAddressArg === 'string') {
// if it is a string, it is a scriptPubkey
Expand Down Expand Up @@ -594,5 +598,5 @@ export async function makeProcessor(
})
}
}
return processor
return dataLayer
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { EdgeTransaction, JsonObject } from 'edge-core-js/types'
import { PluginInfo } from '../../../plugin/types'
import { UtxoTxOtherParams } from '../../engine/types'
import { UtxoWalletTools } from '../../engine/UtxoWalletTools'
import { Processor } from '../Processor'
import { IProcessorTransaction } from '../types'
import { DataLayer } from '../DataLayer'
import { TransactionData } from '../types'

export const fromEdgeTransaction = (
tx: EdgeTransaction
): IProcessorTransaction => {
export const fromEdgeTransaction = (tx: EdgeTransaction): TransactionData => {
const otherParams = tx.otherParams as UtxoTxOtherParams
if (otherParams == null) throw new Error('Invalid transaction data')
if (otherParams.psbt == null)
Expand Down Expand Up @@ -44,7 +42,7 @@ export const fromEdgeTransaction = (
inputs: inputs,
outputs: outputs,
// We can leave ourIns/ourOuts blank because they'll be updated by the
// processor when receiving the transaction from blockbook.
// DataLayer when receiving the transaction from blockbook.
// We may want to calculate these preemptively, but for now this will work.
ourIns: [],
ourOuts: [],
Expand All @@ -54,21 +52,21 @@ export const fromEdgeTransaction = (

interface ToEdgeTransactionArgs {
walletId: string
tx: IProcessorTransaction
tx: TransactionData
walletTools: UtxoWalletTools
processor: Processor
dataLayer: DataLayer
pluginInfo: PluginInfo
}

export const toEdgeTransaction = async (
args: ToEdgeTransactionArgs
): Promise<EdgeTransaction> => {
const { tx, processor, walletTools, pluginInfo, walletId } = args
const { tx, dataLayer, walletTools, pluginInfo, walletId } = args
const { currencyInfo } = pluginInfo
const ourReceiveAddresses: string[] = []
for (const out of tx.ourOuts) {
const { scriptPubkey } = tx.outputs[parseInt(out)]
const address = await processor.fetchAddress(scriptPubkey)
const address = await dataLayer.fetchAddress(scriptPubkey)

if (address?.path != null) {
const { address: addrStr } = walletTools.scriptPubkeyToAddress({
Expand Down
8 changes: 4 additions & 4 deletions src/common/utxobased/db/Models/baselet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'baselet'

import { ChangePath } from '../../../plugin/types'
import { IAddress, IProcessorTransaction, IUTXO } from '../types'
import { AddressData, TransactionData, UtxoData } from '../types'

export const addressPathToPrefix = (path: ChangePath): string =>
`${path.format}_${path.changeIndex}`
Expand All @@ -19,7 +19,7 @@ export const scriptPubkeyByPathOptions: CountBaseOptions = {
bucketSize: 50
}

export type AddressByScriptPubkeyBaselet = HashBase<IAddress>
export type AddressByScriptPubkeyBaselet = HashBase<AddressData>
export const addressByScriptPubkeyOptions: HashBaseOptions = {
name: 'addressByScriptPubkey',
prefixSize: 4
Expand Down Expand Up @@ -50,7 +50,7 @@ export const txIdsByBlockHeightOptions: RangeBaseOptions<
idKey: 'txid'
}

export type TxByIdBaselet = HashBase<IProcessorTransaction>
export type TxByIdBaselet = HashBase<TransactionData>
export const txByIdOptions: HashBaseOptions = {
name: 'txById',
prefixSize: 2
Expand All @@ -68,7 +68,7 @@ export const txIdsByDateOptions: RangeBaseOptions<'date', 'txid'> = {
idKey: 'txid'
}

export type UtxoByIdBaselet = HashBase<IUTXO>
export type UtxoByIdBaselet = HashBase<UtxoData>
export const utxoByIdOptions: HashBaseOptions = {
name: 'utxoById',
prefixSize: 2
Expand Down
Loading
Loading