All files / src/services/chaintracker/chaintracks ChaintracksServiceClient.ts

44.44% Statements 28/63
66.66% Branches 8/12
33.33% Functions 9/27
45.28% Lines 24/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133  57x                                     57x   75x     75x         75x 75x       2x                     42x 42x 42x 42x 42x 42x 41x   1x       1x 1x   1x       6x 6x   6x                                                                                       6x               29x           7x            
/* eslint-disable @typescript-eslint/no-unused-vars */
import { asString, sdk } from '../../../index.client'
 
import { BaseBlockHeader, BlockHeader, isBaseBlockHeader } from './BlockHeaderApi'
 
interface FetchStatus<T> {
    status: 'success' | 'error',
    code?: string,
    description?: string,
    value?: T
}
 
export interface ChaintracksServiceClientOptions {
    useAuthrite: boolean
}
 
/**
 * Connects to a ChaintracksService to implement 'ChaintracksClientApi'
 *
 */
export class ChaintracksServiceClient {
    static createChaintracksServiceClientOptions(): ChaintracksServiceClientOptions {
        const options: ChaintracksServiceClientOptions = {
            useAuthrite: false
        }
        return options
    }
 
    options: ChaintracksServiceClientOptions
 
    constructor(public chain: sdk.Chain, public serviceUrl: string, options?: ChaintracksServiceClientOptions) {
        this.options = options || ChaintracksServiceClient.createChaintracksServiceClientOptions()
    }
 
    async currentHeight(): Promise<number> {
        return await this.getPresentHeight()
    }
 
    async isValidRootForHeight(root: string, height: number): Promise<boolean> {
        const r = await this.findHeaderForHeight(height)
        Iif (!r) return false
        const isValid = root === asString(r.merkleRoot)
        return isValid
    }
 
    async getJsonOrUndefined<T>(path: string): Promise<T | undefined> {
        let e: Error | undefined = undefined
        for (let retry = 0; retry < 3; retry++) {
            try {
                const r = await fetch(`${this.serviceUrl}${path}`)
                const v = <FetchStatus<T>>await r.json()
                if (v.status === 'success')
                    return v.value
                else
                    e = new Error(JSON.stringify(v))
            } catch (eu: unknown) {
                e = eu as Error
            }
            if (e && e.name !== 'ECONNRESET')
                break
        }
        if (e) throw e
    }
 
    async getJson<T>(path: string): Promise<T> {
        const r = await this.getJsonOrUndefined<T>(path)
        Iif (r === undefined)
            throw new Error('Value was undefined. Requested object may not exist.')
        return r
    }
 
    async postJsonVoid<T>(path: string, params: T): Promise<void> {
        const headers = {}
        headers['Content-Type'] = 'application/json'
        const r = await fetch(`${this.serviceUrl}${path}`, {
            body: JSON.stringify(params),
            method: 'POST',
            headers,
            //cache: 'no-cache',
        })
        try {
            const s = <FetchStatus<void>>await r.json()
            Iif (s.status === 'success')
                return
            throw new Error(JSON.stringify(s))
        } catch (e) {
            console.log(`Exception: ${JSON.stringify(e)}`)
            throw new Error(JSON.stringify(e))
        }
    }
 
    //
    // HTTP API FUNCTIONS
    //
 
    async addHeader(header: BaseBlockHeader): Promise<void> {
        const r = await this.postJsonVoid('/addHeaderHex', header)
        Iif (typeof r === 'string')
            throw new Error(r)
    }
 
    async startListening(): Promise<void> {
        return await this.getJsonOrUndefined('/startListening')
    }
    async listening(): Promise<void> { return await this.getJsonOrUndefined('/listening') }
    async getChain(): Promise<sdk.Chain> {
        return this.chain
        //return await this.getJson('/getChain')
    }
 
    async isListening(): Promise<boolean> { return await this.getJson('/isListening') }
    async isSynchronized(): Promise<boolean> { return await this.getJson('/isSynchronized') }
    async getPresentHeight(): Promise<number> { return await this.getJson('/getPresentHeight') }
    async findChainTipHeader(): Promise<BlockHeader> { return await this.getJson('/findChainTipHeaderHex') }
    async findChainTipHashHex(): Promise<string> { return await this.getJson('/findChainTipHashHex') }
 
    async getHeaders(height: number, count: number): Promise<string> {
        return await this.getJson(`/getHeaders?height=${height}&count=${count}`)
    }
    async findHeaderForHeight(height: number): Promise<BlockHeader | undefined> {
        return await this.getJsonOrUndefined(`/findHeaderHexForHeight?height=${height}`)
    }
    async findChainWorkForBlockHash(hash: string): Promise<string | undefined> {
        return await this.getJsonOrUndefined(`/findChainWorkHexForBlockHash?hash=${asString(hash)}`)
    }
    async findHeaderForBlockHash(hash: string): Promise<BlockHeader | undefined> {
        return await this.getJsonOrUndefined(`/findHeaderHexForBlockHash?hash=${asString(hash)}`)
    }
    async findHeaderForMerkleRoot(merkleRoot: string, height?: number): Promise<BlockHeader | undefined> {
        return await this.getJsonOrUndefined(`/findHeaderHexForMerkleRoot?root=${asString(merkleRoot)}&height=${height}`)
    }
}