Skip to content

Commit

Permalink
Move DLCWallet and db to separate project (#1899)
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman authored and nkohen committed Aug 28, 2020
1 parent ca12e3c commit 2c8461f
Show file tree
Hide file tree
Showing 45 changed files with 634 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ import org.bitcoins.crypto._
import org.bitcoins.node.Node
import org.bitcoins.wallet.MockWalletApi
import org.bitcoins.wallet.api.AddressInfo
import org.bitcoins.wallet.models._
import org.bitcoins.dlc.wallet.models._
import org.scalamock.scalatest.MockFactory
import org.scalatest.wordspec.AnyWordSpec
import scodec.bits.ByteVector
import ujson.Value.InvalidData
import ujson._

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.Future
import scala.concurrent.Future
import scala.concurrent.duration.DurationInt

class RoutesSpec extends AnyWordSpec with ScalatestRouteTest with MockFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.bitcoins.wallet
import org.bitcoins.core.api.wallet.db.AccountDb
import org.bitcoins.core.hd.AddressType
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.wallet.api.AnyDLCHDWalletApi
import org.bitcoins.dlc.wallet.AnyDLCHDWalletApi

import scala.concurrent.Future

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.bitcoins.server

import com.typesafe.config.Config
import org.bitcoins.wallet.config.WalletAppConfig
import org.bitcoins.node.config.NodeAppConfig
import org.bitcoins.chain.config.ChainAppConfig

import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import java.nio.file.Path

import com.typesafe.config.Config
import org.bitcoins.chain.config.ChainAppConfig
import org.bitcoins.core.util.StartStopAsync
import org.bitcoins.db.AppConfig
import org.bitcoins.dlc.wallet.DLCAppConfig
import org.bitcoins.node.config.NodeAppConfig
import org.bitcoins.wallet.config.WalletAppConfig

import scala.concurrent.{ExecutionContext, Future}

/**
* A unified config class for all submodules of Bitcoin-S
Expand All @@ -29,12 +29,14 @@ case class BitcoinSAppConfig(
val walletConf: WalletAppConfig = WalletAppConfig(directory, confs: _*)
val nodeConf: NodeAppConfig = NodeAppConfig(directory, confs: _*)
val chainConf: ChainAppConfig = ChainAppConfig(directory, confs: _*)
val dlcConf: DLCAppConfig = DLCAppConfig(directory, confs: _*)

/** Initializes the wallet, node and chain projects */
override def start(): Future[Unit] = {
val futures = List(walletConf.initialize(),
nodeConf.initialize(),
chainConf.initialize())
chainConf.initialize(),
dlcConf.initialize())

Future.sequence(futures).map(_ => ())
}
Expand Down
12 changes: 7 additions & 5 deletions app/server/src/main/scala/org/bitcoins/server/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.bitcoins.core.Core
import org.bitcoins.core.config.{BitcoinNetworks, MainNet, RegTest, TestNet3}
import org.bitcoins.core.util.{BitcoinSLogger, FutureUtil, NetworkUtil}
import org.bitcoins.db._
import org.bitcoins.dlc.wallet.{DLCAppConfig, DLCWallet}
import org.bitcoins.feeprovider.BitcoinerLiveFeeRateProvider
import org.bitcoins.node._
import org.bitcoins.node.config.NodeAppConfig
Expand Down Expand Up @@ -112,6 +113,7 @@ object Main extends App with BitcoinSLogger {
require(nodeConf.isNeutrinoEnabled != nodeConf.isSPVEnabled,
"Either Neutrino or SPV mode should be enabled")
implicit val chainConf: ChainAppConfig = conf.chainConf
implicit val dlcConf: DLCAppConfig = conf.dlcConf

if (nodeConf.peers.isEmpty) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -143,10 +145,10 @@ object Main extends App with BitcoinSLogger {
node <- nodeF
chainApi <- chainApiF
_ = logger.info("Initialized chain api")
wallet <- walletConf.createHDWallet(node,
chainApi,
BitcoinerLiveFeeRateProvider(60),
bip39PasswordOpt)
wallet <- dlcConf.createDLCWallet(node,
chainApi,
BitcoinerLiveFeeRateProvider(60),
bip39PasswordOpt)
callbacks <- createCallbacks(wallet)
_ = nodeConf.addCallbacks(callbacks)
} yield {
Expand Down Expand Up @@ -290,7 +292,7 @@ object Main extends App with BitcoinSLogger {

private def startHttpServer(
node: Node,
wallet: Wallet,
wallet: DLCWallet,
rpcPortOpt: Option[Int])(implicit
system: ActorSystem,
conf: BitcoinSAppConfig): Future[Http.ServerBinding] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import org.bitcoins.core.currency._
import org.bitcoins.core.protocol.transaction.Transaction
import org.bitcoins.core.wallet.utxo.AddressLabelTagType
import org.bitcoins.crypto.NetworkElement
import org.bitcoins.dlc.wallet.AnyDLCHDWalletApi
import org.bitcoins.node.Node
import org.bitcoins.wallet.WalletLogger
import org.bitcoins.wallet.api.AnyDLCHDWalletApi

import scala.concurrent.Future
import scala.util.{Failure, Success}
Expand Down
30 changes: 26 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ lazy val `bitcoin-s` = project
feeProviderTest,
dlc,
dlcTest,
dlcWallet,
dlcWalletTest,
dlcSuredbitsClient,
dlcSuredbitsClientTest,
bitcoindRpc,
Expand Down Expand Up @@ -252,7 +254,7 @@ lazy val appServer = project
wallet,
bitcoindRpc,
feeProvider,
dlc
dlcWallet
)

lazy val appServerTest = project
Expand Down Expand Up @@ -453,7 +455,8 @@ lazy val testkit = project
node,
wallet,
zmq,
dlc
dlc,
dlcWallet
)

lazy val docs = project
Expand Down Expand Up @@ -516,7 +519,7 @@ lazy val dlc = project
// version number needed for MicroJson
libraryDependencies ++= Deps.dlc
)
.dependsOn(core, dbCommons)
.dependsOn(core, appCommons)

