From f6924dfe1cae11b7f8f78d48c914d287fef9d254 Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 14 Apr 2023 16:47:25 -0300 Subject: [PATCH 1/7] Refactor updateBridge to try/catch individually for each operation --- .../java/co/rsk/federate/BtcToRskClient.java | 111 +++++++++++++----- 1 file changed, 82 insertions(+), 29 deletions(-) diff --git a/src/main/java/co/rsk/federate/BtcToRskClient.java b/src/main/java/co/rsk/federate/BtcToRskClient.java index 672e668a5..7bc325b4b 100644 --- a/src/main/java/co/rsk/federate/BtcToRskClient.java +++ b/src/main/java/co/rsk/federate/BtcToRskClient.java @@ -23,7 +23,6 @@ import org.bitcoinj.store.BlockStoreException; import org.ethereum.config.blockchain.upgrades.ActivationConfig; import org.ethereum.config.blockchain.upgrades.ConsensusRule; -import org.ethereum.core.Blockchain; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -154,21 +153,46 @@ public synchronized Map> getTransactionsToSendToRsk() { } public void updateBridge() { + if (federation == null) { + logger.warn("[updateBridge] updateBridge skipped because no Federation is associated to this BtcToRskClient"); + } + if (nodeBlockProcessor.hasBetterBlockToSync()) { + logger.warn("[updateBridge] updateBridge skipped because the node is syncing blocks"); + return; + } + logger.debug("[updateBridge] Updating bridge"); + + // Call receiveHeaders try { - if (federation == null) { - logger.warn("updateBridge skipped because no Federation is associated to this BtcToRskClient"); - } - if (!nodeBlockProcessor.hasBetterBlockToSync()) { - logger.debug("Updating bridge"); - int numberOfBlocksSent = updateBridgeBtcBlockchain(); - logger.debug("Updated bridge blockchain with {} blocks", numberOfBlocksSent); - logger.debug("Updating transactions and sending update"); - updateBridgeBtcCoinbaseTransactions(); - updateBridgeBtcTransactions(); - federatorSupport.sendUpdateCollections(); - } else { - logger.warn("updateBridge skipped because the node is syncing blocks"); - } + int numberOfBlocksSent = updateBridgeBtcBlockchain(); + logger.debug("[updateBridge] Updated bridge blockchain with {} blocks", numberOfBlocksSent); + } catch (Exception e) { + logger.error(e.getMessage(), e); + panicProcessor.panic("btclock", e.getMessage()); + } + + // Call registerBtcCoinbaseTransaction + try { + logger.debug("[updateBridge] Updating transactions and sending update"); + updateBridgeBtcCoinbaseTransactions(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + panicProcessor.panic("btclock", e.getMessage()); + } + + // Call registerBtcTransaction + try { + logger.debug("[updateBridge] Updating transactions and sending update"); + updateBridgeBtcTransactions(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + panicProcessor.panic("btclock", e.getMessage()); + } + + // Call updateCollections + try { + logger.debug("[updateBridge] Sending updateCollections"); + federatorSupport.sendUpdateCollections(); } catch (Exception e) { logger.error(e.getMessage(), e); panicProcessor.panic("btclock", e.getMessage()); @@ -284,7 +308,11 @@ public int updateBridgeBtcBlockchain() throws BlockStoreException, IOException { int bridgeBtcBlockchainBestChainHeight = federatorSupport.getBtcBestBlockChainHeight(); int federatorBtcBlockchainBestChainHeight = bitcoinWrapper.getBestChainHeight(); if (federatorBtcBlockchainBestChainHeight > bridgeBtcBlockchainBestChainHeight) { - logger.debug("BTC blockchain height - Federator : {}, Bridge : {}.", bitcoinWrapper.getBestChainHeight(), bridgeBtcBlockchainBestChainHeight); + logger.debug( + "[updateBridgeBtcBlockchain] BTC blockchain height - Federator : {}, Bridge : {}.", + bitcoinWrapper.getBestChainHeight(), + bridgeBtcBlockchainBestChainHeight + ); // Federator's blockchain has more blocks than bridge's blockchain - go and try to // update the bridge with the latest. @@ -300,7 +328,10 @@ public int updateBridgeBtcBlockchain() throws BlockStoreException, IOException { checkNotNull(commonAncestor, "No best chain block found"); - logger.debug("Matched block {}.", commonAncestor.getHeader().getHash()); + logger.debug( + "[updateBridgeBtcBlockchain] Matched block {}.", + commonAncestor.getHeader().getHash() + ); // We found a common ancestor. Send receiveHeaders with the blocks it is missing. StoredBlock current = bitcoinWrapper.getChainHead(); @@ -310,19 +341,29 @@ public int updateBridgeBtcBlockchain() throws BlockStoreException, IOException { current = bitcoinWrapper.getBlock(current.getHeader().getPrevBlockHash()); } if (headersToSendToBridge.isEmpty()) { - logger.debug("Bridge was just updated, no new blocks to send, matchedBlock: {}.", commonAncestor.getHeader().getHash()); + logger.debug( + "[updateBridgeBtcBlockchain] Bridge was just updated, no new blocks to send, matchedBlock: {}.", + commonAncestor.getHeader().getHash() + ); return 0; } headersToSendToBridge = Lists.reverse(headersToSendToBridge); - logger.debug("Headers missing in the bridge {}.", headersToSendToBridge.size()); + logger.debug( + "[updateBridgeBtcBlockchain] Headers missing in the bridge {}.", + headersToSendToBridge.size() + ); int to = Math.min(amountOfHeadersToSend, headersToSendToBridge.size()); List headersToSendToBridgeSubList = headersToSendToBridge.subList(0, to); federatorSupport.sendReceiveHeaders(headersToSendToBridgeSubList.toArray(new Block[]{})); this.markCoinbasesAsReadyToBeInformed(headersToSendToBridgeSubList); - logger.debug("Invoked receiveHeaders with {} blocks. First {}, Last {}.", headersToSendToBridgeSubList.size(), - headersToSendToBridgeSubList.get(0).getHash(), headersToSendToBridgeSubList.get(headersToSendToBridgeSubList.size()-1).getHash()); + logger.debug( + "[updateBridgeBtcBlockchain] Invoked receiveHeaders with {} blocks. First {}, Last {}.", + headersToSendToBridgeSubList.size(), + headersToSendToBridgeSubList.get(0).getHash(), + headersToSendToBridgeSubList.get(headersToSendToBridgeSubList.size()-1).getHash() + ); return headersToSendToBridgeSubList.size(); } @@ -428,7 +469,9 @@ private StoredBlock findBridgeBtcBlockchainMatchingAncestorUsingBlockLocator() t public void updateBridgeBtcTransactions() throws BlockStoreException { logger.debug("[updateBridgeBtcTransactions] Updating btc transactions"); - Map federatorWalletTxMap = bitcoinWrapper.getTransactionMap(bridgeConstants.getBtc2RskMinimumAcceptableConfirmations()); + Map federatorWalletTxMap = bitcoinWrapper.getTransactionMap( + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations() + ); int numberOfTxsSent = 0; Set txsToSendToRskHashes = this.fileData.getTransactionProofs().keySet(); logger.debug("[updateBridgeBtcTransactions] Tx count: {}", txsToSendToRskHashes.size()); @@ -556,29 +599,39 @@ public void updateBridgeBtcTransactions() throws BlockStoreException { * Gets the first ready to be informed coinbase transaction and informs it */ public void updateBridgeBtcCoinbaseTransactions() { - Optional coinbaseInformationReadyToInform = fileData.getCoinbaseInformationMap().values().stream() - .filter(CoinbaseInformation::isReadyToInform).findFirst(); + Optional coinbaseInformationReadyToInform = fileData.getCoinbaseInformationMap() + .values() + .stream() + .filter(CoinbaseInformation::isReadyToInform) + .findFirst(); if (!coinbaseInformationReadyToInform.isPresent()) { - logger.debug("no coinbase transaction to inform"); + logger.debug("[updateBridgeBtcCoinbaseTransactions] no coinbase transaction to inform"); return; } CoinbaseInformation coinbaseInformation = coinbaseInformationReadyToInform.get(); - logger.debug("coinbase transaction {} ready to be informed for block {}", coinbaseInformation.getCoinbaseTransaction().getTxId(), coinbaseInformation.getBlockHash()); + logger.debug( + "[updateBridgeBtcCoinbaseTransactions] coinbase transaction {} ready to be informed for block {}", + coinbaseInformation.getCoinbaseTransaction().getTxId(), + coinbaseInformation.getBlockHash() + ); long bestBlockNumber = federatorSupport.getRskBestChainHeight(); if (activationConfig.isActive(ConsensusRule.RSKIP143, bestBlockNumber)) { if (!federatorSupport.hasBlockCoinbaseInformed(coinbaseInformation.getBlockHash())) { - logger.debug("informing coinbase transaction {}", coinbaseInformation.getCoinbaseTransaction().getTxId()); + logger.debug( + "[updateBridgeBtcCoinbaseTransactions] informing coinbase transaction {}", + coinbaseInformation.getCoinbaseTransaction().getTxId() + ); federatorSupport.sendRegisterCoinbaseTransaction(coinbaseInformation); } else { - logger.debug("coinbase transaction already informed, removing from map"); + logger.debug("[updateBridgeBtcCoinbaseTransactions] coinbase transaction already informed, removing from map"); // Remove the coinbase from the map fileData.getCoinbaseInformationMap().remove(coinbaseInformation.getBlockHash()); } } else { - logger.debug("RSKIP-143 is not active. Can't send coinbase transactions."); + logger.debug("[updateBridgeBtcCoinbaseTransactions] RSKIP-143 is not active. Can't send coinbase transactions."); // Remove the coinbase from the map fileData.getCoinbaseInformationMap().remove(coinbaseInformation.getBlockHash()); } From 0d83a924c6436bb13a316409492270348a312081 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 19 Apr 2023 18:39:20 -0300 Subject: [PATCH 2/7] Catch exceptions when calling registerBtcTransaction and carry on with the remaining transactions --- .../java/co/rsk/federate/BtcToRskClient.java | 219 ++++++++++-------- 1 file changed, 125 insertions(+), 94 deletions(-) diff --git a/src/main/java/co/rsk/federate/BtcToRskClient.java b/src/main/java/co/rsk/federate/BtcToRskClient.java index 7bc325b4b..421ceeb35 100644 --- a/src/main/java/co/rsk/federate/BtcToRskClient.java +++ b/src/main/java/co/rsk/federate/BtcToRskClient.java @@ -295,9 +295,9 @@ public void onTransaction(Transaction tx) { try { this.btcToRskClientFileStorage.write(this.fileData); } catch (IOException e) { - logger.error(e.getMessage(), e); - panicProcessor.panic("btclock", e.getMessage()); - } + logger.error(e.getMessage(), e); + panicProcessor.panic("btclock", e.getMessage()); + } } } @@ -467,7 +467,7 @@ private StoredBlock findBridgeBtcBlockchainMatchingAncestorUsingBlockLocator() t return matchedBlock; } - public void updateBridgeBtcTransactions() throws BlockStoreException { + protected void updateBridgeBtcTransactions() throws BlockStoreException { logger.debug("[updateBridgeBtcTransactions] Updating btc transactions"); Map federatorWalletTxMap = bitcoinWrapper.getTransactionMap( bridgeConstants.getBtc2RskMinimumAcceptableConfirmations() @@ -485,112 +485,143 @@ public void updateBridgeBtcTransactions() throws BlockStoreException { ); for (Sha256Hash txHash : txsToSendToRskHashes) { - Transaction tx = federatorWalletTxMap.get(txHash); - logger.debug("[updateBridgeBtcTransactions] Evaluating Btc Tx {}", txHash); - if (tx == null) { - logger.debug("[updateBridgeBtcTransactions] Btc tx {} was not found in wallet or is not yet confirmed.", txHash); - // Don't remove it as we still have to wait for its confirmations. - continue; - } - logger.debug("[updateBridgeBtcTransactions] Got Btc Tx {} (wtxid:{})", tx.getTxId(), tx.getWTxId()); - BtcTransaction btcTx = ThinConverter.toThinInstance(bridgeConstants.getBtcParams(), tx); - - if (btcTx.getValueSentToMe(federationWallet).isZero()) { - // Remove the tx from the set to be sent to the Bridge since it's not processable - txsToSendToRskHashes.remove(txHash); - - logger.warn( - "[updateBridgeBtcTransactions] Transaction hash {} does not have any output to the current federation {}", - btcTx.getHash(true), - federation.getAddress() - ); - continue; - } - - long bestBlockNumber = federatorSupport.getRskBestChainHeight(); - PeginInformation peginInformation = new PeginInformation( - btcLockSenderProvider, - peginInstructionsProvider, - activationConfig.forBlock(bestBlockNumber) - ); try { - peginInformation.parse(btcTx); - } catch (PeginInstructionsException e) { - String message = String.format( - "Could not get peg-in information for tx %s", - btcTx.getHash() + Transaction tx = federatorWalletTxMap.get(txHash); + logger.debug("[updateBridgeBtcTransactions] Evaluating Btc Tx {}", txHash); + if (tx == null) { + logger.debug( + "[updateBridgeBtcTransactions] Btc tx {} was not found in wallet or is not yet confirmed.", + txHash + ); + // Don't remove it as we still have to wait for its confirmations. + continue; + } + logger.debug( + "[updateBridgeBtcTransactions] Got Btc Tx {} (wtxid:{})", + tx.getTxId(), + tx.getWTxId() ); - logger.warn("[updateBridgeBtcTransactions] {}", message); - // If tx sender could be retrieved then let the Bridge process the tx and refund the sender - if (peginInformation.getSenderBtcAddress() != null) { - logger.warn("[updateBridgeBtcTransactions] Funds will be refunded to sender."); - } else { + BtcTransaction btcTx = ThinConverter.toThinInstance(bridgeConstants.getBtcParams(), tx); + + if (btcTx.getValueSentToMe(federationWallet).isZero()) { // Remove the tx from the set to be sent to the Bridge since it's not processable txsToSendToRskHashes.remove(txHash); + + logger.warn( + "[updateBridgeBtcTransactions] Transaction hash {} does not have any output to the current federation {}", + btcTx.getHash(true), + federation.getAddress() + ); continue; } - } - // Check if the tx can be processed by the Bridge - if (!isTxProcessable(btcTx, peginInformation.getSenderBtcAddressType())) { - logger.warn( - "[updateBridgeBtcTransactions] Transaction hash {} contains a type {} that it is not processable.", - btcTx.getHash(true), - peginInformation.getSenderBtcAddressType() + long bestBlockNumber = federatorSupport.getRskBestChainHeight(); + PeginInformation peginInformation = new PeginInformation( + btcLockSenderProvider, + peginInstructionsProvider, + activationConfig.forBlock(bestBlockNumber) ); - txsToSendToRskHashes.remove(txHash); - continue; - } - - // Check if the tx was processed (using the tx hash without witness) - if (!federatorSupport.isBtcTxHashAlreadyProcessed(tx.getTxId())) { - logger.debug("[updateBridgeBtcTransactions] Btc Tx {} with enough confirmations and not yet processed", tx.getWTxId()); - synchronized (this) { - List proofs = this.fileData.getTransactionProofs().get(txHash); - if (proofs == null || proofs.isEmpty()) { + try { + peginInformation.parse(btcTx); + } catch (PeginInstructionsException e) { + String message = String.format( + "Could not get peg-in information for tx %s", + btcTx.getHash() + ); + logger.warn("[updateBridgeBtcTransactions] {}", message); + // If tx sender could be retrieved then let the Bridge process the tx and refund the sender + if (peginInformation.getSenderBtcAddress() != null) { + logger.warn("[updateBridgeBtcTransactions] Funds will be refunded to sender."); + } else { + // Remove the tx from the set to be sent to the Bridge since it's not processable + txsToSendToRskHashes.remove(txHash); continue; } + } - StoredBlock txStoredBlock = findBestChainStoredBlockFor(tx); - int blockHeight = txStoredBlock.getHeight(); - PartialMerkleTree pmt = null; - for (Proof proof : proofs) { - if (proof.getBlockHash().equals(txStoredBlock.getHeader().getHash())) { - pmt = proof.getPartialMerkleTree(); + // Check if the tx can be processed by the Bridge + if (!isTxProcessable(btcTx, peginInformation.getSenderBtcAddressType())) { + logger.warn( + "[updateBridgeBtcTransactions] Transaction hash {} contains a type {} that it is not processable.", + btcTx.getHash(true), + peginInformation.getSenderBtcAddressType() + ); + txsToSendToRskHashes.remove(txHash); + continue; + } + + // Check if the tx was processed (using the tx hash without witness) + if (!federatorSupport.isBtcTxHashAlreadyProcessed(tx.getTxId())) { + logger.debug( + "[updateBridgeBtcTransactions] Btc Tx {} with enough confirmations and not yet processed", + tx.getWTxId() + ); + synchronized (this) { + List proofs = this.fileData.getTransactionProofs().get(txHash); + if (proofs == null || proofs.isEmpty()) { + continue; } - } - federatorSupport.sendRegisterBtcTransaction(tx, blockHeight, pmt); - numberOfTxsSent++; + StoredBlock txStoredBlock = findBestChainStoredBlockFor(tx); + int blockHeight = txStoredBlock.getHeight(); + PartialMerkleTree pmt = null; + for (Proof proof : proofs) { + if (proof.getBlockHash().equals(txStoredBlock.getHeader().getHash())) { + pmt = proof.getPartialMerkleTree(); + } + } - // Sent a maximum of 40 registerBtcTransaction txs per federator - if (numberOfTxsSent >= MAXIMUM_REGISTER_BTC_LOCK_TXS_PER_TURN) { - break; - } + // Make sure the proof was found + if (pmt == null) { + logger.error( + "[updateBridgeBtcTransactions] Could not find proof that the transaction {} belongs to the block {}", + txHash, + txStoredBlock.getHeader().getHash() + ); + continue; + } - logger.debug("[updateBridgeBtcTransactions] Invoked registerBtcTransaction for tx {}", txHash); - } - // Tx could be null if having less than the desired amount of confirmations, - // do not clear in that case since we'd leave a tx without processing - } else { - logger.debug("[updateBridgeBtcTransactions] Btc Tx {} already processed", tx.getTxId()); - // Verify if the transaction was processed (using the tx id without witness) - Long txProcessedHeight = federatorSupport.getBtcTxHashProcessedHeight(tx.getTxId()); - Long bestChainHeight = federatorSupport.getRskBestChainHeight(); - - // If the bridge says this transaction was processed at height N, and current height - // is M, with M - N >= K - // with K = BridgeConstants.getBtc2RskMinimumAcceptableConfirmationsOnRsk() - // then remove the transaction from the list - if ((bestChainHeight - txProcessedHeight) >= bridgeConstants.getBtc2RskMinimumAcceptableConfirmationsOnRsk()) { - txsToSendToRskHashes.remove(txHash); - logger.debug( - "[updateBridgeBtcTransactions] Btc Tx {} was processed at height {}, current height is {}. Tx removed from pending lock list", - txHash, - txProcessedHeight, - bestChainHeight - ); + federatorSupport.sendRegisterBtcTransaction(tx, blockHeight, pmt); + numberOfTxsSent++; + + // Sent a maximum of 40 registerBtcTransaction txs per federator + if (numberOfTxsSent >= MAXIMUM_REGISTER_BTC_LOCK_TXS_PER_TURN) { + break; + } + + logger.debug( + "[updateBridgeBtcTransactions] Invoked registerBtcTransaction for tx {}", + txHash + ); + } + // Tx could be null if having less than the desired amount of confirmations, + // do not clear in that case since we'd leave a tx without processing + } else { + logger.debug("[updateBridgeBtcTransactions] Btc Tx {} already processed", tx.getTxId()); + // Verify if the transaction was processed (using the tx id without witness) + Long txProcessedHeight = federatorSupport.getBtcTxHashProcessedHeight(tx.getTxId()); + Long bestChainHeight = federatorSupport.getRskBestChainHeight(); + + // If the bridge says this transaction was processed at height N, and current height + // is M, with M - N >= K + // with K = BridgeConstants.getBtc2RskMinimumAcceptableConfirmationsOnRsk() + // then remove the transaction from the list + if ((bestChainHeight - txProcessedHeight) >= bridgeConstants.getBtc2RskMinimumAcceptableConfirmationsOnRsk()) { + txsToSendToRskHashes.remove(txHash); + logger.debug( + "[updateBridgeBtcTransactions] Btc Tx {} was processed at height {}, current height is {}. Tx removed from pending lock list", + txHash, + txProcessedHeight, + bestChainHeight + ); + } } + } catch (Exception e) { + logger.error( + "[updateBridgeBtcTransactions] An error occurred while informing transaction {} to the Bridge. {}", + txHash, + e.getMessage() + ); } } } From 56539a43f24229b8415a78535f10406cb88f88a2 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 19 Apr 2023 18:40:45 -0300 Subject: [PATCH 3/7] Update updateBridgeBtcTransactions tests --- .../co/rsk/federate/BtcToRskClientTest.java | 537 +++++++++++------- 1 file changed, 320 insertions(+), 217 deletions(-) diff --git a/src/test/java/co/rsk/federate/BtcToRskClientTest.java b/src/test/java/co/rsk/federate/BtcToRskClientTest.java index 2abec2a1f..4da30938f 100644 --- a/src/test/java/co/rsk/federate/BtcToRskClientTest.java +++ b/src/test/java/co/rsk/federate/BtcToRskClientTest.java @@ -18,7 +18,6 @@ import co.rsk.federate.mock.SimpleBlock; import co.rsk.federate.mock.SimpleBtcTransaction; import co.rsk.federate.mock.SimpleFederatorSupport; -import co.rsk.federate.signing.utils.TestUtils; import co.rsk.net.NodeBlockProcessor; import co.rsk.peg.BridgeUtils; import co.rsk.peg.Federation; @@ -927,101 +926,105 @@ public void updateBlockchainWithDeepFork() throws Exception { @Test public void updateNoTransaction() throws Exception { - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh); + BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionWithoutProof() throws Exception { - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(createTransaction()); - bw.setTransactions(txs); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); - BtcToRskClient client = createClientWithMocks(bw, fh); + BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionInClientWithoutProofYet() throws Exception { Transaction tx = createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport, mockBlockchain()); client.onTransaction(tx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionInClientWithBlockProof() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + bitcoinWrapper.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(blocks[3].getHeader().getHash(), tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); BtcToRskClient client = btcToRskClientBuilder - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); - Assert.assertEquals(1, tstrbl.size()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); - SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction tstrbl0 = tstrbl.get(0); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); - Assert.assertSame(tx, tstrbl0.tx); - Assert.assertEquals(3, tstrbl0.blockHeight); - Assert.assertNotNull(tstrbl0.pmt); + Assert.assertSame(tx, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } @Test @@ -1044,12 +1047,12 @@ public void updateTransactionCheckMaximumRegisterBtcLocksTxsPerTurn() throws Exc Block block = createBlock(blocks[3].getHeader().getHash(), txs.toArray(new Transaction[]{})); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); BtcToRskClient client = btcToRskClientBuilder .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) @@ -1062,11 +1065,12 @@ public void updateTransactionCheckMaximumRegisterBtcLocksTxsPerTurn() throws Exc client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); - Assert.assertEquals(BtcToRskClient.MAXIMUM_REGISTER_BTC_LOCK_TXS_PER_TURN, tstrbl.size()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertFalse(txsSentToRegisterBtcTransaction.isEmpty()); + Assert.assertEquals(BtcToRskClient.MAXIMUM_REGISTER_BTC_LOCK_TXS_PER_TURN, txsSentToRegisterBtcTransaction.size()); } @Test @@ -1075,16 +1079,25 @@ public void updateTransactionInClientWithBlockProofInTwoCompetitiveBlocks() thro // It checks the federator sends to the bridge the Proof of the block in the best chain. for (int testNumber = 0; testNumber < 2; testNumber++) { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + bitcoinWrapper.setTransactions(txs); StoredBlock[] blocks = createBlockchain(4); StoredBlock[] blocksAndForkedBlock = Arrays.copyOf(blocks, 6); - Block forkedHeader = new Block(networkParameters, 1, createHash(), createHash(), 1, 1, 1, new ArrayList()); + Block forkedHeader = new Block( + networkParameters, + 1, + createHash(), + createHash(), + 1, + 1, + 1, + new ArrayList<>() + ); StoredBlock forkedBlock = new StoredBlock(forkedHeader, null, 3); blocksAndForkedBlock[5] = forkedBlock; - bw.setBlocks(blocksAndForkedBlock); + bitcoinWrapper.setBlocks(blocksAndForkedBlock); Map appears = new HashMap<>(); appears.put(blocks[3].getHeader().getHash(), 1); appears.put(forkedBlock.getHeader().getHash(), 1); @@ -1095,12 +1108,12 @@ public void updateTransactionInClientWithBlockProofInTwoCompetitiveBlocks() thro // The block in the fork Block block2 = createBlock(forkedBlock.getHeader().getHash(), tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); BtcToRskClient client = btcToRskClientBuilder - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) @@ -1119,36 +1132,46 @@ public void updateTransactionInClientWithBlockProofInTwoCompetitiveBlocks() thro client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); - Assert.assertEquals(1, tstrbl.size()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); - SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction tstrbl0 = tstrbl.get(0); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); - Assert.assertSame(tx, tstrbl0.tx); - Assert.assertEquals(3, tstrbl0.blockHeight); - Assert.assertNotNull(tstrbl0.pmt); - Assert.assertEquals(client.generatePMT(block1, tx), tstrbl0.pmt); + Assert.assertSame(tx, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); + Assert.assertEquals(client.generatePMT(block1, tx), txSentToRegisterBtcTransaction.pmt); } } @Test public void updateTransactionInClientWithBlockProofInLosingFork() throws Exception { - // It checks the federator does not sends a tx to the bridge if it was just included in a block that is not in the best chain. + // Checks that the pegnatorie does not send a tx to the bridge + // if it was just included in a block that is not in the best chain. SimpleBtcTransaction tx = (SimpleBtcTransaction)createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); - bw.setTransactions(txs); + bitcoinWrapper.setTransactions(txs); StoredBlock[] blocks = createBlockchain(4); StoredBlock[] blocksAndForkedBlock = Arrays.copyOf(blocks, 6); - Block forkedHeader = new Block(networkParameters, 1, createHash(), createHash(), 1, 1, 1, new ArrayList()); + Block forkedHeader = new Block( + networkParameters, + 1, + createHash(), + createHash(), + 1, + 1, + 1, + new ArrayList<>() + ); StoredBlock forkedBlock = new StoredBlock(forkedHeader, null, 3); blocksAndForkedBlock[5] = forkedBlock; - bw.setBlocks(blocksAndForkedBlock); + bitcoinWrapper.setBlocks(blocksAndForkedBlock); Map appears = new HashMap<>(); appears.put(forkedBlock.getHeader().getHash(), 1); @@ -1157,9 +1180,9 @@ public void updateTransactionInClientWithBlockProofInLosingFork() throws Excepti // The block in the fork Block block2 = createBlock(forkedBlock.getHeader().getHash(), tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh); + BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport); client.onTransaction(tx); @@ -1167,61 +1190,69 @@ public void updateTransactionInClientWithBlockProofInLosingFork() throws Excepti client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionWithNoSenderValid() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcToRskClient client = btcToRskClientBuilder - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionWithMultisig() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2SHMULTISIG); ActivationConfig activationsConfig = mock(ActivationConfig.class); @@ -1232,40 +1263,49 @@ public void updateTransactionWithMultisig() throws Exception { BtcToRskClient client = btcToRskClientBuilder .withActivationConfig(activationsConfig) - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); + + Assert.assertSame(tx, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } @Test public void updateTransactionWithSegwitCompatible() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2SHP2WPKH); ActivationConfig activationsConfig = mock(ActivationConfig.class); @@ -1276,40 +1316,49 @@ public void updateTransactionWithSegwitCompatible() throws Exception { BtcToRskClient client = btcToRskClientBuilder .withActivationConfig(activationsConfig) - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); + + Assert.assertSame(tx, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } @Test public void updateTransactionWithMultisig_before_rskip143() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction)createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); ActivationConfig activationsConfig = mock(ActivationConfig.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); @@ -1317,17 +1366,24 @@ public void updateTransactionWithMultisig_before_rskip143() throws Exception { doReturn(true).when(activations).isActive(eq(ConsensusRule.RSKIP89)); doReturn(false).when(activations).isActive(eq(ConsensusRule.RSKIP143)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain(), TxSenderAddressType.P2SHMULTISIG, activationsConfig); + BtcToRskClient client = createClientWithMocks( + bitcoinWrapper, + federatorSupport, + mockBlockchain(), + TxSenderAddressType.P2SHMULTISIG, + activationsConfig + ); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1379,23 +1435,24 @@ public void updateTransaction_with_release_before_rskip143() throws Exception { Transaction releaseTx = ThinConverter.toOriginalInstance(bridgeConstants.getBtcParamsString(), releaseTx1); // Construct environment - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(releaseTx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); - releaseTx.addBlockAppearance(blocks[3].getHeader().getHash(), 1); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), releaseTx); + releaseTx.addBlockAppearance(blockWithTx.getHash(), 1); - Block block = createBlock(releaseTx); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcToRskClient client = buildWithFactoryAndSetup( - fh, + federatorSupport, mock(NodeBlockProcessor.class), activationsConfig, - bw, + bitcoinWrapper, bridgeConstants, getMockedBtcToRskClientFileStorage(), new BtcLockSenderProvider(), @@ -1407,35 +1464,38 @@ public void updateTransaction_with_release_before_rskip143() throws Exception { // Ensure tx is loaded and its proof is also loaded client.onTransaction(releaseTx); - client.onBlock(block); + client.onBlock(blockWithTx); // Try to inform tx client.updateBridgeBtcTransactions(); // The release tx should be informed - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); - Assert.assertEquals(releaseTx.getTxId(), tstrbl.get(0).tx.getTxId()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertFalse(txsSentToRegisterBtcTransaction.isEmpty()); + Assert.assertEquals(releaseTx.getTxId(), txsSentToRegisterBtcTransaction.get(0).tx.getTxId()); } @Test public void updateTransactionWithSegwitCompatible_before_rskip143() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createSegwitTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); ActivationConfig activationsConfig = mock(ActivationConfig.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); @@ -1443,35 +1503,44 @@ public void updateTransactionWithSegwitCompatible_before_rskip143() throws Excep doReturn(true).when(activations).isActive(eq(ConsensusRule.RSKIP89)); doReturn(false).when(activations).isActive(eq(ConsensusRule.RSKIP143)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain(), TxSenderAddressType.P2SHP2WPKH, activationsConfig); + BtcToRskClient client = createClientWithMocks( + bitcoinWrapper, + federatorSupport, + mockBlockchain(), + TxSenderAddressType.P2SHP2WPKH, + activationsConfig + ); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionWithSenderUnknown_before_rskip170() throws Exception { - SimpleBtcTransaction tx = (SimpleBtcTransaction)createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); + SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); ActivationConfig activationsConfig = mock(ActivationConfig.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); @@ -1481,40 +1550,44 @@ public void updateTransactionWithSenderUnknown_before_rskip170() throws Exceptio doReturn(false).when(activationsConfig).isActive(eq(ConsensusRule.RSKIP170), anyLong()); BtcToRskClient client = createClientWithMocks( - bw, - fh, + bitcoinWrapper, + federatorSupport, mockBlockchain(), TxSenderAddressType.UNKNOWN, activationsConfig ); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransactionWithSenderUnknown_after_rskip170() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); - StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + + Block block = createBlock(tx); Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(block.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); + StoredBlock[] blocks = createBlockchain(4); + blocks[4] = new StoredBlock(block, null, 4); // Replace the last block with the one containing the transaction + bitcoinWrapper.setBlocks(blocks); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.UNKNOWN); ActivationConfig activationsConfig = mock(ActivationConfig.class); @@ -1526,8 +1599,8 @@ public void updateTransactionWithSenderUnknown_after_rskip170() throws Exception BtcToRskClient client = btcToRskClientBuilder .withActivationConfig(activationsConfig) - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) @@ -1538,27 +1611,31 @@ public void updateTransactionWithSenderUnknown_after_rskip170() throws Exception client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertFalse(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransaction_peginInformationParsingFails_withoutSenderAddress() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); ActivationConfig activationsConfig = mock(ActivationConfig.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); @@ -1573,38 +1650,42 @@ public void updateTransaction_peginInformationParsingFails_withoutSenderAddress( BtcToRskClient client = btcToRskClientBuilder .withActivationConfig(activationsConfig) - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); - Assert.assertNotNull(tstrbl); - Assert.assertTrue(tstrbl.isEmpty()); + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test public void updateTransaction_peginInformationParsingFails_withSenderAddress() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); - Block block = createBlock(tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); ActivationConfig activationsConfig = mock(ActivationConfig.class); ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class); @@ -1624,47 +1705,58 @@ public void updateTransaction_peginInformationParsingFails_withSenderAddress() t BtcToRskClient client = btcToRskClientBuilder .withActivationConfig(activationsConfig) - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) .build(); client.onTransaction(tx); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); - Assert.assertNotNull(tstrbl); - Assert.assertFalse(tstrbl.isEmpty()); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); + + Assert.assertSame(tx, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } @Test public void updateTransaction_noOutputToCurrentFederation() throws Exception { SimpleBtcTransaction txWithoutOutputs = new SimpleBtcTransaction(networkParameters, createHash(), createHash(), false); SimpleBtcTransaction txToTheFederation = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); + Set txs = new HashSet<>(); txs.add(txWithoutOutputs); txs.add(txToTheFederation); - bw.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); + Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), txWithoutOutputs, txToTheFederation); + Map appears = new HashMap<>(); - appears.put(blocks[3].getHeader().getHash(), 1); + appears.put(blockWithTx.getHash(), 1); txWithoutOutputs.setAppearsInHashes(appears); txToTheFederation.setAppearsInHashes(appears); - Block block = createBlock(txWithoutOutputs, txToTheFederation); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); BtcToRskClient client = btcToRskClientBuilder - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) @@ -1672,34 +1764,44 @@ public void updateTransaction_noOutputToCurrentFederation() throws Exception { client.onTransaction(txWithoutOutputs); client.onTransaction(txToTheFederation); - client.onBlock(block); + client.onBlock(blockWithTx); client.updateBridgeBtcTransactions(); - List tstrbl = fh.getTxsSentToRegisterBtcTransaction(); + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(1, txsSentToRegisterBtcTransaction.size()); + + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction = txsSentToRegisterBtcTransaction.get(0); - Assert.assertNotNull(tstrbl); - Assert.assertEquals(1, tstrbl.size()); + Assert.assertSame(txToTheFederation, txSentToRegisterBtcTransaction.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } @Test - public void raiseIfTransactionInClientWithBlockProofNotInBlockchain() throws Exception { + public void ignoreTransactionInClientWithBlockProofNotInBlockchain() throws Exception { SimpleBtcTransaction tx = (SimpleBtcTransaction) createTransaction(); - SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); + tx.setAppearsInHashes(null); + Set txs = new HashSet<>(); txs.add(tx); - bw.setTransactions(txs); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + StoredBlock[] blocks = createBlockchain(4); - bw.setBlocks(blocks); - tx.setAppearsInHashes(null); + bitcoinWrapper.setBlocks(blocks); Block block = createBlock(tx); - SimpleFederatorSupport fh = new SimpleFederatorSupport(); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); BtcToRskClient client = btcToRskClientBuilder - .withBitcoinWrapper(bw) - .withFederatorSupport(fh) + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) .withBridgeConstants(BridgeRegTestConstants.getInstance()) .withBtcLockSenderProvider(btcLockSenderProvider) .withFederation(genesisFederation) @@ -1708,12 +1810,13 @@ public void raiseIfTransactionInClientWithBlockProofNotInBlockchain() throws Exc client.onTransaction(tx); client.onBlock(block); - try { - client.updateBridgeBtcTransactions(); - Assert.fail(); - } catch (IllegalStateException ex) { - Assert.assertEquals("Tx not in the best chain: " + tx.getTxId(), ex.getMessage()); - } + client.updateBridgeBtcTransactions(); + + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test(expected = Exception.class) @@ -2156,7 +2259,7 @@ private StoredBlock[] createBlockchain(int height) { Sha256Hash previousHash = Sha256Hash.wrap("0000000000000000000000000000000000000000000000000000000000000000"); for (int k = 0; k <= height; k++) { - Block header = new Block(networkParameters, 1, previousHash, createHash(), 1, 1, 1, new ArrayList()); + Block header = new Block(networkParameters, 1, previousHash, createHash(), 1, 1, 1, new ArrayList<>()); StoredBlock block = new StoredBlock(header, null, k); blocks[k] = block; previousHash = header.getHash(); From 9efd183570918c2ebe0fe0a7922c43b876c7ec8c Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 21 Apr 2023 17:16:48 -0300 Subject: [PATCH 4/7] Make methods protected in BtcToRskClient --- src/main/java/co/rsk/federate/BtcToRskClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/co/rsk/federate/BtcToRskClient.java b/src/main/java/co/rsk/federate/BtcToRskClient.java index 421ceeb35..899b57c8c 100644 --- a/src/main/java/co/rsk/federate/BtcToRskClient.java +++ b/src/main/java/co/rsk/federate/BtcToRskClient.java @@ -301,7 +301,7 @@ public void onTransaction(Transaction tx) { } } - public int updateBridgeBtcBlockchain() throws BlockStoreException, IOException { + protected int updateBridgeBtcBlockchain() throws BlockStoreException, IOException { long bestBlockNumber = federatorSupport.getRskBestChainHeight(); boolean useBlockDepth = activationConfig.isActive(ConsensusRule.RSKIP89, bestBlockNumber); @@ -629,7 +629,7 @@ protected void updateBridgeBtcTransactions() throws BlockStoreException { /** * Gets the first ready to be informed coinbase transaction and informs it */ - public void updateBridgeBtcCoinbaseTransactions() { + protected void updateBridgeBtcCoinbaseTransactions() { Optional coinbaseInformationReadyToInform = fileData.getCoinbaseInformationMap() .values() .stream() From a8b712b3ce8948af669af4c8c93e9e784d15be24 Mon Sep 17 00:00:00 2001 From: Marcos Date: Mon, 24 Apr 2023 15:10:12 -0300 Subject: [PATCH 5/7] Add new tests for BtcToRskClient. Try to register transactions without block proof --- .../co/rsk/federate/BtcToRskClientTest.java | 224 +++++++++++++++++- 1 file changed, 221 insertions(+), 3 deletions(-) diff --git a/src/test/java/co/rsk/federate/BtcToRskClientTest.java b/src/test/java/co/rsk/federate/BtcToRskClientTest.java index 4da30938f..994572757 100644 --- a/src/test/java/co/rsk/federate/BtcToRskClientTest.java +++ b/src/test/java/co/rsk/federate/BtcToRskClientTest.java @@ -989,15 +989,17 @@ public void updateTransactionInClientWithBlockProof() throws Exception { Set txs = new HashSet<>(); txs.add(tx); - SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); - bitcoinWrapper.setTransactions(txs); StoredBlock[] blocks = createBlockchain(4); - bitcoinWrapper.setBlocks(blocks); Block blockWithTx = createBlock(blocks[3].getHeader().getHash(), tx); + Map appears = new HashMap<>(); appears.put(blockWithTx.getHash(), 1); tx.setAppearsInHashes(appears); + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); @@ -1027,6 +1029,222 @@ public void updateTransactionInClientWithBlockProof() throws Exception { Assert.assertNotNull(txSentToRegisterBtcTransaction.pmt); } + @Test + public void updateTransactionWithAndWithoutBlockProofs() throws Exception { + SimpleBtcTransaction txWithProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithProof2 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof2 = (SimpleBtcTransaction) createTransaction(); + Set txs = new HashSet<>(); + txs.add(txWithProof1); + txs.add(txWithoutProof1); + txs.add(txWithoutProof2); + txs.add(txWithProof2); + + StoredBlock[] blocks = createBlockchain(4); + Block blockWithTxs = createBlock( + blocks[3].getHeader().getHash(), + txWithProof1, + txWithoutProof1, + txWithoutProof2, + txWithProof2 + ); + + Map appears = new HashMap<>(); + appears.put(blockWithTxs.getHash(), 1); + txWithProof1.setAppearsInHashes(appears); + txWithProof2.setAppearsInHashes(appears); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); + BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); + + BtcToRskClient client = btcToRskClientBuilder + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) + .withBridgeConstants(BridgeRegTestConstants.getInstance()) + .withBtcLockSenderProvider(btcLockSenderProvider) + .withFederation(genesisFederation) + .build(); + + client.onTransaction(txWithProof1); + client.onTransaction(txWithoutProof1); + client.onTransaction(txWithoutProof2); + client.onTransaction(txWithProof2); + client.onBlock(blockWithTxs); + + client.updateBridgeBtcTransactions(); + + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(2, txsSentToRegisterBtcTransaction.size()); + + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction1 = txsSentToRegisterBtcTransaction.get(0); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction2 = txsSentToRegisterBtcTransaction.get(1); + + Assert.assertSame(txWithProof1, txSentToRegisterBtcTransaction1.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction1.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction1.pmt); + + Assert.assertSame(txWithProof2, txSentToRegisterBtcTransaction2.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction2.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction2.pmt); + } + + @Test + public void updateTransactionWithAndWithoutRegisteredBlockProofs() throws Exception { + SimpleBtcTransaction txWithProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithProof2 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof2 = (SimpleBtcTransaction) createTransaction(); + Set txs = new HashSet<>(); + txs.add(txWithProof1); + txs.add(txWithoutProof1); + txs.add(txWithoutProof2); + txs.add(txWithProof2); + + StoredBlock[] blocks = createBlockchain(4); + Block registeredBlockWithTxs = createBlock( + blocks[3].getHeader().getHash(), + txWithProof1, + txWithProof2 + ); + Block unregisteredBlockWithTxs = createBlock( + txWithoutProof1, + txWithoutProof2 + ); + + Map appearsInRegistered = new HashMap<>(); + appearsInRegistered.put(registeredBlockWithTxs.getHash(), 1); + txWithProof1.setAppearsInHashes(appearsInRegistered); + txWithProof2.setAppearsInHashes(appearsInRegistered); + + Map appearsInUnregistered = new HashMap<>(); + appearsInUnregistered.put(unregisteredBlockWithTxs.getHash(), 1); + txWithoutProof1.setAppearsInHashes(appearsInUnregistered); + txWithoutProof2.setAppearsInHashes(appearsInUnregistered); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); + BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); + + BtcToRskClient client = btcToRskClientBuilder + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) + .withBridgeConstants(BridgeRegTestConstants.getInstance()) + .withBtcLockSenderProvider(btcLockSenderProvider) + .withFederation(genesisFederation) + .build(); + + client.onTransaction(txWithProof1); + client.onTransaction(txWithoutProof1); + client.onTransaction(txWithoutProof2); + client.onTransaction(txWithProof2); + client.onBlock(registeredBlockWithTxs); + + client.updateBridgeBtcTransactions(); + + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(2, txsSentToRegisterBtcTransaction.size()); + + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction1 = txsSentToRegisterBtcTransaction.get(0); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction2 = txsSentToRegisterBtcTransaction.get(1); + + Assert.assertSame(txWithProof1, txSentToRegisterBtcTransaction1.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction1.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction1.pmt); + + Assert.assertSame(txWithProof2, txSentToRegisterBtcTransaction2.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction2.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction2.pmt); + } + + @Test + public void updateTransactionWithAndWithoutProperlyRegisteredBlockProofs() throws Exception { + SimpleBtcTransaction txWithProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithProof2 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof1 = (SimpleBtcTransaction) createTransaction(); + SimpleBtcTransaction txWithoutProof2 = (SimpleBtcTransaction) createTransaction(); + Set txs = new HashSet<>(); + txs.add(txWithProof1); + txs.add(txWithoutProof1); + txs.add(txWithoutProof2); + txs.add(txWithProof2); + + StoredBlock[] blocks = createBlockchain(4); + Block registeredBlockWithTxs = createBlock( + blocks[3].getHeader().getHash(), + txWithProof1, + txWithProof2 + ); + Block unregisteredBlockWithTxs = createBlock( + txWithoutProof1, + txWithoutProof2 + ); + + Map appearsInRegistered = new HashMap<>(); + appearsInRegistered.put(registeredBlockWithTxs.getHash(), 1); + txWithProof1.setAppearsInHashes(appearsInRegistered); + txWithProof2.setAppearsInHashes(appearsInRegistered); + + Map appearsInUnregistered = new HashMap<>(); + appearsInUnregistered.put(blocks[3].getHeader().getHash(), 1); + txWithoutProof1.setAppearsInHashes(appearsInUnregistered); + txWithoutProof2.setAppearsInHashes(appearsInUnregistered); + + SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); + bitcoinWrapper.setTransactions(txs); + bitcoinWrapper.setBlocks(blocks); + + SimpleFederatorSupport federatorSupport = new SimpleFederatorSupport(); + BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(TxSenderAddressType.P2PKH); + + BtcToRskClient client = btcToRskClientBuilder + .withBitcoinWrapper(bitcoinWrapper) + .withFederatorSupport(federatorSupport) + .withBridgeConstants(BridgeRegTestConstants.getInstance()) + .withBtcLockSenderProvider(btcLockSenderProvider) + .withFederation(genesisFederation) + .build(); + + client.onTransaction(txWithProof1); + client.onTransaction(txWithoutProof1); + client.onTransaction(txWithoutProof2); + client.onTransaction(txWithProof2); + client.onBlock(registeredBlockWithTxs); + client.onBlock(unregisteredBlockWithTxs); + + client.updateBridgeBtcTransactions(); + + List txsSentToRegisterBtcTransaction = + federatorSupport.getTxsSentToRegisterBtcTransaction(); + + Assert.assertNotNull(txsSentToRegisterBtcTransaction); + Assert.assertEquals(2, txsSentToRegisterBtcTransaction.size()); + + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction1 = txsSentToRegisterBtcTransaction.get(0); + SimpleFederatorSupport.TransactionSentToRegisterBtcTransaction txSentToRegisterBtcTransaction2 = txsSentToRegisterBtcTransaction.get(1); + + Assert.assertSame(txWithProof1, txSentToRegisterBtcTransaction1.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction1.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction1.pmt); + + Assert.assertSame(txWithProof2, txSentToRegisterBtcTransaction2.tx); + Assert.assertEquals(3, txSentToRegisterBtcTransaction2.blockHeight); + Assert.assertNotNull(txSentToRegisterBtcTransaction2.pmt); + } + @Test public void updateTransactionCheckMaximumRegisterBtcLocksTxsPerTurn() throws Exception { int AVAILABLE_TXS = 50; From ffa221c9453cb9ccb984400021514abf097a29b8 Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 27 Apr 2023 16:34:13 -0300 Subject: [PATCH 6/7] Remove unused exception throw in method signature --- src/main/java/co/rsk/federate/BtcToRskClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/co/rsk/federate/BtcToRskClient.java b/src/main/java/co/rsk/federate/BtcToRskClient.java index 899b57c8c..2d4ee5e82 100644 --- a/src/main/java/co/rsk/federate/BtcToRskClient.java +++ b/src/main/java/co/rsk/federate/BtcToRskClient.java @@ -467,7 +467,7 @@ private StoredBlock findBridgeBtcBlockchainMatchingAncestorUsingBlockLocator() t return matchedBlock; } - protected void updateBridgeBtcTransactions() throws BlockStoreException { + protected void updateBridgeBtcTransactions() { logger.debug("[updateBridgeBtcTransactions] Updating btc transactions"); Map federatorWalletTxMap = bitcoinWrapper.getTransactionMap( bridgeConstants.getBtc2RskMinimumAcceptableConfirmations() From 997e25ba052262c55cdf7f2a0ca1777da8d13734 Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 27 Apr 2023 16:35:09 -0300 Subject: [PATCH 7/7] Refactor BtcToRskClientTest. Remove unused params --- .../co/rsk/federate/BtcToRskClientTest.java | 149 ++++++++---------- 1 file changed, 64 insertions(+), 85 deletions(-) diff --git a/src/test/java/co/rsk/federate/BtcToRskClientTest.java b/src/test/java/co/rsk/federate/BtcToRskClientTest.java index 994572757..57e9a8147 100644 --- a/src/test/java/co/rsk/federate/BtcToRskClientTest.java +++ b/src/test/java/co/rsk/federate/BtcToRskClientTest.java @@ -45,6 +45,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.*; /** @@ -73,7 +74,7 @@ public void getNoTransactions() { BtcToRskClient client = new BtcToRskClient(); Map> txs = client.getTransactionsToSendToRsk(); - Assert.assertTrue(txs.isEmpty()); + assertTrue(txs.isEmpty()); } @Test @@ -89,27 +90,17 @@ public void addAndGetOneTransaction() throws Exception { List proofs = txs.get(tx.getWTxId()); Assert.assertNotNull(proofs); - Assert.assertTrue(proofs.isEmpty()); - } - - private BtcToRskClient createClientWithMocks(BitcoinWrapper bw, FederatorSupport fs) throws Exception { - return createClientWithMocks(bw, fs, null); - } - - private BtcToRskClient createClientWithMocks(BitcoinWrapper bw, FederatorSupport fs, Blockchain blockchain) throws Exception { - return createClientWithMocks(bw, fs, blockchain, TxSenderAddressType.P2PKH); + assertTrue(proofs.isEmpty()); } private BtcToRskClient createClientWithMocks( BitcoinWrapper bw, FederatorSupport fs, - Blockchain blockchain, int amountOfHeadersToSend) throws Exception { return createClientWithMocks( bw, fs, - blockchain, TxSenderAddressType.P2PKH, activationConfig, amountOfHeadersToSend @@ -118,24 +109,20 @@ private BtcToRskClient createClientWithMocks( private BtcToRskClient createClientWithMocks( BitcoinWrapper bw, - FederatorSupport fs, - Blockchain blockchain, - TxSenderAddressType txSenderAddressType) throws Exception { + FederatorSupport fs) throws Exception { - return createClientWithMocks(bw, fs, blockchain, txSenderAddressType, activationConfig); + return createClientWithMocks(bw, fs, TxSenderAddressType.P2PKH, activationConfig); } private BtcToRskClient createClientWithMocks( BitcoinWrapper bw, FederatorSupport fs, - Blockchain blockchain, TxSenderAddressType txSenderAddressType, ActivationConfig activationConfig) throws Exception { return createClientWithMocks( bw, fs, - blockchain, txSenderAddressType, activationConfig, 100 @@ -145,14 +132,13 @@ private BtcToRskClient createClientWithMocks( private BtcToRskClient createClientWithMocks( BitcoinWrapper bw, FederatorSupport fs, - Blockchain blockchain, TxSenderAddressType txSenderAddressType, ActivationConfig activationConfig, int amountOfHeadersToSend) throws Exception { BtcLockSenderProvider btcLockSenderProvider = mockBtcLockSenderProvider(txSenderAddressType); - BtcToRskClient client = btcToRskClientBuilder + return btcToRskClientBuilder .withActivationConfig(activationConfig) .withBitcoinWrapper(bw) .withFederatorSupport(fs) @@ -161,17 +147,14 @@ private BtcToRskClient createClientWithMocks( .withFederation(genesisFederation) .withAmountOfHeadersToSend(amountOfHeadersToSend) .build(); - - return client; } private BtcToRskClient createClientWithMocksCustomStorageFiles( BitcoinWrapper bw, FederatorSupport fs, - Blockchain blockchain, BtcToRskClientFileStorage btcToRskClientFileStorage) throws Exception { - BtcToRskClient client = btcToRskClientBuilder + return btcToRskClientBuilder .withActivationConfig(activationConfig) .withBitcoinWrapper(bw) .withFederatorSupport(fs) @@ -179,8 +162,6 @@ private BtcToRskClient createClientWithMocksCustomStorageFiles( .withBtcToRskClientFileStorage(btcToRskClientFileStorage) .withFederation(genesisFederation) .build(); - - return client; } private BtcToRskClient createClientWithMocks() throws Exception { @@ -205,13 +186,13 @@ public void addAndGetTwoTransactions() throws Exception { List proofs1 = txs.get(tx1.getWTxId()); Assert.assertNotNull(proofs1); - Assert.assertTrue(proofs1.isEmpty()); + assertTrue(proofs1.isEmpty()); List proofs2 = txs.get(tx2.getWTxId()); tx1.getWTxId(); Assert.assertNotNull(proofs2); - Assert.assertTrue(proofs2.isEmpty()); + assertTrue(proofs2.isEmpty()); } @Test @@ -229,7 +210,7 @@ public void addBlockWithProofOfTransaction() throws Exception { Assert.assertFalse(txs.isEmpty()); Assert.assertEquals(1, txs.size()); - Assert.assertTrue(txs.get(tx.getWTxId()).isEmpty()); + assertTrue(txs.get(tx.getWTxId()).isEmpty()); client.onBlock(block); @@ -420,7 +401,7 @@ public void onBlock_no_transactions() throws Exception { Map> txs = client.getTransactionsToSendToRsk(); - Assert.assertTrue(txs.isEmpty()); + assertTrue(txs.isEmpty()); } @Test @@ -440,7 +421,7 @@ public void onBlock_without_txs_waiting_for_proofs() throws Exception { BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.onBlock(block); @@ -484,8 +465,8 @@ public void onBlock_including_segwit_tx_registers_coinbase() throws Exception { client.onBlock(block); verify(btcToRskClientFileStorageMock, times(1)).write(any()); - Assert.assertTrue(btcToRskClientFileData.getTransactionProofs().get(segwitTx.getWTxId()).stream().anyMatch(b -> b.getBlockHash().equals(block.getHash()))); - Assert.assertTrue(btcToRskClientFileData.getCoinbaseInformationMap().containsKey(block.getHash())); + assertTrue(btcToRskClientFileData.getTransactionProofs().get(segwitTx.getWTxId()).stream().anyMatch(b -> b.getBlockHash().equals(block.getHash()))); + assertTrue(btcToRskClientFileData.getCoinbaseInformationMap().containsKey(block.getHash())); Assert.assertEquals(coinbaseTx, btcToRskClientFileData.getCoinbaseInformationMap().get(block.getHash()).getCoinbaseTransaction()); } @@ -508,13 +489,13 @@ public void onBlock_without_segwit_tx_doesnt_register_coinbase() throws Exceptio BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.onBlock(block); verify(btcToRskClientFileStorageMock, times(1)).write(any()); - Assert.assertTrue(btcToRskClientFileData.getTransactionProofs().get(tx.getWTxId()).stream().anyMatch(b -> b.getBlockHash().equals(block.getHash()))); - Assert.assertTrue(btcToRskClientFileData.getCoinbaseInformationMap().isEmpty()); + assertTrue(btcToRskClientFileData.getTransactionProofs().get(tx.getWTxId()).stream().anyMatch(b -> b.getBlockHash().equals(block.getHash()))); + assertTrue(btcToRskClientFileData.getCoinbaseInformationMap().isEmpty()); } @Test @@ -535,7 +516,7 @@ public void onBlock_coinbase_invalid_witness_reserved_value() throws Exception { BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.onBlock(block); @@ -562,7 +543,7 @@ public void onBlock_including_segwit_tx_coinbase_without_witness() throws Except BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.onBlock(block); @@ -589,7 +570,7 @@ public void onBlock_including_segwit_tx_coinbase_witness_commitment_doesnt_match BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.onBlock(block); @@ -602,7 +583,7 @@ public void onBlock_including_segwit_tx_coinbase_witness_commitment_doesnt_match public void when_markCoinbasesAsReadyToBeInformed_coinbaseInformationMap_isEmpty_return() throws Exception { BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, new BtcToRskClientFileData())); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.markCoinbasesAsReadyToBeInformed(new ArrayList<>()); @@ -617,7 +598,7 @@ public void when_markCoinbasesAsReadyToBeInformed_informedBlocks_isEmpty_return( BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); client.markCoinbasesAsReadyToBeInformed(new ArrayList<>()); @@ -632,7 +613,7 @@ public void when_markCoinbasesAsReadyToBeInformed_informedBlocks_notEmpty_writeT BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); Block block = mock(Block.class); when(block.getHash()).thenReturn(Sha256Hash.ZERO_HASH); @@ -649,7 +630,7 @@ public void updateBlockchainWithoutBlocks_preRskip89() throws Exception { BitcoinWrapper bw = new SimpleBitcoinWrapper(); SimpleFederatorSupport fh = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); Assert.assertEquals(0, numberOfBlocksSent); @@ -664,7 +645,7 @@ public void updateBlockchainWithBetterBlockchainInContract_preRskip89() throws E BitcoinWrapper bw = new SimpleBitcoinWrapper(); SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(10); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); Assert.assertEquals(0, numberOfBlocksSent); @@ -684,7 +665,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByOneBlock_preRskip89() SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(2); fh.setBtcBlockchainBlockLocator(createLocator(blocks, 2, 0)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); @@ -708,7 +689,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByTwoBlocks_preRskip89() SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(2); fh.setBtcBlockchainBlockLocator(createLocator(blocks, 2, 0)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); @@ -733,7 +714,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByThreeBlocks_preRskip89 SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(2); fh.setBtcBlockchainBlockLocator(createLocator(blocks, 1, 1)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); client.updateBridgeBtcBlockchain(); @@ -756,7 +737,7 @@ public void updateBlockchainWithBetterBlockchainInWalletBySixHundredBlocks_preRs StoredBlock[] blocks = createBlockchain(601); bw.setBlocks(blocks); SimpleFederatorSupport fh = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain(), 345); + BtcToRskClient client = createClientWithMocks(bw, fh, 345); fh.setBtcBestBlockChainHeight(1); fh.setBtcBlockchainBlockLocator(createLocator(blocks, 1, 0)); @@ -774,7 +755,7 @@ public void updateBlockchainWithBetterBlockchainInWalletBySixHundredBlocks_preRs public void updateBlockchainWithoutBlocks() throws Exception { BitcoinWrapper bw = new SimpleBitcoinWrapper(); SimpleFederatorSupport fh = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); Assert.assertEquals(0, numberOfBlocksSent); @@ -787,7 +768,7 @@ public void updateBlockchainWithBetterBlockchainInContract() throws Exception { BitcoinWrapper bw = new SimpleBitcoinWrapper(); SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(10); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); Assert.assertEquals(0, numberOfBlocksSent); @@ -805,7 +786,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByOneBlock() throws Exce SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(2); fh.setBlockHashes(createHashChain(blocks, 2)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); @@ -827,7 +808,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByTwoBlocks() throws Exc SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(2); fh.setBlockHashes(createHashChain(blocks, 2)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); int numberOfBlocksSent = client.updateBridgeBtcBlockchain(); @@ -850,7 +831,7 @@ public void updateBlockchainWithBetterBlockchainInWalletByThreeBlocks() throws E SimpleFederatorSupport fh = new SimpleFederatorSupport(); fh.setBtcBestBlockChainHeight(1); fh.setBlockHashes(createHashChain(blocks, 1)); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bw, fh); client.updateBridgeBtcBlockchain(); @@ -871,7 +852,7 @@ public void updateBlockchainWithBetterBlockchainInWalletBySixHundredBlocks() thr StoredBlock[] blocks = createBlockchain(601); bw.setBlocks(blocks); SimpleFederatorSupport fh = new SimpleFederatorSupport(); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain(), 345); + BtcToRskClient client = createClientWithMocks(bw, fh, 345); fh.setBtcBestBlockChainHeight(1); fh.setBlockHashes(createHashChain(blocks, 1)); @@ -889,7 +870,7 @@ public void updateBlockchainWithBetterBlockchainInWalletBySixHundredBlocks() thr public void updateBlockchainWithDeepFork() throws Exception { SimpleBitcoinWrapper bw = new SimpleBitcoinWrapper(); SimpleFederatorSupport fh = spy(new SimpleFederatorSupport()); - BtcToRskClient client = createClientWithMocks(bw, fh, mockBlockchain(), 215); + BtcToRskClient client = createClientWithMocks(bw, fh, 215); // Set the bridge's blockchain to start at height 10 and have a current height of 40 - 30 blocks of maximum // search depth @@ -937,7 +918,7 @@ public void updateNoTransaction() throws Exception { federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -957,7 +938,7 @@ public void updateTransactionWithoutProof() throws Exception { federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -970,7 +951,7 @@ public void updateTransactionInClientWithoutProofYet() throws Exception { SimpleBitcoinWrapper bitcoinWrapper = new SimpleBitcoinWrapper(); bitcoinWrapper.setTransactions(txs); - BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport, mockBlockchain()); + BtcToRskClient client = createClientWithMocks(bitcoinWrapper, federatorSupport); client.onTransaction(tx); @@ -980,7 +961,7 @@ public void updateTransactionInClientWithoutProofYet() throws Exception { federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1412,7 +1393,7 @@ public void updateTransactionInClientWithBlockProofInLosingFork() throws Excepti federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1450,7 +1431,7 @@ public void updateTransactionWithNoSenderValid() throws Exception { federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1587,7 +1568,6 @@ public void updateTransactionWithMultisig_before_rskip143() throws Exception { BtcToRskClient client = createClientWithMocks( bitcoinWrapper, federatorSupport, - mockBlockchain(), TxSenderAddressType.P2SHMULTISIG, activationsConfig ); @@ -1601,7 +1581,7 @@ public void updateTransactionWithMultisig_before_rskip143() throws Exception { federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1648,7 +1628,7 @@ public void updateTransaction_with_release_before_rskip143() throws Exception { releaseInput1.setScriptSig(inputScript); // Verify it was properly signed - assertThat(BridgeUtils.isPegOutTx(releaseTx1, Collections.singletonList(federation), activations), is(true)); + assertTrue(BridgeUtils.isPegOutTx(releaseTx1, Collections.singletonList(federation), activations)); Transaction releaseTx = ThinConverter.toOriginalInstance(bridgeConstants.getBtcParamsString(), releaseTx1); @@ -1724,7 +1704,6 @@ public void updateTransactionWithSegwitCompatible_before_rskip143() throws Excep BtcToRskClient client = createClientWithMocks( bitcoinWrapper, federatorSupport, - mockBlockchain(), TxSenderAddressType.P2SHP2WPKH, activationsConfig ); @@ -1738,7 +1717,7 @@ public void updateTransactionWithSegwitCompatible_before_rskip143() throws Excep federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1770,7 +1749,6 @@ public void updateTransactionWithSenderUnknown_before_rskip170() throws Exceptio BtcToRskClient client = createClientWithMocks( bitcoinWrapper, federatorSupport, - mockBlockchain(), TxSenderAddressType.UNKNOWN, activationsConfig ); @@ -1784,7 +1762,7 @@ public void updateTransactionWithSenderUnknown_before_rskip170() throws Exceptio federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -1883,7 +1861,7 @@ public void updateTransaction_peginInformationParsingFails_withoutSenderAddress( federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test @@ -2034,14 +2012,14 @@ public void ignoreTransactionInClientWithBlockProofNotInBlockchain() throws Exce federatorSupport.getTxsSentToRegisterBtcTransaction(); Assert.assertNotNull(txsSentToRegisterBtcTransaction); - Assert.assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); + assertTrue(txsSentToRegisterBtcTransaction.isEmpty()); } @Test(expected = Exception.class) public void restoreFileData_with_invalid_BtcToRskClient_file_data() throws Exception { BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); when(btcToRskClientFileStorageMock.read(any())).thenThrow(new IOException()); - createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); } @Test @@ -2055,10 +2033,10 @@ public void restoreFileData_getSuccess_true() throws Exception { BtcToRskClientFileReadResult result = spy(new BtcToRskClientFileReadResult(true, newData)); when(btcToRskClientFileStorageMock.read(any())).thenReturn(result); - createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); verify(result, times(1)).getData(); - Assert.assertTrue(newData.getTransactionProofs().containsKey(hash)); + assertTrue(newData.getTransactionProofs().containsKey(hash)); } @Test(expected = Exception.class) @@ -2066,7 +2044,7 @@ public void restoreFileData_getSuccess_false() throws Exception { BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class); BtcToRskClientFileReadResult result = new BtcToRskClientFileReadResult(false, new BtcToRskClientFileData()); when(btcToRskClientFileStorageMock.read(any())).thenReturn(result); - createClientWithMocksCustomStorageFiles(null, null, null, btcToRskClientFileStorageMock); + createClientWithMocksCustomStorageFiles(null, null, btcToRskClientFileStorageMock); } @Test @@ -2081,7 +2059,7 @@ public void updateBridgeBtcCoinbaseTransactions_when_empty_coinbase_map_does_not when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); FederatorSupport federatorSupport = mock(FederatorSupport.class); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, federatorSupport, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, federatorSupport, btcToRskClientFileStorageMock); client.updateBridgeBtcCoinbaseTransactions(); @@ -2107,7 +2085,7 @@ public void updateBridgeBtcCoinbaseTransactions_when_coinbase_map_does_not_have_ when(btcToRskClientFileStorageMock.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, btcToRskClientFileData)); FederatorSupport federatorSupport = mock(FederatorSupport.class); - BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, federatorSupport, null, btcToRskClientFileStorageMock); + BtcToRskClient client = createClientWithMocksCustomStorageFiles(null, federatorSupport, btcToRskClientFileStorageMock); client.updateBridgeBtcCoinbaseTransactions(); @@ -2496,7 +2474,16 @@ private StoredBlock[] createForkedBlockchain(StoredBlock[] currentBlocks, int fo Sha256Hash previousHash = blocks[forkHeight].getHeader().getHash(); for (int i = forkHeight+1; i <= newHeight; i++) { - Block header = new Block(networkParameters, 1, previousHash, createHash(), 1, 1, 1, new ArrayList()); + Block header = new Block( + networkParameters, + 1, + previousHash, + createHash(), + 1, + 1, + 1, + new ArrayList<>() + ); StoredBlock block = new StoredBlock(header, null, i); blocks[i] = block; previousHash = header.getHash(); @@ -2574,14 +2561,6 @@ private Transaction getTx(boolean hasWitness) { return tx; } - private Blockchain mockBlockchain() { - Blockchain blockchain = mock(Blockchain.class); - org.ethereum.core.Block block = mock(org.ethereum.core.Block.class); - when(block.getNumber()).thenReturn(1L); - when(blockchain.getBestBlock()).thenReturn(block); - return blockchain; - } - private void simulatePreRskip89() { when(activationConfig.isActive(eq(ConsensusRule.RSKIP89), anyLong())).thenReturn(false); }