Skip to content

Commit

Permalink
Add extra logs to BtcToRskClient
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-iov committed Dec 5, 2024
1 parent 091f840 commit 2758b09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
35 changes: 24 additions & 11 deletions src/main/java/co/rsk/federate/BtcToRskClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -249,18 +249,20 @@ 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<Proof> 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;
}

// Always use the wtxid for the lock transactions
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);
Expand All @@ -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;
}

Expand All @@ -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());
}
}
Expand All @@ -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);
}

Expand All @@ -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 {
Expand Down Expand Up @@ -436,12 +444,17 @@ protected void markCoinbasesAsReadyToBeInformed(List<Block> 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) {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/co/rsk/federate/BtcToRskClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2758b09

Please sign in to comment.