@@ -38,26 +38,14 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
3838 return blockNumber !== undefined ;
3939 }
4040
41- public async findCommitBuffers ( start : number , end : number ) : Promise < Buffer [ ] > {
42- const blockNumbers : number [ ] = [ ] ;
41+ public async findCommitBuffers ( start : number , end : number , maxBytes : number ) : Promise < Buffer [ ] > {
42+ const buffers : Buffer [ ] = [ ] ;
4343
44- for ( const blockNumber of this . #range ( start , end ) ) {
45- blockNumbers . push ( blockNumber ) ;
44+ for await ( const commit of this . readCommits ( start , end , maxBytes ) ) {
45+ buffers . push ( Buffer . from ( commit . serialized , "hex" ) ) ;
4646 }
4747
48- const buffers = await Promise . all (
49- blockNumbers . map ( async ( blockNumber : number ) => {
50- const commitStorage = await this . #readCommitStorage( blockNumber ) ;
51- if ( ! commitStorage ) {
52- return ;
53- }
54-
55- const commit = await this . commitFactory . fromStorage ( commitStorage ) ;
56- return Buffer . from ( commit . serialized , "hex" ) ;
57- } ) ,
58- ) ;
59-
60- return buffers . filter ( ( commit ) => ! ! commit ) ;
48+ return buffers ;
6149 }
6250
6351 public async getBlock ( blockNumber : number ) : Promise < Contracts . Crypto . Block | undefined > {
@@ -106,12 +94,13 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
10694 }
10795
10896 public async findBlocks ( start : number , end : number ) : Promise < Contracts . Crypto . Block [ ] > {
109- const commitBuffers = await this . findCommitBuffers ( start , end ) ;
97+ const blocks : Contracts . Crypto . Block [ ] = [ ] ;
98+
99+ for await ( const commit of this . readCommits ( start , end , Number . MAX_SAFE_INTEGER ) ) {
100+ blocks . push ( commit . block ) ;
101+ }
110102
111- return await this . #map(
112- commitBuffers ,
113- async ( buffer : Buffer ) => ( await this . commitFactory . fromBytes ( buffer ) ) . block ,
114- ) ;
103+ return blocks ;
115104 }
116105
117106 public async getTransactionByHash ( transactionHash : string ) : Promise < Contracts . Crypto . BlockTransaction | undefined > {
@@ -143,16 +132,29 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
143132 return this . #readTransaction( `${ blockNumber } -${ index } ` ) ;
144133 }
145134
146- public async * readCommits ( start : number , end : number ) : AsyncGenerator < Contracts . Crypto . Commit > {
147- for ( let blockNumber = start ; blockNumber <= end ; blockNumber ++ ) {
148- const data = await this . #readCommitStorage ( blockNumber ) ;
135+ public async * readCommits ( start : number , end : number , maxBytes : number ) : AsyncGenerator < Contracts . Crypto . Commit > {
136+ let from = Math . max ( 0 , start ) ;
137+ let remainingBytes = maxBytes ;
149138
150- if ( ! data ) {
139+ while ( from <= end ) {
140+ const commitsData = await this . storage . getCommitsByBlockRange ( from , end , remainingBytes ) ;
141+ if ( commitsData . length === 0 ) {
151142 return ;
152143 }
153144
154- const commit = await this . commitFactory . fromStorage ( data ) ;
155- yield commit ;
145+ let lastBlockNumber = from ;
146+ for ( const data of commitsData ) {
147+ const commit = await this . commitFactory . fromStorage ( data ) ;
148+ lastBlockNumber = commit . block . number ;
149+ yield commit ;
150+
151+ remainingBytes -= commit . serialized . length / 2 ;
152+ if ( remainingBytes <= 0 ) {
153+ return ;
154+ }
155+ }
156+
157+ from = lastBlockNumber + 1 ;
156158 }
157159 }
158160
@@ -201,19 +203,4 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
201203
202204 return this . transactionFactory . fromStorage ( { ...transactionStorageData , blockHash : blockHeaderData . hash } ) ;
203205 }
204-
205- async #map< T , U > ( data : U [ ] , callback : ( ...arguments_ : U [ ] ) => Promise < T > ) : Promise < T [ ] > {
206- const result : T [ ] = [ ] ;
207- for ( const [ index , datum ] of data . entries ( ) ) {
208- result [ index ] = await callback ( datum ) ;
209- }
210-
211- return result ;
212- }
213-
214- * #range( start : number , end : number ) : Generator < number > {
215- for ( let index = start ; index <= end ; index ++ ) {
216- yield index ;
217- }
218- }
219206}
0 commit comments