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