Browse Source

server: Use BlockOps on BlockEventsProcessor

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
3a8d3cb803
  1. 31
      server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala
  2. 20
      server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala

31
server/app/com/xsn/explorer/processors/BlockEventsProcessor.scala

@ -23,7 +23,8 @@ class BlockEventsProcessor @Inject() (
xsnService: XSNService,
transactionService: TransactionService,
databaseSeeder: DatabaseFutureSeeder,
blockDataHandler: BlockFutureDataHandler) {
blockDataHandler: BlockFutureDataHandler,
blockOps: BlockOps) {
import BlockEventsProcessor._
@ -103,21 +104,6 @@ class BlockEventsProcessor @Inject() (
.toFuture
}
def onRepeatedBlockHeight(): FutureApplicationResult[Result] = {
val result = for {
orphanBlock <- blockDataHandler.getBy(newBlock.height).toFutureOr
replaceCommand = DatabaseSeeder.ReplaceBlockCommand(
orphanBlock = orphanBlock,
newBlock = newBlock,
newTransactions = newTransactions)
_ <- databaseSeeder.replaceBlock(replaceCommand).toFutureOr
} yield ReplacedByBlockHeight
result.toFuture
}
def onMissingBlock(): FutureApplicationResult[Result] = {
blockDataHandler
.getBy(newBlock.hash)
@ -127,13 +113,12 @@ class BlockEventsProcessor @Inject() (
Future.successful { Good(ExistingBlockIgnored(newBlock)) }
case Bad(One(BlockNotFoundError)) =>
val createCommand = DatabaseSeeder.CreateBlockCommand(newBlock, newTransactions)
databaseSeeder
.newBlock(createCommand)
.flatMap {
case Good(_) => Future.successful { Good(MissingBlockProcessed(newBlock)) }
case Bad(One(PostgresForeignKeyViolationError("height", _))) => onRepeatedBlockHeight()
case Bad(errors) => Future.successful(Bad(errors))
blockOps
.createBlock(newBlock, newTransactions)
.map {
case Good(BlockOps.Result.BlockCreated) => Good(MissingBlockProcessed(newBlock))
case Good(BlockOps.Result.BlockReplacedByHeight) => Good(ReplacedByBlockHeight)
case Bad(errors) => Bad(errors)
}
case Bad(errors) =>

20
server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala

@ -30,11 +30,17 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures
new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
lazy val xsnService = new FileBasedXSNService
lazy val blockOps = new BlockOps(
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
lazy val processor = new BlockEventsProcessor(
xsnService,
new TransactionService(xsnService)(Executors.globalEC),
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC),
blockOps)
before {
clearDatabase()
@ -194,7 +200,8 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures
xsnService,
new TransactionService(xsnService)(Executors.globalEC),
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC),
blockOps)
List(block1, block2)
@ -233,7 +240,8 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures
xsnService,
new TransactionService(xsnService)(Executors.globalEC),
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC),
blockOps)
List(block1, block2)
@ -274,7 +282,8 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures
xsnService,
new TransactionService(xsnService)(Executors.globalEC),
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC),
blockOps)
List(block1, block2)
.map(_.hash)
@ -312,7 +321,8 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures
xsnService,
new TransactionService(xsnService)(Executors.globalEC),
new DatabaseFutureSeeder(dataSeeder)(Executors.databaseEC),
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC))
new BlockFutureDataHandler(dataHandler)(Executors.databaseEC),
blockOps)
List(block1, rareBlock3)
.map(_.hash)

Loading…
Cancel
Save