lazy val dlcTest = project
.in(file("dlc-test"))
Expand All @@ -531,8 +534,27 @@ lazy val dlcTest = project
dlc
)

lazy val dlcWallet = project
.in(file("dlc-wallet"))
.settings(CommonSettings.prodSettings: _*)
.settings(
name := "bitcoin-s-dlc-wallet",
libraryDependencies ++= Deps.dlcWallet
)
.dependsOn(wallet, dlc)

lazy val dlcWalletTest = project
.in(file("dlc-wallet-test"))
.settings(CommonSettings.prodSettings: _*)
.settings(
name := "bitcoin-s-dlc-wallet-test",
libraryDependencies ++= Deps.dlcWalletTest
)
.dependsOn(core % testAndCompile, dlcWallet, testkit)

/** Given a database name, returns the appropriate
* Flyway settings we apply to a project (chain, node, wallet) */
* Flyway settings we apply to a project (chain, node, wallet)
*/
def dbFlywaySettings(dbName: String): List[Setting[_]] = {
lazy val DB_HOST = "localhost"
lazy val DB_NAME = s"${dbName}.sqlite"
Expand Down
9 changes: 9 additions & 0 deletions db-commons-test/src/test/resources/db.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ bitcoin-s {
# }
}

dlc = ${sqlite}
dlc {
# this config key is read by Slick
db {
name = dlcdb.sqlite
url = "jdbc:sqlite:"${bitcoin-s.dlc.db.path}${bitcoin-s.dlc.db.name}
}
}

node = ${sqlite}
node {
# this config key is read by Slick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.bitcoins.db
import com.typesafe.config.Config
import org.bitcoins.chain.config.ChainAppConfig
import org.bitcoins.chain.db.ChainDbManagement
import org.bitcoins.dlc.wallet.{DLCAppConfig, DLCDbManagement}
import org.bitcoins.node.config.NodeAppConfig
import org.bitcoins.node.db.NodeDbManagement
import org.bitcoins.testkit.BitcoinSTestAppConfig.ProjectType
Expand All @@ -27,6 +28,13 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
override def appConfig: ChainAppConfig = chainAppConfig
}

def createDLCDbManagement(dlcAppConfig: DLCAppConfig): DLCDbManagement =
new DLCDbManagement with JdbcProfileComponent[DLCAppConfig] {
override val ec: ExecutionContext = system.dispatcher

override def appConfig: DLCAppConfig = dlcAppConfig
}

def createWalletDbManagement(
walletAppConfig: WalletAppConfig): WalletDbManagement =
new WalletDbManagement with JdbcProfileComponent[WalletAppConfig] {
Expand All @@ -51,12 +59,21 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
assert(result == expected)
}

it must "run migrations for dlc db" in {
val dlcAppConfig =
DLCAppConfig(BitcoinSTestAppConfig.tmpDir(), dbConfig(ProjectType.DLC))
val dlcDbManagement = createDLCDbManagement(dlcAppConfig)
val result = dlcDbManagement.migrate()
val expected = if (dlcAppConfig.driverName == "postgresql") 1 else 1
assert(result == expected)
}

it must "run migrations for wallet db" in {
val walletAppConfig = WalletAppConfig(BitcoinSTestAppConfig.tmpDir(),
dbConfig(ProjectType.Wallet))
val walletDbManagement = createWalletDbManagement(walletAppConfig)
val result = walletDbManagement.migrate()
val expected = if (walletAppConfig.driverName == "postgresql") 7 else 9
val expected = if (walletAppConfig.driverName == "postgresql") 6 else 8
assert(result == expected)
}

Expand Down
9 changes: 9 additions & 0 deletions db-commons/src/main/resources/db.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ bitcoin-s {
# }
}

dlc = ${sqlite}
dlc {
# this config key is read by Slick
db {
name = dlcdb.sqlite
url = "jdbc:sqlite:"${bitcoin-s.dlc.db.path}${bitcoin-s.dlc.db.name}
}
}

node = ${sqlite}
node {
# this config key is read by Slick
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.bitcoins.wallet.models
package org.bitcoins.dlc.wallet

import org.bitcoins.core.currency.Satoshis
import org.bitcoins.core.number.UInt32
Expand All @@ -9,13 +9,14 @@ import org.bitcoins.core.protocol.transaction.{
}
import org.bitcoins.crypto.{ECAdaptorSignature, Sha256DigestBE}
import org.bitcoins.db.CRUD
import org.bitcoins.testkit.fixtures.WalletDAOFixture
import org.bitcoins.dlc.wallet.models._
import org.bitcoins.testkit.fixtures.DLCDAOFixture
import org.bitcoins.testkit.wallet.{BitcoinSWalletTest, DLCWalletUtil}
import org.scalatest.Assertion

import scala.concurrent.Future

class DLCDAOTest extends BitcoinSWalletTest with WalletDAOFixture {
class DLCDAOTest extends BitcoinSWalletTest with DLCDAOFixture {

behavior of "DLCDAO"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.bitcoins.wallet.dlc
package org.bitcoins.dlc.wallet

import org.bitcoins.commons.jsonmodels.dlc.DLCMessage.ContractInfo
import org.bitcoins.core.crypto.WitnessTxSigComponent
Expand All @@ -13,10 +13,10 @@ import org.bitcoins.core.protocol.transaction.{
import org.bitcoins.core.script.PreExecutionScriptProgram
import org.bitcoins.core.script.interpreter.ScriptInterpreter
import org.bitcoins.crypto.{SchnorrDigitalSignature, Sha256DigestBE}
import org.bitcoins.dlc.wallet.models.DLCOfferDb
import org.bitcoins.testkit.wallet.DLCWalletUtil.InitializedDLCWallet
import org.bitcoins.testkit.wallet.{BitcoinSDualWalletTest, DLCWalletUtil}
import org.bitcoins.wallet.Wallet
import org.bitcoins.wallet.models.DLCOfferDb
import org.scalatest.{Assertion, FutureOutcome}

import scala.concurrent.Future
Expand All @@ -31,7 +31,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {

behavior of "DLCWallet"

def getInitialOffer(wallet: Wallet): Future[DLCOfferDb] = {
def getInitialOffer(wallet: DLCWallet): Future[DLCOfferDb] = {
wallet.dlcOfferDAO.findAll().map { all =>
require(all.size == 1, "There should only be one dlc initialized")
all.head
Expand Down Expand Up @@ -80,7 +80,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
def dlcExecutionTest(
wallets: FixtureParam,
asInitiator: Boolean,
func: Wallet => Future[Transaction],
func: DLCWallet => Future[Transaction],
expectedOutputs: Int): Future[Assertion] = {
val dlcA = wallets._1.wallet
val dlcB = wallets._2.wallet
Expand Down Expand Up @@ -165,7 +165,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
for {
offer <- getInitialOffer(wallets._1.wallet)
(sig, _) = getSigs(offer.contractInfo)
func = (wallet: Wallet) => wallet.executeDLC(offer.eventId, sig)
func = (wallet: DLCWallet) => wallet.executeDLC(offer.eventId, sig)

result <- dlcExecutionTest(wallets = wallets,
asInitiator = true,
Expand All @@ -178,7 +178,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
for {
offer <- getInitialOffer(wallets._2.wallet)
(_, sig) = getSigs(offer.contractInfo)
func = (wallet: Wallet) => wallet.executeDLC(offer.eventId, sig)
func = (wallet: DLCWallet) => wallet.executeDLC(offer.eventId, sig)

result <- dlcExecutionTest(wallets = wallets,
asInitiator = false,
Expand All @@ -203,7 +203,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
it must "do a refund on a dlc as the initiator" in { wallets =>
for {
offer <- getInitialOffer(wallets._1.wallet)
func = (wallet: Wallet) => wallet.executeDLCRefund(offer.eventId)
func = (wallet: DLCWallet) => wallet.executeDLCRefund(offer.eventId)

result <- dlcExecutionTest(wallets = wallets,
asInitiator = true,
Expand All @@ -215,7 +215,7 @@ class DLCExecutionTest extends BitcoinSDualWalletTest {
it must "do a refund on a dlc as the recipient" in { wallets =>
for {
offer <- getInitialOffer(wallets._2.wallet)
func = (wallet: Wallet) => wallet.executeDLCRefund(offer.eventId)
func = (wallet: DLCWallet) => wallet.executeDLCRefund(offer.eventId)

result <- dlcExecutionTest(wallets = wallets,
asInitiator = false,
Expand Down

0 comments on commit 2c8461f

Please sign in to comment.