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

Add NOWNodes to EngineInfo #396

Merged
merged 5 commits into from
Apr 8, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: New fallback server engine info, starting with NOWNodes blockbook servers

## 2.5.4 (2024-01-26)

- fixed: Check for NaN target totals in `subtractFee` style transactions
Expand Down
2 changes: 1 addition & 1 deletion src/bch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EdgeCorePluginOptions, EdgeCurrencyPlugin } from 'edge-core-js/types'

import { makeCurrencyPlugin } from './common/plugin/makeCurrencyPlugin'
import { makeCurrencyPlugin } from './common/plugin/CurrencyPlugin'
import { info } from './common/utxobased/info/bitcoincash'

const plugin = (options: EdgeCorePluginOptions): EdgeCurrencyPlugin =>
Expand Down
2 changes: 1 addition & 1 deletion src/bsv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EdgeCorePluginOptions, EdgeCurrencyPlugin } from 'edge-core-js/types'

import { makeCurrencyPlugin } from './common/plugin/makeCurrencyPlugin'
import { makeCurrencyPlugin } from './common/plugin/CurrencyPlugin'
import { info } from './common/utxobased/info/bitcoinsv'

const plugin = (options: EdgeCorePluginOptions): EdgeCurrencyPlugin =>
Expand Down
2 changes: 1 addition & 1 deletion src/btc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EdgeCorePluginOptions, EdgeCurrencyPlugin } from 'edge-core-js/types'

import { makeCurrencyPlugin } from './common/plugin/makeCurrencyPlugin'
import { makeCurrencyPlugin } from './common/plugin/CurrencyPlugin'
import { info } from './common/utxobased/info/bitcoin'

const plugin = (options: EdgeCorePluginOptions): EdgeCurrencyPlugin =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import {
EdgeWalletInfo
} from 'edge-core-js/types'

import { makeUtxoEngine } from '../utxobased/engine/makeUtxoEngine'
import { asUtxoUserSettings } from '../utxobased/engine/types'
import { makeCurrencyTools } from './makeCurrencyTools'
import { makeEngineEmitter } from './makeEngineEmitter'
import { makePluginState } from './pluginState'
import {
asUtxoInitOptions,
asUtxoUserSettings
} from '../utxobased/engine/types'
import { makeUtxoEngine } from '../utxobased/engine/UtxoEngine'
import { makeCurrencyTools } from './CurrencyTools'
import { makeEngineEmitter } from './EngineEmitter'
import { makePluginState } from './PluginState'
import { EngineConfig, PluginInfo } from './types'

