-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #759 from wavesplatform/node-438-utx-sync
NODE-438: utx cache to move to network message handler
- Loading branch information
Showing
12 changed files
with
91 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 22 additions & 20 deletions
42
src/main/scala/com/wavesplatform/network/UtxPoolSynchronizer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
package com.wavesplatform.network | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import com.google.common.cache.CacheBuilder | ||
import com.wavesplatform.UtxPool | ||
import com.wavesplatform.state2.diffs.TransactionDiffer.TransactionValidationError | ||
import io.netty.channel.ChannelHandler.Sharable | ||
import com.wavesplatform.settings.SynchronizationSettings.UtxSynchronizerSettings | ||
import com.wavesplatform.state2.ByteStr | ||
import io.netty.channel.group.ChannelGroup | ||
import io.netty.channel.{ChannelHandlerContext, ChannelInboundHandlerAdapter} | ||
import monix.eval.Task | ||
import monix.execution.Scheduler | ||
import monix.execution.schedulers.SchedulerService | ||
import monix.reactive.Observable | ||
import scorex.transaction.Transaction | ||
import scorex.utils.ScorexLogging | ||
|
||
object UtxPoolSynchronizer { | ||
def apply(utx: UtxPool, utxSynchronizerSettings: UtxSynchronizerSettings, allChannels: ChannelGroup, txs: ChannelObservable[Transaction]): Observable[Unit] = { | ||
|
||
implicit val scheduler: Scheduler = Scheduler.singleThread("utx-pool-sync") | ||
|
||
@Sharable | ||
class UtxPoolSynchronizer(utx: UtxPool, allChannels: ChannelGroup) | ||
extends ChannelInboundHandlerAdapter with ScorexLogging { | ||
val dummy = new Object() | ||
|
||
private implicit val scheduler: SchedulerService = Scheduler.singleThread("utx-pool-synchronizer") | ||
val knownTransactions = CacheBuilder | ||
.newBuilder() | ||
.maximumSize(utxSynchronizerSettings.networkTxCacheSize) | ||
.expireAfterWrite(utxSynchronizerSettings.networkTxCacheTime.toMillis, TimeUnit.MILLISECONDS) | ||
.build[ByteStr, Object] | ||
|
||
override def channelRead(ctx: ChannelHandlerContext, msg: AnyRef): Unit = msg match { | ||
case t: Transaction => Task(utx.putIfNew(t) match { | ||
case Right(true) => // log.trace(s"${id(ctx)} Added transaction ${t.id} to UTX pool") | ||
allChannels.broadcastTx(t, Some(ctx.channel())) | ||
case Left(TransactionValidationError(e, _)) => // log.trace(s"${id(ctx)} Error processing transaction ${t.id}: $e") | ||
case Left(e) => // log.trace(s"${id(ctx)} Error processing transaction ${t.id}: $e") | ||
case Right(false) => // log.trace(s"${id(ctx)} TX ${t.id} already known") | ||
}).runAsyncLogErr | ||
case _ => super.channelRead(ctx, msg) | ||
txs.observeOn(scheduler).map { case (channel, tx) => knownTransactions.get(tx.id(), () => { | ||
utx.putIfNew(tx).map(_ => allChannels.broadcast(tx, Some(channel))) | ||
dummy | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters