Remove WalletSync.sync() -> WalletSync.syncFullBlocks() (#2522)

This commit is contained in:
Chris Stewart 2021-01-15 17:05:11 -06:00 committed by GitHub
parent a1a2524b56
commit f3e81d027d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 14 deletions

View file

@ -168,13 +168,16 @@ abstract class SyncUtil extends BitcoinSLogger {
node.NodeChainQueryApi(nodeApi, chainQuery) node.NodeChainQueryApi(nodeApi, chainQuery)
} }
def syncWallet(wallet: Wallet, bitcoind: BitcoindRpcClient)(implicit /** Syncs a wallet against bitcoind by retrieving full blocks and then calling
* [[Wallet.processBlock()]]
*/
def syncWalletFullBlocks(wallet: Wallet, bitcoind: BitcoindRpcClient)(implicit
ec: ExecutionContext): Future[Wallet] = { ec: ExecutionContext): Future[Wallet] = {
WalletSync.sync( WalletSync.syncFullBlocks(
wallet = wallet, wallet = wallet,
getBlockHeaderFunc = SyncUtil.getBlockHeaderFunc(bitcoind), getBlockHeaderFunc = SyncUtil.getBlockHeaderFunc(bitcoind),
getBestBlockHashFunc = SyncUtil.getBestBlockHashFunc(bitcoind), getBestBlockHashFunc = SyncUtil.getBestBlockHashFunc(bitcoind),
getBlock = SyncUtil.getBlockFunc(bitcoind) getBlockFunc = SyncUtil.getBlockFunc(bitcoind)
) )
} }
} }

View file

@ -274,8 +274,8 @@ trait BitcoinSWalletTest extends BitcoinSFixture with EmbeddedPg {
bitcoind = bitcoind, bitcoind = bitcoind,
bip39PasswordOpt = bip39PasswordOpt) bip39PasswordOpt = bip39PasswordOpt)
fundedWallet <- fundWalletWithBitcoind(walletWithBitcoind) fundedWallet <- fundWalletWithBitcoind(walletWithBitcoind)
_ <- _ <- SyncUtil.syncWalletFullBlocks(wallet = fundedWallet.wallet,
SyncUtil.syncWallet(wallet = fundedWallet.wallet, bitcoind = bitcoind) bitcoind = bitcoind)
_ <- BitcoinSWalletTest.awaitWalletBalances(fundedWallet) _ <- BitcoinSWalletTest.awaitWalletBalances(fundedWallet)
} yield fundedWallet } yield fundedWallet
} }
@ -294,8 +294,8 @@ trait BitcoinSWalletTest extends BitcoinSFixture with EmbeddedPg {
.map(_.asInstanceOf[BitcoindV19RpcClient]) .map(_.asInstanceOf[BitcoindV19RpcClient])
wallet <- createWalletWithBitcoindCallbacks(bitcoind, bip39PasswordOpt) wallet <- createWalletWithBitcoindCallbacks(bitcoind, bip39PasswordOpt)
fundedWallet <- fundWalletWithBitcoind(wallet) fundedWallet <- fundWalletWithBitcoind(wallet)
_ <- _ <- SyncUtil.syncWalletFullBlocks(wallet = fundedWallet.wallet,
SyncUtil.syncWallet(wallet = fundedWallet.wallet, bitcoind = bitcoind) bitcoind = bitcoind)
_ <- BitcoinSWalletTest.awaitWalletBalances(fundedWallet) _ <- BitcoinSWalletTest.awaitWalletBalances(fundedWallet)
} yield { } yield {
WalletWithBitcoindV19(fundedWallet.wallet, bitcoind) WalletWithBitcoindV19(fundedWallet.wallet, bitcoind)

View file

@ -22,10 +22,10 @@ class WalletSyncTest extends BitcoinSWalletTest {
val getBlockHeaderFunc = SyncUtil.getBlockHeaderFunc(bitcoind) val getBlockHeaderFunc = SyncUtil.getBlockHeaderFunc(bitcoind)
val getBlockFunc = SyncUtil.getBlockFunc(bitcoind) val getBlockFunc = SyncUtil.getBlockFunc(bitcoind)
val syncedWalletF = WalletSync.sync(wallet, val syncedWalletF = WalletSync.syncFullBlocks(wallet,
getBlockHeaderFunc, getBlockHeaderFunc,
getBestBlockHashFunc, getBestBlockHashFunc,
getBlockFunc) getBlockFunc)
val bitcoindBestHeaderF = bitcoind.getBestBlockHeader() val bitcoindBestHeaderF = bitcoind.getBestBlockHeader()
for { for {

View file

@ -9,11 +9,17 @@ import scala.concurrent.{ExecutionContext, Future}
trait WalletSync extends BitcoinSLogger { trait WalletSync extends BitcoinSLogger {
def sync( /** Synchronizes the bitcoin-s' wallet by retrieving each block and then calling
* [[Wallet.processBlock()]] on the block retrieved
*
* WARNING: This should not be used on resource constrained devices
* as fetching full blocks will use a lot of bandwidth on live networks
*/
def syncFullBlocks(
wallet: Wallet, wallet: Wallet,
getBlockHeaderFunc: DoubleSha256DigestBE => Future[BlockHeader], getBlockHeaderFunc: DoubleSha256DigestBE => Future[BlockHeader],
getBestBlockHashFunc: () => Future[DoubleSha256DigestBE], getBestBlockHashFunc: () => Future[DoubleSha256DigestBE],
getBlock: DoubleSha256DigestBE => Future[Block])(implicit getBlockFunc: DoubleSha256DigestBE => Future[Block])(implicit
ec: ExecutionContext): Future[Wallet] = { ec: ExecutionContext): Future[Wallet] = {
val bestBlockHashF = getBestBlockHashFunc() val bestBlockHashF = getBestBlockHashFunc()
val bestBlockHeaderF = for { val bestBlockHeaderF = for {
@ -26,7 +32,7 @@ trait WalletSync extends BitcoinSLogger {
blocksToSync <- getBlocksToSync(wallet = wallet, blocksToSync <- getBlocksToSync(wallet = wallet,
currentTipBlockHashBE = bestHeader.hashBE, currentTipBlockHashBE = bestHeader.hashBE,
accum = Vector.empty, accum = Vector.empty,
getBlock = getBlock) getBlock = getBlockFunc)
} yield blocksToSync } yield blocksToSync
val syncedWalletF = for { val syncedWalletF = for {