diff --git a/docs/node/node.md b/docs/node/node.md index a11e0dcfda..faa6ce99f3 100644 --- a/docs/node/node.md +++ b/docs/node/node.md @@ -99,19 +99,11 @@ implicit val nodeConfig = appConfig.nodeConf val initNodeF = nodeConfig.start() -//the node requires a chainHandler to store block information -//use a helper method in our testkit to create the chain project -val chainApiF = for { - chainHandler <- ChainUnitTest.createChainHandler() -} yield chainHandler - - //yay! All setup done, let's create a node and then start it! val nodeF = for { - chainApi <- chainApiF peer <- peerF } yield { - NeutrinoNode(chainApi = chainApi, + NeutrinoNode( walletCreationTimeOpt = None, //you can set this to only sync compact filters after the timestamp paramPeers = Vector(peer), nodeConfig = nodeConfig, diff --git a/node/src/main/scala/org/bitcoins/node/NeutrinoNode.scala b/node/src/main/scala/org/bitcoins/node/NeutrinoNode.scala index bb5ec7e5b2..531d208689 100644 --- a/node/src/main/scala/org/bitcoins/node/NeutrinoNode.scala +++ b/node/src/main/scala/org/bitcoins/node/NeutrinoNode.scala @@ -2,6 +2,7 @@ package org.bitcoins.node import akka.actor.ActorSystem import org.bitcoins.asyncutil.AsyncUtil +import org.bitcoins.chain.blockchain.ChainHandler import org.bitcoins.chain.config.ChainAppConfig import org.bitcoins.chain.models.BlockHeaderDAO import org.bitcoins.core.api.chain.ChainApi @@ -21,7 +22,6 @@ import java.time.Instant import scala.concurrent.Future case class NeutrinoNode( - chainApi: ChainApi, walletCreationTimeOpt: Option[Instant], nodeConfig: NodeAppConfig, chainConfig: ChainAppConfig, @@ -83,6 +83,7 @@ case class NeutrinoNode( */ private def syncHelper(syncPeerOpt: Option[Peer]): Future[Unit] = { logger.info(s"Syncing with peerOpt=$syncPeerOpt") + val chainApi: ChainApi = ChainHandler.fromDatabase() val blockchainsF = BlockHeaderDAO()(executionContext, chainConfig).getBlockchains() for { diff --git a/node/src/main/scala/org/bitcoins/node/PeerManager.scala b/node/src/main/scala/org/bitcoins/node/PeerManager.scala index 876e084779..6c0e948afa 100644 --- a/node/src/main/scala/org/bitcoins/node/PeerManager.scala +++ b/node/src/main/scala/org/bitcoins/node/PeerManager.scala @@ -558,7 +558,6 @@ case class PeerManager( (forceReconnect || connectedPeerCount == 0) && isStarted.get if (peers.exists(_ != peer) && syncPeerOpt.isDefined) { node - .copy(chainApi = ChainHandler.fromDatabase()) .syncFromNewPeer() .map(_ => ()) } else if (syncPeerOpt.isDefined) { diff --git a/node/src/main/scala/org/bitcoins/node/config/NodeAppConfig.scala b/node/src/main/scala/org/bitcoins/node/config/NodeAppConfig.scala index 4535bfe122..3f7ed8fff8 100644 --- a/node/src/main/scala/org/bitcoins/node/config/NodeAppConfig.scala +++ b/node/src/main/scala/org/bitcoins/node/config/NodeAppConfig.scala @@ -2,14 +2,7 @@ package org.bitcoins.node.config import akka.actor.ActorSystem import com.typesafe.config.Config -import org.bitcoins.chain.blockchain.ChainHandlerCached import org.bitcoins.chain.config.ChainAppConfig -import org.bitcoins.chain.models.{ - BlockHeaderDAO, - ChainStateDescriptorDAO, - CompactFilterDAO, - CompactFilterHeaderDAO -} import org.bitcoins.core.api.CallbackConfig import org.bitcoins.core.api.node.NodeType import org.bitcoins.core.config.{MainNet, RegTest, SigNet, TestNet3} @@ -233,25 +226,15 @@ object NodeAppConfig extends AppConfigFactoryActorSystem[NodeAppConfig] { nodeConf: NodeAppConfig, chainConf: ChainAppConfig, system: ActorSystem): Future[Node] = { - import system.dispatcher - - val blockHeaderDAO = BlockHeaderDAO() - val filterHeaderDAO = CompactFilterHeaderDAO() - val filterDAO = CompactFilterDAO() - val stateDAO = ChainStateDescriptorDAO() - - val chainF = ChainHandlerCached - .fromDatabase(blockHeaderDAO, filterHeaderDAO, filterDAO, stateDAO) nodeConf.nodeType match { case NodeType.NeutrinoNode => - chainF.map(chain => - NeutrinoNode(chain, - walletCreationTimeOpt, - nodeConf, - chainConf, - system, - paramPeers = peers)) + val n = NeutrinoNode(walletCreationTimeOpt, + nodeConf, + chainConf, + system, + paramPeers = peers) + Future.successful(n) case NodeType.FullNode => Future.failed(new RuntimeException("Not implemented")) case NodeType.BitcoindBackend => 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 6e05291d34..0ca14e7af4 100644 --- a/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala +++ b/testkit/src/main/scala/org/bitcoins/testkit/node/NodeUnitTest.scala @@ -2,10 +2,7 @@ package org.bitcoins.testkit.node import akka.actor.ActorSystem import org.bitcoins.asyncutil.AsyncUtil -import org.bitcoins.chain.blockchain.ChainHandlerCached import org.bitcoins.chain.config.ChainAppConfig -import org.bitcoins.chain.models._ -import org.bitcoins.core.api.chain.ChainApi import org.bitcoins.core.api.node.NodeType import org.bitcoins.node._ import org.bitcoins.node.config.NodeAppConfig @@ -161,22 +158,16 @@ object NodeUnitTest extends P2PLogger { system: ActorSystem): Future[NeutrinoNode] = { import system.dispatcher - val blockHeaderDAO = BlockHeaderDAO() - val filterHeaderDAO = CompactFilterHeaderDAO() - val filterDAO = CompactFilterDAO() - val stateDAO = ChainStateDescriptorDAO() - - val chainApiF = { + val chainConfStartedF = { for { _ <- chainConf.start() - chainApi <- ChainHandlerCached - .fromDatabase(blockHeaderDAO, filterHeaderDAO, filterDAO, stateDAO) } yield { - chainApi + () } } - val nodeF = chainApiF.map(buildNode(peer, _, walletCreationTimeOpt)) + val nodeF = + chainConfStartedF.map(_ => buildNeutrinoNode(peer, walletCreationTimeOpt)) for { node <- nodeF _ <- node.nodeConfig.start() @@ -186,41 +177,19 @@ object NodeUnitTest extends P2PLogger { } } - def buildNode( + private def buildNeutrinoNode( peer: Peer, - chainApi: ChainApi, walletCreationTimeOpt: Option[Instant])(implicit chainConf: ChainAppConfig, nodeConf: NodeAppConfig, system: ActorSystem): NeutrinoNode = { - NeutrinoNode(chainApi, - walletCreationTimeOpt, + NeutrinoNode(walletCreationTimeOpt, nodeConf, chainConf, system, paramPeers = Vector(peer)) } - def buildPeerMessageReceiver( - chainApi: ChainApi, - peer: Peer, - walletCreationTimeOpt: Option[Instant])(implicit - appConfig: BitcoinSAppConfig, - system: ActorSystem): Future[PeerMessageReceiver] = { - val node = buildNode(peer, chainApi, walletCreationTimeOpt)( - appConfig.chainConf, - appConfig.nodeConf, - system) - val controlMessageHandler = ControlMessageHandler(node.peerManager)( - system.dispatcher, - appConfig.nodeConf) - val receiver = - PeerMessageReceiver(controlMessageHandler = controlMessageHandler, - queue = node.peerManager.dataMessageQueueOpt.get, - peer = peer)(system, appConfig.nodeConf) - Future.successful(receiver) - } - def destroyNode(node: Node, appConfig: BitcoinSAppConfig)(implicit ec: ExecutionContext): Future[Unit] = { @@ -254,7 +223,7 @@ object NodeUnitTest extends P2PLogger { } //does not destroys the bitcoinds - def destroyNodeConnectedWithBitcoinds( + private def destroyNodeConnectedWithBitcoinds( nodeConnectedWithBitcoind: NodeConnectedWithBitcoinds)(implicit system: ActorSystem, appConfig: BitcoinSAppConfig): Future[Unit] = { @@ -273,7 +242,7 @@ object NodeUnitTest extends P2PLogger { } /** Creates a neutrino node, a funded bitcoin-s wallet, all of which are connected to bitcoind */ - def createNeutrinoNodeFundedWalletBitcoind( + private def createNeutrinoNodeFundedWalletBitcoind( versionOpt: Option[BitcoindVersion], walletCallbacks: WalletCallbacks)(implicit system: ActorSystem, @@ -339,7 +308,7 @@ object NodeUnitTest extends P2PLogger { } } - def destroyNodeFundedWalletBitcoind( + private def destroyNodeFundedWalletBitcoind( fundedWalletBitcoind: NodeFundedWalletBitcoind)(implicit system: ActorSystem, appConfig: BitcoinSAppConfig): Future[Unit] = { @@ -363,20 +332,23 @@ object NodeUnitTest extends P2PLogger { } def buildPeerMessageReceiver( - chainApi: ChainApi, peer: Peer, walletCreationTimeOpt: Option[Instant])(implicit nodeAppConfig: NodeAppConfig, chainAppConfig: ChainAppConfig, system: ActorSystem): Future[PeerMessageReceiver] = { - val node = buildNode(peer, chainApi, walletCreationTimeOpt) - val controlMessageHandler = - ControlMessageHandler(node.peerManager)(system.dispatcher, nodeAppConfig) - val receiver = - PeerMessageReceiver(controlMessageHandler = controlMessageHandler, - queue = node.peerManager.dataMessageQueueOpt.get, - peer = peer) - Future.successful(receiver) + import system.dispatcher + val nodeF = buildNode(peer, walletCreationTimeOpt) + for { + node <- nodeF + controlMessageHandler = + ControlMessageHandler(node.peerManager)(system.dispatcher, + nodeAppConfig) + receiver = + PeerMessageReceiver(controlMessageHandler = controlMessageHandler, + queue = node.peerManager.dataMessageQueueOpt.get, + peer = peer) + } yield receiver } def createPeer(bitcoind: BitcoindRpcClient)(implicit @@ -403,17 +375,14 @@ object NodeUnitTest extends P2PLogger { val checkConfigF = Future { assert(nodeAppConfig.nodeType == NodeType.NeutrinoNode) } - val chainApiF = for { - _ <- checkConfigF - chainHandler <- ChainUnitTest.createChainHandler() - } yield chainHandler + val nodeF = for { + _ <- checkConfigF + _ <- chainAppConfig.start() _ <- nodeAppConfig.start() peer <- createPeer(bitcoind) - chainApi <- chainApiF } yield { - NeutrinoNode(chainApi, - walletCreationTimeOpt, + NeutrinoNode(walletCreationTimeOpt, paramPeers = Vector(peer), nodeConfig = nodeAppConfig, chainConfig = chainAppConfig, @@ -436,16 +405,13 @@ object NodeUnitTest extends P2PLogger { val checkConfigF = Future { assert(nodeAppConfig.nodeType == NodeType.NeutrinoNode) } - val chainApiF = for { - _ <- checkConfigF - chainHandler <- ChainUnitTest.createChainHandler() - } yield chainHandler + val nodeF = for { + _ <- checkConfigF + _ <- chainAppConfig.start() _ <- nodeAppConfig.start() - chainApi <- chainApiF } yield { - NeutrinoNode(chainApi, - walletCreationTimeOpt, + NeutrinoNode(walletCreationTimeOpt, paramPeers = Vector(peer), nodeConfig = nodeAppConfig, chainConfig = chainAppConfig, @@ -469,18 +435,15 @@ object NodeUnitTest extends P2PLogger { val checkConfigF = Future { assert(nodeAppConfig.nodeType == NodeType.NeutrinoNode) } - val chainApiF = for { - _ <- checkConfigF - chainHandler <- ChainUnitTest.createChainHandler() - } yield chainHandler - val peersF = bitcoinds.map(createPeer(_)) + + val peersF = Future.traverse(bitcoinds)(createPeer(_)) val nodeF = for { + _ <- checkConfigF + _ <- chainAppConfig.start() _ <- nodeAppConfig.start() - chainApi <- chainApiF - peers <- Future.sequence(peersF) + peers <- peersF } yield { - NeutrinoNode(chainApi, - creationTimeOpt, + NeutrinoNode(walletCreationTimeOpt = creationTimeOpt, paramPeers = peers, nodeConfig = nodeAppConfig, chainConfig = chainAppConfig,