export function makeCurrencyPlugin(
pluginOptions: EdgeCorePluginOptions,
pluginInfo: PluginInfo
): EdgeCurrencyPlugin {
const { currencyInfo } = pluginInfo
const { io, log, pluginDisklet } = pluginOptions
const { io, log, pluginDisklet, initOptions } = pluginOptions
const currencyTools = makeCurrencyTools(io, pluginInfo)
const { defaultSettings, pluginId, currencyCode } = currencyInfo
const pluginState = makePluginState({
Expand All @@ -43,6 +46,7 @@ export function makeCurrencyPlugin(
pluginInfo,
pluginDisklet,
currencyTools,
initOptions: asUtxoInitOptions(initOptions),
io,
options: {
...pluginOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'edge-core-js/types'
import { EventEmitter } from 'events'

import { SubscribeAddressResponse } from '../utxobased/network/BlockBookAPI'
import { SubscribeAddressResponse } from '../utxobased/network/blockbookApi'

export declare interface EngineEmitter {
emit: ((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EdgeLog } from 'edge-core-js/types'
import { makeMemlet } from 'memlet'

import AwaitLock from '../utxobased/engine/await-lock'
import { EngineEmitter, EngineEvent } from './makeEngineEmitter'
import { EngineEmitter, EngineEvent } from './EngineEmitter'
import { asLocalWalletMetadata, LocalWalletMetadata } from './types'
import { removeItem } from './utils'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import { Disklet } from 'disklet'
import { EdgeIo, EdgeLog } from 'edge-core-js/types'
import { makeMemlet } from 'memlet'

import { UtxoEngineState } from '../utxobased/engine/makeUtxoEngineState'
import { UtxoUserSettings } from '../utxobased/engine/types'
import { UtxoEngineState } from '../utxobased/engine/UtxoEngineState'
import {
asServerCache,
ServerCache,
ServerList,
ServerScores
} from './serverScores'
} from './ServerScores'

// Info server endpoint to getting ServerListInfo data
const serverListInfoUrl = 'https://info1.edge.app/v1/blockBook/'
// The filename for ServerInfoCache data (see serverScores.ts)
// Perhaps this should be in serverScores.ts file, but that'll take some refactoring
const serverListInfoUrl = 'https://info1.edge.app/v1/blockbook/'
// The filename for ServerInfoCache data (see ServerScores.ts)
// Perhaps this should be in ServerScores.ts file, but that'll take some refactoring
const SERVER_CACHE_FILE = 'serverCache.json'

// ServerListInfo data structure from info server and saved to disk
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface PluginState {
clearCache: () => Promise<void>
getLocalServers: (
numServersWanted: number,
includePatterns: string[]
includePatterns?: Array<string | RegExp>
) => string[]
refreshServers: () => Promise<void>
updateServers: (settings: UtxoUserSettings) => Promise<void>
Expand Down Expand Up @@ -209,7 +209,7 @@ export function makePluginState(settings: PluginStateSettings): PluginState {

getLocalServers(
numServersWanted: number,
includePatterns: string[] = []
includePatterns: Array<string | RegExp> = []
): string[] {
return serverScores.getServers(
knownServers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class ServerScores {
getServers(
servers: ServerList,
numServersWanted: number,
includePatterns: string[] = []
includePatterns: Array<string | RegExp> = []
): string[] {
if (servers == null || Object.keys(servers).length === 0) {
return []
Expand Down Expand Up @@ -236,7 +236,14 @@ export class ServerScores {
const filter = (server: ServerInfo): boolean => {
for (const pattern of includePatterns) {
// make sure that the server URL starts with the required pattern
if (server.serverUrl.indexOf(pattern) === 0) return true
if (
typeof pattern === 'string' &&
server.serverUrl.indexOf(pattern) === 0
)
return true
// Or make sure that the server URL matches regex
if (pattern instanceof RegExp && pattern.test(server.serverUrl))
return true
}
return false
}
Expand Down
12 changes: 10 additions & 2 deletions src/common/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import {
import * as wif from 'wif'

import { asIUTXO, IProcessorTransaction, IUTXO } from '../utxobased/db/types'
import { UtxoInitOptions } from '../utxobased/engine/types'
import { ScriptTemplates } from '../utxobased/info/scriptTemplates/types'
import { UtxoPicker } from '../utxobased/keymanager/utxopicker'
import { EngineEmitter } from './makeEngineEmitter'
import { PluginState } from './pluginState'
import { EngineEmitter } from './EngineEmitter'
import { PluginState } from './PluginState'

export type CurrencyFormat = ReturnType<typeof asCurrencyFormat>
export const asCurrencyFormat = asValue('bip32', 'bip44', 'bip49', 'bip84')
Expand Down Expand Up @@ -70,6 +71,7 @@ export interface EngineInfo {
mempoolSpaceFeeInfoServer?: string
defaultFeeInfo: FeeInfo
scriptTemplates?: ScriptTemplates
serverConfigs?: ServerConfig[]
// Codec Cleaners
asBlockbookAddress?: Cleaner<string>
// Coin specific transaction handling
Expand All @@ -79,6 +81,11 @@ export interface EngineInfo {
) => IProcessorTransaction
}

export interface ServerConfig {
type: 'blockbook-nownode'
uris: string[]
}
Comment on lines +84 to +87
Copy link
Contributor

@swansontec swansontec Apr 4, 2024

Choose a reason for hiding this comment

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

This structure seems weird. Why not put fallbackNownodeServers?: string[] straight into the engine info / init info?

I mean, it's always possible to invent your own custom object type using arrays, like type FakeObject<T> = Array<{ key: string, value: T}>, but this sort of thing is rarely a good idea. Just use normal objects.

The ServerConfig type seems to be implementing this anti-pattern, with type as the key and uris as the value. Rather than using string enums and arrays, it would be simpler to just use named properties right on the relevant objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because: It's easier to enumerate nominal types then fields in typescript.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, we discussed this design some more, and it makes sense if the servers need extra data, such as contract addresses or such. In that case, it might be { type: 'foo', servers: [], queryParams: '' }, and so we want the flexibility of the array approach. This will impact the way the info server assembles the core rollup payload, so we might need a follow-up PR over there.


/**
* Coin Info
*/
Expand Down Expand Up @@ -196,6 +203,7 @@ export interface EngineConfig {
pluginDisklet: Disklet
currencyTools: EdgeCurrencyTools
options: EngineOptions
initOptions: UtxoInitOptions
io: EdgeIo
pluginState: PluginState
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/utxobased/db/Models/ProcessorTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import BN from 'bn.js'
import { EdgeTransaction, JsonObject } from 'edge-core-js/types'

import { PluginInfo } from '../../../plugin/types'
import { UTXOPluginWalletTools } from '../../engine/makeUtxoWalletTools'
import { UtxoTxOtherParams } from '../../engine/types'
import { Processor } from '../makeProcessor'
import { UtxoWalletTools } from '../../engine/UtxoWalletTools'
import { Processor } from '../Processor'
import { IProcessorTransaction } from '../types'

export const fromEdgeTransaction = (
Expand Down Expand Up @@ -54,7 +54,7 @@ export const fromEdgeTransaction = (
interface ToEdgeTransactionArgs {
walletId: string
tx: IProcessorTransaction
walletTools: UTXOPluginWalletTools
walletTools: UtxoWalletTools
processor: Processor
pluginInfo: PluginInfo
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { makeMemlet } from 'memlet'

import { unixTime } from '../../../util/unixTime'
import { AddressPath, ChangePath } from '../../plugin/types'
import { makeBaselets } from './makeBaselets'
import { makeBaselets } from './Baselets'
import { addressPathToPrefix, TxIdByDate } from './Models/baselet'
import {
IAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/common/utxobased/db/util/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BIP43PurposeTypeEnum,
ScriptTypeEnum
} from '../../keymanager/keymanager'
import { Processor } from '../makeProcessor'
import { Processor } from '../Processor'
import { IProcessorTransaction, IUTXO } from '../types'

export const utxoFromProcessorTransactionInput = async (
Expand Down
Loading
Loading