Skip to content

Commit

Permalink
ALL-4863: avoid copying, inherit common rpc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rostislavjadavan committed Feb 20, 2024
1 parent 0c825a8 commit 0f957f0
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 212 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
coverage
*.log
.env

jest-junit.xml
40 changes: 2 additions & 38 deletions src/dto/rpc/DogeRpcSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,10 @@

import { JsonRpcResponse } from '../JsonRpcResponse.dto'
import { AbstractRpcInterface } from './AbstractJsonRpcInterface'
import { UtxoBasedCommonRpcInterface } from './UtxoBasedRpcSuite'

export interface DogeRpcSuite extends DogeRpcInterface, AbstractRpcInterface {}

export interface DogeRpcInterface {
// blockchain methods
getBestBlockHash(): Promise<JsonRpcResponse<string>>
export interface DogeRpcInterface extends UtxoBasedCommonRpcInterface{
getBlock(hashOrHeight: string, verbose?: boolean): Promise<JsonRpcResponse<any>>
getBlockChainInfo(): Promise<JsonRpcResponse<any>>
getBlockCount(): Promise<JsonRpcResponse<number>>
getBlockHash(height: number): Promise<JsonRpcResponse<string>>
getBlockHeader(hash: string, verbose?: boolean): Promise<JsonRpcResponse<any>>
getBlockStats(hash: string): Promise<JsonRpcResponse<any>>
getChainTips(): Promise<JsonRpcResponse<any>>
getDifficulty(): Promise<JsonRpcResponse<number>>
getMempoolAncestors(txId: string, verbose?: boolean): Promise<JsonRpcResponse<any>>
getMempoolDescendants(txId: string, verbose?: boolean): Promise<JsonRpcResponse<any>>
getMempoolEntry(txId: string): Promise<JsonRpcResponse<any>>
getMempoolInfo(): Promise<JsonRpcResponse<any>>
getRawMemPool(verbose?: boolean): Promise<JsonRpcResponse<any>>
getTxOut(txId: string, index: number, includeMempool?: boolean): Promise<JsonRpcResponse<any>>
getTxOutProof(txIds: string[], blockhash?: string): Promise<JsonRpcResponse<any>>
verifyTxOutProof(proof: string): Promise<JsonRpcResponse<any>>

// raw transactions methods
createRawTransaction(
inputs: any[],
outputs: any,
locktime?: number,
replaceable?: boolean,
): Promise<JsonRpcResponse<string>>
decodeRawTransaction(hexstring: string): Promise<JsonRpcResponse<any>>
decodeScript(hexstring: string): Promise<JsonRpcResponse<any>>
getRawTransaction(txId: string, verbose?: boolean): Promise<JsonRpcResponse<any>>
sendRawTransaction(hexstring: string): Promise<JsonRpcResponse<string>>

// utility methods
estimateSmartFee(
blocks: number,
estimateMode?: 'UNSET' | 'ECONOMICAL' | 'CONSERVATIVE',
): Promise<JsonRpcResponse<any>>
validateAddress(address: string): Promise<JsonRpcResponse<any>>
verifyMessage(address: string, signature: string, message: string): Promise<JsonRpcResponse<boolean>>
}
7 changes: 5 additions & 2 deletions src/dto/rpc/UtxoBasedRpcSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { AbstractRpcInterface } from './AbstractJsonRpcInterface'

export interface UtxoBasedRpcSuite extends UtxoBasedRpcInterface, AbstractRpcInterface {}

export interface UtxoBasedRpcInterface {
export interface UtxoBasedCommonRpcInterface {
// blockchain methods
getBestBlockHash(): Promise<JsonRpcResponse<string>>
getBlock(hashOrHeight: string, verbose?: 0 | 1 | 2): Promise<JsonRpcResponse<any>>
getBlockChainInfo(): Promise<JsonRpcResponse<any>>
getBlockCount(): Promise<JsonRpcResponse<number>>
getBlockHash(height: number): Promise<JsonRpcResponse<string>>
Expand Down Expand Up @@ -46,6 +45,10 @@ export interface UtxoBasedRpcInterface {
verifyMessage(address: string, signature: string, message: string): Promise<JsonRpcResponse<boolean>>
}

export interface UtxoBasedRpcInterface extends UtxoBasedCommonRpcInterface{
getBlock(hashOrHeight: string, verbose?: 0 | 1 | 2): Promise<JsonRpcResponse<any>>
}

export interface UtxoBasedRpcInterfaceEstimateFee extends UtxoBasedRpcInterface {
estimateFee(): Promise<JsonRpcResponse<any>>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { DogeRpcInterface, JsonRpcResponse } from '../../../dto'
import { JsonRpcResponse, UtxoBasedCommonRpcInterface } from '../../../dto'

export abstract class AbstractDogeRpc implements DogeRpcInterface {
export abstract class AbstractCommonUtxoRpc implements UtxoBasedCommonRpcInterface {
protected abstract rpcCall<T>(method: string, params?: unknown[]): Promise<T>

async createRawTransaction(
Expand Down Expand Up @@ -40,10 +40,6 @@ export abstract class AbstractDogeRpc implements DogeRpcInterface {
return this.rpcCall<JsonRpcResponse<string>>('getbestblockhash')
}

async getBlock(hashOrHeight: string, verbose = true): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblock', [hashOrHeight, verbose])
}

async getBlockChainInfo(): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblockchaininfo')
}
Expand Down
123 changes: 2 additions & 121 deletions src/service/rpc/utxo/AbstractUtxoRpc.ts
Original file line number Diff line number Diff line change
@@ -1,130 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { JsonRpcResponse, UtxoBasedRpcInterface } from '../../../dto'
import { AbstractCommonUtxoRpc } from './AbstractCommonUtxoRpc'

export abstract class AbstractUtxoRpc implements UtxoBasedRpcInterface {
export abstract class AbstractUtxoRpc extends AbstractCommonUtxoRpc implements UtxoBasedRpcInterface {
protected abstract rpcCall<T>(method: string, params?: unknown[]): Promise<T>

async createRawTransaction(
inputs: any[],
outputs: any,
locktime: number,
replaceable: boolean,
): Promise<JsonRpcResponse<string>> {
const params: unknown[] = [inputs, outputs]
if (locktime) {
params.push(locktime)
}
if (replaceable) {
params.push(replaceable)
}
return this.rpcCall<JsonRpcResponse<string>>('createrawtransaction', params)
}

async decodeRawTransaction(hexstring: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('decoderawtransaction', [hexstring])
}

async decodeScript(hexstring: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('decodescript', [hexstring])
}

async estimateSmartFee(blocks: number, estimateMode?: string): Promise<JsonRpcResponse<any>> {
const params: unknown[] = [blocks]
if (estimateMode) {
params.push(estimateMode)
}
return this.rpcCall<JsonRpcResponse<any>>('estimatesmartfee', params)
}

async getBestBlockHash(): Promise<JsonRpcResponse<string>> {
return this.rpcCall<JsonRpcResponse<string>>('getbestblockhash')
}

async getBlock(hashOrHeight: string, verbose: 0 | 1 | 2 = 1): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblock', [hashOrHeight, verbose])
}

async getBlockChainInfo(): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblockchaininfo')
}

async getBlockCount(): Promise<JsonRpcResponse<number>> {
return this.rpcCall<JsonRpcResponse<number>>('getblockcount')
}

async getBlockHash(height: number): Promise<JsonRpcResponse<string>> {
return this.rpcCall<JsonRpcResponse<string>>('getblockhash', [height])
}

async getBlockHeader(hash: string, verbose = true): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblockheader', [hash, verbose])
}

async getBlockStats(hash: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblockstats', [hash])
}

async getChainTips(): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getchaintips')
}

async getDifficulty(): Promise<JsonRpcResponse<number>> {
return this.rpcCall<JsonRpcResponse<number>>('getdifficulty')
}

async getMempoolAncestors(txId: string, verbose = false): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getmempoolancestors', [txId, verbose])
}

async getMempoolDescendants(txId: string, verbose = false): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getmempooldescendants', [txId, verbose])
}

async getMempoolEntry(txId: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getmempoolentry', [txId])
}

async getMempoolInfo(): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getmempoolinfo')
}

