Add unit test for moving seeds and making sure we can get the same public key after moving (#3441)

This commit is contained in:
Chris Stewart 2021-07-23 13:00:27 -05:00 committed by GitHub
parent fc88aa077b
commit 1032669f21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 118 additions and 14 deletions

View file

@ -24,12 +24,14 @@ object RValueDbHelper {
account: HDAccount,
chainType: Int,
keyIndex: Int): RValueDb = {
RValueDb(nonce,
eventName,
account.purpose,
account.coin.coinType,
account.index,
chainType,
keyIndex)
RValueDb(
nonce = nonce,
eventName = eventName,
purpose = account.purpose,
accountCoin = account.coin.coinType,
accountIndex = account.index,
chainType = chainType,
keyIndex = keyIndex
)
}
}

View file

@ -0,0 +1,62 @@
package org.bitcoins.dlc.oracle.config
import org.bitcoins.keymanager.WalletStorage
import org.bitcoins.testkit.BitcoinSTestAppConfig
import org.bitcoins.testkit.fixtures.DLCOracleAppConfigFixture
import java.nio.file.Files
class DLCOracleAppConfigTest extends DLCOracleAppConfigFixture {
behavior of "DLCOracleAppConfig"
it must "initialize the same oracle twice" in {
dlcOracleAppConfig: DLCOracleAppConfig =>
val dlcOracle1F = dlcOracleAppConfig.initialize()
val dlcOracle2F = dlcOracleAppConfig.initialize()
for {
dlcOracle1 <- dlcOracle1F
dlcOracle2 <- dlcOracle2F
} yield {
assert(dlcOracle1.publicKey == dlcOracle2.publicKey)
}
}
it must "initialize the oracle, move the seed somewhere else, and then start the oracle again and get the same pubkeys" in {
dlcOracleAppConfig: DLCOracleAppConfig =>
val seedFile = dlcOracleAppConfig.seedPath
val dlcOracle1F = dlcOracleAppConfig.initialize()
val pubKeyBeforeMoveF = dlcOracle1F.map(_.publicKey)
//stop old oracle
val stoppedF = for {
_ <- dlcOracle1F
_ <- dlcOracleAppConfig.stop()
} yield ()
//move the seed file to a new datadir
val newDatadir = BitcoinSTestAppConfig.tmpDir()
val newSeedPath = newDatadir
.resolve("seeds")
.resolve(WalletStorage.ENCRYPTED_SEED_FILE_NAME)
//create seed directory
Files.createDirectories(newSeedPath.getParent)
//copy seed file to new directory
Files.copy(seedFile, newSeedPath)
//start the new app config from the new datadir
val dlcOracle2F = DLCOracleAppConfig
.fromDatadir(newDatadir)
.initialize()
for {
_ <- stoppedF
pubKey1 <- pubKeyBeforeMoveF
pubKey2 <- dlcOracle2F.map(_.publicKey)
} yield {
assert(pubKey1 == pubKey2)
}
}
}

View file

@ -0,0 +1,27 @@
package org.bitcoins.testkit.fixtures
import org.bitcoins.dlc.oracle.config.DLCOracleAppConfig
import org.bitcoins.testkit.oracle.OracleTestUtil
import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg}
import org.scalatest.FutureOutcome
import scala.concurrent.Future
trait DLCOracleAppConfigFixture extends BitcoinSFixture with EmbeddedPg {
override type FixtureParam = DLCOracleAppConfig
def withFixture(test: OneArgAsyncTest): FutureOutcome = {
val builder: () => Future[DLCOracleAppConfig] = () => {
val conf: DLCOracleAppConfig =
BitcoinSTestAppConfig.getDLCOracleWithEmbeddedDbTestConfig(pgUrl)
val _ = conf.migrate()
Future.successful(conf)
}
val destroy: DLCOracleAppConfig => Future[Unit] = dlcOracleAppConfig => {
OracleTestUtil.destroyDLCOracleAppConfig(dlcOracleAppConfig)
}
makeDependentFixture(builder, destroy = destroy)(test)
}
}

View file

@ -2,7 +2,7 @@ package org.bitcoins.testkit.fixtures
import org.bitcoins.dlc.oracle.DLCOracle
import org.bitcoins.dlc.oracle.config.DLCOracleAppConfig
import org.bitcoins.testkit.util.FileUtil
import org.bitcoins.testkit.oracle.OracleTestUtil
import org.bitcoins.testkit.{BitcoinSTestAppConfig, EmbeddedPg}
import org.scalatest._
@ -23,12 +23,7 @@ trait DLCOracleFixture extends BitcoinSFixture with EmbeddedPg {
}
val destroy: DLCOracle => Future[Unit] = dlcOracle => {
val conf = dlcOracle.conf
val _ = conf.clean()
for {
_ <- conf.stop()
_ = FileUtil.deleteTmpDir(conf.baseDatadir)
} yield ()
OracleTestUtil.destroyDLCOracleAppConfig(dlcOracle.conf)
}
makeDependentFixture(builder, destroy = destroy)(test)
}

View file

@ -0,0 +1,18 @@
package org.bitcoins.testkit.oracle
import org.bitcoins.dlc.oracle.config.DLCOracleAppConfig
import org.bitcoins.testkit.util.FileUtil
import scala.concurrent.{ExecutionContext, Future}
object OracleTestUtil {
def destroyDLCOracleAppConfig(config: DLCOracleAppConfig)(implicit
ec: ExecutionContext): Future[Unit] = {
val _ = config.clean()
for {
_ <- config.stop()
_ = FileUtil.deleteTmpDir(config.baseDatadir)
} yield ()
}
}