Search in sources :

Example 6 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 7 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_true.

@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_true() 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);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
    when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
    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, true);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_LP_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 8 with BtcLockSender

use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.

the class BridgeSupportTest method registerFastBridgeBtcTransaction_surpasses_locking_cap_and_tries_to_register_again.

@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_tries_to_register_again() 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);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, co.rsk.core.Coin.valueOf(1));
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
    BtcLockSender btcLockSender = mock(BtcLockSender.class);
    BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
    when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
    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(// The first time we simulate a lower locking cap than the value to register, to force the reimburse
    Coin.COIN, // The next time we simulate a hight locking cap, to verify the user can't attempt to register the already reimbursed tx
    Coin.FIFTY_COINS).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);
    Keccak256 dHash = PegTestUtils.createHash3(0);
    BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, dHash, btcAddress, lbcAddress, btcAddress, false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_USER_ERROR_CODE), result);
    // Update repository
    bridgeSupport.save();
    result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, dHash, btcAddress, lbcAddress, btcAddress, false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_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 9 with BtcLockSender

use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.

the class PeginInformationTest method parse_fromBtcLockSender.

@Test
public void parse_fromBtcLockSender() throws PeginInstructionsException {
    // Arrange
    BtcECKey key = new BtcECKey();
    RskAddress rskDestinationAddressFromBtcLockSender = new RskAddress(ECKey.fromPublicOnly(key.getPubKey()).getAddress());
    Address btcRefundAddressFromBtcLockSender = key.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));
    PeginInstructionsProvider peginInstructionsProviderMock = mock(PeginInstructionsProvider.class);
    ActivationConfig.ForBlock activationsMock = mock(ActivationConfig.ForBlock.class);
    // Act
    PeginInformation peginInformation = new PeginInformation(btcLockSenderProviderMock, peginInstructionsProviderMock, activationsMock);
    peginInformation.parse(btcTx);
    // Assert
    Assert.assertEquals(0, peginInformation.getProtocolVersion());
    Assert.assertEquals(rskDestinationAddressFromBtcLockSender, peginInformation.getRskDestinationAddress());
    Assert.assertEquals(btcRefundAddressFromBtcLockSender, peginInformation.getBtcRefundAddress());
    Assert.assertEquals(btcRefundAddressFromBtcLockSender, peginInformation.getSenderBtcAddress());
    Assert.assertEquals(senderBtcAddressType, peginInformation.getSenderBtcAddressType());
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) 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)

Example 10 with BtcLockSender

use of co.rsk.peg.btcLockSender.BtcLockSender in project rskj by rsksmart.

the class PeginInformationTest method parse_fromPeginInstructions_invalidProtocolVersion.

@Test(expected = PeginInstructionsException.class)
public void parse_fromPeginInstructions_invalidProtocolVersion() throws PeginInstructionsException {
    // Arrange
    BtcECKey address1Key = new BtcECKey();
    RskAddress rskDestinationAddressFromBtcLockSender = new RskAddress(ECKey.fromPublicOnly(address1Key.getPubKey()).getAddress());
    Address btcRefundAddressFromBtcLockSender = address1Key.toAddress(networkParameters);
    BtcTransaction btcTx = new BtcTransaction(networkParameters);
    BtcLockSender btcLockSenderMock = mock(P2pkhBtcLockSender.class);
    when(btcLockSenderMock.getRskAddress()).thenReturn(rskDestinationAddressFromBtcLockSender);
    when(btcLockSenderMock.getBTCAddress()).thenReturn(btcRefundAddressFromBtcLockSender);
    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());
    PeginInstructionsVersion1 peginInstructionsMock = mock(PeginInstructionsVersion1.class);
    when(peginInstructionsMock.getProtocolVersion()).thenReturn(0);
    when(peginInstructionsMock.getRskDestinationAddress()).thenReturn(rskDestinationAddressFromPeginInstructions);
    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);
}
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) 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