@@ -64,7 +64,7 @@ const fetchBlock = async (url: string, block: number): Promise<Message> => {
6464
6565 if ( ! response . ok ) {
6666 if ( response . status === 404 ) {
67- await sleep ( 700 ) ;
67+ await sleep ( 100 ) ;
6868 }
6969
7070 throw new Error ( `status: ${ response . status } ` ) ;
@@ -78,7 +78,6 @@ const fetchBlock = async (url: string, block: number): Promise<Message> => {
7878 ) ;
7979} ;
8080
81- /*
8281const fetchFinal = async ( url : string ) : Promise < Message > => {
8382 return await retry (
8483 async ( ) => {
@@ -98,14 +97,13 @@ const fetchFinal = async (url: string): Promise<Message> => {
9897 { exponential : true , logger : retryLogger , retries } ,
9998 ) ;
10099} ;
101- */
102100
103101export const streamBlock = ( config : BlockStreamConfig ) => {
104102 const url = config . url ?? endpoint ( config . network ) ;
105103 const limit = config . limit ?? 10 ;
106104 let isFetching = false ;
107105 let block = config . start ;
108- const highWaterMark = limit * 2 ;
106+ const highWaterMark = limit * 5 ;
109107
110108 const readable = new Readable ( {
111109 highWaterMark,
@@ -119,31 +117,47 @@ export const streamBlock = (config: BlockStreamConfig) => {
119117 isFetching = true ;
120118
121119 try {
122- /*
123120 const remaining = highWaterMark - readable . readableLength ;
124121
122+ logger . warn ( { fetchingBlock : block , queueSize : readable . readableLength } ) ;
123+
125124 if ( block % 10 === 0 && remaining >= 5 ) {
126- const final = (await fetchFinal(url)).block.header.height;
125+ const finalBlocks = [ ] ;
126+
127+ for ( let i = 0 ; i < 2 ; i ++ ) {
128+ finalBlocks . push ( ( await fetchFinal ( url ) ) . block . header . height ) ;
129+
130+ if ( i === 0 ) {
131+ await sleep ( 100 ) ;
132+ }
133+ }
127134
135+ const final = Math . max ( ...finalBlocks ) ;
128136 const promises : Promise < Message > [ ] = [ ] ;
129137 const concurrency = Math . min ( limit , final - block , remaining ) ;
130138
131- for (let i = 0; i < concurrency; i++) {
132- promises.push(fetchBlock(url, block + i));
133- }
139+ logger . warn ( { concurrency, finalBlock : final } ) ;
134140
135- const results = await Promise.all(promises);
141+ if ( concurrency > 0 ) {
142+ for ( let i = 0 ; i < concurrency ; i ++ ) {
143+ promises . push ( fetchBlock ( url , block + i ) ) ;
144+ }
145+
146+ const results = await Promise . all ( promises ) ;
147+
148+ for ( const result of results ) {
149+ if ( result ) {
150+ const message : Message = camelCaseKeys ( result ) ;
136151
137- for (const result of results) {
138- if (result && !readable.push(camelCaseKeys(result))) {
139- return;
152+ if ( ! readable . push ( message ) ) {
153+ return ;
154+ }
155+
156+ block = message . block . header . height + 1 ;
157+ }
140158 }
141159 }
142-
143- block += concurrency;
144- return;
145160 }
146- */
147161
148162 const result = await fetchBlock ( url , block ) ;
149163
@@ -164,7 +178,7 @@ export const streamBlock = (config: BlockStreamConfig) => {
164178 }
165179 } ;
166180
167- const interval = setInterval ( fetchBlocks , 500 ) ;
181+ const interval = setInterval ( fetchBlocks , 100 ) ;
168182
169183 readable . on ( 'close' , ( ) => clearInterval ( interval ) ) ;
170184
0 commit comments