Search in sources :

Example 1 with BtcLockSender

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);
    }
}
Also used : PeginInstructions(co.rsk.peg.pegininstructions.PeginInstructions) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException)

Example 2 with BtcLockSender

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;
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender)

Example 3 with BtcLockSender

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);
}
Also used : RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) Keccak256(co.rsk.crypto.Keccak256) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 4 with BtcLockSender

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;
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender)

Example 5 with BtcLockSender

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());
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) PeginInstructionsVersion1(co.rsk.peg.pegininstructions.PeginInstructionsVersion1) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) P2pkhBtcLockSender(co.rsk.peg.btcLockSender.P2pkhBtcLockSender) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) TxSenderAddressType(co.rsk.peg.btcLockSender.BtcLockSender.TxSenderAddressType) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) RskAddress(co.rsk.core.RskAddress) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Aggregations

BtcLockSender (co.rsk.peg.btcLockSender.BtcLockSender)11 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)10 RskAddress (co.rsk.core.RskAddress)8 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)8 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)8 Test (org.junit.Test)8 Address (co.rsk.bitcoinj.core.Address)5 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)5 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)5 P2pkhBtcLockSender (co.rsk.peg.btcLockSender.P2pkhBtcLockSender)5 TxSenderAddressType (co.rsk.peg.btcLockSender.BtcLockSender.TxSenderAddressType)4 PeginInstructionsVersion1 (co.rsk.peg.pegininstructions.PeginInstructionsVersion1)4 Keccak256 (co.rsk.crypto.Keccak256)3 BigInteger (java.math.BigInteger)3 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)3 Repository (org.ethereum.core.Repository)3 ECKey (org.ethereum.crypto.ECKey)3 MutableRepository (org.ethereum.db.MutableRepository)3 InternalTransaction (org.ethereum.vm.program.InternalTransaction)3 PeginInstructions (co.rsk.peg.pegininstructions.PeginInstructions)1