From 3e8d76334ea20be14f428d701f41f7ebd6ee1932 Mon Sep 17 00:00:00 2001
From: Anthony Keenan <anthonykeenan@gmail.com>
Date: Wed, 14 Feb 2018 16:42:56 +0000
Subject: [PATCH] CORDA-939 Modify Api Scanner to check api for internal
 exposures (#2510)

* Update check api changes to look for internals

* Update several more uses of internal

* Make check-api-changes script filter out internal class usages

* Make CordaClock part of API

* Update api-current.txt

* Remove exclusion of nodeapi.internal

* Remove access to CordaPersistence from public api

* Don't expose DB Connection from StartedMockNode and remove unnecessary transaction from CustomVaultQueryTest

* Make internal tests that use need db access use InternalMockNetwork

* Make test certificates internal

* Address further review comments

* Revert some accidental changes to api-current.txt

* Address Shams' review comments

* Update Api Scanner to filter out CordaInternal attribute

* Update api-current.txt

* Remove superfluous brackets

* Add transaction to StartedMockNode

* More leaky transaction fixes
---
 .ci/api-current.txt                           | 36 +-----------
 .ci/check-api-changes.sh                      | 20 ++++++-
 .../confidential/IdentitySyncFlowTests.kt     |  5 +-
 .../confidential/SwapIdentitiesFlowTests.kt   |  5 +-
 .../core/flows/CollectSignaturesFlowTests.kt  | 15 ++---
 .../core/identity/PartyAndCertificateTest.kt  |  2 +-
 .../internal/ResolveTransactionsFlowTest.kt   | 15 ++---
 .../LedgerTransactionQueryTests.kt            |  3 +-
 .../net/corda/docs/CustomVaultQueryTest.kt    | 14 ++---
 .../docs/FxTransactionBuildTutorialTest.kt    |  9 +--
 .../finance/flows/CashPaymentFlowTests.kt     | 13 +++--
 gradle-plugins/api-scanner/build.gradle       |  2 +
 .../main/java/net/corda/plugins/ScanApi.java  | 13 +++++
 ...tachmentsClassLoaderStaticContractTests.kt |  2 +-
 .../internal/AttachmentsClassLoaderTests.kt   |  2 +-
 .../node/services/AttachmentLoadingTests.kt   |  2 +-
 .../node/services/BFTNotaryServiceTests.kt    | 17 +++---
 .../node/services/RaftNotaryServiceTests.kt   |  3 +-
 .../registration/NodeRegistrationTest.kt      |  2 +-
 .../corda/node/{internal => }/CordaClock.kt   |  2 +-
 .../net/corda/node/internal/AbstractNode.kt   |  1 +
 .../kotlin/net/corda/node/internal/Node.kt    |  2 +
 .../services/events/NodeSchedulerService.kt   |  4 +-
 .../net/corda/node/utilities/DemoClock.kt     |  2 +-
 .../cordapp/CordappProviderImplTests.kt       |  2 +-
 .../identity/InMemoryIdentityServiceTests.kt  |  2 +
 .../PersistentIdentityServiceTests.kt         |  2 +
 .../services/network/NetworkMapCacheTest.kt   |  4 +-
 .../services/network/NetworkMapClientTest.kt  |  2 +-
 .../services/network/NetworkMapUpdaterTest.kt |  1 +
 .../network/NetworkParametersReaderTest.kt    |  2 +-
 .../services/schema/NodeSchemaServiceTest.kt  |  3 +-
 .../corda/node/utilities/ClockUtilsTest.kt    |  4 +-
 .../kotlin/net/corda/testing/driver/Driver.kt |  2 +-
 .../testing/driver/internal/DriverInternal.kt |  3 +-
 .../testing/node/InMemoryMessagingNetwork.kt  | 16 +++++-
 .../net/corda/testing/node/MockNetwork.kt     | 11 +++-
 .../net/corda/testing/node/MockServices.kt    | 18 ++++--
 .../net/corda/testing/node/TestClock.kt       |  2 +-
 .../testing/node/internal/DriverDSLImpl.kt    |  2 +-
 .../node/internal/InternalMockNetwork.kt      |  4 +-
 .../testing/core/SerializationTestHelpers.kt  | 43 +--------------
 .../net/corda/testing/core/TestConstants.kt   |  4 --
 .../net/corda/testing/core/TestUtils.kt       |  2 +
 .../kotlin/net/corda/testing/dsl/TestDSL.kt   |  2 +-
 .../InternalSerializationTestHelpers.kt       | 55 ++++++++++++++++++-
 .../testing/internal/InternalTestConstants.kt |  7 +++
 .../MockCordappConfigProvider.kt              |  2 +-
 .../MockCordappProvider.kt                    |  6 +-
 .../testing/internal/TestNodeInfoBuilder.kt   |  2 -
 50 files changed, 223 insertions(+), 171 deletions(-)
 rename node/src/main/kotlin/net/corda/node/{internal => }/CordaClock.kt (98%)
 create mode 100644 testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalTestConstants.kt
 rename testing/test-utils/src/main/kotlin/net/corda/testing/{node => internal}/MockCordappConfigProvider.kt (92%)
 rename testing/test-utils/src/main/kotlin/net/corda/testing/{services => internal}/MockCordappProvider.kt (94%)

diff --git a/.ci/api-current.txt b/.ci/api-current.txt
index ddf0e06ee7..b4c94505e9 100644
--- a/.ci/api-current.txt
+++ b/.ci/api-current.txt
@@ -1233,7 +1233,6 @@ public abstract class net.corda.core.flows.FlowLogic extends java.lang.Object
   @org.jetbrains.annotations.Nullable public net.corda.core.utilities.ProgressTracker getProgressTracker()
   @org.jetbrains.annotations.NotNull public final net.corda.core.flows.StateMachineRunId getRunId()
   @org.jetbrains.annotations.NotNull public final net.corda.core.node.ServiceHub getServiceHub()
-  @net.corda.core.CordaInternal @org.jetbrains.annotations.NotNull public final net.corda.core.internal.FlowStateMachine getStateMachine()
   @co.paralleluniverse.fibers.Suspendable @org.jetbrains.annotations.NotNull public final net.corda.core.flows.FlowSession initiateFlow(net.corda.core.identity.Party)
   @co.paralleluniverse.fibers.Suspendable public final void persistFlowStackSnapshot()
   @kotlin.Deprecated @co.paralleluniverse.fibers.Suspendable @org.jetbrains.annotations.NotNull public net.corda.core.utilities.UntrustworthyData receive(Class, net.corda.core.identity.Party)
@@ -1242,7 +1241,6 @@ public abstract class net.corda.core.flows.FlowLogic extends java.lang.Object
   public final void recordAuditEvent(String, String, Map)
   @kotlin.Deprecated @co.paralleluniverse.fibers.Suspendable public void send(net.corda.core.identity.Party, Object)
   @kotlin.Deprecated @co.paralleluniverse.fibers.Suspendable @org.jetbrains.annotations.NotNull public net.corda.core.utilities.UntrustworthyData sendAndReceive(Class, net.corda.core.identity.Party, Object)
-  @net.corda.core.CordaInternal public final void setStateMachine(net.corda.core.internal.FlowStateMachine)
   @co.paralleluniverse.fibers.Suspendable @kotlin.jvm.JvmStatic public static final void sleep(java.time.Duration)
   @co.paralleluniverse.fibers.Suspendable public Object subFlow(net.corda.core.flows.FlowLogic)
   @org.jetbrains.annotations.Nullable public final net.corda.core.messaging.DataFeed track()
@@ -3700,7 +3698,6 @@ public final class net.corda.testing.driver.DriverParameters extends java.lang.O
   public String toString()
 ##
 @net.corda.core.DoNotImplement public interface net.corda.testing.driver.InProcess extends net.corda.testing.driver.NodeHandle
-  @org.jetbrains.annotations.NotNull public abstract net.corda.nodeapi.internal.persistence.CordaPersistence getDatabase()
   @org.jetbrains.annotations.NotNull public abstract net.corda.node.services.api.StartedNodeServices getServices()
   @org.jetbrains.annotations.NotNull public abstract rx.Observable registerInitiatedFlow(Class)
 ##
@@ -3805,7 +3802,6 @@ public static final class net.corda.testing.node.ClusterSpec$Raft extends net.co
   public String toString()
 ##
 @javax.annotation.concurrent.ThreadSafe public final class net.corda.testing.node.InMemoryMessagingNetwork extends net.corda.core.serialization.SingletonSerializeAsToken
-  public <init>(boolean, net.corda.testing.node.InMemoryMessagingNetwork$ServicePeerAllocationStrategy, org.apache.activemq.artemis.utils.ReusableLatch)
   @org.jetbrains.annotations.NotNull public synchronized final List getEndpoints()
   @org.jetbrains.annotations.NotNull public final rx.Observable getReceivedMessages()
   @org.jetbrains.annotations.NotNull public final rx.Observable getSentMessages()
@@ -3888,10 +3884,6 @@ public static final class net.corda.testing.node.InMemoryMessagingNetwork$Servic
   @org.jetbrains.annotations.Nullable public abstract net.corda.testing.node.InMemoryMessagingNetwork$MessageTransfer pumpReceive(boolean)
   public abstract void stop()
 ##
-public static final class net.corda.testing.node.InMemoryMessagingNetwork$pumpSend$$inlined$schedule$1 extends java.util.TimerTask
-  public <init>(net.corda.testing.node.InMemoryMessagingNetwork, net.corda.testing.node.InMemoryMessagingNetwork$MessageTransfer, net.corda.core.internal.concurrent.OpenFuture)
-  public void run()
-##
 public class net.corda.testing.node.MessagingServiceSpy extends java.lang.Object implements net.corda.node.services.messaging.MessagingService
   public <init>(net.corda.node.services.messaging.MessagingService)
   @org.jetbrains.annotations.NotNull public net.corda.node.services.messaging.MessageHandlerRegistration addMessageHandler(String, kotlin.jvm.functions.Function2)
@@ -4018,6 +4010,7 @@ public class net.corda.testing.node.MockServices extends java.lang.Object implem
   public <init>(List, net.corda.core.identity.CordaX500Name, net.corda.core.node.services.IdentityService)
   public <init>(net.corda.core.identity.CordaX500Name)
   public <init>(net.corda.core.identity.CordaX500Name, net.corda.core.node.services.IdentityService)
+  public final void addMockCordapp(String)
   @org.jetbrains.annotations.NotNull public net.corda.core.transactions.SignedTransaction addSignature(net.corda.core.transactions.SignedTransaction)
   @org.jetbrains.annotations.NotNull public net.corda.core.transactions.SignedTransaction addSignature(net.corda.core.transactions.SignedTransaction, java.security.PublicKey)
   @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializeAsToken cordaService(Class)
@@ -4033,7 +4026,6 @@ public class net.corda.testing.node.MockServices extends java.lang.Object implem
   @org.jetbrains.annotations.NotNull public net.corda.core.node.services.IdentityService getIdentityService()
   @org.jetbrains.annotations.NotNull public net.corda.core.node.services.KeyManagementService getKeyManagementService()
   @org.jetbrains.annotations.NotNull public static final net.corda.node.VersionInfo getMOCK_VERSION_INFO()
-  @org.jetbrains.annotations.NotNull public final net.corda.testing.services.MockCordappProvider getMockCordappProvider()
   @org.jetbrains.annotations.NotNull public net.corda.core.node.NodeInfo getMyInfo()
   @org.jetbrains.annotations.NotNull public net.corda.core.node.services.NetworkMapCache getNetworkMapCache()
   @org.jetbrains.annotations.NotNull public net.corda.core.node.services.TransactionVerifierService getTransactionVerifierService()
@@ -4139,7 +4131,6 @@ public final class net.corda.testing.node.NotarySpec extends java.lang.Object
 ##
 public final class net.corda.testing.node.StartedMockNode extends java.lang.Object
   @org.jetbrains.annotations.NotNull public final List findStateMachines(Class)
-  @org.jetbrains.annotations.NotNull public final net.corda.nodeapi.internal.persistence.CordaPersistence getDatabase()
   public final int getId()
   @org.jetbrains.annotations.NotNull public final net.corda.core.node.NodeInfo getInfo()
   @org.jetbrains.annotations.NotNull public final net.corda.node.services.messaging.MessagingService getNetwork()
@@ -4152,7 +4143,7 @@ public final class net.corda.testing.node.StartedMockNode extends java.lang.Obje
 ##
 public static final class net.corda.testing.node.StartedMockNode$Companion extends java.lang.Object
 ##
-@javax.annotation.concurrent.ThreadSafe public final class net.corda.testing.node.TestClock extends net.corda.node.internal.MutableClock
+@javax.annotation.concurrent.ThreadSafe public final class net.corda.testing.node.TestClock extends net.corda.node.MutableClock
   public <init>(java.time.Clock)
   public synchronized final void advanceBy(java.time.Duration)
   public synchronized final void setTo(java.time.Instant)
@@ -4411,24 +4402,7 @@ public static final class net.corda.testing.core.SerializationEnvironmentRule$Co
 public static final class net.corda.testing.core.SerializationEnvironmentRule$apply$1 extends org.junit.runners.model.Statement
   public void evaluate()
 ##
-public final class net.corda.testing.core.SerializationTestHelpersKt extends java.lang.Object
-  @org.jetbrains.annotations.NotNull public static final net.corda.testing.core.GlobalSerializationEnvironment setGlobalSerialization(boolean)
-##
-public static final class net.corda.testing.core.SerializationTestHelpersKt$createTestSerializationEnv$1 extends net.corda.core.serialization.internal.SerializationEnvironmentImpl
-  @org.jetbrains.annotations.NotNull public String toString()
-##
-public static final class net.corda.testing.core.SerializationTestHelpersKt$setGlobalSerialization$1 extends java.lang.Object implements net.corda.testing.core.GlobalSerializationEnvironment, net.corda.core.serialization.internal.SerializationEnvironment
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationContext getCheckpointContext()
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationContext getP2pContext()
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationContext getRpcClientContext()
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationContext getRpcServerContext()
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationFactory getSerializationFactory()
-  @org.jetbrains.annotations.NotNull public net.corda.core.serialization.SerializationContext getStorageContext()
-  public void unset()
-##
 public final class net.corda.testing.core.TestConstants extends java.lang.Object
-  @org.jetbrains.annotations.NotNull public static final net.corda.nodeapi.internal.crypto.CertificateAndKeyPair getDEV_INTERMEDIATE_CA()
-  @org.jetbrains.annotations.NotNull public static final net.corda.nodeapi.internal.crypto.CertificateAndKeyPair getDEV_ROOT_CA()
   @kotlin.jvm.JvmField @org.jetbrains.annotations.NotNull public static final net.corda.core.identity.CordaX500Name ALICE_NAME
   @kotlin.jvm.JvmField @org.jetbrains.annotations.NotNull public static final net.corda.core.identity.CordaX500Name BOB_NAME
   @kotlin.jvm.JvmField @org.jetbrains.annotations.NotNull public static final net.corda.core.identity.CordaX500Name BOC_NAME
@@ -4681,9 +4655,3 @@ public static final class net.corda.testing.services.MockAttachmentStorage$Compa
 public static final class net.corda.testing.services.MockAttachmentStorage$openAttachment$1 extends net.corda.core.internal.AbstractAttachment
   @org.jetbrains.annotations.NotNull public net.corda.core.crypto.SecureHash getId()
 ##
-public final class net.corda.testing.services.MockCordappProvider extends net.corda.node.internal.cordapp.CordappProviderImpl
-  public <init>(net.corda.node.internal.cordapp.CordappLoader, net.corda.core.node.services.AttachmentStorage)
-  public final void addMockCordapp(String, net.corda.testing.services.MockAttachmentStorage)
-  @org.jetbrains.annotations.Nullable public net.corda.core.crypto.SecureHash getContractAttachmentID(String)
-  @org.jetbrains.annotations.NotNull public final List getCordappRegistry()
-##
diff --git a/.ci/check-api-changes.sh b/.ci/check-api-changes.sh
index 987d0ced3c..5896f6ebbf 100755
--- a/.ci/check-api-changes.sh
+++ b/.ci/check-api-changes.sh
@@ -31,8 +31,7 @@ if [ $removalCount -gt 0 ]; then
 fi
 
 # Adding new abstract methods could also break the API.
-# However, first exclude anything with the @DoNotImplement annotation.
-
+# However, first exclude classes marked with the @DoNotImplement annotation
 function forUserImpl() {
     awk '/DoNotImplement/,/^##/{ next }{ print }' $1
 }
@@ -45,13 +44,28 @@ $newAbstracts
 EOF
 `
 
+#Get a list of any methods that expose classes in .internal. namespaces, and any classes which extend/implement
+#an internal class
+newInternalExposures=$(echo "$userDiffContents" | grep "^+" | grep "\.internal\." )
+
+internalCount=`grep -v "^$" <<EOF | wc -l
+$newInternalExposures
+EOF
+`
+
+echo "Number of new internal class exposures: "$internalCount
+if [ $internalCount -gt 0 ]; then
+    echo "$newInternalExposures"
+    echo
+fi
+
 echo "Number of new abstract APIs: "$abstractCount
 if [ $abstractCount -gt 0 ]; then
     echo "$newAbstracts"
     echo
 fi
 
-badChanges=$(($removalCount + $abstractCount))
+badChanges=$(($removalCount + $abstractCount + $internalCount))
 if [ $badChanges -gt 255 ]; then
     echo "OVERFLOW! Number of bad API changes: $badChanges"
     badChanges=255
diff --git a/confidential-identities/src/test/kotlin/net/corda/confidential/IdentitySyncFlowTests.kt b/confidential-identities/src/test/kotlin/net/corda/confidential/IdentitySyncFlowTests.kt
index 506f021b68..2b6863a2ba 100644
--- a/confidential-identities/src/test/kotlin/net/corda/confidential/IdentitySyncFlowTests.kt
+++ b/confidential-identities/src/test/kotlin/net/corda/confidential/IdentitySyncFlowTests.kt
@@ -19,6 +19,7 @@ import net.corda.testing.core.BOB_NAME
 import net.corda.testing.core.CHARLIE_NAME
 import net.corda.testing.core.singleIdentity
 import net.corda.testing.node.MockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork
 import net.corda.testing.node.startFlow
 import org.junit.After
 import org.junit.Before
@@ -28,12 +29,12 @@ import kotlin.test.assertNotNull
 import kotlin.test.assertNull
 
 class IdentitySyncFlowTests {
-    private lateinit var mockNet: MockNetwork
+    private lateinit var mockNet: InternalMockNetwork
 
     @Before
     fun before() {
         // We run this in parallel threads to help catch any race conditions that may exist.
-        mockNet = MockNetwork(
+        mockNet = InternalMockNetwork(
                 networkSendManuallyPumped = false,
                 threadPerNode = true,
                 cordappPackages = listOf("net.corda.finance.contracts.asset")
diff --git a/confidential-identities/src/test/kotlin/net/corda/confidential/SwapIdentitiesFlowTests.kt b/confidential-identities/src/test/kotlin/net/corda/confidential/SwapIdentitiesFlowTests.kt
index f8fbc6668d..1f3cb3b529 100644
--- a/confidential-identities/src/test/kotlin/net/corda/confidential/SwapIdentitiesFlowTests.kt
+++ b/confidential-identities/src/test/kotlin/net/corda/confidential/SwapIdentitiesFlowTests.kt
@@ -4,18 +4,19 @@ import net.corda.core.identity.*
 import net.corda.core.utilities.getOrThrow
 import net.corda.testing.core.*
 import net.corda.testing.node.MockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork
 import org.junit.Before
 import net.corda.testing.node.startFlow
 import org.junit.Test
 import kotlin.test.*
 
 class SwapIdentitiesFlowTests {
-    private lateinit var mockNet: MockNetwork
+    private lateinit var mockNet: InternalMockNetwork
 
     @Before
     fun setup() {
         // We run this in parallel threads to help catch any race conditions that may exist.
-        mockNet = MockNetwork(emptyList(), networkSendManuallyPumped = false, threadPerNode = true)
+        mockNet = InternalMockNetwork(emptyList(), networkSendManuallyPumped = false, threadPerNode = true)
     }
 
     @Test
diff --git a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt
index 1586c9aa07..e2c24061a3 100644
--- a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt
+++ b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt
@@ -11,12 +11,13 @@ import net.corda.core.identity.groupAbstractPartyByWellKnownParty
 import net.corda.core.transactions.SignedTransaction
 import net.corda.core.transactions.TransactionBuilder
 import net.corda.core.utilities.getOrThrow
+import net.corda.node.internal.StartedNode
 import net.corda.testing.contracts.DummyContract
 import net.corda.testing.core.*
 import net.corda.testing.internal.rigorousMock
-import net.corda.testing.node.MockNetwork
-import net.corda.testing.node.StartedMockNode
 import net.corda.testing.node.MockServices
+import net.corda.testing.node.internal.InternalMockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork.MockNode
 import net.corda.testing.node.startFlow
 import org.junit.After
 import org.junit.Before
@@ -29,10 +30,10 @@ class CollectSignaturesFlowTests {
         private val miniCorp = TestIdentity(CordaX500Name("MiniCorp", "London", "GB"))
     }
 
-    private lateinit var mockNet: MockNetwork
-    private lateinit var aliceNode: StartedMockNode
-    private lateinit var bobNode: StartedMockNode
-    private lateinit var charlieNode: StartedMockNode
+    private lateinit var mockNet: InternalMockNetwork
+    private lateinit var aliceNode: StartedNode<MockNode>
+    private lateinit var bobNode: StartedNode<MockNode>
+    private lateinit var charlieNode: StartedNode<MockNode>
     private lateinit var alice: Party
     private lateinit var bob: Party
     private lateinit var charlie: Party
@@ -40,7 +41,7 @@ class CollectSignaturesFlowTests {
 
     @Before
     fun setup() {
-        mockNet = MockNetwork(cordappPackages = listOf("net.corda.testing.contracts"))
+        mockNet = InternalMockNetwork(cordappPackages = listOf("net.corda.testing.contracts"))
         aliceNode = mockNet.createPartyNode(ALICE_NAME)
         bobNode = mockNet.createPartyNode(BOB_NAME)
         charlieNode = mockNet.createPartyNode(CHARLIE_NAME)
diff --git a/core/src/test/kotlin/net/corda/core/identity/PartyAndCertificateTest.kt b/core/src/test/kotlin/net/corda/core/identity/PartyAndCertificateTest.kt
index f89f6a7829..8831e5905e 100644
--- a/core/src/test/kotlin/net/corda/core/identity/PartyAndCertificateTest.kt
+++ b/core/src/test/kotlin/net/corda/core/identity/PartyAndCertificateTest.kt
@@ -7,9 +7,9 @@ import net.corda.core.serialization.deserialize
 import net.corda.core.serialization.serialize
 import net.corda.nodeapi.internal.crypto.X509KeyStore
 import net.corda.nodeapi.internal.crypto.X509Utilities
-import net.corda.testing.core.DEV_ROOT_CA
 import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.core.getTestPartyAndCertificate
+import net.corda.testing.internal.DEV_ROOT_CA
 import org.assertj.core.api.Assertions.assertThat
 import org.junit.Rule
 import org.junit.Test
diff --git a/core/src/test/kotlin/net/corda/core/internal/ResolveTransactionsFlowTest.kt b/core/src/test/kotlin/net/corda/core/internal/ResolveTransactionsFlowTest.kt
index fa6ab2b0ff..f6d9432f6a 100644
--- a/core/src/test/kotlin/net/corda/core/internal/ResolveTransactionsFlowTest.kt
+++ b/core/src/test/kotlin/net/corda/core/internal/ResolveTransactionsFlowTest.kt
@@ -8,10 +8,11 @@ import net.corda.core.identity.Party
 import net.corda.core.transactions.SignedTransaction
 import net.corda.core.utilities.getOrThrow
 import net.corda.core.utilities.sequence
+import net.corda.node.internal.StartedNode
 import net.corda.testing.contracts.DummyContract
-import net.corda.testing.node.MockNetwork
 import net.corda.testing.core.singleIdentity
-import net.corda.testing.node.StartedMockNode
+import net.corda.testing.node.internal.InternalMockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork.MockNode
 import net.corda.testing.node.startFlow
 import org.junit.After
 import org.junit.Before
@@ -27,17 +28,17 @@ import kotlin.test.assertNull
 
 // DOCSTART 3
 class ResolveTransactionsFlowTest {
-    private lateinit var mockNet: MockNetwork
-    private lateinit var notaryNode: StartedMockNode
-    private lateinit var megaCorpNode: StartedMockNode
-    private lateinit var miniCorpNode: StartedMockNode
+    private lateinit var mockNet: InternalMockNetwork
+    private lateinit var notaryNode: StartedNode<MockNode>
+    private lateinit var megaCorpNode: StartedNode<MockNode>
+    private lateinit var miniCorpNode: StartedNode<MockNode>
     private lateinit var megaCorp: Party
     private lateinit var miniCorp: Party
     private lateinit var notary: Party
 
     @Before
     fun setup() {
-        mockNet = MockNetwork(cordappPackages = listOf("net.corda.testing.contracts"))
+        mockNet = InternalMockNetwork(cordappPackages = listOf("net.corda.testing.contracts"))
         notaryNode = mockNet.defaultNotaryNode
         megaCorpNode = mockNet.createPartyNode(CordaX500Name("MegaCorp", "London", "GB"))
         miniCorpNode = mockNet.createPartyNode(CordaX500Name("MiniCorp", "London", "GB"))
diff --git a/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt b/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt
index fc87fd23ea..9482764d99 100644
--- a/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt
+++ b/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt
@@ -10,6 +10,7 @@ import net.corda.core.identity.Party
 import net.corda.node.services.api.IdentityServiceInternal
 import net.corda.testing.contracts.DummyContract
 import net.corda.testing.core.*
+import net.corda.testing.internal.MockCordappProvider
 import net.corda.testing.internal.rigorousMock
 import net.corda.testing.node.MockServices
 import org.junit.Before
@@ -37,7 +38,7 @@ class LedgerTransactionQueryTests {
 
     @Before
     fun setup() {
-        services.mockCordappProvider.addMockCordapp(DummyContract.PROGRAM_ID, services.attachments)
+        services.addMockCordapp(DummyContract.PROGRAM_ID)
     }
 
     interface Commands {
diff --git a/docs/source/example-code/src/test/kotlin/net/corda/docs/CustomVaultQueryTest.kt b/docs/source/example-code/src/test/kotlin/net/corda/docs/CustomVaultQueryTest.kt
index 2b194b1e94..6867695965 100644
--- a/docs/source/example-code/src/test/kotlin/net/corda/docs/CustomVaultQueryTest.kt
+++ b/docs/source/example-code/src/test/kotlin/net/corda/docs/CustomVaultQueryTest.kt
@@ -74,16 +74,14 @@ class CustomVaultQueryTest {
 
     private fun getBalances(): Pair<Map<Currency, Amount<Currency>>, Map<Currency, Amount<Currency>>> {
         // Print out the balances
-        val balancesNodesA =
-            nodeA.database.transaction {
-                nodeA.services.getCashBalances()
-            }
+        val balancesNodesA = nodeA.transaction {
+            nodeA.services.getCashBalances()
+        }
         println("BalanceA\n" + balancesNodesA)
 
-        val balancesNodesB =
-            nodeB.database.transaction {
-                nodeB.services.getCashBalances()
-            }
+        val balancesNodesB = nodeB.transaction {
+            nodeB.services.getCashBalances()
+        }
         println("BalanceB\n" + balancesNodesB)
 
         return Pair(balancesNodesA, balancesNodesB)
diff --git a/docs/source/example-code/src/test/kotlin/net/corda/docs/FxTransactionBuildTutorialTest.kt b/docs/source/example-code/src/test/kotlin/net/corda/docs/FxTransactionBuildTutorialTest.kt
index 0805565e03..67a4c919f7 100644
--- a/docs/source/example-code/src/test/kotlin/net/corda/docs/FxTransactionBuildTutorialTest.kt
+++ b/docs/source/example-code/src/test/kotlin/net/corda/docs/FxTransactionBuildTutorialTest.kt
@@ -68,13 +68,14 @@ class FxTransactionBuildTutorialTest {
         doIt.getOrThrow()
         // Get the balances when the vault updates
         nodeAVaultUpdate.get()
-        val balancesA = nodeA.database.transaction {
+        val balancesA = nodeA.transaction {
             nodeA.services.getCashBalances()
         }
         nodeBVaultUpdate.get()
-        val balancesB = nodeB.database.transaction {
+        val balancesB = nodeB.transaction {
             nodeB.services.getCashBalances()
         }
+
         println("BalanceA\n" + balancesA)
         println("BalanceB\n" + balancesB)
         // Verify the transfers occurred as expected
@@ -86,10 +87,10 @@ class FxTransactionBuildTutorialTest {
 
     private fun printBalances() {
         // Print out the balances
-        nodeA.database.transaction {
+        nodeA.transaction {
             println("BalanceA\n" + nodeA.services.getCashBalances())
         }
-        nodeB.database.transaction {
+        nodeB.transaction {
             println("BalanceB\n" + nodeB.services.getCashBalances())
         }
     }
diff --git a/finance/src/test/kotlin/net/corda/finance/flows/CashPaymentFlowTests.kt b/finance/src/test/kotlin/net/corda/finance/flows/CashPaymentFlowTests.kt
index 2d3b901f9a..d9b74c3cec 100644
--- a/finance/src/test/kotlin/net/corda/finance/flows/CashPaymentFlowTests.kt
+++ b/finance/src/test/kotlin/net/corda/finance/flows/CashPaymentFlowTests.kt
@@ -9,10 +9,11 @@ import net.corda.core.utilities.getOrThrow
 import net.corda.finance.DOLLARS
 import net.corda.finance.`issued by`
 import net.corda.finance.contracts.asset.Cash
+import net.corda.node.internal.StartedNode
 import net.corda.testing.core.*
 import net.corda.testing.node.InMemoryMessagingNetwork.ServicePeerAllocationStrategy.RoundRobin
-import net.corda.testing.node.MockNetwork
-import net.corda.testing.node.StartedMockNode
+import net.corda.testing.node.internal.InternalMockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork.MockNode
 import net.corda.testing.node.startFlow
 import org.junit.After
 import org.junit.Before
@@ -21,16 +22,16 @@ import kotlin.test.assertEquals
 import kotlin.test.assertFailsWith
 
 class CashPaymentFlowTests {
-    private lateinit var mockNet: MockNetwork
+    private lateinit var mockNet: InternalMockNetwork
     private val initialBalance = 2000.DOLLARS
     private val ref = OpaqueBytes.of(0x01)
-    private lateinit var bankOfCordaNode: StartedMockNode
+    private lateinit var bankOfCordaNode: StartedNode<MockNode>
     private lateinit var bankOfCorda: Party
-    private lateinit var aliceNode: StartedMockNode
+    private lateinit var aliceNode: StartedNode<MockNode>
 
     @Before
     fun start() {
-        mockNet = MockNetwork(servicePeerAllocationStrategy = RoundRobin(), cordappPackages = listOf("net.corda.finance.contracts.asset"))
+        mockNet = InternalMockNetwork(servicePeerAllocationStrategy = RoundRobin(), cordappPackages = listOf("net.corda.finance.contracts.asset"))
         bankOfCordaNode = mockNet.createPartyNode(BOC_NAME)
         bankOfCorda = bankOfCordaNode.info.identityFromX500Name(BOC_NAME)
         aliceNode = mockNet.createPartyNode(ALICE_NAME)
diff --git a/gradle-plugins/api-scanner/build.gradle b/gradle-plugins/api-scanner/build.gradle
index c178472d58..13dca34301 100644
--- a/gradle-plugins/api-scanner/build.gradle
+++ b/gradle-plugins/api-scanner/build.gradle
@@ -5,6 +5,7 @@ apply plugin: 'com.jfrog.artifactory'
 description "Generates a summary of the artifact's public API"
 
 repositories {
+    mavenLocal()
     mavenCentral()
 }
 
@@ -17,3 +18,4 @@ dependencies {
 publish {
     name project.name
 }
+
diff --git a/gradle-plugins/api-scanner/src/main/java/net/corda/plugins/ScanApi.java b/gradle-plugins/api-scanner/src/main/java/net/corda/plugins/ScanApi.java
index eb2b7e5599..a9567035f6 100644
--- a/gradle-plugins/api-scanner/src/main/java/net/corda/plugins/ScanApi.java
+++ b/gradle-plugins/api-scanner/src/main/java/net/corda/plugins/ScanApi.java
@@ -202,6 +202,14 @@ public class ScanApi extends DefaultTask {
                     // These classes belong to internal Corda packages.
                     return;
                 }
+                if (className.contains("$$inlined$")) {
+                    /*
+                     * These classes are internally generated by the Kotlin compiler
+                     * and are not exposed as part of the public API
+                     * TODO: Filter out using EnclosingMethod attribute in classfile
+                     */
+                    return;
+                }
                 ClassInfo classInfo = allInfo.get(className);
                 if (classInfo.getClassLoaders() == null) {
                     // Ignore classes that belong to one of our target ClassLoader's parents.
@@ -275,6 +283,7 @@ public class ScanApi extends DefaultTask {
             for (MethodInfo method : methods) {
                 if (isVisible(method.getAccessFlags()) // Only public and protected methods
                         && isValid(method.getAccessFlags(), METHOD_MASK) // Excludes bridge and synthetic methods
+                        && !hasCordaInternal(method.getAnnotationNames()) // Excludes methods annotated as @CordaInternal
                         && !isKotlinInternalScope(method)) {
                     writer.append("  ").println(filterAnnotationsFor(method));
                 }
@@ -355,6 +364,10 @@ public class ScanApi extends DefaultTask {
         return method.getMethodName().indexOf('$') >= 0;
     }
 
+    private static boolean hasCordaInternal(Collection<String> annotationNames) {
+        return annotationNames.contains("net.corda.core.CordaInternal");
+    }
+
     private static boolean isValid(int modifiers, int mask) {
         return (modifiers & mask) == modifiers;
     }
diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderStaticContractTests.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderStaticContractTests.kt
index 12ec7230b8..c264264bc6 100644
--- a/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderStaticContractTests.kt
+++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderStaticContractTests.kt
@@ -17,7 +17,7 @@ import net.corda.testing.core.DUMMY_NOTARY_NAME
 import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.core.TestIdentity
 import net.corda.testing.internal.rigorousMock
-import net.corda.testing.node.MockCordappConfigProvider
+import net.corda.testing.internal.MockCordappConfigProvider
 import net.corda.testing.services.MockAttachmentStorage
 import org.junit.Assert.*
 import org.junit.Rule
diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderTests.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderTests.kt
index 77406d26a1..5ee6355980 100644
--- a/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderTests.kt
+++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/AttachmentsClassLoaderTests.kt
@@ -23,7 +23,7 @@ import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.core.TestIdentity
 import net.corda.testing.internal.kryoSpecific
 import net.corda.testing.internal.rigorousMock
-import net.corda.testing.node.MockCordappConfigProvider
+import net.corda.testing.internal.MockCordappConfigProvider
 import net.corda.testing.services.MockAttachmentStorage
 import org.apache.commons.io.IOUtils
 import org.junit.Assert.*
diff --git a/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt
index 65dd56c3f1..01582516b5 100644
--- a/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt
+++ b/node/src/integration-test/kotlin/net/corda/node/services/AttachmentLoadingTests.kt
@@ -29,7 +29,7 @@ import net.corda.testing.driver.NodeHandle
 import net.corda.testing.driver.driver
 import net.corda.testing.internal.rigorousMock
 import net.corda.testing.internal.withoutTestSerialization
-import net.corda.testing.node.MockCordappConfigProvider
+import net.corda.testing.internal.MockCordappConfigProvider
 import net.corda.testing.services.MockAttachmentStorage
 import org.junit.Assert.assertEquals
 import org.junit.Rule
diff --git a/node/src/integration-test/kotlin/net/corda/node/services/BFTNotaryServiceTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/BFTNotaryServiceTests.kt
index e972d71468..e4ca31dc7d 100644
--- a/node/src/integration-test/kotlin/net/corda/node/services/BFTNotaryServiceTests.kt
+++ b/node/src/integration-test/kotlin/net/corda/node/services/BFTNotaryServiceTests.kt
@@ -13,25 +13,26 @@ import net.corda.core.identity.CordaX500Name
 import net.corda.core.identity.Party
 import net.corda.core.internal.deleteIfExists
 import net.corda.core.internal.div
+import net.corda.core.node.NotaryInfo
 import net.corda.core.transactions.SignedTransaction
 import net.corda.core.transactions.TransactionBuilder
 import net.corda.core.utilities.NetworkHostAndPort
 import net.corda.core.utilities.Try
 import net.corda.core.utilities.getOrThrow
+import net.corda.node.internal.StartedNode
 import net.corda.node.services.config.BFTSMaRtConfiguration
 import net.corda.node.services.config.NotaryConfig
 import net.corda.node.services.transactions.minClusterSize
 import net.corda.node.services.transactions.minCorrectReplicas
 import net.corda.nodeapi.internal.DevIdentityGenerator
 import net.corda.nodeapi.internal.network.NetworkParametersCopier
-import net.corda.core.node.NotaryInfo
-import net.corda.testing.core.chooseIdentity
 import net.corda.testing.common.internal.testNetworkParameters
 import net.corda.testing.contracts.DummyContract
+import net.corda.testing.core.chooseIdentity
 import net.corda.testing.core.dummyCommand
-import net.corda.testing.node.MockNetwork
-import net.corda.testing.node.StartedMockNode
 import net.corda.testing.node.MockNodeParameters
+import net.corda.testing.node.internal.InternalMockNetwork
+import net.corda.testing.node.internal.InternalMockNetwork.MockNode
 import net.corda.testing.node.startFlow
 import org.junit.After
 import org.junit.Before
@@ -41,13 +42,13 @@ import kotlin.test.assertEquals
 import kotlin.test.assertTrue
 
 class BFTNotaryServiceTests {
-    private lateinit var mockNet: MockNetwork
+    private lateinit var mockNet: InternalMockNetwork
     private lateinit var notary: Party
-    private lateinit var node: StartedMockNode
+    private lateinit var node: StartedNode<MockNode>
 
     @Before
     fun before() {
-        mockNet = MockNetwork(emptyList())
+        mockNet = InternalMockNetwork(emptyList())
     }
 
     @After
@@ -153,7 +154,7 @@ class BFTNotaryServiceTests {
         }
     }
 
-    private fun StartedMockNode.signInitialTransaction(notary: Party, block: TransactionBuilder.() -> Any?): SignedTransaction {
+    private fun StartedNode<MockNode>.signInitialTransaction(notary: Party, block: TransactionBuilder.() -> Any?): SignedTransaction {
         return services.signInitialTransaction(
                 TransactionBuilder(notary).apply {
                     addCommand(dummyCommand(services.myInfo.chooseIdentity().owningKey))
diff --git a/node/src/integration-test/kotlin/net/corda/node/services/RaftNotaryServiceTests.kt b/node/src/integration-test/kotlin/net/corda/node/services/RaftNotaryServiceTests.kt
index 20b07017ad..09f8733f1b 100644
--- a/node/src/integration-test/kotlin/net/corda/node/services/RaftNotaryServiceTests.kt
+++ b/node/src/integration-test/kotlin/net/corda/node/services/RaftNotaryServiceTests.kt
@@ -17,6 +17,7 @@ import net.corda.testing.driver.driver
 import net.corda.testing.core.dummyCommand
 import net.corda.testing.driver.DriverParameters
 import net.corda.testing.driver.InProcess
+import net.corda.testing.driver.internal.InProcessImpl
 import net.corda.testing.node.ClusterSpec
 import net.corda.testing.node.NotarySpec
 import net.corda.testing.node.startFlow
@@ -62,7 +63,7 @@ class RaftNotaryServiceTests {
     }
 
     private fun issueState(nodeHandle: InProcess, notary: Party): StateAndRef<*> {
-        return nodeHandle.database.transaction {
+        return (nodeHandle as InProcessImpl).database.transaction {
 
             val builder = DummyContract.generateInitial(Random().nextInt(), notary, nodeHandle.services.myInfo.chooseIdentity().ref(0))
             val stx = nodeHandle.services.signInitialTransaction(builder)
diff --git a/node/src/integration-test/kotlin/net/corda/node/utilities/registration/NodeRegistrationTest.kt b/node/src/integration-test/kotlin/net/corda/node/utilities/registration/NodeRegistrationTest.kt
index 560987cdf7..c8ab1e3952 100644
--- a/node/src/integration-test/kotlin/net/corda/node/utilities/registration/NodeRegistrationTest.kt
+++ b/node/src/integration-test/kotlin/net/corda/node/utilities/registration/NodeRegistrationTest.kt
@@ -16,10 +16,10 @@ import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_CLIENT_CA
 import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_INTERMEDIATE_CA
 import net.corda.nodeapi.internal.crypto.X509Utilities.CORDA_ROOT_CA
 import net.corda.testing.common.internal.testNetworkParameters
-import net.corda.testing.core.DEV_ROOT_CA
 import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.core.singleIdentity
 import net.corda.testing.driver.PortAllocation
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.node.NotarySpec
 import net.corda.testing.node.internal.CompatibilityZoneParams
 import net.corda.testing.node.internal.internalDriver
diff --git a/node/src/main/kotlin/net/corda/node/internal/CordaClock.kt b/node/src/main/kotlin/net/corda/node/CordaClock.kt
similarity index 98%
rename from node/src/main/kotlin/net/corda/node/internal/CordaClock.kt
rename to node/src/main/kotlin/net/corda/node/CordaClock.kt
index c08961ee77..f838364bf6 100644
--- a/node/src/main/kotlin/net/corda/node/internal/CordaClock.kt
+++ b/node/src/main/kotlin/net/corda/node/CordaClock.kt
@@ -1,4 +1,4 @@
-package net.corda.node.internal
+package net.corda.node
 
 import net.corda.core.serialization.SerializeAsToken
 import net.corda.core.serialization.SerializeAsTokenContext
diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt
index c00e1fc175..92d58d863d 100644
--- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt
+++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt
@@ -30,6 +30,7 @@ import net.corda.core.transactions.SignedTransaction
 import net.corda.core.utilities.NetworkHostAndPort
 import net.corda.core.utilities.debug
 import net.corda.core.utilities.getOrThrow
+import net.corda.node.CordaClock
 import net.corda.node.VersionInfo
 import net.corda.node.internal.classloading.requireAnnotation
 import net.corda.node.internal.cordapp.CordappConfigFileProvider
diff --git a/node/src/main/kotlin/net/corda/node/internal/Node.kt b/node/src/main/kotlin/net/corda/node/internal/Node.kt
index 910c454e74..8762a1bf0e 100644
--- a/node/src/main/kotlin/net/corda/node/internal/Node.kt
+++ b/node/src/main/kotlin/net/corda/node/internal/Node.kt
@@ -16,6 +16,8 @@ import net.corda.core.serialization.internal.SerializationEnvironmentImpl
 import net.corda.core.serialization.internal.nodeSerializationEnv
 import net.corda.core.utilities.NetworkHostAndPort
 import net.corda.core.utilities.contextLogger
+import net.corda.node.CordaClock
+import net.corda.node.SimpleClock
 import net.corda.node.VersionInfo
 import net.corda.node.internal.artemis.ArtemisBroker
 import net.corda.node.internal.artemis.BrokerAddresses
diff --git a/node/src/main/kotlin/net/corda/node/services/events/NodeSchedulerService.kt b/node/src/main/kotlin/net/corda/node/services/events/NodeSchedulerService.kt
index edc508c0e3..b79d7b3acd 100644
--- a/node/src/main/kotlin/net/corda/node/services/events/NodeSchedulerService.kt
+++ b/node/src/main/kotlin/net/corda/node/services/events/NodeSchedulerService.kt
@@ -21,8 +21,8 @@ import net.corda.core.schemas.PersistentStateRef
 import net.corda.core.serialization.SingletonSerializeAsToken
 import net.corda.core.utilities.contextLogger
 import net.corda.core.utilities.trace
-import net.corda.node.internal.CordaClock
-import net.corda.node.internal.MutableClock
+import net.corda.node.CordaClock
+import net.corda.node.MutableClock
 import net.corda.node.services.api.FlowStarter
 import net.corda.node.services.api.NodePropertiesStore
 import net.corda.node.services.api.SchedulerService
diff --git a/node/src/main/kotlin/net/corda/node/utilities/DemoClock.kt b/node/src/main/kotlin/net/corda/node/utilities/DemoClock.kt
index 0d6ab6b48c..ac2e82e962 100644
--- a/node/src/main/kotlin/net/corda/node/utilities/DemoClock.kt
+++ b/node/src/main/kotlin/net/corda/node/utilities/DemoClock.kt
@@ -1,7 +1,7 @@
 package net.corda.node.utilities
 
 import net.corda.core.internal.until
-import net.corda.node.internal.MutableClock
+import net.corda.node.MutableClock
 import java.time.Clock
 import java.time.LocalDate
 import javax.annotation.concurrent.ThreadSafe
diff --git a/node/src/test/kotlin/net/corda/node/internal/cordapp/CordappProviderImplTests.kt b/node/src/test/kotlin/net/corda/node/internal/cordapp/CordappProviderImplTests.kt
index 934c906e5d..9d12bffb05 100644
--- a/node/src/test/kotlin/net/corda/node/internal/cordapp/CordappProviderImplTests.kt
+++ b/node/src/test/kotlin/net/corda/node/internal/cordapp/CordappProviderImplTests.kt
@@ -4,7 +4,7 @@ import com.typesafe.config.Config
 import com.typesafe.config.ConfigFactory
 import net.corda.core.internal.cordapp.CordappConfigProvider
 import net.corda.core.node.services.AttachmentStorage
-import net.corda.testing.node.MockCordappConfigProvider
+import net.corda.testing.internal.MockCordappConfigProvider
 import net.corda.testing.services.MockAttachmentStorage
 import org.assertj.core.api.Assertions.assertThat
 import org.junit.Assert
diff --git a/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt
index f1256db27d..ff11860082 100644
--- a/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt
+++ b/node/src/test/kotlin/net/corda/node/services/identity/InMemoryIdentityServiceTests.kt
@@ -11,6 +11,8 @@ import net.corda.nodeapi.internal.crypto.CertificateType
 import net.corda.nodeapi.internal.crypto.X509Utilities
 import net.corda.nodeapi.internal.crypto.x509Certificates
 import net.corda.testing.core.*
+import net.corda.testing.internal.DEV_INTERMEDIATE_CA
+import net.corda.testing.internal.DEV_ROOT_CA
 import org.junit.Rule
 import org.junit.Test
 import kotlin.test.assertEquals
diff --git a/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt b/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt
index dda6516173..6ac282ef88 100644
--- a/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt
+++ b/node/src/test/kotlin/net/corda/node/services/identity/PersistentIdentityServiceTests.kt
@@ -15,6 +15,8 @@ import net.corda.nodeapi.internal.crypto.x509Certificates
 import net.corda.nodeapi.internal.persistence.CordaPersistence
 import net.corda.nodeapi.internal.persistence.DatabaseConfig
 import net.corda.testing.core.*
+import net.corda.testing.internal.DEV_INTERMEDIATE_CA
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties
 import net.corda.testing.node.makeTestIdentityService
 import org.junit.After
diff --git a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapCacheTest.kt b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapCacheTest.kt
index e8809bb833..03bb1a1667 100644
--- a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapCacheTest.kt
+++ b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapCacheTest.kt
@@ -6,9 +6,9 @@ import net.corda.testing.core.ALICE_NAME
 import net.corda.testing.core.BOB_NAME
 import net.corda.node.services.api.NetworkMapCacheInternal
 import net.corda.testing.core.getTestPartyAndCertificate
-import net.corda.testing.node.MockNetwork
 import net.corda.testing.node.MockNodeParameters
 import net.corda.testing.core.singleIdentity
+import net.corda.testing.node.internal.InternalMockNetwork
 import org.assertj.core.api.Assertions.assertThat
 import org.junit.After
 import org.junit.Test
@@ -18,7 +18,7 @@ import kotlin.test.assertNotNull
 import kotlin.test.assertNull
 
 class NetworkMapCacheTest {
-    private val mockNet = MockNetwork(emptyList())
+    private val mockNet = InternalMockNetwork(emptyList())
 
     @After
     fun teardown() {
diff --git a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapClientTest.kt b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapClientTest.kt
index 1edd35db01..283ae2c71a 100644
--- a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapClientTest.kt
+++ b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapClientTest.kt
@@ -7,10 +7,10 @@ import net.corda.core.serialization.serialize
 import net.corda.core.utilities.seconds
 import net.corda.testing.core.ALICE_NAME
 import net.corda.testing.core.BOB_NAME
-import net.corda.testing.core.DEV_ROOT_CA
 import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.common.internal.testNetworkParameters
 import net.corda.testing.driver.PortAllocation
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.internal.TestNodeInfoBuilder
 import net.corda.testing.internal.createNodeInfoAndSigned
 import net.corda.testing.internal.signWith
diff --git a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapUpdaterTest.kt b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapUpdaterTest.kt
index 8609520c73..a00bf44a35 100644
--- a/node/src/test/kotlin/net/corda/node/services/network/NetworkMapUpdaterTest.kt
+++ b/node/src/test/kotlin/net/corda/node/services/network/NetworkMapUpdaterTest.kt
@@ -28,6 +28,7 @@ import net.corda.nodeapi.internal.network.ParametersUpdate
 import net.corda.nodeapi.internal.network.verifiedNetworkMapCert
 import net.corda.testing.common.internal.testNetworkParameters
 import net.corda.testing.core.*
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.internal.TestNodeInfoBuilder
 import net.corda.testing.internal.createNodeInfoAndSigned
 import org.assertj.core.api.Assertions.assertThat
diff --git a/node/src/test/kotlin/net/corda/node/services/network/NetworkParametersReaderTest.kt b/node/src/test/kotlin/net/corda/node/services/network/NetworkParametersReaderTest.kt
index b6b9e107cd..abd8d33fd1 100644
--- a/node/src/test/kotlin/net/corda/node/services/network/NetworkParametersReaderTest.kt
+++ b/node/src/test/kotlin/net/corda/node/services/network/NetworkParametersReaderTest.kt
@@ -12,9 +12,9 @@ import net.corda.nodeapi.internal.network.NETWORK_PARAMS_UPDATE_FILE_NAME
 import net.corda.nodeapi.internal.network.NetworkParametersCopier
 import net.corda.nodeapi.internal.network.verifiedNetworkMapCert
 import net.corda.testing.common.internal.testNetworkParameters
-import net.corda.testing.core.DEV_ROOT_CA
 import net.corda.testing.core.SerializationEnvironmentRule
 import net.corda.testing.driver.PortAllocation
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.node.internal.network.NetworkMapServer
 import org.junit.After
 import org.junit.Before
diff --git a/node/src/test/kotlin/net/corda/node/services/schema/NodeSchemaServiceTest.kt b/node/src/test/kotlin/net/corda/node/services/schema/NodeSchemaServiceTest.kt
index 3c6e6d3b39..aa31cf7dd8 100644
--- a/node/src/test/kotlin/net/corda/node/services/schema/NodeSchemaServiceTest.kt
+++ b/node/src/test/kotlin/net/corda/node/services/schema/NodeSchemaServiceTest.kt
@@ -14,6 +14,7 @@ import net.corda.node.services.schema.NodeSchemaService.NodeNotaryV1
 import net.corda.testing.driver.DriverParameters
 import net.corda.testing.driver.InProcess
 import net.corda.testing.driver.driver
+import net.corda.testing.driver.internal.InProcessImpl
 import net.corda.testing.internal.vault.DummyLinearStateSchemaV1
 import net.corda.testing.node.MockNetwork
 import org.hibernate.annotations.Cascade
@@ -80,7 +81,7 @@ class NodeSchemaServiceTest {
     fun `custom schemas are loaded eagerly`() {
         val expected = setOf("PARENTS", "CHILDREN")
         val tables = driver(DriverParameters(startNodesInProcess = true)) {
-            (defaultNotaryNode.getOrThrow() as InProcess).database.transaction {
+            (defaultNotaryNode.getOrThrow() as InProcessImpl).database.transaction {
                 session.createNativeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES").list()
             }
         }
diff --git a/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt b/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt
index d8dbfac65f..571dee482b 100644
--- a/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt
+++ b/node/src/test/kotlin/net/corda/node/utilities/ClockUtilsTest.kt
@@ -8,8 +8,8 @@ import com.google.common.util.concurrent.SettableFuture
 import net.corda.core.utilities.getOrThrow
 import net.corda.core.utilities.hours
 import net.corda.core.utilities.minutes
-import net.corda.node.internal.CordaClock
-import net.corda.node.internal.SimpleClock
+import net.corda.node.CordaClock
+import net.corda.node.SimpleClock
 import net.corda.node.services.events.NodeSchedulerService
 import net.corda.testing.node.TestClock
 import org.junit.After
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/driver/Driver.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/driver/Driver.kt
index 883d61b88f..bf06b7cb08 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/driver/Driver.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/driver/Driver.kt
@@ -25,6 +25,7 @@ import java.net.InetSocketAddress
 import java.net.ServerSocket
 import java.nio.file.Path
 import java.nio.file.Paths
+import java.sql.Connection
 import java.util.concurrent.atomic.AtomicInteger
 
 /**
@@ -58,7 +59,6 @@ interface OutOfProcess : NodeHandle {
 
 @DoNotImplement
 interface InProcess : NodeHandle {
-    val database: CordaPersistence
     val services: StartedNodeServices
     /**
      * Register a flow that is initiated by another flow
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/driver/internal/DriverInternal.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/driver/internal/DriverInternal.kt
index 3f658d6086..972677dd4d 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/driver/internal/DriverInternal.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/driver/internal/DriverInternal.kt
@@ -15,6 +15,7 @@ import net.corda.testing.driver.OutOfProcess
 import net.corda.testing.node.User
 import rx.Observable
 import java.nio.file.Path
+import java.sql.Connection
 
 interface NodeHandleInternal : NodeHandle {
     val configuration: NodeConfiguration
@@ -57,7 +58,7 @@ data class InProcessImpl(
         private val onStopCallback: () -> Unit,
         private val node: StartedNode<Node>
 ) : InProcess, NodeHandleInternal {
-    override val database: CordaPersistence get() = node.database
+    val database: CordaPersistence = node.database
     override val services: StartedNodeServices get() = node.services
     override val rpcUsers: List<User> = configuration.rpcUsers.map { User(it.username, it.password, it.permissions) }
     override fun stop() {
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt
index 744ceb1dbd..e2aa643797 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/InMemoryMessagingNetwork.kt
@@ -1,5 +1,6 @@
 package net.corda.testing.node
 
+import net.corda.core.CordaInternal
 import net.corda.core.DoNotImplement
 import net.corda.core.crypto.CompositeKey
 import net.corda.core.identity.CordaX500Name
@@ -48,7 +49,7 @@ import kotlin.concurrent.thread
  *     a service is addressed.
  */
 @ThreadSafe
-class InMemoryMessagingNetwork internal constructor(
+class InMemoryMessagingNetwork private constructor(
         private val sendManuallyPumped: Boolean,
         private val servicePeerAllocationStrategy: ServicePeerAllocationStrategy = InMemoryMessagingNetwork.ServicePeerAllocationStrategy.Random(),
         private val messagesInFlight: ReusableLatch = ReusableLatch()
@@ -56,6 +57,13 @@ class InMemoryMessagingNetwork internal constructor(
     companion object {
         private const val MESSAGES_LOG_NAME = "messages"
         private val log = LoggerFactory.getLogger(MESSAGES_LOG_NAME)
+
+        internal fun create(
+                sendManuallyPumped: Boolean,
+                servicePeerAllocationStrategy: ServicePeerAllocationStrategy = InMemoryMessagingNetwork.ServicePeerAllocationStrategy.Random(),
+                messagesInFlight: ReusableLatch = ReusableLatch()): InMemoryMessagingNetwork {
+            return InMemoryMessagingNetwork(sendManuallyPumped, servicePeerAllocationStrategy, messagesInFlight)
+        }
     }
 
     private var counter = 0   // -1 means stopped.
@@ -115,7 +123,8 @@ class InMemoryMessagingNetwork internal constructor(
         val peerHandle = PeerHandle(id, description)
         peersMapping[peerHandle.description] = peerHandle // Assume that the same name - the same entity in MockNetwork.
         notaryService?.let { if (it.owningKey !is CompositeKey) peersMapping[it.name] = peerHandle }
-        val serviceHandles = notaryService?.let { listOf(ServiceHandle(it.party)) } ?: emptyList() //TODO only notary can be distributed?
+        val serviceHandles = notaryService?.let { listOf(ServiceHandle(it.party)) }
+                ?: emptyList() //TODO only notary can be distributed?
         synchronized(this) {
             val node = InMemoryMessaging(manuallyPumped, peerHandle, executor, database)
             handleEndpointMap[peerHandle] = node
@@ -304,7 +313,8 @@ class InMemoryMessagingNetwork internal constructor(
 
         override fun getAddressOfParty(partyInfo: PartyInfo): MessageRecipients {
             return when (partyInfo) {
-                is PartyInfo.SingleNode -> peersMapping[partyInfo.party.name] ?: throw IllegalArgumentException("No StartedMockNode for party ${partyInfo.party.name}")
+                is PartyInfo.SingleNode -> peersMapping[partyInfo.party.name]
+                        ?: throw IllegalArgumentException("No StartedMockNode for party ${partyInfo.party.name}")
                 is PartyInfo.DistributedNode -> ServiceHandle(partyInfo.party)
             }
         }
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt
index 66a930ce3e..ad951d99c1 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNetwork.kt
@@ -13,6 +13,8 @@ import net.corda.node.services.api.StartedNodeServices
 import net.corda.node.services.config.NodeConfiguration
 import net.corda.node.services.messaging.MessagingService
 import net.corda.nodeapi.internal.persistence.CordaPersistence
+import net.corda.nodeapi.internal.persistence.DatabaseTransaction
+import net.corda.nodeapi.internal.persistence.TransactionIsolationLevel
 import net.corda.testing.core.DUMMY_NOTARY_NAME
 import net.corda.testing.node.internal.InternalMockNetwork
 import net.corda.testing.node.internal.setMessagingServiceSpy
@@ -87,7 +89,6 @@ class StartedMockNode private constructor(private val node: StartedNode<Internal
     }
 
     val services get() : StartedNodeServices = node.services
-    val database get() : CordaPersistence = node.database
     val id get() : Int = node.internals.id
     val info get() : NodeInfo = node.services.myInfo
     val network get() : MessagingService = node.network
@@ -110,6 +111,12 @@ class StartedMockNode private constructor(private val node: StartedNode<Internal
 
     /** Returns the currently live flows of type [flowClass], and their corresponding result future. */
     fun <F : FlowLogic<*>> findStateMachines(flowClass: Class<F>): List<Pair<F, CordaFuture<*>>> = node.smm.findStateMachines(flowClass)
+
+    fun <T> transaction(statement: () -> T): T {
+        return node.database.transaction {
+            statement()
+        }
+    }
 }
 
 /**
@@ -211,4 +218,4 @@ open class MockNetwork(
 
     /** Get the base directory for the given node id. **/
     fun baseDirectory(nodeId: Int): Path = internalMockNetwork.baseDirectory(nodeId)
-}
\ No newline at end of file
+}
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt
index a86f4e380b..feeef6ab8b 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt
@@ -1,6 +1,7 @@
 package net.corda.testing.node
 
 import com.google.common.collect.MutableClassToInstanceMap
+import net.corda.core.contracts.ContractClassName
 import net.corda.core.cordapp.CordappProvider
 import net.corda.core.crypto.*
 import net.corda.core.flows.FlowLogic
@@ -31,11 +32,10 @@ import net.corda.node.services.vault.NodeVaultService
 import net.corda.nodeapi.internal.persistence.CordaPersistence
 import net.corda.nodeapi.internal.persistence.DatabaseConfig
 import net.corda.nodeapi.internal.persistence.HibernateConfiguration
-import net.corda.testing.common.internal.testNetworkParameters
-import net.corda.testing.core.DEV_ROOT_CA
 import net.corda.testing.core.TestIdentity
+import net.corda.testing.internal.DEV_ROOT_CA
 import net.corda.testing.services.MockAttachmentStorage
-import net.corda.testing.services.MockCordappProvider
+import net.corda.testing.internal.MockCordappProvider
 import org.bouncycastle.operator.ContentSigner
 import rx.Observable
 import rx.subjects.PublishSubject
@@ -209,7 +209,7 @@ open class MockServices private constructor(
             return NodeInfo(emptyList(), listOf(initialIdentity.identity), 1, serial = 1L)
         }
     override val transactionVerifierService: TransactionVerifierService get() = InMemoryTransactionVerifierService(2)
-    val mockCordappProvider = MockCordappProvider(cordappLoader, attachments)
+    private val mockCordappProvider: MockCordappProvider = MockCordappProvider(cordappLoader, attachments)
     override val cordappProvider: CordappProvider get() = mockCordappProvider
 
     internal fun makeVaultService(hibernateConfig: HibernateConfiguration, schemaService: SchemaService): VaultServiceInternal {
@@ -221,12 +221,17 @@ open class MockServices private constructor(
     val cordappServices: MutableClassToInstanceMap<SerializeAsToken> = MutableClassToInstanceMap.create<SerializeAsToken>()
     override fun <T : SerializeAsToken> cordaService(type: Class<T>): T {
         require(type.isAnnotationPresent(CordaService::class.java)) { "${type.name} is not a Corda service" }
-        return cordappServices.getInstance(type) ?: throw IllegalArgumentException("Corda service ${type.name} does not exist")
+        return cordappServices.getInstance(type)
+                ?: throw IllegalArgumentException("Corda service ${type.name} does not exist")
     }
 
     override fun jdbcSession(): Connection = throw UnsupportedOperationException()
 
     override fun registerUnloadHandler(runOnStop: () -> Unit) = throw UnsupportedOperationException()
+
+    fun addMockCordapp(contractClassName: ContractClassName) {
+        mockCordappProvider.addMockCordapp(contractClassName, attachments)
+    }
 }
 
 class MockKeyManagementService(val identityService: IdentityService,
@@ -252,7 +257,8 @@ class MockKeyManagementService(val identityService: IdentityService,
     private fun getSigner(publicKey: PublicKey): ContentSigner = getSigner(getSigningKeyPair(publicKey))
 
     private fun getSigningKeyPair(publicKey: PublicKey): KeyPair {
-        val pk = publicKey.keys.firstOrNull { keyStore.containsKey(it) } ?: throw IllegalArgumentException("Public key not found: ${publicKey.toStringShort()}")
+        val pk = publicKey.keys.firstOrNull { keyStore.containsKey(it) }
+                ?: throw IllegalArgumentException("Public key not found: ${publicKey.toStringShort()}")
         return KeyPair(pk, keyStore[pk]!!)
     }
 
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/TestClock.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/TestClock.kt
index 75d6563ae4..c99f1d128d 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/TestClock.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/TestClock.kt
@@ -1,7 +1,7 @@
 package net.corda.testing.node
 
 import net.corda.core.internal.until
-import net.corda.node.internal.MutableClock
+import net.corda.node.MutableClock
 import java.time.Clock
 import java.time.Duration
 import java.time.Instant
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/DriverDSLImpl.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/DriverDSLImpl.kt
index eb3ae63268..f5978eded5 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/DriverDSLImpl.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/DriverDSLImpl.kt
@@ -43,7 +43,7 @@ import net.corda.testing.common.internal.testNetworkParameters
 import net.corda.testing.core.ALICE_NAME
 import net.corda.testing.core.BOB_NAME
 import net.corda.testing.core.DUMMY_BANK_A_NAME
-import net.corda.testing.core.setGlobalSerialization
+import net.corda.testing.internal.setGlobalSerialization
 import net.corda.testing.driver.*
 import net.corda.testing.driver.internal.InProcessImpl
 import net.corda.testing.driver.internal.NodeHandleInternal
diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt
index b3366e8dbc..05646d3f20 100644
--- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt
+++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/internal/InternalMockNetwork.kt
@@ -50,7 +50,6 @@ import net.corda.nodeapi.internal.network.NetworkParametersCopier
 import net.corda.nodeapi.internal.persistence.CordaPersistence
 import net.corda.nodeapi.internal.persistence.DatabaseConfig
 import net.corda.testing.common.internal.testNetworkParameters
-import net.corda.testing.core.setGlobalSerialization
 import net.corda.testing.internal.rigorousMock
 import net.corda.testing.internal.testThreadFactory
 import net.corda.testing.node.InMemoryMessagingNetwork
@@ -60,6 +59,7 @@ import net.corda.testing.node.MockNetworkParameters
 import net.corda.testing.node.MockNodeParameters
 import net.corda.testing.node.MockServices.Companion.MOCK_VERSION_INFO
 import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties
+import net.corda.testing.internal.setGlobalSerialization
 import net.corda.testing.node.TestClock
 import org.apache.activemq.artemis.utils.ReusableLatch
 import org.apache.sshd.common.util.security.SecurityUtils
@@ -120,7 +120,7 @@ open class InternalMockNetwork(private val cordappPackages: List<String>,
         private set
     private val filesystem = Jimfs.newFileSystem(unix())
     private val busyLatch = ReusableLatch()
-    val messagingNetwork = InMemoryMessagingNetwork(networkSendManuallyPumped, servicePeerAllocationStrategy, busyLatch)
+    val messagingNetwork = InMemoryMessagingNetwork.create(networkSendManuallyPumped, servicePeerAllocationStrategy, busyLatch)
     // A unique identifier for this network to segregate databases with the same nodeID but different networks.
     private val networkId = random63BitValue()
     private val networkParameters: NetworkParametersCopier
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/core/SerializationTestHelpers.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/core/SerializationTestHelpers.kt
index 698003380d..2feefc269b 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/core/SerializationTestHelpers.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/core/SerializationTestHelpers.kt
@@ -16,6 +16,8 @@ import net.corda.nodeapi.internal.serialization.*
 import net.corda.nodeapi.internal.serialization.amqp.AMQPClientSerializationScheme
 import net.corda.nodeapi.internal.serialization.amqp.AMQPServerSerializationScheme
 import net.corda.testing.common.internal.asContextEnv
+import net.corda.testing.internal.createTestSerializationEnv
+import net.corda.testing.internal.inVMExecutors
 import net.corda.testing.internal.rigorousMock
 import net.corda.testing.internal.testThreadFactory
 import org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnector
@@ -26,8 +28,6 @@ import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.ExecutorService
 import java.util.concurrent.Executors
 
-private val inVMExecutors = ConcurrentHashMap<SerializationEnvironment, ExecutorService>()
-
 /** @param inheritable whether new threads inherit the environment, use sparingly. */
 class SerializationEnvironmentRule(private val inheritable: Boolean = false) : TestRule {
     companion object {
@@ -78,42 +78,3 @@ interface GlobalSerializationEnvironment : SerializationEnvironment {
     fun unset()
 }
 
-/**
- * Should only be used by Driver and MockNode.
- * @param armed true to install, false to do nothing and return a dummy env.
- */
-fun setGlobalSerialization(armed: Boolean): GlobalSerializationEnvironment {
-    return if (armed) {
-        object : GlobalSerializationEnvironment, SerializationEnvironment by createTestSerializationEnv("<global>") {
-            override fun unset() {
-                _globalSerializationEnv.set(null)
-                inVMExecutors.remove(this)
-            }
-        }.also {
-            _globalSerializationEnv.set(it)
-        }
-    } else {
-        rigorousMock<GlobalSerializationEnvironment>().also {
-            doNothing().whenever(it).unset()
-        }
-    }
-}
-
-private fun createTestSerializationEnv(label: String): SerializationEnvironmentImpl {
-    val factory = SerializationFactoryImpl().apply {
-        registerScheme(KryoClientSerializationScheme())
-        registerScheme(KryoServerSerializationScheme())
-        registerScheme(AMQPClientSerializationScheme(emptyList()))
-        registerScheme(AMQPServerSerializationScheme(emptyList()))
-    }
-    return object : SerializationEnvironmentImpl(
-            factory,
-            AMQP_P2P_CONTEXT,
-            KRYO_RPC_SERVER_CONTEXT,
-            KRYO_RPC_CLIENT_CONTEXT,
-            AMQP_STORAGE_CONTEXT,
-            KRYO_CHECKPOINT_CONTEXT
-    ) {
-        override fun toString() = "testSerializationEnv($label)"
-    }
-}
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestConstants.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestConstants.kt
index 0a87461d8d..6cbecab924 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestConstants.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestConstants.kt
@@ -30,10 +30,6 @@ val BOB_NAME = CordaX500Name("Bob Plc", "Rome", "IT")
 @JvmField
 val CHARLIE_NAME = CordaX500Name("Charlie Ltd", "Athens", "GR")
 
-val DEV_INTERMEDIATE_CA: CertificateAndKeyPair by lazy { net.corda.nodeapi.internal.DEV_INTERMEDIATE_CA }
-
-val DEV_ROOT_CA: CertificateAndKeyPair by lazy { net.corda.nodeapi.internal.DEV_ROOT_CA }
-
 fun dummyCommand(vararg signers: PublicKey = arrayOf(generateKeyPair().public)) = Command<TypeOnlyCommandData>(DummyCommandData, signers.toList())
 
 object DummyCommandData : TypeOnlyCommandData()
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestUtils.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestUtils.kt
index 762ea036be..5229cde993 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestUtils.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/core/TestUtils.kt
@@ -20,6 +20,8 @@ import net.corda.nodeapi.internal.crypto.CertificateAndKeyPair
 import net.corda.nodeapi.internal.crypto.CertificateType
 import net.corda.nodeapi.internal.crypto.X509CertificateFactory
 import net.corda.nodeapi.internal.crypto.X509Utilities
+import net.corda.testing.internal.DEV_INTERMEDIATE_CA
+import net.corda.testing.internal.DEV_ROOT_CA
 import java.math.BigInteger
 import java.security.KeyPair
 import java.security.PublicKey
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt
index 8548ecc1fb..bf2745f8e3 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/dsl/TestDSL.kt
@@ -14,7 +14,7 @@ import net.corda.core.transactions.SignedTransaction
 import net.corda.core.transactions.TransactionBuilder
 import net.corda.core.transactions.WireTransaction
 import net.corda.testing.services.MockAttachmentStorage
-import net.corda.testing.services.MockCordappProvider
+import net.corda.testing.internal.MockCordappProvider
 import net.corda.testing.core.dummyCommand
 import java.io.InputStream
 import java.security.PublicKey
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt
index f11693d527..aaa9e3a15d 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalSerializationTestHelpers.kt
@@ -1,8 +1,19 @@
 package net.corda.testing.internal
 
-import net.corda.core.serialization.internal._contextSerializationEnv
-import net.corda.core.serialization.internal._inheritableContextSerializationEnv
+import com.nhaarman.mockito_kotlin.doNothing
+import com.nhaarman.mockito_kotlin.whenever
+import net.corda.client.rpc.internal.KryoClientSerializationScheme
+import net.corda.core.serialization.internal.*
+import net.corda.node.serialization.KryoServerSerializationScheme
+import net.corda.nodeapi.internal.serialization.*
+import net.corda.nodeapi.internal.serialization.amqp.AMQPClientSerializationScheme
+import net.corda.nodeapi.internal.serialization.amqp.AMQPServerSerializationScheme
+import net.corda.testing.core.GlobalSerializationEnvironment
 import net.corda.testing.core.SerializationEnvironmentRule
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ExecutorService
+
+val inVMExecutors = ConcurrentHashMap<SerializationEnvironment, ExecutorService>()
 
 /**
  * For example your test class uses [SerializationEnvironmentRule] but you want to turn it off for one method.
@@ -17,3 +28,43 @@ fun <T> withoutTestSerialization(callable: () -> T): T { // TODO: Delete this, s
         property.set(env)
     }
 }
+
+internal fun createTestSerializationEnv(label: String): SerializationEnvironmentImpl {
+    val factory = SerializationFactoryImpl().apply {
+        registerScheme(KryoClientSerializationScheme())
+        registerScheme(KryoServerSerializationScheme())
+        registerScheme(AMQPClientSerializationScheme(emptyList()))
+        registerScheme(AMQPServerSerializationScheme(emptyList()))
+    }
+    return object : SerializationEnvironmentImpl(
+            factory,
+            AMQP_P2P_CONTEXT,
+            KRYO_RPC_SERVER_CONTEXT,
+            KRYO_RPC_CLIENT_CONTEXT,
+            AMQP_STORAGE_CONTEXT,
+            KRYO_CHECKPOINT_CONTEXT
+    ) {
+        override fun toString() = "testSerializationEnv($label)"
+    }
+}
+
+/**
+ * Should only be used by Driver and MockNode.
+ * @param armed true to install, false to do nothing and return a dummy env.
+ */
+fun setGlobalSerialization(armed: Boolean): GlobalSerializationEnvironment {
+    return if (armed) {
+        object : GlobalSerializationEnvironment, SerializationEnvironment by createTestSerializationEnv("<global>") {
+            override fun unset() {
+                _globalSerializationEnv.set(null)
+                inVMExecutors.remove(this)
+            }
+        }.also {
+            _globalSerializationEnv.set(it)
+        }
+    } else {
+        rigorousMock<GlobalSerializationEnvironment>().also {
+            doNothing().whenever(it).unset()
+        }
+    }
+}
\ No newline at end of file
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalTestConstants.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalTestConstants.kt
new file mode 100644
index 0000000000..d9165951a8
--- /dev/null
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/InternalTestConstants.kt
@@ -0,0 +1,7 @@
+package net.corda.testing.internal
+
+import net.corda.nodeapi.internal.crypto.CertificateAndKeyPair
+
+val DEV_INTERMEDIATE_CA: CertificateAndKeyPair by lazy { net.corda.nodeapi.internal.DEV_INTERMEDIATE_CA }
+
+val DEV_ROOT_CA: CertificateAndKeyPair by lazy { net.corda.nodeapi.internal.DEV_ROOT_CA }
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/node/MockCordappConfigProvider.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappConfigProvider.kt
similarity index 92%
rename from testing/test-utils/src/main/kotlin/net/corda/testing/node/MockCordappConfigProvider.kt
rename to testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappConfigProvider.kt
index d351154982..6f2f99cfa8 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/node/MockCordappConfigProvider.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappConfigProvider.kt
@@ -1,4 +1,4 @@
-package net.corda.testing.node
+package net.corda.testing.internal
 
 import com.typesafe.config.Config
 import com.typesafe.config.ConfigFactory
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/services/MockCordappProvider.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappProvider.kt
similarity index 94%
rename from testing/test-utils/src/main/kotlin/net/corda/testing/services/MockCordappProvider.kt
rename to testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappProvider.kt
index 82ca31d6f9..4638d73fb3 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/services/MockCordappProvider.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/MockCordappProvider.kt
@@ -1,4 +1,4 @@
-package net.corda.testing.services
+package net.corda.testing.internal
 
 import net.corda.core.contracts.ContractClassName
 import net.corda.core.cordapp.Cordapp
@@ -7,7 +7,7 @@ import net.corda.core.node.services.AttachmentId
 import net.corda.core.node.services.AttachmentStorage
 import net.corda.node.internal.cordapp.CordappLoader
 import net.corda.node.internal.cordapp.CordappProviderImpl
-import net.corda.testing.node.MockCordappConfigProvider
+import net.corda.testing.services.MockAttachmentStorage
 import java.nio.file.Paths
 import java.util.*
 
@@ -31,7 +31,7 @@ class MockCordappProvider(
                 serializationWhitelists = emptyList(),
                 serializationCustomSerializers = emptyList(),
                 customSchemas = emptySet(),
-                jarPath = Paths.get(".").toUri().toURL())
+                jarPath = Paths.get("").toUri().toURL())
         if (cordappRegistry.none { it.first.contractClassNames.contains(contractClassName) }) {
             cordappRegistry.add(Pair(cordapp, findOrImportAttachment(contractClassName.toByteArray(), attachments)))
         }
diff --git a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/TestNodeInfoBuilder.kt b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/TestNodeInfoBuilder.kt
index c99ca190cf..8757adde2b 100644
--- a/testing/test-utils/src/main/kotlin/net/corda/testing/internal/TestNodeInfoBuilder.kt
+++ b/testing/test-utils/src/main/kotlin/net/corda/testing/internal/TestNodeInfoBuilder.kt
@@ -12,8 +12,6 @@ import net.corda.nodeapi.internal.createDevNodeCa
 import net.corda.nodeapi.internal.crypto.CertificateAndKeyPair
 import net.corda.nodeapi.internal.crypto.CertificateType
 import net.corda.nodeapi.internal.crypto.X509Utilities
-import net.corda.testing.core.DEV_INTERMEDIATE_CA
-import net.corda.testing.core.DEV_ROOT_CA
 import java.security.KeyPair
 import java.security.PrivateKey
 import java.security.cert.X509Certificate