Search in sources :

Example 1 with PeginInstructions

use of co.rsk.peg.pegininstructions.PeginInstructions 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 PeginInstructions

use of co.rsk.peg.pegininstructions.PeginInstructions in project rskj by rsksmart.

the class BridgeSupportTest method when_registerBtcTransaction_invalidPeginProtocolVersion_afterFork_no_lock_and_refund.

@Test
public void when_registerBtcTransaction_invalidPeginProtocolVersion_afterFork_no_lock_and_refund() throws BlockStoreException, IOException, PeginInstructionsException, BridgeIllegalArgumentException {
    // Arrange
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddressFromBtcLockSender = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    RskAddress rskDestinationAddress = new RskAddress(new byte[20]);
    Coin amountToLock = Coin.COIN.multiply(10);
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    tx1.addOutput(amountToLock, federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash blockMerkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
    int height = 50;
    StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    co.rsk.bitcoinj.core.BtcBlock headBlock = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(2), Sha256Hash.of(new byte[] { 1 }), 1, 1, 1, new ArrayList<>());
    StoredBlock chainHead = new StoredBlock(headBlock, new BigInteger("0"), 132);
    when(btcBlockStore.getChainHead()).thenReturn(chainHead);
    when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddressFromBtcLockSender, rskAddress);
    PeginInstructions peginInstructions = mock(PeginInstructions.class);
    when(peginInstructions.getProtocolVersion()).thenReturn(99);
    when(peginInstructions.getRskDestinationAddress()).thenReturn(rskDestinationAddress);
    PeginInstructionsProvider peginInstructionsProvider = mock(PeginInstructionsProvider.class);
    when(peginInstructionsProvider.buildPeginInstructions(any())).thenReturn(Optional.of(peginInstructions));
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, peginInstructionsProvider, mock(Block.class), mockFactory, activations);
    // Act
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
    // Assert
    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(1, provider.getReleaseTransactionSet().getEntries().size());
    List<BtcTransaction> releaseTxs = provider.getReleaseTransactionSet().getEntries().stream().map(ReleaseTransactionSet.Entry::getTransaction).collect(Collectors.toList());
    // First release tx should correspond to the 5 BTC lock tx
    BtcTransaction releaseTx = releaseTxs.get(0);
    Assert.assertEquals(1, releaseTx.getOutputs().size());
    Assert.assertThat(amountToLock.subtract(releaseTx.getOutput(0).getValue()), is(lessThanOrEqualTo(Coin.MILLICOIN)));
    Assert.assertEquals(btcAddressFromBtcLockSender, releaseTx.getOutput(0).getScriptPubKey().getToAddress(btcParams));
    Assert.assertEquals(1, releaseTx.getInputs().size());
    Assert.assertEquals(tx1.getHash(), releaseTx.getInput(0).getOutpoint().getHash());
    Assert.assertEquals(0, releaseTx.getInput(0).getOutpoint().getIndex());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
Also used : PeginInstructions(co.rsk.peg.pegininstructions.PeginInstructions) RskAddress(co.rsk.core.RskAddress) 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) Script(co.rsk.bitcoinj.script.Script) 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) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

PeginInstructions (co.rsk.peg.pegininstructions.PeginInstructions)2 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)1 Script (co.rsk.bitcoinj.script.Script)1 RskAddress (co.rsk.core.RskAddress)1 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)1 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)1 BtcLockSender (co.rsk.peg.btcLockSender.BtcLockSender)1 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)1 PeginInstructionsException (co.rsk.peg.pegininstructions.PeginInstructionsException)1 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)1 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)1 BigInteger (java.math.BigInteger)1 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)1 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)1 Block (org.ethereum.core.Block)1 Repository (org.ethereum.core.Repository)1 Transaction (org.ethereum.core.Transaction)1 ECKey (org.ethereum.crypto.ECKey)1 MutableRepository (org.ethereum.db.MutableRepository)1 InternalTransaction (org.ethereum.vm.program.InternalTransaction)1