Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misleading pegout logs and variables #251

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ protected void coinsReceivedOrSent(Transaction tx) {
listener.onTransaction(tx);
}
if (PegUtilsLegacy.isPegOutTx(btcTx, Collections.singletonList(watchedFederation), federatorSupport.getConfigForBestBlock())) {
LOGGER.debug("[coinsReceivedOrSent] [btctx:{}] is a release", tx.getWTxId());
LOGGER.debug("[coinsReceivedOrSent] [btctx with hash {} and witness hash {}] is a pegout", tx.getTxId(), tx.getWTxId());
listener.onTransaction(tx);
}
}
Expand Down
167 changes: 88 additions & 79 deletions src/main/java/co/rsk/federate/btcreleaseclient/BtcReleaseClient.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BtcReleaseClientStorageAccessor(
throw new InvalidStorageFileException(message, e);
}
}
if (!readResult.getSuccess()) {
if (Boolean.FALSE.equals(readResult.getSuccess())) {
String message = "Error reading storage file for BtcReleaseClient";
logger.error(message);
throw new InvalidStorageFileException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import org.ethereum.util.RLP;
import org.ethereum.util.RLPElement;
import org.ethereum.util.RLPList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BtcReleaseClientFileStorageImpl implements BtcReleaseClientFileStorage {

private static final Logger logger = LoggerFactory.getLogger(BtcReleaseClientFileStorageImpl.class);
private final FileStorageInfo storageInfo;

public BtcReleaseClientFileStorageImpl(FileStorageInfo storageInfo) {
Expand Down Expand Up @@ -87,6 +90,7 @@ private BtcReleaseClientFileReadResult readFromRlp(byte[] fileData) {
}
}
} catch (Exception e) {
logger.error("[readFromRlp] error trying to read file data.", e);
return new BtcReleaseClientFileReadResult(Boolean.FALSE, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public class PowHSMSignerMessageBuilder extends SignerMessageBuilder {
public PowHSMSignerMessageBuilder(
ReceiptStore receiptStore,
ReleaseCreationInformation releaseCreationInformation) {
super(releaseCreationInformation.getBtcTransaction());
super(releaseCreationInformation.getPegoutBtcTx());

this.txReceipt = releaseCreationInformation.getTransactionReceipt();
this.rskBlock = releaseCreationInformation.getBlock();
this.rskBlock = releaseCreationInformation.getPegoutCreationBlock();
this.receiptStore = receiptStore;
this.envelopeCreated = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,73 @@
import org.ethereum.core.TransactionReceipt;

public class ReleaseCreationInformation {
private final Block block;
private final Block pegoutCreationBlock;
private final TransactionReceipt transactionReceipt;
private final Keccak256 releaseRskTxHash;
private final Keccak256 informingRskTxHash;
private final BtcTransaction btcTransaction;

private final Keccak256 pegoutCreationRskTxHash;
private final BtcTransaction pegoutBtcTx;
private final Keccak256 pegoutConfirmationRskTxHash;

/**
*
* @param block The rsk block where the BTC transaction was created
* @param transactionReceipt The rsk transaction receipt where the btc transaction was created
* @param releaseRskTxHash The rsk transaction hash where the release was requested
* @param btcTransaction The BTC transaction to sign
* @param pegoutCreationBlock The rsk block where pegout was created
* @param transactionReceipt The rsk transaction receipt where pegout was created
* @param pegoutCreationRskTxHash The rsk transaction hash where the pegout was created
* @param pegoutBtcTx The BTC transaction to sign
**/
public ReleaseCreationInformation(
Block block,
Block pegoutCreationBlock,
TransactionReceipt transactionReceipt,
Keccak256 releaseRskTxHash,
BtcTransaction btcTransaction
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx
) {
this(block, transactionReceipt, releaseRskTxHash, btcTransaction, releaseRskTxHash);
this(pegoutCreationBlock, transactionReceipt, pegoutCreationRskTxHash, pegoutBtcTx, pegoutCreationRskTxHash);
}

/**
*
* @param block The rsk block where the BTC transaction was created
* @param transactionReceipt The rsk transaction receipt where the btc transaction was created
* @param releaseRskTxHash The rsk transaction hash where the release was requested
* @param btcTransaction The BTC transaction to sign
* @param informingRskTxHash The rsk transaction hash where the release was confirmed to be signed
* @param pegoutCreationBlock The rsk block where the pegout was created
* @param transactionReceipt The rsk transaction receipt where the pegout was created
* @param pegoutCreationRskTxHash The rsk transaction hash where the pegout was created
* @param pegoutBtcTx The BTC transaction to sign
* @param pegoutConfirmationRskTxHash The rsk transaction hash where the pegout was confirmed to be signed
**/
public ReleaseCreationInformation(
Block block,
Block pegoutCreationBlock,
TransactionReceipt transactionReceipt,
Keccak256 releaseRskTxHash,
BtcTransaction btcTransaction,
Keccak256 informingRskTxHash
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx,
Keccak256 pegoutConfirmationRskTxHash
) {
this.block = block;
this.pegoutCreationBlock = pegoutCreationBlock;
this.transactionReceipt = transactionReceipt;
this.releaseRskTxHash = releaseRskTxHash;
this.btcTransaction = btcTransaction;
this.informingRskTxHash = informingRskTxHash;
this.pegoutCreationRskTxHash = pegoutCreationRskTxHash;
this.pegoutBtcTx = pegoutBtcTx;
this.pegoutConfirmationRskTxHash = pegoutConfirmationRskTxHash;
}

public Block getBlock() {
return block;
public Block getPegoutCreationBlock() {
return pegoutCreationBlock;
}

/**
* gets the receipt of the rsk transaction that created the release BTC transaction
* gets the receipt of the rsk transaction that created the pegout BTC transaction
**/
public TransactionReceipt getTransactionReceipt() {
return transactionReceipt;
}

/**
* gets the hash of the rsk transaction that originated the release
* gets the hash of the rsk transaction that originated the pegout
**/
public Keccak256 getReleaseRskTxHash() {
return releaseRskTxHash;
public Keccak256 getPegoutCreationRskTxHash() {
return pegoutCreationRskTxHash;
}

public Keccak256 getInformingRskTxHash() {
return informingRskTxHash;
public Keccak256 getPegoutConfirmationRskTxHash() {
return pegoutConfirmationRskTxHash;
}

public BtcTransaction getBtcTransaction() {
return btcTransaction;
public BtcTransaction getPegoutBtcTx() {
return pegoutBtcTx;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
import co.rsk.bitcoinj.core.BtcTransaction;
import co.rsk.crypto.Keccak256;
import co.rsk.peg.BridgeEvents;
import org.ethereum.core.Block;
import org.ethereum.core.CallTransaction;
import org.ethereum.core.Transaction;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.db.BlockStore;
import org.ethereum.db.ReceiptStore;
import org.ethereum.db.TransactionInfo;
import org.ethereum.core.*;
import org.ethereum.db.*;
import org.ethereum.vm.LogInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -42,37 +37,37 @@ public ReleaseCreationInformationGetter(
/* Use this method if the originating rsk tx hash and the informing rsk tx hash match */
public ReleaseCreationInformation getTxInfoToSign(
int version,
Keccak256 rskTxHash,
BtcTransaction btcTransaction
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx
) throws HSMReleaseCreationInformationException {
return getTxInfoToSign(version, rskTxHash, btcTransaction, rskTxHash);
return getTxInfoToSign(version, pegoutCreationRskTxHash, pegoutBtcTx, pegoutCreationRskTxHash);
}

public ReleaseCreationInformation getTxInfoToSign(
int version,
Keccak256 rskTxHash,
BtcTransaction btcTransaction,
Keccak256 informingRskTxHash
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx,
Keccak256 pegoutConfirmationRskTxHash
) throws HSMReleaseCreationInformationException {
if (version == 1) {
return getBaseReleaseCreationInformation(rskTxHash, btcTransaction, informingRskTxHash);
return getBaseReleaseCreationInformation(pegoutCreationRskTxHash, pegoutBtcTx, pegoutConfirmationRskTxHash);
} else if (version >= 2) {
return getTxInfoToSignVersion2(rskTxHash, btcTransaction, informingRskTxHash);
return getTxInfoToSignVersion2(pegoutCreationRskTxHash, pegoutBtcTx, pegoutConfirmationRskTxHash);
} else {
throw new HSMReleaseCreationInformationException("Unsupported version " + version);
}
}

protected ReleaseCreationInformation getBaseReleaseCreationInformation(
Keccak256 rskTxHash,
BtcTransaction btcTransaction,
Keccak256 informingRskTxHash
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx,
Keccak256 pegoutConfirmationRskTxHash
) throws HSMReleaseCreationInformationException {
TransactionInfo transactionInfo = receiptStore.getInMainChain(rskTxHash.getBytes(), blockStore).orElse(null);
TransactionInfo transactionInfo = receiptStore.getInMainChain(pegoutCreationRskTxHash.getBytes(), blockStore).orElse(null);
if (transactionInfo == null) {
String message = String.format(
"Transaction hash %s could not be found in best chain",
rskTxHash
"Rsk transaction %s where the pegout was created could not be found in best chain",
pegoutCreationRskTxHash
);
logger.error("[getTxInfoToSign] {}", message);
throw new HSMReleaseCreationInformationException(message);
Expand All @@ -83,33 +78,33 @@ protected ReleaseCreationInformation getBaseReleaseCreationInformation(
return new ReleaseCreationInformation(
block,
transactionReceipt,
rskTxHash,
btcTransaction,
informingRskTxHash
pegoutCreationRskTxHash,
pegoutBtcTx,
pegoutConfirmationRskTxHash
);
}

protected ReleaseCreationInformation getTxInfoToSignVersion2(
Keccak256 rskTxHash,
BtcTransaction btcTransaction,
Keccak256 informingRskTxHash
Keccak256 pegoutCreationRskTxHash,
BtcTransaction pegoutBtcTx,
Keccak256 pegoutConfirmationRskTxHash
) throws HSMReleaseCreationInformationException {
try {
ReleaseCreationInformation baseReleaseCreationInformation =
getBaseReleaseCreationInformation(rskTxHash, btcTransaction, informingRskTxHash);
Block block = baseReleaseCreationInformation.getBlock();
getBaseReleaseCreationInformation(pegoutCreationRskTxHash, pegoutBtcTx, pegoutConfirmationRskTxHash);
Block block = baseReleaseCreationInformation.getPegoutCreationBlock();
TransactionReceipt transactionReceipt = baseReleaseCreationInformation.getTransactionReceipt();

// Get transaction from the block, searching by tx hash, and set it in the tx receipt
logger.trace("[getTxInfoToSign] Searching for transaction {} in block {} ({})", rskTxHash, block.getHash(), block.getNumber());
logger.trace("[getTxInfoToSign] Searching for rsk transaction {} in block {} ({})", pegoutCreationRskTxHash, block.getHash(), block.getNumber());
List<Transaction> transactions = block.getTransactionsList().stream()
.filter(t -> t.getHash().equals(rskTxHash))
.filter(t -> t.getHash().equals(pegoutCreationRskTxHash))
.collect(Collectors.toList());
logger.trace("[getTxInfoToSign] Transactions found {}", transactions.size());
if(transactions.size() != 1) {
String message = String.format(
"Transaction hash %s could not be found in block %s or more than 1 result obtained. Filter size: %d",
rskTxHash,
"Rsk transaction %s could not be found in block %s or more than 1 result obtained. Filter size: %d",
pegoutCreationRskTxHash,
block.getHash().toHexString(),
transactions.size()
);
Expand All @@ -119,24 +114,24 @@ protected ReleaseCreationInformation getTxInfoToSignVersion2(
Transaction transaction = transactions.get(0);
transactionReceipt.setTransaction(transaction);

return searchEventInFollowingBlocks(block.getNumber(), btcTransaction, rskTxHash, informingRskTxHash);
return searchEventInFollowingBlocks(block.getNumber(), pegoutBtcTx, pegoutCreationRskTxHash, pegoutConfirmationRskTxHash);
} catch (Exception e) {
throw new HSMReleaseCreationInformationException("Unhandled exception occured", e);
throw new HSMReleaseCreationInformationException("Unhandled exception occurred", e);
}
}

private ReleaseCreationInformation searchEventInFollowingBlocks(
long blockNumber,
BtcTransaction btcTransaction,
Keccak256 rskTxHash,
Keccak256 informingRskTxHash
BtcTransaction pegoutBtcTx,
Keccak256 pegoutCreationRskTxHash,
Keccak256 pegoutConfirmationRskTxHash
) throws HSMReleaseCreationInformationException {
Block block = blockStore.getChainBlockByNumber(blockNumber);
// If the block cannot be found by its number, the event cannot be
// searched further.
if (block == null) {
throw new HSMReleaseCreationInformationException(
String.format("[searchEventInFollowingBlocks] Block not found. Rsk Transaction hash: [%s]", rskTxHash)
String.format("[searchEventInFollowingBlocks] Block not found. Rsk Transaction hash: [%s]", pegoutCreationRskTxHash)
);
}

Expand All @@ -153,8 +148,13 @@ private ReleaseCreationInformation searchEventInFollowingBlocks(

rskTxReceipt.setTransaction(rskTx);

Optional<ReleaseCreationInformation> releaseCreationInformation =
getInformationFromEvent(block, rskTxReceipt, btcTransaction, rskTxHash, informingRskTxHash);
Optional<ReleaseCreationInformation> releaseCreationInformation = getInformationFromEvent(
block,
rskTxReceipt,
pegoutBtcTx,
pegoutCreationRskTxHash,
pegoutConfirmationRskTxHash
);
if (releaseCreationInformation.isPresent()) {
return releaseCreationInformation.get();
}
Expand All @@ -164,24 +164,24 @@ private ReleaseCreationInformation searchEventInFollowingBlocks(
// then the event does not exist.
if (block.getNumber() == blockStore.getBestBlock().getNumber()) {
throw new HSMReleaseCreationInformationException(
String.format("[searchEventInFollowingBlocks] Event not found. Rsk Transaction hash: [%s]", rskTxHash)
String.format("[searchEventInFollowingBlocks] Event not found. Rsk transaction: [%s]", pegoutCreationRskTxHash)
);
}
// If the event was not found in this block, the next block is
// requested and the same search is performed.
return searchEventInFollowingBlocks(blockNumber + 1, btcTransaction, rskTxHash, informingRskTxHash);
return searchEventInFollowingBlocks(blockNumber + 1, pegoutBtcTx, pegoutCreationRskTxHash, pegoutConfirmationRskTxHash);
}

private Optional<ReleaseCreationInformation> getInformationFromEvent(
Block block,
TransactionReceipt transactionReceipt,
BtcTransaction btcTransaction,
Keccak256 releaseRskTxHash,
Keccak256 informingRskTxHash
BtcTransaction pegoutBtcTx,
Keccak256 pegoutCreationRskTxHash,
Keccak256 pegoutConfirmationRskTxHash
) {
boolean hasLogs = !transactionReceipt.getLogInfoList().isEmpty();
logger.trace(
"[getInformationFromEvent] tx ({}) in block ({} - {}). has logs? {}",
"[getInformationFromEvent] Rsk Transaction ({}) in block ({} - {}). has logs? {}",
transactionReceipt.getTransaction().getHash(),
block.getNumber(),
block.getHash(),
Expand All @@ -192,7 +192,7 @@ private Optional<ReleaseCreationInformation> getInformationFromEvent(
for (LogInfo logInfo : logs) {
// You should check that the event is Release and contains the hash of the transaction.
boolean hasReleaseRequestEvent = Arrays.equals(logInfo.getTopics().get(0).getData(), releaseRequestedSignatureTopic);
if (hasReleaseRequestEvent && (Arrays.equals(logInfo.getTopics().get(2).getData(), btcTransaction.getHash().getBytes()))) {
if (hasReleaseRequestEvent && (Arrays.equals(logInfo.getTopics().get(2).getData(), pegoutBtcTx.getHash().getBytes()))) {
logger.debug(
"[getInformationFromEvent] Found transaction {} and block {}",
transactionReceipt.getTransaction().getHash(),
Expand All @@ -202,9 +202,9 @@ private Optional<ReleaseCreationInformation> getInformationFromEvent(
new ReleaseCreationInformation(
block,
transactionReceipt,
releaseRskTxHash,
btcTransaction,
informingRskTxHash
pegoutCreationRskTxHash,
pegoutBtcTx,
pegoutConfirmationRskTxHash
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public SignerMessageBuilderFactory(ReceiptStore receiptStore) {

public SignerMessageBuilder buildFromConfig(
int version,
ReleaseCreationInformation releaseCreationInformation
ReleaseCreationInformation pegoutCreationInformation
) throws HSMUnsupportedVersionException {
SignerMessageBuilder messageBuilder;
if (version == 1) {
messageBuilder = new SignerMessageBuilderV1(releaseCreationInformation.getBtcTransaction());
messageBuilder = new SignerMessageBuilderV1(pegoutCreationInformation.getPegoutBtcTx());
} else if (version >= 2) {
messageBuilder = new PowHSMSignerMessageBuilder(receiptStore, releaseCreationInformation);
messageBuilder = new PowHSMSignerMessageBuilder(receiptStore, pegoutCreationInformation);
} else {
String message = String.format("Unsupported HSM signer version: %d", version);
logger.debug("[buildFromConfig] {}", message);
Expand Down
Loading