Merged in trader-demo-fix (pull request #121)

Trader demo fix: buyer now records completed transactions and doesn't attempt to reuse the same spent cash states
This commit is contained in:
Andrius Dagys 2016-05-31 14:36:34 +01:00
commit b4ee6a09da

View File

@ -2,7 +2,6 @@ package com.r3corda.demos
import co.paralleluniverse.fibers.Suspendable
import com.google.common.net.HostAndPort
import com.typesafe.config.ConfigFactory
import com.r3corda.contracts.CommercialPaper
import com.r3corda.core.contracts.*
import com.r3corda.core.crypto.Party
@ -11,28 +10,29 @@ import com.r3corda.core.crypto.generateKeyPair
import com.r3corda.core.days
import com.r3corda.core.logElapsedTime
import com.r3corda.core.messaging.SingleMessageRecipient
import com.r3corda.node.services.statemachine.StateMachineManager
import com.r3corda.node.internal.Node
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.core.node.NodeInfo
import com.r3corda.node.services.network.NetworkMapService
import com.r3corda.node.services.persistence.NodeAttachmentService
import com.r3corda.node.services.transactions.NotaryService
import com.r3corda.core.node.services.ServiceType
import com.r3corda.node.services.messaging.ArtemisMessagingService
import com.r3corda.node.services.wallet.NodeWalletService
import com.r3corda.core.protocols.ProtocolLogic
import com.r3corda.core.random63BitValue
import com.r3corda.core.seconds
import com.r3corda.core.serialization.deserialize
import com.r3corda.node.utilities.ANSIProgressRenderer
import com.r3corda.core.utilities.BriefLogFormatter
import com.r3corda.core.utilities.Emoji
import com.r3corda.core.utilities.ProgressTracker
import joptsimple.OptionParser
import joptsimple.OptionSet
import com.r3corda.node.internal.Node
import com.r3corda.node.services.config.NodeConfigurationFromConfig
import com.r3corda.node.services.messaging.ArtemisMessagingService
import com.r3corda.node.services.network.NetworkMapService
import com.r3corda.node.services.persistence.NodeAttachmentService
import com.r3corda.node.services.statemachine.StateMachineManager
import com.r3corda.node.services.transactions.NotaryService
import com.r3corda.node.services.wallet.NodeWalletService
import com.r3corda.node.utilities.ANSIProgressRenderer
import com.r3corda.protocols.NotaryProtocol
import com.r3corda.protocols.TwoPartyTradeProtocol
import com.typesafe.config.ConfigFactory
import joptsimple.OptionParser
import joptsimple.OptionSet
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
@ -215,7 +215,7 @@ class TraderDemoProtocolBuyer(private val attachmentsPath: Path, val notary: Par
// Self issue some cash.
//
// TODO: At some point this demo should be extended to have a central bank node.
(serviceHub.walletService as NodeWalletService).fillWithSomeTestCash(notary, 1500.DOLLARS)
(serviceHub.walletService as NodeWalletService).fillWithSomeTestCash(notary, 3000.DOLLARS)
while (true) {
// Wait around until a node asks to start a trade with us. In a real system, this part would happen out of band
@ -239,17 +239,25 @@ class TraderDemoProtocolBuyer(private val attachmentsPath: Path, val notary: Par
// This invokes the trading protocol and out pops our finished transaction.
val tradeTX: SignedTransaction = subProtocol(buyer)
serviceHub.recordTransactions(listOf(tradeTX))
logger.info("Purchase complete - we are a happy customer! Final transaction is: " +
"\n\n${Emoji.renderIfSupported(tradeTX.tx)}")
logIssuanceAttachment(tradeTX)
logBalance()
} catch(e: Exception) {
logger.error("Something went wrong whilst trading!", e)
}
}
}
private fun logBalance() {
val balances = serviceHub.walletService.cashBalances.entries.map { "${it.key.currencyCode} ${it.value}" }
logger.info("Remaining balance: ${balances.joinToString()}")
}
private fun logIssuanceAttachment(tradeTX: SignedTransaction) {
// Find the original CP issuance.
val search = TransactionGraphSearch(serviceHub.storageService.validatedTransactions, listOf(tradeTX.tx))
@ -305,6 +313,7 @@ class TraderDemoProtocolSeller(val myAddress: HostAndPort,
val seller = TwoPartyTradeProtocol.Seller(otherSide, notary, commercialPaper, 1000.DOLLARS, cpOwnerKey,
sessionID, progressTracker.childrenFor[TRADING]!!)
val tradeTX: SignedTransaction = subProtocol(seller)
serviceHub.recordTransactions(listOf(tradeTX))
logger.info("Sale completed - we have a happy customer!\n\nFinal transaction is:\n\n${Emoji.renderIfSupported(tradeTX.tx)}")
}