diff --git a/docs/wallet/wallet-rescan.md b/docs/wallet/wallet-rescan.md index 6a1dcd5367..ab9dd97e7a 100644 --- a/docs/wallet/wallet-rescan.md +++ b/docs/wallet/wallet-rescan.md @@ -44,6 +44,7 @@ import org.bitcoins.testkit.BitcoinSTestAppConfig import org.bitcoins.testkit.fixtures._ import org.bitcoins.testkit.wallet._ import org.bitcoins.server.BitcoinSAppConfig +import org.bitcoins.wallet.config.WalletAppConfig import akka.actor.ActorSystem import scala.concurrent.{ExecutionContext, Future, Await} import scala.concurrent.duration.DurationInt @@ -55,6 +56,7 @@ import scala.concurrent.duration.DurationInt implicit val system: ActorSystem = ActorSystem(s"wallet-rescan-example") implicit val ec: ExecutionContext = system.dispatcher implicit val appConfig: BitcoinSAppConfig = BitcoinSTestAppConfig.getNeutrinoTestConfig() +implicit val walletAppConfig: WalletAppConfig = appConfig.walletConf val bip39PasswordOpt = None //ok now let's spin up a bitcoind and a bitcoin-s wallet with funds in it diff --git a/node-test/src/test/scala/org/bitcoins/node/BroadcastTransactionTest.scala b/node-test/src/test/scala/org/bitcoins/node/BroadcastTransactionTest.scala index 420722185f..9d0629d147 100644 --- a/node-test/src/test/scala/org/bitcoins/node/BroadcastTransactionTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/BroadcastTransactionTest.scala @@ -19,7 +19,7 @@ class BroadcastTransactionTest extends NodeTestWithCachedBitcoindV19 { /** Wallet config with data directory set to user temp directory */ override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNodeConnectedWithBitcoindV19 diff --git a/node-test/src/test/scala/org/bitcoins/node/DisconnectedPeerTest.scala b/node-test/src/test/scala/org/bitcoins/node/DisconnectedPeerTest.scala index c35ff95bb8..47fe6b84e3 100644 --- a/node-test/src/test/scala/org/bitcoins/node/DisconnectedPeerTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/DisconnectedPeerTest.scala @@ -2,24 +2,20 @@ package org.bitcoins.node import org.bitcoins.server.BitcoinSAppConfig import org.bitcoins.testkit.BitcoinSTestAppConfig +import org.bitcoins.testkit.node.NodeUnitTest import org.bitcoins.testkitcore.Implicits._ import org.bitcoins.testkitcore.gen.TransactionGenerators -import org.bitcoins.testkit.node.{CachedBitcoinSAppConfig, NodeUnitTest} import org.scalatest.FutureOutcome -class DisconnectedPeerTest extends NodeUnitTest with CachedBitcoinSAppConfig { +class DisconnectedPeerTest extends NodeUnitTest { override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) - - /** Wallet config with data directory set to user temp directory */ - implicit override protected lazy val cachedConfig: BitcoinSAppConfig = - getFreshConfig + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNode def withFixture(test: OneArgAsyncTest): FutureOutcome = - withDisconnectedSpvNode(test)(system, cachedConfig) + withDisconnectedSpvNode(test)(system, getFreshConfig) it must "fail to broadcast a transaction when disconnected" in { node => val tx = TransactionGenerators.transaction.sampleSome diff --git a/node-test/src/test/scala/org/bitcoins/node/NeutrinoNodeTest.scala b/node-test/src/test/scala/org/bitcoins/node/NeutrinoNodeTest.scala index 35606dbaae..40a21ee0ee 100644 --- a/node-test/src/test/scala/org/bitcoins/node/NeutrinoNodeTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/NeutrinoNodeTest.scala @@ -4,7 +4,6 @@ import akka.actor.Cancellable import org.bitcoins.crypto.DoubleSha256DigestBE import org.bitcoins.server.BitcoinSAppConfig import org.bitcoins.testkit.BitcoinSTestAppConfig -import org.bitcoins.testkit.fixtures.UsesExperimentalBitcoind import org.bitcoins.testkit.node.fixture.NeutrinoNodeConnectedWithBitcoind import org.bitcoins.testkit.node.{ NodeTestUtil, @@ -35,7 +34,7 @@ class NeutrinoNodeTest extends NodeTestWithCachedBitcoindNewest { behavior of "NeutrinoNode" - it must "receive notification that a block occurred on the p2p network" taggedAs UsesExperimentalBitcoind in { + it must "receive notification that a block occurred on the p2p network for neutrino" in { nodeConnectedWithBitcoind: NeutrinoNodeConnectedWithBitcoind => val node = nodeConnectedWithBitcoind.node @@ -62,7 +61,7 @@ class NeutrinoNodeTest extends NodeTestWithCachedBitcoindNewest { } } - it must "stay in sync with a bitcoind instance" taggedAs UsesExperimentalBitcoind in { + it must "stay in sync with a bitcoind instance for neutrino" in { nodeConnectedWithBitcoind: NeutrinoNodeConnectedWithBitcoind => val node = nodeConnectedWithBitcoind.node val bitcoind = nodeConnectedWithBitcoind.bitcoind diff --git a/node-test/src/test/scala/org/bitcoins/node/SpvNodeTest.scala b/node-test/src/test/scala/org/bitcoins/node/SpvNodeTest.scala index 7bde0b3201..185531a1da 100644 --- a/node-test/src/test/scala/org/bitcoins/node/SpvNodeTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/SpvNodeTest.scala @@ -19,7 +19,7 @@ class SpvNodeTest extends NodeTestWithCachedBitcoindNewest { /** Wallet config with data directory set to user temp directory */ override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNodeConnectedWithBitcoind diff --git a/node-test/src/test/scala/org/bitcoins/node/SpvNodeWithWalletTest.scala b/node-test/src/test/scala/org/bitcoins/node/SpvNodeWithWalletTest.scala index 8355b2bc88..c41cd4dece 100644 --- a/node-test/src/test/scala/org/bitcoins/node/SpvNodeWithWalletTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/SpvNodeWithWalletTest.scala @@ -19,7 +19,7 @@ class SpvNodeWithWalletTest extends NodeTestWithCachedBitcoindNewest { /** Wallet config with data directory set to user temp directory */ override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNodeFundedWalletBitcoind diff --git a/node-test/src/test/scala/org/bitcoins/node/UpdateBloomFilterTest.scala b/node-test/src/test/scala/org/bitcoins/node/UpdateBloomFilterTest.scala index c3719d83be..bebc265af6 100644 --- a/node-test/src/test/scala/org/bitcoins/node/UpdateBloomFilterTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/UpdateBloomFilterTest.scala @@ -16,7 +16,7 @@ class UpdateBloomFilterTest extends NodeTestWithCachedBitcoindNewest { /** Wallet config with data directory set to user temp directory */ override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNodeFundedWalletBitcoind diff --git a/node-test/src/test/scala/org/bitcoins/node/models/BroadcastAbleTransactionDAOTest.scala b/node-test/src/test/scala/org/bitcoins/node/models/BroadcastAbleTransactionDAOTest.scala index b311dff738..c4a17bcc8e 100644 --- a/node-test/src/test/scala/org/bitcoins/node/models/BroadcastAbleTransactionDAOTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/models/BroadcastAbleTransactionDAOTest.scala @@ -1,16 +1,10 @@ package org.bitcoins.node.models -import org.bitcoins.server.BitcoinSAppConfig +import org.bitcoins.testkit.fixtures.NodeDAOFixture import org.bitcoins.testkitcore.Implicits._ import org.bitcoins.testkitcore.gen.TransactionGenerators -import org.bitcoins.testkit.fixtures.NodeDAOFixture -import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg} -class BroadcastAbleTransactionDAOTest extends NodeDAOFixture with EmbeddedPg { - - /** Wallet config with data directory set to user temp directory */ - override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) +class BroadcastAbleTransactionDAOTest extends NodeDAOFixture { behavior of "BroadcastAbleTransactionDAO" diff --git a/node-test/src/test/scala/org/bitcoins/node/networking/peer/DataMessageHandlerTest.scala b/node-test/src/test/scala/org/bitcoins/node/networking/peer/DataMessageHandlerTest.scala index 3efb7654a0..fdaf9be910 100644 --- a/node-test/src/test/scala/org/bitcoins/node/networking/peer/DataMessageHandlerTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/networking/peer/DataMessageHandlerTest.scala @@ -21,7 +21,7 @@ class DataMessageHandlerTest extends NodeUnitTest { /** Wallet config with data directory set to user temp directory */ override protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) override type FixtureParam = SpvNodeConnectedWithBitcoindV19 diff --git a/node-test/src/test/scala/org/bitcoins/node/networking/peer/MerkleBuffersTest.scala b/node-test/src/test/scala/org/bitcoins/node/networking/peer/MerkleBuffersTest.scala index 84eac95c94..7673b8d419 100644 --- a/node-test/src/test/scala/org/bitcoins/node/networking/peer/MerkleBuffersTest.scala +++ b/node-test/src/test/scala/org/bitcoins/node/networking/peer/MerkleBuffersTest.scala @@ -78,6 +78,10 @@ class MerkleBuffersTest extends BitcoinSAsyncTest with CachedBitcoinSAppConfig { } } + } + override def afterAll(): Unit = { + super[CachedBitcoinSAppConfig].afterAll() + super[BitcoinSAsyncTest].afterAll() } } diff --git a/testkit/src/main/scala/org/bitcoins/testkit/BitcoinSTestAppConfig.scala b/testkit/src/main/scala/org/bitcoins/testkit/BitcoinSTestAppConfig.scala index 6d036db8dc..e534e4d151 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/BitcoinSTestAppConfig.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/BitcoinSTestAppConfig.scala @@ -52,7 +52,8 @@ object BitcoinSTestAppConfig { def getSpvWithEmbeddedDbTestConfig( pgUrl: () => Option[String], - config: Config*)(implicit ec: ExecutionContext): BitcoinSAppConfig = { + config: Vector[Config])(implicit + ec: ExecutionContext): BitcoinSAppConfig = { val overrideConf = ConfigFactory .parseString { """ diff --git a/testkit/src/main/scala/org/bitcoins/testkit/chain/ChainUnitTest.scala b/testkit/src/main/scala/org/bitcoins/testkit/chain/ChainUnitTest.scala index 968277f4de..58dc030cab 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/chain/ChainUnitTest.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/chain/ChainUnitTest.scala @@ -580,7 +580,11 @@ object ChainUnitTest extends ChainVerificationLogger { val stopBitcoindF = BitcoindRpcTestUtil.stopServer(bitcoindChainHandler.bitcoindRpc) val dropTableF = ChainUnitTest.destroyAllTables() - stopBitcoindF.flatMap(_ => dropTableF) + val stoppedChainAppConfigF = dropTableF.flatMap(_ => chainAppConfig.stop()) + for { + _ <- stopBitcoindF + _ <- stoppedChainAppConfigF + } yield () } def destroyBitcoind(bitcoind: BitcoindRpcClient)(implicit diff --git a/testkit/src/main/scala/org/bitcoins/testkit/fixtures/NodeDAOFixture.scala b/testkit/src/main/scala/org/bitcoins/testkit/fixtures/NodeDAOFixture.scala index 106959f0ed..864210e8a5 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/fixtures/NodeDAOFixture.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/fixtures/NodeDAOFixture.scala @@ -1,7 +1,10 @@ package org.bitcoins.testkit.fixtures +import org.bitcoins.chain.config.ChainAppConfig import org.bitcoins.node.config.NodeAppConfig import org.bitcoins.node.models.BroadcastAbleTransactionDAO +import org.bitcoins.server.BitcoinSAppConfig +import org.bitcoins.testkit.BitcoinSTestAppConfig import org.bitcoins.testkit.node.{CachedBitcoinSAppConfig, NodeUnitTest} import org.scalatest._ @@ -12,6 +15,10 @@ case class NodeDAOs(txDAO: BroadcastAbleTransactionDAO) /** Provides a fixture where all DAOs used by the node projects are provided */ trait NodeDAOFixture extends NodeUnitTest with CachedBitcoinSAppConfig { + /** Wallet config with data directory set to user temp directory */ + override protected def getFreshConfig: BitcoinSAppConfig = + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, Vector.empty) + private lazy val daos = { val tx = BroadcastAbleTransactionDAO() NodeDAOs(tx) @@ -21,17 +28,29 @@ trait NodeDAOFixture extends NodeUnitTest with CachedBitcoinSAppConfig { def withFixture(test: OneArgAsyncTest): FutureOutcome = { makeFixture(build = () => { - cachedNodeConf - .start() - .map(_ => daos) + for { + _ <- cachedChainConf.start() + _ <- cachedNodeConf.start() + } yield daos }, - destroy = () => destroyAppConfig(cachedNodeConf))(test) + destroy = + () => destroyAppConfig(cachedChainConf, cachedNodeConf))(test) } - private def destroyAppConfig(nodeConfig: NodeAppConfig): Future[Unit] = { + private def destroyAppConfig( + chainConfig: ChainAppConfig, + nodeConfig: NodeAppConfig): Future[Unit] = { + for { _ <- nodeConfig.dropAll() _ <- nodeConfig.stop() + _ <- chainConfig.dropAll() + _ <- chainConfig.stop() } yield () } + + override def afterAll(): Unit = { + super[CachedBitcoinSAppConfig].afterAll() + super[NodeUnitTest].afterAll() + } } diff --git a/testkit/src/main/scala/org/bitcoins/testkit/node/NodeTestWithCachedBitcoind.scala b/testkit/src/main/scala/org/bitcoins/testkit/node/NodeTestWithCachedBitcoind.scala index 23b613c570..4d273c234c 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/node/NodeTestWithCachedBitcoind.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/node/NodeTestWithCachedBitcoind.scala @@ -1,12 +1,11 @@ package org.bitcoins.testkit.node import akka.actor.ActorSystem -import org.bitcoins.node.{Node, NodeType} import org.bitcoins.node.models.Peer +import org.bitcoins.node.{Node, NodeType} import org.bitcoins.rpc.client.common.BitcoindRpcClient import org.bitcoins.rpc.client.v19.BitcoindV19RpcClient import org.bitcoins.server.BitcoinSAppConfig -import org.bitcoins.testkit.chain.ChainUnitTest import org.bitcoins.testkit.node.NodeUnitTest.{createPeer, syncNeutrinoNode} import org.bitcoins.testkit.node.fixture.{ NeutrinoNodeConnectedWithBitcoind, @@ -146,8 +145,6 @@ trait NodeTestWithCachedBitcoind extends BaseNodeTest { _: CachedBitcoind[_] => val destroyNodeF = NodeUnitTest.destroyNode(node) for { _ <- destroyNodeF - _ <- ChainUnitTest.destroyAllTables()(node.chainAppConfig, - system.dispatcher) //need to stop chainAppConfig too since this is a test _ <- node.chainAppConfig.stop() } yield () diff --git a/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala b/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala index 25cde25707..cb33ca1cdc 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala @@ -246,6 +246,7 @@ object NodeUnitTest extends P2PLogger { def destroyNode(node: Node)(implicit ec: ExecutionContext): Future[Unit] = { for { _ <- node.stop() + _ <- node.chainAppConfig.stop() } yield { () } @@ -303,6 +304,7 @@ object NodeUnitTest extends P2PLogger { require(appConfig.nodeType == NodeType.SpvNode) for { node <- createSpvNode(createPeer(bitcoind)) + _ <- appConfig.walletConf.start() fundedWallet <- BitcoinSWalletTest.fundedWalletAndBitcoind( bitcoind, node, diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BaseWalletTest.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BaseWalletTest.scala index afebd79156..797b6b0321 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BaseWalletTest.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BaseWalletTest.scala @@ -14,7 +14,7 @@ import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg} import org.bitcoins.wallet.config.WalletAppConfig import org.scalatest.AsyncTestSuite -import scala.concurrent.Future +import scala.concurrent.{ExecutionContext, Future} /** Base test trait for all the tests in our walletTest module */ trait BaseWalletTest extends EmbeddedPg { _: AsyncTestSuite => @@ -39,10 +39,10 @@ trait BaseWalletTest extends EmbeddedPg { _: AsyncTestSuite => "00000000496dcc754fabd97f3e2df0a7337eab417d75537fecf97a7ebb0e7c75") /** Wallet config with data directory set to user temp directory */ - implicit protected def getFreshConfig: BitcoinSAppConfig = - BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl) + protected def getFreshConfig: BitcoinSAppConfig = + BaseWalletTest.getFreshConfig(pgUrl, Vector.empty) - implicit protected def getFreshWalletAppConfig: WalletAppConfig = { + protected def getFreshWalletAppConfig: WalletAppConfig = { getFreshConfig.walletConf } @@ -123,3 +123,19 @@ trait BaseWalletTest extends EmbeddedPg { _: AsyncTestSuite => } } + +object BaseWalletTest { + + def getFreshConfig(pgUrl: () => Option[String], config: Vector[Config])( + implicit ec: ExecutionContext): BitcoinSAppConfig = { + BitcoinSTestAppConfig.getSpvWithEmbeddedDbTestConfig(pgUrl, config) + } + + def getFreshWalletAppConfig( + pgUrl: () => Option[String], + config: Vector[Config])(implicit + ec: ExecutionContext): WalletAppConfig = { + getFreshConfig(pgUrl, config).walletConf + } + +} diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTest.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTest.scala index 3e47cc5c35..00b2828716 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTest.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTest.scala @@ -29,7 +29,6 @@ import org.bitcoins.testkit.EmbeddedPg import org.bitcoins.testkit.chain.SyncUtil import org.bitcoins.testkit.fixtures.BitcoinSFixture import org.bitcoins.testkit.keymanager.KeyManagerTestUtil -import org.bitcoins.testkit.util.FileUtil import org.bitcoins.testkit.wallet.FundWalletUtil.FundedWallet import org.bitcoins.testkitcore.Implicits.GeneratorOps import org.bitcoins.testkitcore.gen._ @@ -46,19 +45,13 @@ trait BitcoinSWalletTest with EmbeddedPg { import BitcoinSWalletTest._ - override def afterAll(): Unit = { - Await.result(getFreshConfig.chainConf.stop(), 1.minute) - Await.result(getFreshConfig.nodeConf.stop(), 1.minute) - Await.result(getFreshConfig.walletConf.stop(), 1.minute) - super.afterAll() - } - def nodeApi: NodeApi = MockNodeApi /** Lets you customize the parameters for the created wallet */ val withNewConfiguredWallet: Config => OneArgAsyncTest => FutureOutcome = { walletConfig => - val newWalletConf = getFreshWalletAppConfig.withOverrides(walletConfig) + implicit val newWalletConf = + getFreshWalletAppConfig.withOverrides(walletConfig) val km = createNewKeyManager()(newWalletConf) val bip39PasswordOpt = KeyManagerTestUtil.bip39PasswordOpt makeDependentFixture( @@ -75,9 +68,8 @@ trait BitcoinSWalletTest * peered with a bitcoind so the funds in the wallet are not tied to an * underlying blockchain */ - def withFundedWallet( - test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + def withFundedWallet(test: OneArgAsyncTest, bip39PasswordOpt: Option[String])( + implicit walletAppConfig: WalletAppConfig): FutureOutcome = { makeDependentFixture( build = () => FundWalletUtil.createFundedWallet(nodeApi, @@ -91,7 +83,8 @@ trait BitcoinSWalletTest def withFundedSegwitWallet( test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + bip39PasswordOpt: Option[String])(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { makeDependentFixture( build = () => FundWalletUtil.createFundedWallet(nodeApi, @@ -114,9 +107,8 @@ trait BitcoinSWalletTest withNewConfiguredWallet(segwitWalletConf)(test) } - def withNewWallet( - test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = + def withNewWallet(test: OneArgAsyncTest, bip39PasswordOpt: Option[String])( + implicit walletAppConfig: WalletAppConfig): FutureOutcome = makeDependentFixture( build = { () => createDefaultWallet(nodeApi, chainQueryApi, bip39PasswordOpt) @@ -125,7 +117,8 @@ trait BitcoinSWalletTest def withNewWallet2Accounts( test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + bip39PasswordOpt: Option[String])(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { makeDependentFixture( build = { () => createWallet2Accounts(nodeApi, chainQueryApi, bip39PasswordOpt) @@ -133,7 +126,8 @@ trait BitcoinSWalletTest destroy = destroyWallet)(test) } - def withNewWalletAndBitcoind(test: OneArgAsyncTest): FutureOutcome = { + def withNewWalletAndBitcoind(test: OneArgAsyncTest)(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoind] = composeBuildersAndWrap( builder = { () => BitcoinSFixture.createBitcoindWithFunds() @@ -150,7 +144,8 @@ trait BitcoinSWalletTest def withNewWalletAndBitcoindV19( test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + bip39PasswordOpt: Option[String])(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoind] = composeBuildersAndWrap( builder = { () => BitcoinSFixture @@ -170,7 +165,8 @@ trait BitcoinSWalletTest def withFundedWalletAndBitcoindV19( test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + bip39PasswordOpt: Option[String])(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoindV19] = { () => for { bitcoind <- @@ -192,30 +188,11 @@ trait BitcoinSWalletTest def withWalletConfig(test: OneArgAsyncTest): FutureOutcome = { val builder: () => Future[WalletAppConfig] = () => { - val baseConf = getFreshConfig.walletConf - val walletNameOpt = if (NumberGenerator.bool.sampleSome) { - Some(StringGenerators.genNonEmptyString.sampleSome) - } else None - - val walletConf = walletNameOpt match { - case Some(walletName) => - val walletNameOverride = ConfigFactory.parseString( - s"bitcoin-s.wallet.walletName = $walletName" - ) - - BitcoinSAppConfig( - baseConf.baseDatadir, - (walletNameOverride +: baseConf.configOverrides): _*).walletConf - case None => baseConf - } - - walletConf.start().map(_ => walletConf) + createWalletAppConfig(pgUrl, Vector.empty) } val destroy: WalletAppConfig => Future[Unit] = walletAppConfig => { - FileUtil.deleteTmpDir(walletAppConfig.datadir) - walletAppConfig.stop() - Future.unit + destroyWalletAppConfig(walletAppConfig) } makeDependentFixture(builder, destroy = destroy)(test) } @@ -299,6 +276,30 @@ object BitcoinSWalletTest extends WalletLogger { } } + def createWalletAppConfig( + pgUrl: () => Option[String], + configs: Vector[Config])(implicit + ec: ExecutionContext): Future[WalletAppConfig] = { + val baseConf = BaseWalletTest.getFreshWalletAppConfig(pgUrl, configs) + val walletNameOpt = if (NumberGenerator.bool.sampleSome) { + Some(StringGenerators.genNonEmptyString.sampleSome) + } else None + + val walletConf = walletNameOpt match { + case Some(walletName) => + val walletNameOverride = ConfigFactory.parseString( + s"bitcoin-s.wallet.walletName = $walletName" + ) + + BitcoinSAppConfig( + baseConf.baseDatadir, + (walletNameOverride +: baseConf.configOverrides): _*).walletConf + case None => baseConf + } + + walletConf.start().map(_ => walletConf) + } + /** Returns a function that can be used to create a wallet fixture. * If you pass in a configuration to this method that configuration * is given to the wallet as user-provided overrides. You could for @@ -311,13 +312,12 @@ object BitcoinSWalletTest extends WalletLogger { extraConfig: Option[Config], nodeApi: NodeApi, chainQueryApi: ChainQueryApi)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, ec: ExecutionContext): () => Future[Wallet] = () => { - val defaultConf = config.walletConf val walletConfig = extraConfig match { - case None => defaultConf - case Some(c) => defaultConf.withOverrides(c) + case None => config + case Some(c) => config.withOverrides(c) } // we want to check we're not overwriting @@ -341,13 +341,13 @@ object BitcoinSWalletTest extends WalletLogger { chainQueryApi: ChainQueryApi, bip39PasswordOpt: Option[String], extraConfig: Option[Config] = None)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, ec: ExecutionContext): Future[Wallet] = { val newWalletConf = extraConfig match { case None => - config.walletConf + config case Some(walletConf) => - config.walletConf.withOverrides(walletConf) + config.withOverrides(walletConf) } val km = createNewKeyManager(bip39PasswordOpt = bip39PasswordOpt)(newWalletConf) @@ -366,7 +366,7 @@ object BitcoinSWalletTest extends WalletLogger { bitcoind: BitcoindRpcClient, bip39PasswordOpt: Option[String], extraConfig: Option[Config] = None)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, system: ActorSystem): Future[WalletWithBitcoind] = { import system.dispatcher //we need to create a promise so we can inject the wallet with the callback @@ -404,7 +404,7 @@ object BitcoinSWalletTest extends WalletLogger { chainQueryApi: ChainQueryApi, bip39PasswordOpt: Option[String], extraConfig: Option[Config] = None)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, system: ActorSystem): Future[Wallet] = { implicit val ec: ExecutionContextExecutor = system.dispatcher @@ -440,7 +440,7 @@ object BitcoinSWalletTest extends WalletLogger { def createWalletWithBitcoind(bitcoind: BitcoindRpcClient)(implicit system: ActorSystem, - config: BitcoinSAppConfig): Future[WalletWithBitcoind] = { + config: WalletAppConfig): Future[WalletWithBitcoind] = { createWalletWithBitcoindCallbacks(bitcoind, None) } @@ -460,7 +460,7 @@ object BitcoinSWalletTest extends WalletLogger { bitcoind: BitcoindV19RpcClient, bip39PasswordOpt: Option[String])(implicit system: ActorSystem, - config: BitcoinSAppConfig): Future[WalletWithBitcoindV19] = { + config: WalletAppConfig): Future[WalletWithBitcoindV19] = { import system.dispatcher for { created <- createWalletWithBitcoindCallbacks(bitcoind, bip39PasswordOpt) @@ -471,7 +471,7 @@ object BitcoinSWalletTest extends WalletLogger { bitcoind: BitcoindRpcClient, bip39PasswordOpt: Option[String])(implicit system: ActorSystem, - config: BitcoinSAppConfig): Future[WalletWithBitcoindRpc] = { + config: WalletAppConfig): Future[WalletWithBitcoindRpc] = { import system.dispatcher for { created <- createWalletWithBitcoindCallbacks(bitcoind, bip39PasswordOpt) @@ -583,10 +583,15 @@ object BitcoinSWalletTest extends WalletLogger { _ <- wallet.walletConfig.dropTable("flyway_schema_history") _ <- wallet.walletConfig.dropAll() _ <- wallet.stop() - _ <- wallet.walletConfig.stop() + _ <- destroyWalletAppConfig(wallet.walletConfig) } yield () } + def destroyWalletAppConfig(walletAppConfig: WalletAppConfig): Future[Unit] = { + val stoppedF = walletAppConfig.stop() + stoppedF + } + /** Constructs callbacks for the wallet from the node to process blocks and compact filters */ def createNeutrinoNodeCallbacksForWallet(wallet: Wallet)(implicit ec: ExecutionContext): NodeCallbacks = { @@ -624,7 +629,7 @@ object BitcoinSWalletTest extends WalletLogger { * [[BitcoinSWalletTest]]. This will future won't be completed until balances satisfy [[isSameWalletBalances()]] */ def awaitWalletBalances(fundedWallet: WalletWithBitcoind)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, system: ActorSystem): Future[Unit] = { AsyncUtil.retryUntilSatisfiedF(conditionF = () => isSameWalletBalances(fundedWallet), @@ -632,11 +637,11 @@ object BitcoinSWalletTest extends WalletLogger { } private def isSameWalletBalances(fundedWallet: WalletWithBitcoind)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, system: ActorSystem): Future[Boolean] = { import system.dispatcher - val defaultAccount = config.walletConf.defaultAccount - val hdAccount1 = WalletTestUtil.getHdAccount1(config.walletConf) + val defaultAccount = config.defaultAccount + val hdAccount1 = WalletTestUtil.getHdAccount1(config) val expectedDefaultAmt = BitcoinSWalletTest.expectedDefaultAmt val expectedAccount1Amt = BitcoinSWalletTest.expectedAccount1Amt val defaultBalanceF = fundedWallet.wallet.getBalance(defaultAccount) diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTestCachedBitcoind.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTestCachedBitcoind.scala index 702902a3b1..693bbe5ba4 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTestCachedBitcoind.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/BitcoinSWalletTestCachedBitcoind.scala @@ -17,6 +17,7 @@ import org.bitcoins.testkit.wallet.BitcoinSWalletTest.{ destroyWallet, fundWalletWithBitcoind } +import org.bitcoins.wallet.config.WalletAppConfig import org.scalatest.{FutureOutcome, Outcome} import scala.concurrent.Future @@ -42,7 +43,8 @@ trait BitcoinSWalletTestCachedBitcoind def withFundedWalletAndBitcoindCached( test: OneArgAsyncTest, bip39PasswordOpt: Option[String], - bitcoind: BitcoindRpcClient): FutureOutcome = { + bitcoind: BitcoindRpcClient)(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoind] = { () => for { walletWithBitcoind <- createWalletWithBitcoindCallbacks( @@ -65,7 +67,8 @@ trait BitcoinSWalletTestCachedBitcoind def withNewWalletAndBitcoindCached( test: OneArgAsyncTest, bip39PasswordOpt: Option[String], - bitcoind: BitcoindRpcClient): FutureOutcome = { + bitcoind: BitcoindRpcClient)(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoind] = composeBuildersAndWrap( builder = { () => Future.successful(bitcoind) @@ -86,7 +89,8 @@ trait BitcoinSWalletTestCachedBitcoind def withFundedWalletAndBitcoind( test: OneArgAsyncTest, - bip39PasswordOpt: Option[String]): FutureOutcome = { + bip39PasswordOpt: Option[String])(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val bitcoindF = BitcoinSFixture .createBitcoindWithFunds(None) @@ -157,7 +161,8 @@ trait BitcoinSWalletTestCachedBitcoinV19 def withFundedWalletAndBitcoindCachedV19( test: OneArgAsyncTest, bip39PasswordOpt: Option[String], - bitcoind: BitcoindV19RpcClient): FutureOutcome = { + bitcoind: BitcoindV19RpcClient)(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoindV19] = { () => for { walletWithBitcoind <- createWalletWithBitcoindCallbacks( @@ -183,7 +188,8 @@ trait BitcoinSWalletTestCachedBitcoinV19 def withNewWalletAndBitcoindCachedV19( test: OneArgAsyncTest, bip39PasswordOpt: Option[String], - bitcoind: BitcoindV19RpcClient): FutureOutcome = { + bitcoind: BitcoindV19RpcClient)(implicit + walletAppConfig: WalletAppConfig): FutureOutcome = { val builder: () => Future[WalletWithBitcoind] = composeBuildersAndWrap( builder = { () => Future.successful(bitcoind) @@ -207,9 +213,10 @@ trait BitcoinSWalletTestCachedBitcoinV19 override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withFundedWalletAndBitcoindCachedV19(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withFundedWalletAndBitcoindCachedV19( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/FundWalletUtil.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/FundWalletUtil.scala index 918c1a4f88..3059d33be2 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/wallet/FundWalletUtil.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/FundWalletUtil.scala @@ -2,19 +2,19 @@ package org.bitcoins.testkit.wallet import akka.actor.ActorSystem import com.typesafe.config.Config +import grizzled.slf4j.Logging import org.bitcoins.core.api.chain.ChainQueryApi import org.bitcoins.core.api.node.NodeApi import org.bitcoins.core.currency.CurrencyUnit import org.bitcoins.core.hd.HDAccount import org.bitcoins.core.protocol.BitcoinAddress import org.bitcoins.core.protocol.transaction.TransactionOutput -import grizzled.slf4j.Logging import org.bitcoins.crypto.DoubleSha256DigestBE import org.bitcoins.rpc.client.common.BitcoindRpcClient -import org.bitcoins.server.BitcoinSAppConfig import org.bitcoins.testkit.wallet.FundWalletUtil.FundedWallet import org.bitcoins.testkitcore.util.TransactionTestUtil import org.bitcoins.wallet.Wallet +import org.bitcoins.wallet.config.WalletAppConfig import scala.concurrent.{ExecutionContext, Future} @@ -129,7 +129,7 @@ object FundWalletUtil extends FundWalletUtil { chainQueryApi: ChainQueryApi, bip39PasswordOpt: Option[String], extraConfig: Option[Config] = None)(implicit - config: BitcoinSAppConfig, + config: WalletAppConfig, system: ActorSystem): Future[FundedWallet] = { import system.dispatcher diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoind.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoind.scala new file mode 100644 index 0000000000..18f3819f16 --- /dev/null +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoind.scala @@ -0,0 +1,13 @@ +package org.bitcoins.testkit.wallet + +import org.bitcoins.rpc.client.common.BitcoindRpcClient +import org.bitcoins.wallet.config.WalletAppConfig + +sealed trait WalletAppConfigWithBitcoind { + def bitcoind: BitcoindRpcClient + def walletAppConfig: WalletAppConfig +} + +case class WalletAppConfigWithBitcoindRpc( + walletAppConfig: WalletAppConfig, + bitcoind: BitcoindRpcClient) diff --git a/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoindFixtures.scala b/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoindFixtures.scala new file mode 100644 index 0000000000..d050403266 --- /dev/null +++ b/testkit/src/main/scala/org/bitcoins/testkit/wallet/WalletAppConfigWithBitcoindFixtures.scala @@ -0,0 +1,63 @@ +package org.bitcoins.testkit.wallet + +import org.bitcoins.rpc.client.common.BitcoindRpcClient +import org.bitcoins.testkit.EmbeddedPg +import org.bitcoins.testkit.rpc.{ + BitcoindFixturesCached, + CachedBitcoind, + CachedBitcoindNewest +} +import org.bitcoins.testkit.util.BitcoinSAsyncFixtureTest +import org.scalatest.{FutureOutcome, Outcome} + +import scala.concurrent.Future + +trait WalletAppConfigWithBitcoindFixtures + extends BitcoinSAsyncFixtureTest + with BitcoindFixturesCached + with EmbeddedPg { _: CachedBitcoind[_] => + + override def afterAll(): Unit = { + super[BitcoinSAsyncFixtureTest].afterAll() + } +} + +trait WalletAppConfigWithBitcoindNewestFixtures + extends WalletAppConfigWithBitcoindFixtures + with CachedBitcoindNewest { + + override def afterAll(): Unit = { + super[CachedBitcoindNewest].afterAll() + super[WalletAppConfigWithBitcoindFixtures].afterAll() + } + + override type FixtureParam = WalletAppConfigWithBitcoindRpc + + override def withFixture(test: OneArgAsyncTest): FutureOutcome = { + val f: Future[Outcome] = for { + bitcoind <- cachedBitcoindWithFundsF + futOutcome = withWalletAppConfigBitcoindCached(test, bitcoind) + fut <- futOutcome.toFuture + } yield fut + new FutureOutcome(f) + } + + def withWalletAppConfigBitcoindCached( + test: OneArgAsyncTest, + bitcoind: BitcoindRpcClient): FutureOutcome = { + makeDependentFixture[WalletAppConfigWithBitcoindRpc]( + () => { + val walletConfig = + BaseWalletTest.getFreshWalletAppConfig(pgUrl, Vector.empty) + val model = WalletAppConfigWithBitcoindRpc(walletConfig, bitcoind) + Future.successful(model) + }, + { case walletAppConfigWithBitcoindRpc => + BitcoinSWalletTest.destroyWalletAppConfig( + walletAppConfigWithBitcoindRpc.walletAppConfig) + } + )(test) + } +} + +object WalletAppConfigWithBitcoindFixtures {} diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/AddressHandlingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/AddressHandlingTest.scala index 296ba710f3..8802dbeff4 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/AddressHandlingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/AddressHandlingTest.scala @@ -21,7 +21,7 @@ class AddressHandlingTest extends BitcoinSWalletTest { type FixtureParam = FundedWallet override def withFixture(test: OneArgAsyncTest): FutureOutcome = { - withFundedWallet(test, getBIP39PasswordOpt()) + withFundedWallet(test, getBIP39PasswordOpt())(getFreshWalletAppConfig) } behavior of "AddressHandling" diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/AddressTagIntegrationTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/AddressTagIntegrationTest.scala index 4f233f818f..15bc678d4c 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/AddressTagIntegrationTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/AddressTagIntegrationTest.scala @@ -18,7 +18,7 @@ class AddressTagIntegrationTest extends BitcoinSWalletTest { override type FixtureParam = WalletWithBitcoind override def withFixture(test: OneArgAsyncTest): FutureOutcome = - withNewWalletAndBitcoind(test) + withNewWalletAndBitcoind(test)(getFreshWalletAppConfig) behavior of "Address Tag - integration test" diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBackendTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBackendTest.scala index af59cf3b5f..a9b92f8c8b 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBackendTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBackendTest.scala @@ -2,43 +2,18 @@ package org.bitcoins.wallet import org.bitcoins.commons.jsonmodels.wallet.SyncHeightDescriptor import org.bitcoins.core.currency._ -import org.bitcoins.db.AppConfig -import org.bitcoins.rpc.client.common.BitcoindRpcClient -import org.bitcoins.server.{BitcoinSAppConfig, BitcoindRpcBackendUtil} -import org.bitcoins.testkit.rpc.{ - BitcoindFixturesFundedCached, - CachedBitcoindNewest +import org.bitcoins.server.BitcoindRpcBackendUtil +import org.bitcoins.testkit.wallet.{ + BitcoinSWalletTest, + WalletAppConfigWithBitcoindNewestFixtures } -import org.bitcoins.testkit.util.BitcoinSAsyncFixtureTest -import org.bitcoins.testkit.wallet.BitcoinSWalletTest -import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg} -class BitcoindBackendTest - extends BitcoinSAsyncFixtureTest - with BitcoindFixturesFundedCached - with CachedBitcoindNewest - with EmbeddedPg { +class BitcoindBackendTest extends WalletAppConfigWithBitcoindNewestFixtures { - override type FixtureParam = BitcoindRpcClient - - /** Wallet config with data directory set to user temp directory */ - - implicit protected def config: BitcoinSAppConfig = - BitcoinSTestAppConfig.getNeutrinoWithEmbeddedDbTestConfig(pgUrl) - - override def beforeAll(): Unit = { - AppConfig.throwIfDefaultDatadir(config.walletConf) - super[EmbeddedPg].beforeAll() - } - - override def afterAll(): Unit = { - super[CachedBitcoindNewest].afterAll() - super[EmbeddedPg].afterAll() - } - - it must "correctly catch up to bitcoind" in { bitcoind => + it must "correctly catch up to bitcoind" in { walletAppConfigWithBitcoind => + val bitcoind = walletAppConfigWithBitcoind.bitcoind + implicit val walletAppConfig = walletAppConfigWithBitcoind.walletAppConfig val amountToSend = Bitcoins.one - for { header <- bitcoind.getBestBlockHeader() diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBlockPollingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBlockPollingTest.scala index 07077707d5..31a591f3fa 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBlockPollingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/BitcoindBlockPollingTest.scala @@ -2,77 +2,57 @@ package org.bitcoins.wallet import org.bitcoins.asyncutil.AsyncUtil import org.bitcoins.core.currency._ -import org.bitcoins.db.AppConfig -import org.bitcoins.rpc.client.common.BitcoindRpcClient -import org.bitcoins.server.{BitcoinSAppConfig, BitcoindRpcBackendUtil} -import org.bitcoins.testkit.rpc.{ - BitcoindFixturesFundedCached, - CachedBitcoindNewest +import org.bitcoins.server.BitcoindRpcBackendUtil +import org.bitcoins.testkit.wallet.{ + BitcoinSWalletTest, + WalletAppConfigWithBitcoindNewestFixtures } -import org.bitcoins.testkit.util.BitcoinSAsyncFixtureTest -import org.bitcoins.testkit.wallet.BitcoinSWalletTest -import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg} import scala.concurrent.duration.DurationInt class BitcoindBlockPollingTest - extends BitcoinSAsyncFixtureTest - with BitcoindFixturesFundedCached - with CachedBitcoindNewest - with EmbeddedPg { + extends WalletAppConfigWithBitcoindNewestFixtures { - override type FixtureParam = BitcoindRpcClient + it must "properly setup and poll blocks from bitcoind" in { + walletAppConfigWithBitcoind => + val bitcoind = walletAppConfigWithBitcoind.bitcoind + implicit val walletAppConfig = walletAppConfigWithBitcoind.walletAppConfig - /** Wallet config with data directory set to user temp directory */ - implicit protected def config: BitcoinSAppConfig = - BitcoinSTestAppConfig.getNeutrinoWithEmbeddedDbTestConfig(pgUrl) + val amountToSend = Bitcoins.one - override def beforeAll(): Unit = { - AppConfig.throwIfDefaultDatadir(config.walletConf) - super[EmbeddedPg].beforeAll() - } + for { + // Setup wallet + tmpWallet <- + BitcoinSWalletTest.createDefaultWallet(bitcoind, bitcoind, None) + wallet = + BitcoindRpcBackendUtil.createWalletWithBitcoindCallbacks(bitcoind, + tmpWallet) + // Assert wallet is empty + isEmpty <- wallet.isEmpty() + _ = assert(isEmpty) - override def afterAll(): Unit = { - super[CachedBitcoindNewest].afterAll() - super[EmbeddedPg].afterAll() - } + // Send to wallet + addr <- wallet.getNewAddress() + _ <- bitcoind.sendToAddress(addr, amountToSend) - it must "properly setup and poll blocks from bitcoind" in { bitcoind => - val amountToSend = Bitcoins.one + // assert wallet hasn't seen it yet + firstBalance <- wallet.getBalance() + _ = assert(firstBalance == Satoshis.zero) - for { - // Setup wallet - tmpWallet <- - BitcoinSWalletTest.createDefaultWallet(bitcoind, bitcoind, None) - wallet = - BitcoindRpcBackendUtil.createWalletWithBitcoindCallbacks(bitcoind, - tmpWallet) - // Assert wallet is empty - isEmpty <- wallet.isEmpty() - _ = assert(isEmpty) + // Setup block polling + _ = BitcoindRpcBackendUtil.startBitcoindBlockPolling(wallet, + bitcoind, + 1.second) + _ <- bitcoind.generateToAddress(6, addr) - // Send to wallet - addr <- wallet.getNewAddress() - _ <- bitcoind.sendToAddress(addr, amountToSend) + // Wait for it to process + _ <- AsyncUtil.awaitConditionF( + () => wallet.getBalance().map(_ > Satoshis.zero), + 1.second) - // assert wallet hasn't seen it yet - firstBalance <- wallet.getBalance() - _ = assert(firstBalance == Satoshis.zero) - - // Setup block polling - _ = BitcoindRpcBackendUtil.startBitcoindBlockPolling(wallet, - bitcoind, - 1.second) - _ <- bitcoind.generateToAddress(6, addr) - - // Wait for it to process - _ <- AsyncUtil.awaitConditionF( - () => wallet.getBalance().map(_ > Satoshis.zero), - 1.second) - - balance <- wallet.getBalance() - //clean up - _ <- wallet.walletConfig.stop() - } yield assert(balance == amountToSend) + balance <- wallet.getBalance() + //clean up + _ <- wallet.walletConfig.stop() + } yield assert(balance == amountToSend) } } diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/FundTransactionHandlingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/FundTransactionHandlingTest.scala index 2c75c80476..237bc5bb04 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/FundTransactionHandlingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/FundTransactionHandlingTest.scala @@ -23,9 +23,10 @@ class FundTransactionHandlingTest override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withFundedWalletAndBitcoindCached(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withFundedWalletAndBitcoindCached( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/MultiWalletTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/MultiWalletTest.scala index df17e01fd3..1efce987be 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/MultiWalletTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/MultiWalletTest.scala @@ -7,6 +7,9 @@ import org.bitcoins.testkit.util.BitcoinSAsyncTest import org.bitcoins.testkit.wallet.BitcoinSWalletTest import org.bitcoins.testkit.wallet.BitcoinSWalletTest._ import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg} +import org.scalatest.Assertion + +import scala.concurrent.Future class MultiWalletTest extends BitcoinSAsyncTest with EmbeddedPg { @@ -24,18 +27,26 @@ class MultiWalletTest extends BitcoinSAsyncTest with EmbeddedPg { val configA = BitcoinSAppConfig(dir, walletNameConfA.withFallback(dbConf)) val configB = BitcoinSAppConfig(dir, walletNameConfB.withFallback(dbConf)) - for { - walletA <- BitcoinSWalletTest.createDefaultWallet( - MockNodeApi, - MockChainQueryApi, - bip39PasswordOpt)(configA, system.dispatcher) + val walletAF = BitcoinSWalletTest.createDefaultWallet( + MockNodeApi, + MockChainQueryApi, + bip39PasswordOpt)(configA, system.dispatcher) + + val walletBF = BitcoinSWalletTest.createDefaultWallet( + MockNodeApi, + MockChainQueryApi, + bip39PasswordOpt)(configB, system.dispatcher) + + val assertionF: Future[Assertion] = for { + walletA <- walletAF accountA <- walletA.getDefaultAccount() - walletB <- BitcoinSWalletTest.createDefaultWallet( - MockNodeApi, - MockChainQueryApi, - bip39PasswordOpt)(configB, system.dispatcher) + walletB <- walletBF accountB <- walletB.getDefaultAccount() + _ <- walletA.walletConfig.stop() + _ <- walletB.walletConfig.stop() } yield assert(accountA.xpub != accountB.xpub) + + assertionF } } diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessBlockTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessBlockTest.scala index dea42b2af9..07fa0ab3e5 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessBlockTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessBlockTest.scala @@ -20,9 +20,10 @@ class ProcessBlockTest extends BitcoinSWalletTestCachedBitcoinV19 { override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withNewWalletAndBitcoindCachedV19(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withNewWalletAndBitcoindCachedV19( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala index a15cceee24..ecd6c6712f 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/ProcessTransactionTest.scala @@ -14,7 +14,7 @@ class ProcessTransactionTest extends BitcoinSWalletTest { override type FixtureParam = WalletApi override def withFixture(test: OneArgAsyncTest): FutureOutcome = { - withNewWallet(test, getBIP39PasswordOpt()) + withNewWallet(test, getBIP39PasswordOpt())(getFreshWalletAppConfig) } behavior of "Wallet.processTransaction" diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/RescanHandlingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/RescanHandlingTest.scala index 4087c28530..77b79cdd44 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/RescanHandlingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/RescanHandlingTest.scala @@ -17,7 +17,7 @@ import scala.concurrent.Future class RescanHandlingTest extends BitcoinSWalletTestCachedBitcoinV19 { /** Wallet config with data directory set to user temp directory */ - implicit override protected def getFreshConfig: BitcoinSAppConfig = + override protected def getFreshConfig: BitcoinSAppConfig = BitcoinSTestAppConfig.getNeutrinoWithEmbeddedDbTestConfig(pgUrl) override type FixtureParam = WalletWithBitcoind diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/TrezorAddressTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/TrezorAddressTest.scala index 7412e12671..8e6486531f 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/TrezorAddressTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/TrezorAddressTest.scala @@ -241,7 +241,7 @@ class TrezorAddressTest extends BitcoinSWalletTest with EmptyFixture { case other => fail(s"unknown purpose: $other") } - for { + val assertionsF: Future[Seq[Assertion]] = for { wallet <- getWallet(conf) existingAccounts <- wallet.listAccounts(purpose) _ <- createNeededAccounts(wallet, @@ -290,9 +290,11 @@ class TrezorAddressTest extends BitcoinSWalletTest with EmptyFixture { } nestedAssertions.flatten } + assertions + } - wallet.stop() - assert(assertions.forall(_.isCompleted)) + assertionsF.flatMap { _ => + conf.stop().map(_ => succeed) } } diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOHandlingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOHandlingTest.scala index ab4263b6fb..c216bbc626 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOHandlingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOHandlingTest.scala @@ -14,7 +14,7 @@ class UTXOHandlingTest extends BitcoinSWalletTest { override type FixtureParam = Wallet override def withFixture(test: OneArgAsyncTest): FutureOutcome = { - withNewWallet(test, getBIP39PasswordOpt()) + withNewWallet(test, getBIP39PasswordOpt())(getFreshWalletAppConfig) } it must "correctly update txo state based on confirmations" in { wallet => diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOLifeCycleTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOLifeCycleTest.scala index 2b3ec5a70f..325cdade19 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOLifeCycleTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/UTXOLifeCycleTest.scala @@ -28,9 +28,10 @@ class UTXOLifeCycleTest extends BitcoinSWalletTestCachedBitcoindNewest { override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withFundedWalletAndBitcoindCached(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withFundedWalletAndBitcoindCached( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletBloomTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletBloomTest.scala index a497cf90d7..cb0e54d7ea 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletBloomTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletBloomTest.scala @@ -19,9 +19,10 @@ class WalletBloomTest extends BitcoinSWalletTestCachedBitcoindNewest { override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withFundedWalletAndBitcoindCached(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withFundedWalletAndBitcoindCached( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletCallbackTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletCallbackTest.scala index 3303cababc..12832cba01 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletCallbackTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletCallbackTest.scala @@ -13,7 +13,7 @@ class WalletCallbackTest extends BitcoinSWalletTest { type FixtureParam = FundedWallet override def withFixture(test: OneArgAsyncTest): FutureOutcome = { - withFundedWallet(test, getBIP39PasswordOpt()) + withFundedWallet(test, getBIP39PasswordOpt())(getFreshWalletAppConfig) } behavior of "WalletCallbacks" diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletIntegrationTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletIntegrationTest.scala index b53591424b..bba35b5c8c 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletIntegrationTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletIntegrationTest.scala @@ -27,9 +27,10 @@ class WalletIntegrationTest extends BitcoinSWalletTestCachedBitcoindNewest { override def withFixture(test: OneArgAsyncTest): FutureOutcome = { val f: Future[Outcome] = for { bitcoind <- cachedBitcoindWithFundsF - futOutcome = withNewWalletAndBitcoindCached(test, - getBIP39PasswordOpt(), - bitcoind) + futOutcome = withNewWalletAndBitcoindCached( + test, + getBIP39PasswordOpt(), + bitcoind)(getFreshWalletAppConfig) fut <- futOutcome.toFuture } yield fut new FutureOutcome(f) diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletSendingTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletSendingTest.scala index ad83ab99fe..98cc651611 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletSendingTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletSendingTest.scala @@ -33,7 +33,7 @@ class WalletSendingTest extends BitcoinSWalletTest { override type FixtureParam = FundedWallet override def withFixture(test: OneArgAsyncTest): FutureOutcome = - withFundedWallet(test, getBIP39PasswordOpt()) + withFundedWallet(test, getBIP39PasswordOpt())(getFreshWalletAppConfig) behavior of "Wallet" diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletUnitTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletUnitTest.scala index 972ebfb992..5be5983f60 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/WalletUnitTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/WalletUnitTest.scala @@ -24,7 +24,7 @@ class WalletUnitTest extends BitcoinSWalletTest { override type FixtureParam = Wallet override def withFixture(test: OneArgAsyncTest): FutureOutcome = - withNewWallet(test, bip39PasswordOpt) + withNewWallet(test, bip39PasswordOpt)(getFreshWalletAppConfig) behavior of "Wallet - unit test" @@ -163,9 +163,13 @@ class WalletUnitTest extends BitcoinSWalletTest { it must "be able to call initialize twice without throwing an exception if we have the same key manager" in { wallet: Wallet => - val twiceF = Wallet.initialize(wallet, bip39PasswordOpt).flatMap { _ => - Wallet.initialize(wallet, bip39PasswordOpt) - } + val twiceF = Wallet + .initialize(wallet, bip39PasswordOpt)(wallet.walletConfig, + executionContext) + .flatMap { _ => + Wallet.initialize(wallet, bip39PasswordOpt)(wallet.walletConfig, + executionContext) + } twiceF.map(_ => succeed) @@ -174,12 +178,17 @@ class WalletUnitTest extends BitcoinSWalletTest { it must "be able to detect an incompatible key manager with a wallet" in { wallet: Wallet => recoverToSucceededIf[RuntimeException] { - Wallet.initialize(wallet, bip39PasswordOpt).flatMap { _ => - //use a BIP39 password to make the key-managers different - Wallet.initialize( - wallet, - Some("random-password-to-make-key-managers-different")) - } + Wallet + .initialize(wallet, bip39PasswordOpt)(wallet.walletConfig, + executionContext) + .flatMap { _ => + //use a BIP39 password to make the key-managers different + Wallet.initialize( + wallet, + Some("random-password-to-make-key-managers-different"))( + wallet.walletConfig, + executionContext) + } } }