diff --git a/src/main/java/co/rsk/federate/BtcToRskClient.java b/src/main/java/co/rsk/federate/BtcToRskClient.java index 22f78250c..abcd42817 100644 --- a/src/main/java/co/rsk/federate/BtcToRskClient.java +++ b/src/main/java/co/rsk/federate/BtcToRskClient.java @@ -226,13 +226,12 @@ public void updateBridge() { panicProcessor.panic("btclock", e.getMessage()); } } - } @Override public void onBlock(Block block) { synchronized (this) { - logger.debug("onBlock {}", block.getHash()); + logger.debug("[onBlock] {}", block.getHash()); PartialMerkleTree tree; Transaction coinbase = null; boolean dataToWrite = false; @@ -241,6 +240,7 @@ public void onBlock(Block block) { if (tx.isCoinBase()) { // safe keep the coinbase and move on + logger.debug("[onBlock] Transaction {} is the coinbase", tx.getTxId()); coinbase = tx; continue; } @@ -249,11 +249,12 @@ public void onBlock(Block block) { // this tx is not important move on continue; } + logger.debug("[onBlock] Found transaction {} that needs to be registered in the Bridge", tx.getTxId()); List proofs = fileData.getTransactionProofs().get(tx.getWTxId()); boolean blockInProofs = proofs.stream().anyMatch(p -> p.getBlockHash().equals(block.getHash())); if (blockInProofs) { - logger.info("Proof for tx {} in block {} already stored", tx, block.getHash()); + logger.info("[onBlock] Proof for tx {} in block {} already stored", tx.getTxId(), block.getHash()); continue; } @@ -261,6 +262,7 @@ public void onBlock(Block block) { tree = generatePMT(block, tx, tx.hasWitnesses()); // If the transaction has a witness, then we need to store the coinbase information to inform it if (tx.hasWitnesses() && !coinbaseRegistered) { + logger.debug("[onBlock] Transaction {} has witness, will need to store the coinbase information to inform it", tx.getTxId()); // We don't want to generate the PMT with the wtxid for the coinbase // as it doesn't have a corresponding hash in the witness root PartialMerkleTree coinbasePmt = generatePMT(block, coinbase, false); @@ -279,18 +281,19 @@ public void onBlock(Block block) { coinbaseInformation.getBlockHash(), coinbaseInformation ); + logger.debug("[onBlock] Coinbase information for transaction {} successully stored", tx.getTxId()); // Register the coinbase just once per block coinbaseRegistered = true; } catch (Exception e) { - logger.error(e.getMessage(), e); + logger.error("[onBlock] {}", e.getMessage()); // Without a coinbase related to this transaction the Bridge would reject the transaction return; } } proofs.add(new Proof(block.getHash(), tree)); - logger.info("New proof for tx {} in block {}", tx, block.getHash()); + logger.info("[onBlock] New proof for tx {} in block {}", tx.getTxId(), block.getHash()); dataToWrite = true; } @@ -300,9 +303,9 @@ public void onBlock(Block block) { try { this.btcToRskClientFileStorage.write(fileData); - logger.info("Stored proofs for block {}", block.getHash()); + logger.info("[onBlock] Stored proofs for block {}", block.getHash()); } catch (IOException e) { - logger.error(e.getMessage(), e); + logger.error("[onBlock] {}", e.getMessage()); panicProcessor.panic("btclock", e.getMessage()); } } @@ -322,7 +325,7 @@ private void validateCoinbaseInformation(CoinbaseInformation coinbaseInformation coinbaseInformation.getBlockHash(), coinbaseTransaction.getHash() ); - logger.error("[coinbaseInformationIsValid] {}", message); + logger.error("[validateCoinbaseInformation] {}", message); throw new IllegalArgumentException(message); } @@ -340,14 +343,19 @@ private void validateCoinbaseInformation(CoinbaseInformation coinbaseInformation coinbaseInformation.getBlockHash(), coinbaseTransaction.getHash() ); - logger.error("[coinbaseInformationIsValid] {}", message); + logger.error("[validateCoinbaseInformation] {}", message); return new IllegalArgumentException(message); }); + logger.debug( + "[validateCoinbaseInformation] Block {} with segwit peg-in tx {} has a valid witness merkle root", + coinbaseInformation.getBlockHash(), + coinbaseTransaction.getHash() + ); } @Override public void onTransaction(Transaction tx) { - logger.debug("onTransaction {}", tx.getWTxId()); + logger.debug("[onTransaction] {} (wtxid:{})", tx.getTxId(), tx.getWTxId()); synchronized (this) { this.fileData.getTransactionProofs().put(tx.getWTxId(), new ArrayList<>()); try { @@ -436,12 +444,17 @@ protected void markCoinbasesAsReadyToBeInformed(List informedBlocks) thro return; } boolean modified = false; - for (Block informedBlock:informedBlocks) { + for (Block informedBlock : informedBlocks) { if (coinbaseInformationMap.containsKey(informedBlock.getHash())) { CoinbaseInformation coinbaseInformation = coinbaseInformationMap.get(informedBlock.getHash()); coinbaseInformation.setReadyToInform(true); this.fileData.getCoinbaseInformationMap().put(informedBlock.getHash(), coinbaseInformation); modified = true; + logger.debug( + "[markCoinbasesAsReadyToBeInformed] Marked coinbase {} for block {} as ready to be informed", + coinbaseInformation.getCoinbaseTransaction().getTxId(), + informedBlock.getHash() + ); } } if (!modified) { diff --git a/src/test/java/co/rsk/federate/BtcToRskClientTest.java b/src/test/java/co/rsk/federate/BtcToRskClientTest.java index d36a27282..8bd245f68 100644 --- a/src/test/java/co/rsk/federate/BtcToRskClientTest.java +++ b/src/test/java/co/rsk/federate/BtcToRskClientTest.java @@ -66,7 +66,7 @@ void setup() throws PeginInstructionsException, IOException { when(activationConfig.forBlock(anyLong())).thenReturn(mock(ActivationConfig.ForBlock.class)); when(activationConfig.isActive(eq(ConsensusRule.RSKIP89), anyLong())).thenReturn(true); - bridgeRegTestConstants = BridgeRegTestConstants.getInstance(); + bridgeRegTestConstants = new BridgeRegTestConstants(); networkParameters = ThinConverter.toOriginalInstance(bridgeRegTestConstants.getBtcParamsString()); federationPrivateKeys = TestUtils.getFederationPrivateKeys(9); activeFederation = TestUtils.createFederation(bridgeRegTestConstants.getBtcParams(), federationPrivateKeys); @@ -657,6 +657,7 @@ void when_markCoinbasesAsReadyToBeInformed_informedBlocks_isEmpty_return() throw void when_markCoinbasesAsReadyToBeInformed_informedBlocks_notEmpty_writeToStorage() throws Exception { BtcToRskClientFileData btcToRskClientFileData = new BtcToRskClientFileData(); CoinbaseInformation coinbaseInformation = mock(CoinbaseInformation.class); + when(coinbaseInformation.getCoinbaseTransaction()).thenReturn(mock(Transaction.class)); btcToRskClientFileData.getCoinbaseInformationMap().put(Sha256Hash.ZERO_HASH, coinbaseInformation); BtcToRskClientFileStorage btcToRskClientFileStorageMock = mock(BtcToRskClientFileStorage.class);