Search in sources :

Example 1 with PeginInstructionsException

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

the class BridgeSupport method processPegIn.

protected void processPegIn(BtcTransaction btcTx, Transaction rskTx, int height, Sha256Hash btcTxHash) throws IOException, RegisterBtcTransactionException {
    logger.debug("[processPegIn] This is a lock tx {}", btcTx);
    Coin totalAmount = computeTotalAmountSent(btcTx);
    PeginInformation peginInformation = new PeginInformation(btcLockSenderProvider, peginInstructionsProvider, activations);
    try {
        peginInformation.parse(btcTx);
    } catch (PeginInstructionsException e) {
        if (activations.isActive(ConsensusRule.RSKIP170)) {
            if (activations.isActive(ConsensusRule.RSKIP181)) {
                eventLogger.logRejectedPegin(btcTx, RejectedPeginReason.PEGIN_V1_INVALID_PAYLOAD);
            }
            // If possible to get the sender address, refund
            refundTxSender(btcTx, rskTx, peginInformation, totalAmount);
            markTxAsProcessed(btcTx);
        }
        String message = String.format("Error while trying to parse peg-in information for tx %s. %s", btcTx.getHash(), e.getMessage());
        logger.warn("[processPegIn] {}", message);
        throw new RegisterBtcTransactionException(message);
    }
    int protocolVersion = peginInformation.getProtocolVersion();
    logger.debug("[processPegIn] Protocol version: {}", protocolVersion);
    switch(protocolVersion) {
        case 0:
            processPegInVersionLegacy(btcTx, rskTx, height, peginInformation, totalAmount);
            break;
        case 1:
            processPegInVersion1(btcTx, rskTx, peginInformation, totalAmount);
            break;
        default:
            markTxAsProcessed(btcTx);
            String message = String.format("Invalid peg-in protocol version: %d", protocolVersion);
            logger.warn("[processPegIn] {}", message);
            throw new RegisterBtcTransactionException(message);
    }
    markTxAsProcessed(btcTx);
    logger.info("[processPegIn] BTC Tx {} processed in RSK", btcTxHash);
}
Also used : Coin(co.rsk.bitcoinj.core.Coin) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException)

Example 2 with PeginInstructionsException

use of co.rsk.peg.pegininstructions.PeginInstructionsException 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 3 with PeginInstructionsException

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

the class BridgeSupportTest method processPegIn_errorParsingPeginInstructions_afterRskip170_refundSender.

@Test
public void processPegIn_errorParsingPeginInstructions_afterRskip170_refundSender() throws IOException, PeginInstructionsException {
    // Arrange
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
    Repository repository = createRepository();
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Address btcSenderAddress = srcKey1.toAddress(btcParams);
    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 = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcSenderAddress, rskAddress);
    PeginInstructionsProvider peginInstructionsProvider = mock(PeginInstructionsProvider.class);
    when(peginInstructionsProvider.buildPeginInstructions(btcTx)).thenThrow(PeginInstructionsException.class);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, peginInstructionsProvider, 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 ex) {
        // Assert
        Assert.assertTrue(ex instanceof RegisterBtcTransactionException);
        Assert.assertEquals(1, releaseTransactionSet.getEntries().size());
        // Check rejection tx input was created from btc tx and sent to the btc refund address indicated by the user
        boolean successfulRejection = false;
        for (ReleaseTransactionSet.Entry e : releaseTransactionSet.getEntries()) {
            BtcTransaction refundTx = e.getTransaction();
            if (refundTx.getInput(0).getOutpoint().getHash() == btcTx.getHash() && refundTx.getOutput(0).getScriptPubKey().getToAddress(btcParams).equals(btcSenderAddress)) {
                successfulRejection = true;
                break;
            }
        }
        Assert.assertTrue(successfulRejection);
    }
}
Also used : Script(co.rsk.bitcoinj.script.Script) RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) 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) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 4 with PeginInstructionsException

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

the class PeginInformation method parseFromPeginInstructions.

private void parseFromPeginInstructions(PeginInstructions peginInstructions) throws PeginInstructionsException {
    this.protocolVersion = peginInstructions.getProtocolVersion();
    this.rskDestinationAddress = peginInstructions.getRskDestinationAddress();
    logger.trace("[parseFromPeginInstructions] Protocol version: {}", peginInstructions.getProtocolVersion());
    logger.trace("[parseFromPeginInstructions] RSK destination address: {}", peginInstructions.getRskDestinationAddress());
    switch(protocolVersion) {
        case 1:
            PeginInstructionsVersion1 peginInstructionsV1 = (PeginInstructionsVersion1) peginInstructions;
            parseFromPeginInstructionsVersion1(peginInstructionsV1);
            break;
        default:
            String message = String.format("Invalid protocol version: %d", protocolVersion);
            logger.warn("[parseFromPeginInstructions] {}", message);
            throw new PeginInstructionsException(message);
    }
}
Also used : PeginInstructionsVersion1(co.rsk.peg.pegininstructions.PeginInstructionsVersion1) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException)

Aggregations

PeginInstructionsException (co.rsk.peg.pegininstructions.PeginInstructionsException)4 Coin (co.rsk.bitcoinj.core.Coin)1 Script (co.rsk.bitcoinj.script.Script)1 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 RskAddress (co.rsk.core.RskAddress)1 BtcLockSender (co.rsk.peg.btcLockSender.BtcLockSender)1 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)1 PeginInstructions (co.rsk.peg.pegininstructions.PeginInstructions)1 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)1 PeginInstructionsVersion1 (co.rsk.peg.pegininstructions.PeginInstructionsVersion1)1 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)1 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)1 IOException (java.io.IOException)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