Skip to content
Draft
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
7 changes: 2 additions & 5 deletions src/main/java/co/rsk/federate/FedNodeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import co.rsk.federate.signing.hsm.config.PowHSMConfig;
import co.rsk.federate.signing.hsm.message.ReleaseCreationInformationGetter;
import co.rsk.federate.signing.hsm.message.SignerMessageBuilderFactory;
import co.rsk.federate.signing.hsm.requirements.AncestorBlockUpdater;
import co.rsk.federate.signing.hsm.requirements.ReleaseRequirementsEnforcer;
import co.rsk.federate.watcher.FederationWatcher;
import co.rsk.federate.watcher.FederationWatcherListener;
Expand Down Expand Up @@ -353,10 +352,8 @@ private void startFederate() throws Exception {
fedNodeContext.getBlockStore()
),
new ReleaseRequirementsEnforcer(
new AncestorBlockUpdater(
fedNodeContext.getBlockStore(),
hsmBookkeepingClient
)
fedNodeContext.getBlockStore(),
hsmBookkeepingClient
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import co.rsk.federate.signing.SegwitSigHashCalculatorImpl;
import co.rsk.federate.signing.SigHashCalculator;
import co.rsk.federate.signing.hsm.HSMClientException;
import co.rsk.federate.signing.hsm.HSMUnsupportedVersionException;
import co.rsk.federate.signing.hsm.HSMVersion;
import co.rsk.federate.signing.hsm.SignerException;
import co.rsk.federate.signing.hsm.message.HSMReleaseCreationInformationException;
import co.rsk.federate.signing.hsm.message.ReleaseCreationInformation;
Expand Down Expand Up @@ -98,6 +100,7 @@ public class BtcReleaseClient {

private PeerGroup peerGroup;
private ECDSASigner signer;
private HSMVersion signerVersion;
private BtcReleaseEthereumListener blockListener;
private SignerMessageBuilderFactory signerMessageBuilderFactory;
private ReleaseCreationInformationGetter releaseCreationInformationGetter;
Expand Down Expand Up @@ -128,6 +131,14 @@ public void setup(
) throws BtcReleaseClientException {
this.signer = signer;
logger.debug("[setup] Signer: {}", signer.getClass());
try {
int version = signer.getVersionForKeyId(BTC.getKeyId());
this.signerVersion = HSMVersion.fromNumber(version);
logger.info("[setup] Signer version: {}", signerVersion.getNumber());
} catch (SignerException | HSMUnsupportedVersionException e) {
logger.error("[setup] Wrong signer version", e);
throw new BtcReleaseClientException("Error configuring signer", e);
}
Comment thread
Copilot marked this conversation as resolved.

org.bitcoinj.core.Context btcContext = new org.bitcoinj.core.Context(
ThinConverter.toOriginalInstance(bridgeConstants.getBtcParamsString()));
Expand Down Expand Up @@ -201,7 +212,7 @@ public void tearDown() {
private class BtcReleaseEthereumListener extends EthereumListenerAdapter {
@Override
public void onBestBlock(org.ethereum.core.Block block, List<TransactionReceipt> receipts) {
if (!shouldProcessPegouts()) {
if (shouldSkipProcessingPegouts()) {
logger.warn("[onBestBlock] Node is not ready to process pegouts");
return;
}
Expand All @@ -222,7 +233,7 @@ public void onBestBlock(org.ethereum.core.Block block, List<TransactionReceipt>

@Override
public void onBlock(org.ethereum.core.Block block, List<TransactionReceipt> receipts) {
if (!shouldProcessPegouts()) {
if (shouldSkipProcessingPegouts()) {
logger.warn("[onBlock] Node is not ready to process pegouts");
return;
}
Expand All @@ -240,15 +251,15 @@ public void onBlock(org.ethereum.core.Block block, List<TransactionReceipt> rece
pegoutTxs.forEach(BtcReleaseClient.this::onBtcRelease);
}

private boolean shouldProcessPegouts() {
private boolean shouldSkipProcessingPegouts() {
boolean hasBetterBlockToSync = nodeBlockProcessor.hasBetterBlockToSync();
logger.trace(
"[shouldProcessPegouts] isPegoutEnabled: {}, hasBetterBlockToSync: {}",
"[shouldSkipProcessingPegouts] isPegoutEnabled: {}, hasBetterBlockToSync: {}",
isPegoutEnabled,
hasBetterBlockToSync
);

return isPegoutEnabled && !hasBetterBlockToSync;
return !isPegoutEnabled || hasBetterBlockToSync;
Comment thread
julia-zack marked this conversation as resolved.
}

/**
Expand Down Expand Up @@ -305,7 +316,6 @@ private BtcTransaction convertToBtcTxFromSolidityData(byte[] dataFromBtcReleaseT
protected void processReleases(Set<Map.Entry<Keccak256, BtcTransaction>> pegouts) {
try {
logger.info("[processReleases] Starting signing process with {} pegouts", pegouts.size());
int version = signer.getVersionForKeyId(BTC.getKeyId());
// Get pegout information and store it in a new list
List<ReleaseCreationInformation> pegoutsReadyToSign = new ArrayList<>();
for (Map.Entry<Keccak256, BtcTransaction> pegout : pegouts) {
Expand All @@ -325,7 +335,7 @@ protected void processReleases(Set<Map.Entry<Keccak256, BtcTransaction>> pegouts
pegoutsReadyToSign.sort((a, b) -> (int) (b.getPegoutCreationBlock().getNumber() - a.getPegoutCreationBlock().getNumber()));
// Sign only the first element
if (!pegoutsReadyToSign.isEmpty()) {
signRelease(version, pegoutsReadyToSign.get(0));
signRelease(signerVersion, pegoutsReadyToSign.get(0));
}
} catch (Exception e) {
logger.error("[processReleases] There was an error trying to process pegouts", e);
Expand Down Expand Up @@ -478,7 +488,9 @@ protected void validateTxIsNotAlreadySigned(
}
}

protected void signRelease(int signerVersion, ReleaseCreationInformation pegoutCreationInformation) {
private void signRelease(HSMVersion signerVersion, ReleaseCreationInformation pegoutCreationInformation) {
co.rsk.bitcoinj.core.Context.propagate(new co.rsk.bitcoinj.core.Context(bridgeConstants.getBtcParams()));

Keccak256 pegoutCreationRskTxHash = pegoutCreationInformation.getPegoutCreationRskTxHash();
BtcTransaction pegoutBtcTx = pegoutCreationInformation.getPegoutBtcTx();
logger.debug(
Expand All @@ -490,12 +502,11 @@ protected void signRelease(int signerVersion, ReleaseCreationInformation pegoutC
try {
logger.trace("[signRelease] Enforce signer requirements");
releaseRequirementsEnforcer.enforce(signerVersion, pegoutCreationInformation);
co.rsk.bitcoinj.core.Context.propagate(new co.rsk.bitcoinj.core.Context(bridgeConstants.getBtcParams()));
List<byte[]> signatures = new ArrayList<>();
int inputsSize = pegoutBtcTx.getInputs().size();
for (int inputIndex = 0; inputIndex < inputsSize; inputIndex++) {
SignerMessageBuilder messageBuilder = signerMessageBuilderFactory.buildFromConfig(
signerVersion,
signerVersion.getNumber(),
pegoutCreationInformation,
inputIndex
);
Expand All @@ -515,8 +526,10 @@ protected void signRelease(int signerVersion, ReleaseCreationInformation pegoutC
} catch (SignerException e) {
String message = String.format("Error signing pegout created in rsk transaction %s", pegoutCreationRskTxHash);
logger.error(message, e);
} catch (HSMClientException | SignerMessageBuilderException | ReleaseRequirementsEnforcerException e) {
logger.error("[signRelease] {}", e.getMessage());
} catch (ReleaseRequirementsEnforcerException e) {
logger.info("[signRelease] {}", e.getMessage(), e);
} catch (HSMClientException | SignerMessageBuilderException e) {
logger.error("[signRelease] {}", e.getMessage(), e);
} catch (Exception e) {
Comment thread
Copilot marked this conversation as resolved.
String message = String.format(
"[signRelease] There was an error trying to sign pegout created in rsk tx: %s and btc transaction: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
*/
public abstract class HSMClientException extends Exception {

public HSMClientException(String message) {
protected HSMClientException(String message) {
super(message);
}

public HSMClientException(String message, Throwable reason) {
protected HSMClientException(String message, Throwable reason) {
super(message, reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void start() {
}

try {
if (hsmBookkeepingClient.getHSMPointer().isInProgress()) {
if (hsmBookkeepingClient.getPowHSMState().isInProgress()) {
logger.debug("[start] HSM status is in progress, resetting HSM to fix it");
hsmBookkeepingClient.resetAdvanceBlockchain();
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public boolean isStarted() {
}

private Block getHsmBestBlock() throws HSMClientException {
Keccak256 bestBlockHSMHash = hsmBookkeepingClient.getHSMPointer().getBestBlockHash();
Keccak256 bestBlockHSMHash = hsmBookkeepingClient.getPowHSMState().getBestBlockHash();
Block block = blockStore.getBlockByHash(bestBlockHSMHash.getBytes());
if (block == null) {
String message = "HSM best block hash doesn't exist in blockStore: " + bestBlockHSMHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void validateHSMStateAndBlockHeaders(List<String> blockHeaders, String m
));
}
// If HSM has an advanceBlockchain or updateAncestorBlock in progress, then it can't be called.
if (getHSMPointer().isInProgress()) {
if (getPowHSMState().isInProgress()) {
throw new HSMBlockchainBookkeepingRelatedException(String.format(
"[%s] HSM is already updating its state. Not going to proceed with this request.",
methodName
Expand Down Expand Up @@ -187,7 +187,7 @@ public void advanceBlockchain(List<Block> blocks) throws HSMClientException {
}

@Override
public PowHSMState getHSMPointer() throws HSMClientException {
public PowHSMState getPowHSMState() throws HSMClientException {
ObjectNode command = this.hsmClientProtocol.buildCommand(BLOCKCHAIN_STATE.getCommand(), hsmVersion);
JsonNode response = this.hsmClientProtocol.send(command);

Comment thread
julia-zack marked this conversation as resolved.
Expand All @@ -204,7 +204,7 @@ public PowHSMState getHSMPointer() throws HSMClientException {
String bestBlockHash = state.get(BEST_BLOCK.getFieldName()).asText();
String ancestorBlockHash = state.get(ANCESTOR_BLOCK.getFieldName()).asText();

logger.trace("[getHSMPointer] HSM State: BestBlock: {}, ancestor: {}, inProgress:{}", bestBlockHash, ancestorBlockHash, inProgress);
logger.trace("[getPowHSMState] HSM State: BestBlock: {}, ancestor: {}, inProgress:{}", bestBlockHash, ancestorBlockHash, inProgress);

return new PowHSMState(bestBlockHash, ancestorBlockHash, inProgress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface HSMBookkeepingClient {

void advanceBlockchain(List<Block> blocks) throws HSMClientException;

PowHSMState getHSMPointer() throws HSMClientException;
PowHSMState getPowHSMState() throws HSMClientException;

void resetAdvanceBlockchain() throws HSMClientException;

Expand Down

This file was deleted.

Loading
Loading