use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.
the class PeginInformation method parse.
public void parse(BtcTransaction btcTx) throws PeginInstructionsException {
logger.trace("[parse] Trying to parse peg-in information from btc tx {}", btcTx.getHash());
// Get information from tx sender first
Optional<BtcLockSender> btcLockSenderOptional = btcLockSenderProvider.tryGetBtcLockSender(btcTx);
if (btcLockSenderOptional.isPresent()) {
BtcLockSender btcLockSender = btcLockSenderOptional.get();
parseFromBtcLockSender(btcLockSender);
}
// If HF is active and peg-in instructions were provided then override the info obtained from BtcLockSender
Optional<PeginInstructions> peginInstructionsOptional = Optional.empty();
if (activations.isActive(ConsensusRule.RSKIP170)) {
peginInstructionsOptional = peginInstructionsProvider.buildPeginInstructions(btcTx);
if (peginInstructionsOptional.isPresent()) {
PeginInstructions peginInstructions = peginInstructionsOptional.get();
parseFromPeginInstructions(peginInstructions);
}
}
// If BtcLockSender could not be parsed and peg-in instructions were not provided, then this tx can't be processed
if (!btcLockSenderOptional.isPresent() && !peginInstructionsOptional.isPresent()) {
String message = String.format("Could not get peg-in information for tx %s", btcTx.getHash());
logger.warn("[parse] {}", message);
throw new PeginInstructionsException(message);
}
}
use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.
the class BridgeSupportTest method getBtcLockSenderProvider.
private BtcLockSenderProvider getBtcLockSenderProvider(TxSenderAddressType txSenderAddressType, Address btcAddress, RskAddress rskAddress) {
BtcLockSender btcLockSender = mock(BtcLockSender.class);
when(btcLockSender.getTxSenderAddressType()).thenReturn(txSenderAddressType);
when(btcLockSender.getBTCAddress()).thenReturn(btcAddress);
when(btcLockSender.getRskAddress()).thenReturn(rskAddress);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
return btcLockSenderProvider;
}
use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.
the class BridgeSupportTest method registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_false.
@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_false() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(true);
ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
when(provider.isFastBridgeFederationDerivationHashUsed(any(), any())).thenReturn(false);
BtcLockSender btcLockSender = mock(BtcLockSender.class);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
Repository repository = mock(Repository.class);
when(repository.getBalance(any())).thenReturn(co.rsk.core.Coin.valueOf(1));
Context btcContext = mock(Context.class);
when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), btcLockSenderProvider, new PeginInstructionsProvider(), repository, mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
doReturn(Coin.COIN).when(bridgeSupport).getLockingCap();
doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
Address btcAddress = Address.fromBase58(btcParams, "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
RskAddress lbcAddress = new RskAddress(key.getAddress());
BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, getFastBridgeFederationAddress());
byte[] pmtSerialized = Hex.decode("ab");
InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, PegTestUtils.createHash3(0), btcAddress, lbcAddress, btcAddress, false);
Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_USER_ERROR_CODE), result);
}
use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method getBtcLockSenderProvider.
private BtcLockSenderProvider getBtcLockSenderProvider(TxSenderAddressType txSenderAddressType, Address btcAddress, RskAddress rskAddress) {
BtcLockSender btcLockSender = mock(BtcLockSender.class);
when(btcLockSender.getTxSenderAddressType()).thenReturn(txSenderAddressType);
when(btcLockSender.getBTCAddress()).thenReturn(btcAddress);
when(btcLockSender.getRskAddress()).thenReturn(rskAddress);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
return btcLockSenderProvider;
}
use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.
the class PeginInformationTest method parse_fromPeginInstructions.
@Test
public void parse_fromPeginInstructions() throws PeginInstructionsException {
// Arrange
BtcECKey address1Key = new BtcECKey();
RskAddress rskDestinationAddressFromBtcLockSender = new RskAddress(ECKey.fromPublicOnly(address1Key.getPubKey()).getAddress());
Address btcRefundAddressFromBtcLockSender = address1Key.toAddress(networkParameters);
TxSenderAddressType senderBtcAddressType = TxSenderAddressType.P2PKH;
BtcTransaction btcTx = new BtcTransaction(networkParameters);
BtcLockSender btcLockSenderMock = mock(P2pkhBtcLockSender.class);
when(btcLockSenderMock.getRskAddress()).thenReturn(rskDestinationAddressFromBtcLockSender);
when(btcLockSenderMock.getBTCAddress()).thenReturn(btcRefundAddressFromBtcLockSender);
when(btcLockSenderMock.getTxSenderAddressType()).thenReturn(senderBtcAddressType);
BtcLockSenderProvider btcLockSenderProviderMock = mock(BtcLockSenderProvider.class);
when(btcLockSenderProviderMock.tryGetBtcLockSender(btcTx)).thenReturn(Optional.of(btcLockSenderMock));
BtcECKey address2Key = new BtcECKey();
RskAddress rskDestinationAddressFromPeginInstructions = new RskAddress(ECKey.fromPublicOnly(address2Key.getPubKey()).getAddress());
Address btcRefundAddressFromPeginInstructions = address2Key.toAddress(networkParameters);
PeginInstructionsVersion1 peginInstructionsMock = mock(PeginInstructionsVersion1.class);
when(peginInstructionsMock.getProtocolVersion()).thenReturn(1);
when(peginInstructionsMock.getRskDestinationAddress()).thenReturn(rskDestinationAddressFromPeginInstructions);
when(peginInstructionsMock.getBtcRefundAddress()).thenReturn(Optional.of(btcRefundAddressFromPeginInstructions));
PeginInstructionsProvider peginInstructionsProviderMock = mock(PeginInstructionsProvider.class);
when(peginInstructionsProviderMock.buildPeginInstructions(btcTx)).thenReturn(Optional.of(peginInstructionsMock));
ActivationConfig.ForBlock activationsMock = mock(ActivationConfig.ForBlock.class);
when(activationsMock.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
// Act
PeginInformation peginInformation = new PeginInformation(btcLockSenderProviderMock, peginInstructionsProviderMock, activationsMock);
peginInformation.parse(btcTx);
// Assert
Assert.assertEquals(1, peginInformation.getProtocolVersion());
Assert.assertEquals(rskDestinationAddressFromPeginInstructions, peginInformation.getRskDestinationAddress());
Assert.assertEquals(btcRefundAddressFromPeginInstructions, peginInformation.getBtcRefundAddress());
Assert.assertEquals(btcRefundAddressFromBtcLockSender, peginInformation.getSenderBtcAddress());
Assert.assertEquals(senderBtcAddressType, peginInformation.getSenderBtcAddressType());
Assert.assertNotEquals(rskDestinationAddressFromBtcLockSender, peginInformation.getRskDestinationAddress());
Assert.assertNotEquals(btcRefundAddressFromBtcLockSender, peginInformation.getBtcRefundAddress());
}
Aggregations