Search in sources :

Example 51 with BtcLockSenderProvider

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

the class BridgeSupportTestPowerMock method registerBtcTransactionMigrationTx.

@Test
public void registerBtcTransactionMigrationTx() throws BlockStoreException, AddressFormatException, IOException, BridgeIllegalArgumentException {
    NetworkParameters parameters = bridgeConstants.getBtcParams();
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, parameters);
    List<BtcECKey> retiringFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, parameters);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = Mockito.mock(Block.class);
    Mockito.when(executionBlock.getNumber()).thenReturn(15L);
    Repository track = repository.startTracking();
    BtcTransaction tx = new BtcTransaction(parameters);
    Address activeFederationAddress = activeFederation.getAddress();
    tx.addOutput(Coin.COIN, activeFederationAddress);
    // Create previous tx
    BtcTransaction prevTx = new BtcTransaction(btcParams);
    TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, retiringFederation.getAddress());
    prevTx.addOutput(prevOut);
    // Create tx input
    tx.addInput(prevOut);
    // Create tx input base script sig
    Script scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(retiringFederation);
    // Create sighash
    Script redeemScript = ScriptBuilder.createRedeemScript(retiringFederation.getNumberOfSignaturesRequired(), retiringFederation.getBtcPublicKeys());
    Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    // Sign by federator 0
    BtcECKey.ECDSASignature sig0 = retiringFederationKeys.get(0).sign(sighash);
    TransactionSignature txSig0 = new TransactionSignature(sig0, BtcTransaction.SigHash.ALL, false);
    int sigIndex0 = scriptSig.getSigInsertionIndex(sighash, retiringFederation.getBtcPublicKeys().get(0));
    scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig0.encodeToBitcoin(), sigIndex0, 1, 1);
    // Sign by federator 1
    BtcECKey.ECDSASignature sig1 = retiringFederationKeys.get(1).sign(sighash);
    TransactionSignature txSig1 = new TransactionSignature(sig1, BtcTransaction.SigHash.ALL, false);
    int sigIndex1 = scriptSig.getSigInsertionIndex(sighash, retiringFederation.getBtcPublicKeys().get(1));
    scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig1.encodeToBitcoin(), sigIndex1, 1, 1);
    // Set scipt sign to tx input
    tx.getInput(0).setScriptSig(scriptSig);
    BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, bridgeConstants, activationsBeforeForks);
    provider.setNewFederation(activeFederation);
    provider.setOldFederation(retiringFederation);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(track, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, track, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), executionBlock, mockFactory, activations);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
    int height = 30;
    mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), 30, pmt.bitcoinSerialize());
    bridgeSupport.save();
    track.commit();
    List<UTXO> activeFederationBtcUTXOs = provider.getNewFederationBtcUTXOs();
    List<Coin> activeFederationBtcCoins = activeFederationBtcUTXOs.stream().map(UTXO::getValue).collect(Collectors.toList());
    assertThat(activeFederationBtcUTXOs, hasSize(1));
    assertThat(activeFederationBtcCoins, hasItem(Coin.COIN));
}
Also used : RskAddress(co.rsk.core.RskAddress) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) Script(co.rsk.bitcoinj.script.Script) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) MutableRepository(org.ethereum.db.MutableRepository) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 52 with BtcLockSenderProvider

use of co.rsk.peg.btcLockSender.BtcLockSenderProvider 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 53 with BtcLockSenderProvider

use of co.rsk.peg.btcLockSender.BtcLockSenderProvider 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)

Example 54 with BtcLockSenderProvider

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

the class PeginInformationTest method parse_fromPeginInstructions_withoutBtcRefundAddress.

@Test
public void parse_fromPeginInstructions_withoutBtcRefundAddress() 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());
    PeginInstructionsVersion1 peginInstructionsMock = mock(PeginInstructionsVersion1.class);
    when(peginInstructionsMock.getProtocolVersion()).thenReturn(1);
    when(peginInstructionsMock.getRskDestinationAddress()).thenReturn(rskDestinationAddressFromPeginInstructions);
    when(peginInstructionsMock.getBtcRefundAddress()).thenReturn(Optional.empty());
    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(btcRefundAddressFromBtcLockSender, peginInformation.getBtcRefundAddress());
    Assert.assertEquals(btcRefundAddressFromBtcLockSender, peginInformation.getSenderBtcAddress());
    Assert.assertEquals(senderBtcAddressType, peginInformation.getSenderBtcAddressType());
    Assert.assertNotEquals(rskDestinationAddressFromBtcLockSender, peginInformation.getRskDestinationAddress());
}
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)

Example 55 with BtcLockSenderProvider

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

the class BridgeSupportFactory method newInstance.

public BridgeSupport newInstance(Repository repository, Block executionBlock, RskAddress contractAddress, List<LogInfo> logs) {
    ActivationConfig.ForBlock activations = activationConfig.forBlock(executionBlock.getNumber());
    Context btcContext = new Context(bridgeConstants.getBtcParams());
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    FederationSupport federationSupport = new FederationSupport(bridgeConstants, provider, executionBlock);
    BridgeEventLogger eventLogger;
    if (logs == null) {
        eventLogger = null;
    } else {
        eventLogger = new BridgeEventLoggerImpl(bridgeConstants, activations, logs);
    }
    BtcLockSenderProvider btcLockSenderProvider = new BtcLockSenderProvider();
    PeginInstructionsProvider peginInstructionsProvider = new PeginInstructionsProvider();
    return new BridgeSupport(bridgeConstants, provider, eventLogger, btcLockSenderProvider, peginInstructionsProvider, repository, executionBlock, btcContext, federationSupport, btcBlockStoreFactory, activations);
}
Also used : Context(co.rsk.bitcoinj.core.Context) BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger)

Aggregations

BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)56 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)51 Test (org.junit.Test)49 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)47 RskAddress (co.rsk.core.RskAddress)41 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)40 MutableRepository (org.ethereum.db.MutableRepository)40 Repository (org.ethereum.core.Repository)38 Block (org.ethereum.core.Block)35 InternalTransaction (org.ethereum.vm.program.InternalTransaction)35 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)28 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)26 Transaction (org.ethereum.core.Transaction)26 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)22 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)22 ECKey (org.ethereum.crypto.ECKey)21 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)15 Script (co.rsk.bitcoinj.script.Script)14 BtcLockSender (co.rsk.peg.btcLockSender.BtcLockSender)10 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)9