mirror of
https://github.com/corda/corda.git
synced 2025-06-21 00:23:09 +00:00
StateMachine rewrite
This commit is contained in:
@ -18,10 +18,8 @@ import net.corda.core.utilities.ByteSequence
|
||||
import net.corda.core.utilities.OpaqueBytes
|
||||
import net.corda.core.utilities.getOrThrow
|
||||
import net.corda.core.utilities.trace
|
||||
import net.corda.node.services.messaging.Message
|
||||
import net.corda.node.services.messaging.MessageHandlerRegistration
|
||||
import net.corda.node.services.messaging.MessagingService
|
||||
import net.corda.node.services.messaging.ReceivedMessage
|
||||
import net.corda.node.services.messaging.*
|
||||
import net.corda.node.services.statemachine.DeduplicationId
|
||||
import net.corda.node.utilities.AffinityExecutor
|
||||
import net.corda.nodeapi.internal.persistence.CordaPersistence
|
||||
import net.corda.testing.node.internal.InMemoryMessage
|
||||
@ -290,9 +288,16 @@ class InMemoryMessagingNetwork private constructor(
|
||||
private data class InMemoryReceivedMessage(override val topic: String,
|
||||
override val data: ByteSequence,
|
||||
override val platformVersion: Int,
|
||||
override val uniqueMessageId: String,
|
||||
override val uniqueMessageId: DeduplicationId,
|
||||
override val debugTimestamp: Instant,
|
||||
override val peer: CordaX500Name) : ReceivedMessage
|
||||
override val peer: CordaX500Name,
|
||||
override val senderUUID: String? = null,
|
||||
override val senderSeqNo: Long? = null,
|
||||
/** Note this flag is never set in the in memory network. */
|
||||
override val isSessionInit: Boolean = false) : ReceivedMessage {
|
||||
|
||||
override val additionalHeaders: Map<String, String> = emptyMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* A class that provides an abstraction over the nodes' messaging service that also contains the ability to
|
||||
@ -319,7 +324,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
private val peerHandle: PeerHandle,
|
||||
private val executor: AffinityExecutor,
|
||||
private val database: CordaPersistence) : SingletonSerializeAsToken(), InternalMockMessagingService {
|
||||
private inner class Handler(val topicSession: String, val callback: (ReceivedMessage, MessageHandlerRegistration) -> Unit) : MessageHandlerRegistration
|
||||
private inner class Handler(val topicSession: String, val callback: MessageHandler) : MessageHandlerRegistration
|
||||
|
||||
@Volatile
|
||||
private var running = true
|
||||
@ -330,7 +335,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
}
|
||||
|
||||
private val state = ThreadBox(InnerState())
|
||||
private val processedMessages: MutableSet<String> = Collections.synchronizedSet(HashSet<String>())
|
||||
private val processedMessages: MutableSet<DeduplicationId> = Collections.synchronizedSet(HashSet<DeduplicationId>())
|
||||
|
||||
override val myAddress: PeerHandle get() = peerHandle
|
||||
|
||||
@ -353,7 +358,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override fun addMessageHandler(topic: String, callback: (ReceivedMessage, MessageHandlerRegistration) -> Unit): MessageHandlerRegistration {
|
||||
override fun addMessageHandler(topic: String, callback: MessageHandler): MessageHandlerRegistration {
|
||||
check(running)
|
||||
val (handler, transfers) = state.locked {
|
||||
val handler = Handler(topic, callback).apply { handlers.add(this) }
|
||||
@ -374,7 +379,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
state.locked { check(handlers.remove(registration as Handler)) }
|
||||
}
|
||||
|
||||
override fun send(message: Message, target: MessageRecipients, retryId: Long?, sequenceKey: Any, additionalHeaders: Map<String, String>) {
|
||||
override fun send(message: Message, target: MessageRecipients, retryId: Long?, sequenceKey: Any) {
|
||||
check(running)
|
||||
msgSend(this, message, target)
|
||||
if (!sendManuallyPumped) {
|
||||
@ -400,7 +405,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
override fun cancelRedelivery(retryId: Long) {}
|
||||
|
||||
/** Returns the given (topic & session, data) pair as a newly created message object. */
|
||||
override fun createMessage(topic: String, data: ByteArray, deduplicationId: String): Message {
|
||||
override fun createMessage(topic: String, data: ByteArray, deduplicationId: DeduplicationId, additionalHeaders: Map<String, String>): Message {
|
||||
return InMemoryMessage(topic, OpaqueBytes(data), deduplicationId)
|
||||
}
|
||||
|
||||
@ -465,7 +470,7 @@ class InMemoryMessagingNetwork private constructor(
|
||||
database.transaction {
|
||||
for (handler in deliverTo) {
|
||||
try {
|
||||
handler.callback(transfer.toReceivedMessage(), handler)
|
||||
handler.callback(transfer.toReceivedMessage(), handler, DummyDeduplicationHandler())
|
||||
} catch (e: Exception) {
|
||||
log.error("Caught exception in handler for $this/${handler.topicSession}", e)
|
||||
}
|
||||
@ -489,5 +494,12 @@ class InMemoryMessagingNetwork private constructor(
|
||||
message.debugTimestamp,
|
||||
sender.name)
|
||||
}
|
||||
|
||||
private class DummyDeduplicationHandler : DeduplicationHandler {
|
||||
override fun afterDatabaseTransaction() {
|
||||
}
|
||||
override fun insideDatabaseTransaction() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package net.corda.testing.node.internal
|
||||
|
||||
import net.corda.core.utilities.ByteSequence
|
||||
import net.corda.node.services.messaging.Message
|
||||
import net.corda.node.services.statemachine.DeduplicationId
|
||||
import java.time.Instant
|
||||
|
||||
/**
|
||||
@ -9,7 +10,11 @@ import java.time.Instant
|
||||
*/
|
||||
data class InMemoryMessage(override val topic: String,
|
||||
override val data: ByteSequence,
|
||||
override val uniqueMessageId: String,
|
||||
override val debugTimestamp: Instant = Instant.now()) : Message {
|
||||
override val uniqueMessageId: DeduplicationId,
|
||||
override val debugTimestamp: Instant = Instant.now(),
|
||||
override val senderUUID: String? = null) : Message {
|
||||
|
||||
override val additionalHeaders: Map<String, String> = emptyMap()
|
||||
|
||||
override fun toString() = "$topic#${String(data.bytes)}"
|
||||
}
|
@ -15,7 +15,6 @@ import net.corda.core.internal.VisibleForTesting
|
||||
import net.corda.core.internal.createDirectories
|
||||
import net.corda.core.internal.createDirectory
|
||||
import net.corda.core.internal.uncheckedCast
|
||||
import net.corda.core.messaging.CordaRPCOps
|
||||
import net.corda.core.messaging.MessageRecipients
|
||||
import net.corda.core.messaging.RPCOps
|
||||
import net.corda.core.messaging.SingleMessageRecipient
|
||||
@ -231,7 +230,7 @@ open class InternalMockNetwork(private val cordappPackages: List<String>,
|
||||
private val entropyRoot = args.entropyRoot
|
||||
var counter = entropyRoot
|
||||
override val log get() = staticLog
|
||||
override val serverThread: AffinityExecutor =
|
||||
override val serverThread: AffinityExecutor.ServiceAffinityExecutor =
|
||||
if (mockNet.threadPerNode) {
|
||||
ServiceAffinityExecutor("Mock node $id thread", 1)
|
||||
} else {
|
||||
|
@ -1,18 +1,25 @@
|
||||
package net.corda.testing.node.internal
|
||||
|
||||
import net.corda.core.concurrent.CordaFuture
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.internal.concurrent.doneFuture
|
||||
import net.corda.core.messaging.DataFeed
|
||||
import net.corda.core.serialization.SingletonSerializeAsToken
|
||||
import net.corda.core.toFuture
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.node.services.api.WritableTransactionStorage
|
||||
import rx.Observable
|
||||
import rx.subjects.PublishSubject
|
||||
import java.util.HashMap
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* A class which provides an implementation of [WritableTransactionStorage] which is used in [MockServices]
|
||||
*/
|
||||
open class MockTransactionStorage : WritableTransactionStorage, SingletonSerializeAsToken() {
|
||||
override fun trackTransaction(id: SecureHash): CordaFuture<SignedTransaction> {
|
||||
return txns[id]?.let { doneFuture(it) } ?: _updatesPublisher.filter { it.id == id }.toFuture()
|
||||
}
|
||||
|
||||
override fun track(): DataFeed<List<SignedTransaction>, SignedTransaction> {
|
||||
return DataFeed(txns.values.toList(), _updatesPublisher)
|
||||
}
|
||||
|
Reference in New Issue
Block a user