Skip to content

Commit

Permalink
NodeInfo is no longer lateinit. (corda#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
andr3ej authored Nov 17, 2017
1 parent 787de9d commit aabc3c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
30 changes: 15 additions & 15 deletions node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ abstract class AbstractNode(val configuration: NodeConfiguration,

protected val services: ServiceHubInternal get() = _services
private lateinit var _services: ServiceHubInternalImpl
protected lateinit var info: NodeInfo
protected val nodeStateObservable: PublishSubject<NodeState> = PublishSubject.create<NodeState>()
protected var myNotaryIdentity: PartyAndCertificate? = null
protected lateinit var checkpointStorage: CheckpointStorage
Expand Down Expand Up @@ -166,7 +165,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
check(started == null) { "Node has already been started" }
log.info("Generating nodeInfo ...")
initCertificate()
val keyPairs = initNodeInfo()
val (keyPairs, info) = initNodeInfo()
val identityKeypair = keyPairs.first { it.public == info.legalIdentities.first().owningKey }
val serialisedNodeInfo = info.serialize()
val signature = identityKeypair.sign(serialisedNodeInfo)
Expand All @@ -178,13 +177,13 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
check(started == null) { "Node has already been started" }
log.info("Node starting up ...")
initCertificate()
val keyPairs = initNodeInfo()
val (keyPairs, info) = initNodeInfo()
val schemaService = NodeSchemaService(cordappLoader)
// Do all of this in a database transaction so anything that might need a connection has one.
val (startedImpl, schedulerService) = initialiseDatabasePersistence(schemaService) { database ->
val transactionStorage = makeTransactionStorage(database)
val stateLoader = StateLoaderImpl(transactionStorage)
val nodeServices = makeServices(keyPairs, schemaService, transactionStorage, stateLoader, database)
val nodeServices = makeServices(keyPairs, schemaService, transactionStorage, stateLoader, database, info)
val notaryService = makeNotaryService(nodeServices, database)
smm = makeStateMachineManager(database)
val flowStarter = FlowStarterImpl(serverThread, smm)
Expand Down Expand Up @@ -244,7 +243,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
}
}

private fun initNodeInfo(): Set<KeyPair> {
private fun initNodeInfo(): Pair<Set<KeyPair>, NodeInfo> {
val (identity, identityKeyPair) = obtainIdentity(notaryConfig = null)
val keyPairs = mutableSetOf(identityKeyPair)

Expand All @@ -253,13 +252,13 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
keyPairs += notaryIdentityKeyPair
notaryIdentity
}
info = NodeInfo(
val info = NodeInfo(
myAddresses(),
listOf(identity, myNotaryIdentity).filterNotNull(),
versionInfo.platformVersion,
platformClock.instant().toEpochMilli()
)
return keyPairs
return Pair(keyPairs, info)
}

protected abstract fun myAddresses(): List<NetworkHostAndPort>
Expand Down Expand Up @@ -485,12 +484,12 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
* Builds node internal, advertised, and plugin services.
* Returns a list of tokenizable services to be added to the serialisation context.
*/
private fun makeServices(keyPairs: Set<KeyPair>, schemaService: SchemaService, transactionStorage: WritableTransactionStorage, stateLoader: StateLoader, database: CordaPersistence): MutableList<Any> {
private fun makeServices(keyPairs: Set<KeyPair>, schemaService: SchemaService, transactionStorage: WritableTransactionStorage, stateLoader: StateLoader, database: CordaPersistence, info: NodeInfo): MutableList<Any> {
checkpointStorage = DBCheckpointStorage()
val metrics = MetricRegistry()
attachments = NodeAttachmentService(metrics)
val cordappProvider = CordappProviderImpl(cordappLoader, attachments)
val identityService = makeIdentityService()
val identityService = makeIdentityService(info)
val keyManagementService = makeKeyManagementService(identityService, keyPairs)
_services = ServiceHubInternalImpl(
identityService,
Expand All @@ -500,8 +499,9 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
stateLoader,
MonitoringService(metrics),
cordappProvider,
database)
network = makeMessagingService(database)
database,
info)
network = makeMessagingService(database, info)
val tokenizableServices = mutableListOf(attachments, network, services.vaultService,
services.keyManagementService, services.identityService, platformClock,
services.auditService, services.monitoringService, services.networkMapCache, services.schemaService,
Expand Down Expand Up @@ -606,7 +606,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
}
}

private fun makeIdentityService(): IdentityService {
private fun makeIdentityService(info: NodeInfo): IdentityService {
val trustStore = KeyStoreWrapper(configuration.trustStoreFile, configuration.trustStorePassword)
val caKeyStore = KeyStoreWrapper(configuration.nodeKeystore, configuration.keyStorePassword)
val trustRoot = trustStore.getX509Certificate(X509Utilities.CORDA_ROOT_CA)
Expand Down Expand Up @@ -635,7 +635,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
_started = null
}

protected abstract fun makeMessagingService(database: CordaPersistence): MessagingService
protected abstract fun makeMessagingService(database: CordaPersistence, info: NodeInfo): MessagingService
protected abstract fun startMessagingService(rpcOps: RPCOps)

private fun obtainIdentity(notaryConfig: NotaryConfig?): Pair<PartyAndCertificate, KeyPair> {
Expand Down Expand Up @@ -714,7 +714,8 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
private val stateLoader: StateLoader,
override val monitoringService: MonitoringService,
override val cordappProvider: CordappProviderInternal,
override val database: CordaPersistence
override val database: CordaPersistence,
override val myInfo: NodeInfo
) : SingletonSerializeAsToken(), ServiceHubInternal, StateLoader by stateLoader {
override val rpcFlows = ArrayList<Class<out FlowLogic<*>>>()
override val stateMachineRecordedTransactionMapping = DBTransactionMappingStorage()
Expand All @@ -726,7 +727,6 @@ abstract class AbstractNode(val configuration: NodeConfiguration,
override val attachments: AttachmentStorage get() = this@AbstractNode.attachments
override val networkService: MessagingService get() = network
override val clock: Clock get() = platformClock
override val myInfo: NodeInfo get() = info
override val myNodeStateObservable: Observable<NodeState> get() = nodeStateObservable
override val configuration: NodeConfiguration get() = this@AbstractNode.configuration
override fun <T : SerializeAsToken> cordaService(type: Class<T>): T {
Expand Down
3 changes: 2 additions & 1 deletion node/src/main/kotlin/net/corda/node/internal/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.corda.core.internal.concurrent.openFuture
import net.corda.core.internal.concurrent.thenMatch
import net.corda.core.internal.uncheckedCast
import net.corda.core.messaging.RPCOps
import net.corda.core.node.NodeInfo
import net.corda.core.node.ServiceHub
import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.utilities.loggerFor
Expand Down Expand Up @@ -127,7 +128,7 @@ open class Node(configuration: NodeConfiguration,
private var shutdownHook: ShutdownHook? = null

private lateinit var userService: RPCUserService
override fun makeMessagingService(database: CordaPersistence): MessagingService {
override fun makeMessagingService(database: CordaPersistence, info: NodeInfo): MessagingService {
userService = RPCUserServiceImpl(configuration.rpcUsers)

val serverAddress = configuration.messagingServerAddress ?: makeLocalMessageBroker()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import net.corda.core.internal.uncheckedCast
import net.corda.core.messaging.MessageRecipients
import net.corda.core.messaging.RPCOps
import net.corda.core.messaging.SingleMessageRecipient
import net.corda.core.node.NodeInfo
import net.corda.core.node.services.IdentityService
import net.corda.core.node.services.KeyManagementService
import net.corda.core.serialization.SerializationWhitelist
Expand Down Expand Up @@ -238,7 +239,7 @@ class MockNetwork(defaultParameters: MockNetworkParameters = MockNetworkParamete

// We only need to override the messaging service here, as currently everything that hits disk does so
// through the java.nio API which we are already mocking via Jimfs.
override fun makeMessagingService(database: CordaPersistence): MessagingService {
override fun makeMessagingService(database: CordaPersistence, info: NodeInfo): MessagingService {
require(id >= 0) { "Node ID must be zero or positive, was passed: " + id }
return mockNet.messagingNetwork.createNodeWithID(
!mockNet.threadPerNode,
Expand Down

0 comments on commit aabc3c5

Please sign in to comment.