import { ShortChannelId } from "@node-lightning/core" ; import { ILogger } from "@node-lightning/logger" ; import { expect } from "chai" ; import { GossipError , GossipErrorCode } from "../../lib/gossip/GossipError" ; import { GossipQueriesSync , GossipQueriesSyncState } from "../../lib/gossip/GossipQueriesSync" ; import { ReplyChannelRangeMessage } from "../../lib/messages/ReplyChannelRangeMessage" ; import { ReplyShortChannelIdsEndMessage } from "../../lib/messages/ReplyShortChannelIdsEndMessage" ; import { createFakeLogger , createFakePeer , wait } from "../_test-utils" ; describe ( "GossipQueriesSync" , ( ) => { let chainHash ; let sut ; let peer ; let logger ; beforeEach ( ( ) => { chainHash = Buffer . alloc ( 32 ) ; peer = createFakePeer ( ) ; logger = createFakeLogger ( ) ; sut = new GossipQueriesSync ( chainHash , peer , logger ) ; } ) ; it ( "integration - success" , async ( ) => { ( sut as any ) . _syncWatcher . completeAfterMs = 50 ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Idle ) ; const promise = sut . queryRange ( ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannelRange ) ; const replyRangeMsg = new ReplyChannelRangeMessage ( ) ; replyRangeMsg . fullInformation = true ; replyRangeMsg . firstBlocknum = 0 ; replyRangeMsg . numberOfBlocks = 0xffffffff ; replyRangeMsg . shortChannelIds . push ( new ShortChannelId ( 1 , 1 , 1 ) ) ; sut . handleWireMessage ( replyRangeMsg ) ; await wait ( 0 ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannels ) ; const replyChannelsMsg = new ReplyShortChannelIdsEndMessage ( ) ; replyChannelsMsg . complete = true ; sut . handleWireMessage ( replyChannelsMsg ) ; await wait ( 0 ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingMessages ) ; await wait ( 100 ) ; await promise ; } ) ; it ( "integration - rejects on range failure" , async ( ) => { expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Idle ) ; const promise = sut . queryRange ( ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannelRange ) ; const replyRangeMsg = new ReplyChannelRangeMessage ( ) ; replyRangeMsg . fullInformation = false ; replyRangeMsg . firstBlocknum = 0 ; replyRangeMsg . numberOfBlocks = 0xffffffff ; sut . handleWireMessage ( replyRangeMsg ) ; return promise . catch ( ( err ) => { expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Failed ) ; expect ( err . code ) . to . equal ( GossipErrorCode . ReplyChannelRangeNoInformation ) ; } ) ; } ) ; it ( "integration - rejects on channels failure" , async ( ) => { expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Idle ) ; const promise = sut . queryRange ( ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannelRange ) ; const replyRangeMsg = new ReplyChannelRangeMessage ( ) ; replyRangeMsg . fullInformation = true ; replyRangeMsg . firstBlocknum = 0 ; replyRangeMsg . numberOfBlocks = 0xffffffff ; replyRangeMsg . shortChannelIds . push ( new ShortChannelId ( 1 , 1 , 1 ) ) ; sut . handleWireMessage ( replyRangeMsg ) ; await wait ( 0 ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannels ) ; const replyChannelsMsg = new ReplyShortChannelIdsEndMessage ( ) ; replyChannelsMsg . complete = false ; sut . handleWireMessage ( replyChannelsMsg ) ; return promise . catch ( ( err ) => { expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Failed ) ; expect ( err . code ) . to . equal ( GossipErrorCode . ReplyChannelsNoInfo ) ; } ) ; } ) ; it ( "integration - large reply" , async ( ) => { ( sut as any ) . _syncWatcher . completeAfterMs = 50 ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . Idle ) ; const promise = sut . queryRange ( ) ; expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannelRange ) ; const messages = 12 ; for ( let i = 0 ; i < messages ; i ++ ) { const scids = [ ] ; for ( let j = 0 ; j < 8000 ; j ++ ) { scids . push ( new ShortChannelId ( i * 8000 + j , 0 , 0 ) ) ; } const replyRangeMsg = new ReplyChannelRangeMessage ( ) ; replyRangeMsg . fullInformation = i === messages - 1 ; replyRangeMsg . firstBlocknum = i * 8000 ; replyRangeMsg . numberOfBlocks = 1 ; replyRangeMsg . shortChannelIds = scids ; sut . handleWireMessage ( replyRangeMsg ) ; await wait ( 0 ) ; } expect ( sut . state ) . to . equal ( GossipQueriesSyncState . AwaitingChannels ) ; while ( sut . state !== GossipQueriesSyncState . AwaitingMessages ) { const replyChannelsMsg = new ReplyShortChannelIdsEndMessage ( ) ; replyChannelsMsg . complete = true ; sut . handleWireMessage ( replyChannelsMsg ) ; await wait ( 0 ) ; } await wait ( 100 ) ; await promise ; } ) ; } ) ;