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);
}
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);
}
}
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);
}
}
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);
}
}
Aggregations