Skip to content

Commit

Permalink
Merge pull request #281 from rsksmart/new-configs-poc
Browse files Browse the repository at this point in the history
Adds new configs PoC
  • Loading branch information
marcos-iov authored Jul 3, 2024
2 parents 5c82b8e + 806cae4 commit 485f911
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 81 deletions.
98 changes: 57 additions & 41 deletions src/main/java/co/rsk/federate/BtcToRskClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import co.rsk.bitcoinj.core.BtcTransaction;
import co.rsk.federate.config.FedNodeSystemProperties;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.federate.adapter.ThinConverter;
import co.rsk.federate.bitcoin.BitcoinWrapper;
Expand Down Expand Up @@ -77,23 +78,24 @@ public class BtcToRskClient implements BlockListener, TransactionListener {
ScheduledExecutorService updateBridgeTimer; // Timer that updates the bridge periodically
private int amountOfHeadersToSend; // Set amount of headers to inform in a single call
private BtcToRskClientFileData fileData = new BtcToRskClientFileData();
private boolean shouldUpdateBridgeBtcBlockchain;
private boolean shouldUpdateBridgeBtcCoinbaseTransactions;
private boolean shouldUpdateBridgeBtcTransactions;
private boolean shouldUpdateCollections;

public BtcToRskClient() {}

/// This constructor should only be used by tests.
protected BtcToRskClient(
ActivationConfig activationConfig,
BitcoinWrapper bitcoinWrapper,
FederatorSupport federatorSupport,
BridgeConstants bridgeConstants,
BtcToRskClientFileStorage btcToRskClientFileStorage,
BtcLockSenderProvider btcLockSenderProvider,
PeginInstructionsProvider peginInstructionsProvider,
Federation federation,
boolean isUpdateBridgeTimerEnabled,
int amountOfHeadersToSend
FedNodeSystemProperties config
) throws Exception {
this.activationConfig = activationConfig;
this.bitcoinWrapper = bitcoinWrapper;
this.federatorSupport = federatorSupport;
this.bridgeConstants = bridgeConstants;
Expand All @@ -102,31 +104,25 @@ protected BtcToRskClient(
this.btcLockSenderProvider = btcLockSenderProvider;
this.peginInstructionsProvider = peginInstructionsProvider;
this.federation = federation;
this.isUpdateBridgeTimerEnabled = isUpdateBridgeTimerEnabled;
this.amountOfHeadersToSend = amountOfHeadersToSend;
setConfigVariables(config);
}

public synchronized void setup(
ActivationConfig activationConfig,
BitcoinWrapper bitcoinWrapper,
BridgeConstants bridgeConstants,
BtcToRskClientFileStorage btcToRskClientFileStorage,
BtcLockSenderProvider btcLockSenderProvider,
PeginInstructionsProvider peginInstructionsProvider,
boolean isUpdateBridgeTimerEnabled,
int amountOfHeadersToSend
FedNodeSystemProperties config
) throws Exception {
this.activationConfig = activationConfig;
this.bridgeConstants = bridgeConstants;
this.btcToRskClientFileStorage = btcToRskClientFileStorage;
this.restoreFileData();
this.bitcoinWrapper = bitcoinWrapper;
this.btcLockSenderProvider = btcLockSenderProvider;
this.peginInstructionsProvider = peginInstructionsProvider;
this.isUpdateBridgeTimerEnabled = isUpdateBridgeTimerEnabled;
bitcoinWrapper.addBlockListener(this);
this.isUpdateBridgeTimerEnabled = isUpdateBridgeTimerEnabled;
this.amountOfHeadersToSend = amountOfHeadersToSend;
setConfigVariables(config);
}

public void start(Federation federation) {
Expand Down Expand Up @@ -204,41 +200,50 @@ public void updateBridge() {
}
logger.debug("[updateBridge] Updating bridge");

// Call receiveHeaders
try {
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());
if(shouldUpdateBridgeBtcBlockchain) {
// Call receiveHeaders
try {
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());
if(shouldUpdateBridgeBtcCoinbaseTransactions) {
// 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());
if(shouldUpdateBridgeBtcTransactions) {
// 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());
if(shouldUpdateCollections) {
// Call updateCollections
try {
logger.debug("[updateBridge] Sending updateCollections");
federatorSupport.sendUpdateCollections();
} catch (Exception e) {
logger.error(e.getMessage(), e);
panicProcessor.panic("btclock", e.getMessage());
}
}

}

@Override
Expand Down Expand Up @@ -815,6 +820,17 @@ protected PartialMerkleTree generatePMT(Block block, Transaction transaction) {
return generatePMT(block, transaction, transaction.hasWitnesses());
}

private void setConfigVariables(FedNodeSystemProperties config) {
this.activationConfig = config.getActivationConfig();
this.isUpdateBridgeTimerEnabled = config.isUpdateBridgeTimerEnabled();
this.isUpdateBridgeTimerEnabled = config.isUpdateBridgeTimerEnabled();
this.amountOfHeadersToSend = config.getAmountOfHeadersToSend();
this.shouldUpdateBridgeBtcBlockchain = config.shouldUpdateBridgeBtcBlockchain();
this.shouldUpdateBridgeBtcCoinbaseTransactions = config.shouldUpdateBridgeBtcCoinbaseTransactions();
this.shouldUpdateBridgeBtcTransactions = config.shouldUpdateBridgeBtcTransactions();
this.shouldUpdateCollections = config.shouldUpdateCollections();
}

public static class Factory {
private final FederatorSupport federatorSupport;
private final NodeBlockProcessor nodeBlockProcessor;
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/co/rsk/federate/FedNodeRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,20 @@ private void startFederate() throws Exception {
bitcoinWrapper = createAndSetupBitcoinWrapper(btcLockSenderProvider, peginInstructionsProvider);

btcToRskClientActive.setup(
config.getActivationConfig(),
bitcoinWrapper,
bridgeConstants,
btcToRskClientFileStorage,
btcLockSenderProvider,
peginInstructionsProvider,
config.isUpdateBridgeTimerEnabled(),
config.getAmountOfHeadersToSend()
config
);
btcToRskClientRetiring.setup(
config.getActivationConfig(),
bitcoinWrapper,
bridgeConstants,
btcToRskClientFileStorage,
btcLockSenderProvider,
peginInstructionsProvider,
config.isUpdateBridgeTimerEnabled(),
config.getAmountOfHeadersToSend()
config
);
BtcLogMonitor btcLogMonitor = new BtcLogMonitor(bitcoinWrapper, federateLogger);
btcLogMonitor.start();
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/co/rsk/federate/config/FedNodeSystemProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,25 @@ public Duration getPegoutSignedCacheTtl() {
return Duration.ofMinutes(
getInt("federator.pegoutSignedCacheTtlInMinutes", 30));
}

public boolean shouldUpdateBridgeBtcBlockchain() {
return configFromFiles.hasPath("federator.updateBridgeBtcBlockchain") &&
configFromFiles.getBoolean("federator.updateBridgeBtcBlockchain");
}

public boolean shouldUpdateBridgeBtcCoinbaseTransactions() {
return configFromFiles.hasPath("federator.updateBridgeBtcCoinbaseTransactions") &&
configFromFiles.getBoolean("federator.updateBridgeBtcCoinbaseTransactions");
}

public boolean shouldUpdateBridgeBtcTransactions() {
return configFromFiles.hasPath("federator.updateBridgeBtcTransactions") &&
configFromFiles.getBoolean("federator.updateBridgeBtcTransactions");
}

public boolean shouldUpdateCollections() {
return !configFromFiles.hasPath("federator.updateCollections") ||
configFromFiles.getBoolean("federator.updateCollections");
}

}
27 changes: 13 additions & 14 deletions src/test/java/co/rsk/federate/BtcToRskClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import co.rsk.federate.config.FedNodeSystemProperties;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.federate.bitcoin.BitcoinWrapper;
import co.rsk.federate.io.BtcToRskClientFileData;
Expand All @@ -27,8 +28,7 @@ public class BtcToRskClientBuilder {
private BtcLockSenderProvider btcLockSenderProvider;
private PeginInstructionsProvider peginInstructionsProvider;
private Federation federation;
private boolean isUpdateBridgeTimerEnabled;
private int amountOfHeadersToSend;
private FedNodeSystemProperties config;

public BtcToRskClientBuilder() throws PeginInstructionsException, IOException {
this.activationConfig = mock(ActivationConfig.class);
Expand All @@ -39,13 +39,18 @@ public BtcToRskClientBuilder() throws PeginInstructionsException, IOException {
this.btcLockSenderProvider = mock(BtcLockSenderProvider.class);
this.peginInstructionsProvider = mock(PeginInstructionsProvider.class);
this.federation = mock(Federation.class);
this.isUpdateBridgeTimerEnabled = true;
this.amountOfHeadersToSend = 100;
this.config = mock(FedNodeSystemProperties.class);

when(activationConfig.forBlock(anyLong())).thenReturn(mock(ActivationConfig.ForBlock.class));
when(btcToRskClientFileStorage.read(any())).thenReturn(new BtcToRskClientFileReadResult(true, new BtcToRskClientFileData()));
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.empty());
when(peginInstructionsProvider.buildPeginInstructions(any())).thenReturn(Optional.empty());
when(config.getActivationConfig()).thenReturn(this.activationConfig);
when(config.shouldUpdateBridgeBtcBlockchain()).thenReturn(true);
when(config.shouldUpdateBridgeBtcTransactions()).thenReturn(true);
when(config.shouldUpdateBridgeBtcCoinbaseTransactions()).thenReturn(true);
when(config.isUpdateBridgeTimerEnabled()).thenReturn(true);
when(config.getAmountOfHeadersToSend()).thenReturn(100);
}

public BtcToRskClientBuilder withActivationConfig(ActivationConfig activationConfig) {
Expand Down Expand Up @@ -88,28 +93,22 @@ public BtcToRskClientBuilder withFederation(Federation federation) {
return this;
}

public BtcToRskClientBuilder withUpdateBridgeTimerEnabled(boolean isUpdateBridgeTimerEnabled) {
this.isUpdateBridgeTimerEnabled = isUpdateBridgeTimerEnabled;
return this;
}

public BtcToRskClientBuilder withAmountOfHeadersToSend(int amountOfHeadersToSend) {
this.amountOfHeadersToSend = amountOfHeadersToSend;
public BtcToRskClientBuilder withFedNodeSystemProperties(FedNodeSystemProperties config) {
this.config = config;
return this;
}

public BtcToRskClient build () throws Exception {
when(config.getActivationConfig()).thenReturn(activationConfig);
return new BtcToRskClient(
activationConfig,
bitcoinWrapper,
federatorSupport,
bridgeConstants,
btcToRskClientFileStorage,
btcLockSenderProvider,
peginInstructionsProvider,
federation,
isUpdateBridgeTimerEnabled,
amountOfHeadersToSend
config
);
}
}
Loading

0 comments on commit 485f911

Please sign in to comment.