Search in sources :

Example 11 with BtcLockSenderProvider

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

the class BridgeSupportTest method getFastBridgeWallet_ok.

@Test
public void getFastBridgeWallet_ok() {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
    Context btcContext = mock(Context.class);
    when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
    BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, mock(BridgeStorageProvider.class), mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
    Federation fed = bridgeConstants.getGenesisFederation();
    Keccak256 derivationHash = PegTestUtils.createHash3(1);
    Script fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(fed.getRedeemScript(), Sha256Hash.wrap(derivationHash.getBytes()));
    Script fastBridgeP2SH = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
    FastBridgeFederationInformation fastBridgeFederationInformation = new FastBridgeFederationInformation(derivationHash, fed.getP2SHScript().getPubKeyHash(), fastBridgeP2SH.getPubKeyHash());
    BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
    tx.addOutput(Coin.COIN, fastBridgeFederationInformation.getFastBridgeFederationAddress(bridgeConstants.getBtcParams()));
    List<UTXO> utxoList = new ArrayList<>();
    UTXO utxo = new UTXO(tx.getHash(), 0, Coin.COIN, 0, false, fastBridgeP2SH);
    utxoList.add(utxo);
    Wallet obtainedWallet = bridgeSupport.getFastBridgeWallet(btcContext, utxoList, fastBridgeFederationInformation);
    Assert.assertEquals(Coin.COIN, obtainedWallet.getBalance());
}
Also used : Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) Wallet(co.rsk.bitcoinj.wallet.Wallet) Keccak256(co.rsk.crypto.Keccak256) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Block(org.ethereum.core.Block) FastBridgeFederationInformation(co.rsk.peg.fastbridge.FastBridgeFederationInformation) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 12 with BtcLockSenderProvider

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

the class BridgeSupportTest method processPegIn_errorParsingPeginInstructions_beforeRskip170_dontRefundSender.

@Test
public void processPegIn_errorParsingPeginInstructions_beforeRskip170_dontRefundSender() throws IOException {
    // Arrange
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(false);
    Repository repository = createRepository();
    BtcTransaction btcTx = new BtcTransaction(btcParams);
    btcTx.addOutput(Coin.COIN.multiply(10), bridgeConstants.getGenesisFederation().getAddress());
    btcTx.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
    when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
    BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
    when(btcLockSenderProvider.tryGetBtcLockSender(btcTx)).thenReturn(Optional.empty());
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, mock(PeginInstructionsProvider.class), mock(Block.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
    // Act
    try {
        bridgeSupport.processPegIn(btcTx, mock(Transaction.class), 0, mock(Sha256Hash.class));
        // Should have thrown a RegisterBtcTransactionException
        Assert.fail();
    } catch (Exception e) {
        // Assert
        Assert.assertTrue(e instanceof RegisterBtcTransactionException);
        Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
    }
}
Also used : Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) VMException(org.ethereum.vm.exception.VMException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 13 with BtcLockSenderProvider

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

the class BridgeSupportTest method when_registerBtcTransaction_sender_not_recognized_before_rskip170_no_lock_and_no_refund.

@Test
public void when_registerBtcTransaction_sender_not_recognized_before_rskip170_no_lock_and_no_refund() throws Exception {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = mock(Block.class);
    when(executionBlock.getNumber()).thenReturn(10L);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Coin amountToLock = Coin.COIN.multiply(5);
    // First transaction goes only to the first federation
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    tx1.addOutput(amountToLock, federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
    when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.empty());
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.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), tx1.bitcoinSerialize(), height, pmt.bitcoinSerialize());
    Assert.assertEquals(co.rsk.core.Coin.ZERO, repository.getBalance(rskAddress));
    Assert.assertEquals(LIMIT_MONETARY_BASE, repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    Assert.assertEquals(0, provider.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertFalse(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
Also used : ECKey(org.ethereum.crypto.ECKey) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 14 with BtcLockSenderProvider

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

the class BridgeSupportTest method addSignature_fedPubKey_belongs_to_active_federation.

@Test
public void addSignature_fedPubKey_belongs_to_active_federation() throws Exception {
    // Setup
    FederationSupport mockFederationSupport = mock(FederationSupport.class);
    // Creates new federation
    List<BtcECKey> federation1Keys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), new Context(bridgeConstants.getBtcParams()), mockFederationSupport, null, null);
    when(mockFederationSupport.getActiveFederation()).thenReturn(activeFederation);
    when(provider.getRskTxsWaitingForSignatures()).thenReturn(new TreeMap<>());
    bridgeSupport.addSignature(BtcECKey.fromPrivate(Hex.decode("fa01")), null, PegTestUtils.createHash3(1).getBytes());
    verify(provider, times(1)).getRskTxsWaitingForSignatures();
}
Also used : PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 15 with BtcLockSenderProvider

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

the class BridgeSupportTest method addSignature_fedPubKey_no_belong_to_retiring_or_active_federation.

@Test
public void addSignature_fedPubKey_no_belong_to_retiring_or_active_federation() throws Exception {
    // Setup
    FederationSupport mockFederationSupport = mock(FederationSupport.class);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), new Context(bridgeConstants.getBtcParams()), mockFederationSupport, null, null);
    // Creates retiring federation
    List<BtcECKey> federation1Keys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams);
    // Creates active federation
    List<BtcECKey> activeFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")));
    activeFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 0L, btcParams);
    when(mockFederationSupport.getActiveFederation()).thenReturn(activeFederation);
    when(mockFederationSupport.getRetiringFederation()).thenReturn(retiringFederation);
    bridgeSupport.addSignature(BtcECKey.fromPrivate(Hex.decode("fa05")), null, PegTestUtils.createHash3(1).getBytes());
    verify(provider, times(0)).getRskTxsWaitingForSignatures();
}
Also used : BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

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