async getRawMemPool(verbose = false): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getrawmempool', [verbose])
}

async getRawTransaction(txId: string, verbose = false): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getrawtransaction', [txId, verbose])
}

async getTxOut(txId: string, index: number, includeMempool = true): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('gettxout', [txId, index, includeMempool])
}

async getTxOutProof(txIds: string[], blockhash?: string): Promise<JsonRpcResponse<any>> {
const params: unknown[] = [txIds]
if (blockhash) {
params.push(blockhash)
}
return this.rpcCall<JsonRpcResponse<any>>('gettxoutproof', params)
}

async sendRawTransaction(hexstring: string): Promise<JsonRpcResponse<string>> {
return this.rpcCall<JsonRpcResponse<string>>('sendrawtransaction', [hexstring])
}

async validateAddress(address: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('validateaddress', [address])
}

async verifyMessage(
address: string,
signature: string,
message: string,
): Promise<JsonRpcResponse<boolean>> {
return this.rpcCall<JsonRpcResponse<boolean>>('verifymessage', [address, signature, message])
}

async verifyTxOutProof(proof: string): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('verifytxoutproof', [proof])
}
}
8 changes: 6 additions & 2 deletions src/service/rpc/utxo/DogeLoadBalancedRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { Container, Service } from 'typedi'
import { JsonRpcCall, JsonRpcResponse, DogeRpcSuite } from '../../../dto'
import { Utils } from '../../../util'
import { AbstractDogeRpc } from './AbstractDogeRpc'
// Need to import like this to keep browser working
import { LoadBalancer } from '../generic/LoadBalancer'
import { AbstractCommonUtxoRpc } from './AbstractCommonUtxoRpc'


@Service({
Expand All @@ -13,7 +13,7 @@ import { LoadBalancer } from '../generic/LoadBalancer'
},
transient: true,
})
export class DogeLoadBalancedRpc extends AbstractDogeRpc implements DogeRpcSuite {
export class DogeLoadBalancedRpc extends AbstractCommonUtxoRpc implements DogeRpcSuite {
protected readonly loadBalancer: LoadBalancer

constructor(id: string) {
Expand Down Expand Up @@ -41,4 +41,8 @@ export class DogeLoadBalancedRpc extends AbstractDogeRpc implements DogeRpcSuite
getRpcNodeUrl(): string {
return this.loadBalancer.getActiveNormalUrlWithFallback().url
}

async getBlock(hashOrHeight: string, verbose = true): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('getblock', [hashOrHeight, verbose])
}
}
42 changes: 0 additions & 42 deletions src/service/rpc/utxo/DogeRpc.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/service/rpc/utxo/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './AbstractUtxoRpc'
export * from './UtxoRpc'
export * from './DogeRpc'

0 comments on commit 0f957f0

Please sign in to comment.