Skip to content

Commit

Permalink
fix: deleted boilerplate code like getters
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlen committed Dec 17, 2024
1 parent 710857d commit b751f1c
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions rskj-core/src/test/java/co/rsk/peg/RegisterBtcTransactionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ void setUp() throws Exception{
track = repository.startTracking();

BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider();
FeePerKbSupport feePerKbSupport = getFeePerKbSupport(repository, bridgeConstants);
StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository);

FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(bridgeStorageAccessor);
FeePerKbSupport feePerKbSupport = new FeePerKbSupportImpl(
bridgeConstants.getFeePerKbConstants(),
feePerKbStorageProvider
);

Federation federation = P2shErpFederationBuilder.builder().build();
FederationStorageProvider federationStorageProvider = getFederationStorageProvider(track, federation);
FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track);
federationStorageProvider.setNewFederation(federation);
FederationConstants federationConstants = bridgeConstants.getFederationConstants();
federationSupport = FederationSupportBuilder.builder()
.withFederationConstants(federationConstants)
Expand All @@ -75,7 +82,8 @@ void setUp() throws Exception{
BtcBlockStoreWithCache btcBlockStoreWithCache = btcBlockStoreFactory.newInstance(track, bridgeConstants, bridgeStorageProvider, activations);

BtcECKey btcPublicKey = BitcoinTestUtils.getBtcEcKeyFromSeed("seed");
rskReceiver = getRskReceiver(btcPublicKey);
ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey());
rskReceiver = new RskAddress(ecKey.getAddress());
bitcoinTransaction = createPegInTransaction(federationSupport.getActiveFederation().getAddress(), minimumPeginValue, btcPublicKey);

pmtWithTransactions = createValidPmtForTransactions(List.of(bitcoinTransaction.getHash()), bridgeConstants.getBtcParams());
Expand Down Expand Up @@ -110,7 +118,9 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt
co.rsk.core.Coin expectedReceiverBalance = receiverBalance.add(co.rsk.core.Coin.fromBitcoin(minimumPeginValue));

// Act
registerBtcTransactionAndCommit();
bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeSupport.save();
track.commit();

// Assert
Optional<Long> heightIfBtcTxHashIsAlreadyProcessed = bridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(bitcoinTransaction.getHash());
Expand All @@ -126,25 +136,23 @@ void whenRegisterALegacyBtcTransaction_shouldRegisterTheNewUtxoAndTransferTheRbt
@Test
void whenRegisterARepeatedLegacyBtcTransaction_shouldNotPerformAnyChange() throws Exception {
// Arrange
registerBtcTransactionAndCommit();
bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeSupport.save();
track.commit();

co.rsk.core.Coin expectedReceiverBalance = track.getBalance(rskReceiver);
List<UTXO> expectedFederationUTXOs = federationSupport.getActiveFederationBtcUTXOs();

// Act
registerBtcTransactionAndCommit();
bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeSupport.save();
track.commit();

// Assert
assertEquals(expectedFederationUTXOs, federationSupport.getActiveFederationBtcUTXOs());
assertEquals(expectedReceiverBalance, repository.getBalance(rskReceiver));
}

private void registerBtcTransactionAndCommit() throws Exception {
bridgeSupport.registerBtcTransaction(rskTx, bitcoinTransaction.bitcoinSerialize(), btcBlockWithPmtHeight, pmtWithTransactions.bitcoinSerialize());
bridgeSupport.save();
track.commit();
}

private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput output) {
return new UTXO(
bitcoinTransaction.getHash(),
Expand All @@ -156,26 +164,6 @@ private static UTXO utxoOf(BtcTransaction bitcoinTransaction, TransactionOutput
);
}

private RskAddress getRskReceiver(BtcECKey btcPublicKey) {
ECKey ecKey = ECKey.fromPublicOnly(btcPublicKey.getPubKey());
return new RskAddress(ecKey.getAddress());
}

private FederationStorageProvider getFederationStorageProvider(Repository track, Federation federation) {
FederationStorageProvider federationStorageProvider = createFederationStorageProvider(track);
federationStorageProvider.setNewFederation(federation);
return federationStorageProvider;
}

private static FeePerKbSupport getFeePerKbSupport(Repository repository, BridgeConstants bridgeConstants) {
StorageAccessor bridgeStorageAccessor = new BridgeStorageAccessorImpl(repository);
FeePerKbStorageProvider feePerKbStorageProvider = new FeePerKbStorageProviderImpl(bridgeStorageAccessor);
return new FeePerKbSupportImpl(
bridgeConstants.getFeePerKbConstants(),
feePerKbStorageProvider
);
}

private BtcTransaction createPegInTransaction(Address federationAddress, Coin coin, BtcECKey pubKey) {
BtcTransaction btcTx = new BtcTransaction(btcParams);
btcTx.addInput(BitcoinTestUtils.createHash(0), 0, ScriptBuilder.createInputScript(null, pubKey));
Expand Down

0 comments on commit b751f1c

Please sign in to comment.