All files / lib/messages QueryChannelRangeMessage.ts

97.3% Statements 36/37
80% Branches 8/10
100% Functions 4/4
97.14% Lines 34/35

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 891x 1x 1x 1x 1x     1x   3x 3x   3x 3x 3x 3x     3x 2x   2x 2x 2x 2x 2x             3x           37x                                                             3x 3x 3x 3x 3x     3x 2x 2x 2x 2x 2x 2x     3x      
import { BufferReader, BufferWriter } from "@node-lightning/bufio";
import { BitField } from "@node-lightning/core";
import { QueryChannelRangeFlags } from "../flags/QueryChannelRangeFlags";
import { MessageType } from "../MessageType";
import { readTlvs } from "../serialize/readTlvs";
import { IWireMessage } from "./IWireMessage";
 
export class QueryChannelRangeMessage implements IWireMessage {
    public static deserialize(payload: Buffer): QueryChannelRangeMessage {
        const instance = new QueryChannelRangeMessage();
        const reader = new BufferReader(payload);
 
        reader.readUInt16BE(); // read type bytes
        instance.chainHash = reader.readBytes(32);
        instance.firstBlocknum = reader.readUInt32BE();
        instance.numberOfBlocks = reader.readUInt32BE();
 
        // Parse any TLVs that might exist
        readTlvs(reader, (type: bigint, valueReader: BufferReader) => {
            switch (type) {
                case BigInt(1): {
                    const options = valueReader.readBigSize();
                    const bitfield = new BitField<QueryChannelRangeFlags>(options);
                    instance.timestamps = bitfield.isSet(QueryChannelRangeFlags.timestamps);
                    instance.checksums = bitfield.isSet(QueryChannelRangeFlags.checksums);
                    return true;
                }
                default:
                    return false;
            }
        });
 
        return instance;
    }
 
    /**
     * Type 263
     */
    public type: MessageType = MessageType.QueryChannelRange;
 
    /**
     * 32-byte hash that uniquely identifies the chain that the reply
     * should refer to
     */
    public chainHash: Buffer;
 
    /**
     * The first block it wants to know channels for
     */
    public firstBlocknum: number;
 
    /**
     * Number of blocks to return, must be 1 or greater
     */
    public numberOfBlocks: number;
 
    /**
     * When gossip_queries_ex is enabled, this asks the remote node to include
     * the timestamps of update messages.
     */
    public timestamps: boolean;
 
    /**
     * When gossip_queries_ex is enabled, this asks the remote node to include
     * checksums of update messages.
     */
    public checksums: boolean;
 
    public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeUInt16BE(MessageType.QueryChannelRange);
        writer.writeBytes(this.chainHash);
        writer.writeUInt32BE(this.firstBlocknum);
        writer.writeUInt32BE(this.numberOfBlocks);
 
        // write the options TLV
        if (this.timestamps || this.checksums) {
            const bitfield = new BitField<QueryChannelRangeFlags>();
            if (this.timestamps) bitfield.set(QueryChannelRangeFlags.timestamps);
            Eif (this.checksums) bitfield.set(QueryChannelRangeFlags.checksums);
            writer.writeBigSize(1);
            writer.writeBigSize(BufferReader.bigSizeBytes(bitfield.value));
            writer.writeBigSize(bitfield.value);
        }
 
        return writer.toBuffer();
    }
}