All files / src/monitor/tasks TaskNewHeader.ts

16% Statements 4/25
0% Branches 0/12
0% Functions 0/4
16% Lines 4/25

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    57x 57x     57x 57x                                                                          
import { sdk } from '../../index.client';
import { Monitor } from '../Monitor';
import { TaskCheckForProofs } from './TaskCheckForProofs';
import { WalletMonitorTask } from './WalletMonitorTask';
 
 
export class TaskNewHeader extends WalletMonitorTask {
    static taskName = 'NewHeader';
    header?: sdk.BlockHeader
 
    constructor(monitor: Monitor, public triggerMsecs = 1 * monitor.oneMinute) {
        super(monitor, TaskNewHeader.taskName);
    }
 
    async getHeader() : Promise<sdk.BlockHeader> {
        return await this.monitor.chaintracks.findChainTipHeader()
    }
 
    trigger(nowMsecsSinceEpoch: number): { run: boolean; } {
        const run = true
        return { run };
    }
 
    async runTask(): Promise<string> {
        let log = ''
        const oldHeader = this.header
        this.header = await this.getHeader()
        let isNew = true
        if (!oldHeader) {
            log = `first header: ${this.header.height} ${this.header.hash}`
        } else if (oldHeader.height < this.header.height) {
            const skip = this.header.height - oldHeader.height - 1
            const skipped = skip > 0 ? ` SKIPPED ${skip}` : ''
            log = `new header: ${this.header.height} ${this.header.hash}${skipped}`
        } else if (oldHeader.height === this.header.height && oldHeader.hash != this.header.hash) {
            log = `reorg header: ${this.header.height} ${this.header.hash}`
        } else {
            isNew = false
        }
        Iif (isNew)
            TaskCheckForProofs.checkNow = true
        return log
    }
}