@@ -16,6 +16,12 @@ export class Service implements Contracts.TransactionPool.Service {
1616 @inject ( Identifiers . State . Store )
1717 private readonly stateStore ! : Contracts . State . Store ;
1818
19+ @inject ( Identifiers . TransactionPool . Broadcaster )
20+ private readonly broadcaster ! : Contracts . TransactionPool . Broadcaster ;
21+
22+ @inject ( Identifiers . Cryptography . Configuration )
23+ private readonly cryptoConfiguration ! : Contracts . Crypto . Configuration ;
24+
1925 @inject ( Identifiers . TransactionPool . Storage )
2026 private readonly storage ! : Contracts . TransactionPool . Storage ;
2127
@@ -55,7 +61,7 @@ export class Service implements Contracts.TransactionPool.Service {
5561 return this . mempool . getSize ( ) ;
5662 }
5763
58- public async commit ( sendersAddresses : string [ ] ) : Promise < void > {
64+ public async commit ( sendersAddresses : string [ ] , consumedGas : number ) : Promise < void > {
5965 await this . #lock. runExclusive ( async ( ) => {
6066 if ( this . #disposed) {
6167 return ;
@@ -70,6 +76,8 @@ export class Service implements Contracts.TransactionPool.Service {
7076 }
7177
7278 await this . #cleanUp( ) ;
79+
80+ await this . #rebroadcastStorageTransactions( consumedGas ) ;
7381 } ) ;
7482 }
7583
@@ -242,4 +250,35 @@ export class Service implements Contracts.TransactionPool.Service {
242250
243251 await this . mempool . addTransaction ( transaction ) ;
244252 }
253+
254+ async #rebroadcastStorageTransactions( consumedGas : number ) : Promise < void > {
255+ const blockNumber = this . stateStore . getBlockNumber ( ) ;
256+ const milestones = this . cryptoConfiguration . getMilestone ( blockNumber ) ;
257+
258+ const threshold = this . pluginConfiguration . getRequired < number > (
259+ "maxBlockGasUtilizationTransactionRebroadcastThreshold" ,
260+ ) ;
261+
262+ // If block is not full rebroadcast local transactions.
263+ if ( consumedGas > milestones . block . maxGasLimit * ( threshold / 100 ) ) {
264+ return ;
265+ }
266+
267+ const limit = this . pluginConfiguration . getRequired < number > ( "maxTransactionsPerRequest" ) ;
268+ const broadcastTransactions : Contracts . Crypto . Transaction [ ] = [ ] ;
269+
270+ // Get old transactions up to current block number.
271+ for ( const { serialized } of this . storage . getOldTransactions ( blockNumber , limit ) ) {
272+ const transaction = await this . transactionFactory . fromBytes ( serialized ) ;
273+ broadcastTransactions . push ( transaction ) ;
274+ }
275+
276+ if ( broadcastTransactions . length > 0 ) {
277+ this . logger . info ( `Rebroadcasting ${ broadcastTransactions . length } transaction(s) from storage` ) ;
278+
279+ this . broadcaster
280+ . broadcastTransactions ( broadcastTransactions )
281+ . catch ( ( error ) => this . logger . error ( error . stack ) ) ;
282+ }
283+ }
245284}
0 commit comments