use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_ignores_pegin_instructions_before_rskip_170_activation.
@Test
public void registerBtcTransaction_ignores_pegin_instructions_before_rskip_170_activation() throws BlockStoreException, IOException, PeginInstructionsException, BridgeIllegalArgumentException {
// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(false);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddressFromBtcLockSender = srcKey1.toAddress(btcParams);
RskAddress rskDerivedAddress = 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);
// Whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
whitelist.put(btcAddressFromBtcLockSender, new OneOffWhiteListEntry(btcAddressFromBtcLockSender, amountToLock));
BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddressFromBtcLockSender, rskDerivedAddress);
PeginInstructionsProvider peginInstructionsProvider = getPeginInstructionsProviderForVersion1(rskDestinationAddress, Optional.empty());
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
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(co.rsk.core.Coin.ZERO, repository.getBalance(rskDestinationAddress));
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskDerivedAddress));
Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash(true)).isPresent());
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method getTransactionType_migration_tx.
@Test
public void getTransactionType_migration_tx() {
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, mock(ActivationConfig.ForBlock.class));
Federation retiringFederation = bridgeConstants.getGenesisFederation();
List<BtcECKey> retiringFederationPrivateKeys = BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS;
List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 1L, btcParams);
when(mockFederationSupport.getActiveFederation()).thenReturn(activeFederation);
when(mockFederationSupport.getRetiringFederation()).thenReturn(retiringFederation);
BtcTransaction migrationTx = new BtcTransaction(btcParams);
migrationTx.addOutput(Coin.COIN, activeFederation.getAddress());
TransactionInput migrationTxInput = new TransactionInput(btcParams, null, new byte[] {}, new TransactionOutPoint(btcParams, 0, Sha256Hash.ZERO_HASH));
migrationTx.addInput(migrationTxInput);
// Sign it using the Federation members
Script redeemScript = createBaseRedeemScriptThatSpendsFromTheFederation(retiringFederation);
Script inputScript = createBaseInputScriptThatSpendsFromTheFederation(retiringFederation);
migrationTxInput.setScriptSig(inputScript);
Sha256Hash sighash = migrationTx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
for (int i = 0; i < retiringFederation.getNumberOfSignaturesRequired(); i++) {
BtcECKey federatorPrivKey = retiringFederationPrivateKeys.get(i);
BtcECKey federatorPublicKey = retiringFederation.getBtcPublicKeys().get(i);
BtcECKey.ECDSASignature sig = federatorPrivKey.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
int sigIndex = inputScript.getSigInsertionIndex(sighash, federatorPublicKey);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex, 1, 1);
}
migrationTxInput.setScriptSig(inputScript);
Assert.assertEquals(TxType.MIGRATION, bridgeSupport.getTransactionType(migrationTx));
when(mockFederationSupport.getRetiringFederation()).thenReturn(null);
when(provider.getLastRetiredFederationP2SHScript()).thenReturn(Optional.of(retiringFederation.getP2SHScript()));
Assert.assertEquals(TxType.MIGRATION, bridgeSupport.getTransactionType(migrationTx));
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method when_registerBtcTransaction_usesLegacyType_afterFork_notWhitelisted_no_lock_and_refund.
@Test
public void when_registerBtcTransaction_usesLegacyType_afterFork_notWhitelisted_no_lock_and_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);
// First transaction goes only to the first federation
BtcTransaction tx1 = new BtcTransaction(btcParams);
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddress = srcKey1.toAddress(btcParams);
RskAddress rskAddress = new RskAddress(key.getAddress());
Coin amountToLock = Coin.COIN.multiply(5);
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);
// Don't whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddress, rskAddress);
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.assertThat(whitelist.isWhitelisted(btcAddress), is(false));
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(1, provider.getReleaseTransactionSet().getEntries().size());
List<BtcTransaction> releaseTxs = provider.getReleaseTransactionSet().getEntries().stream().map(ReleaseTransactionSet.Entry::getTransaction).sorted(Comparator.comparing(BtcTransaction::getOutputSum)).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(btcAddress, releaseTx.getOutput(0).getAddressFromP2PKHScript(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());
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method when_registerBtcTransaction_sender_not_recognized_after_rskip170_lock.
@Test
public void when_registerBtcTransaction_sender_not_recognized_after_rskip170_lock() throws Exception {
// Assert
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);
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());
RskAddress rskDestinationAddress = new RskAddress(new byte[20]);
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());
PeginInstructionsProvider peginInstructionsProvider = getPeginInstructionsProviderForVersion1(rskDestinationAddress, 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, 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);
// Act
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmt.bitcoinSerialize());
// Assert
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(co.rsk.core.Coin.ZERO, repository.getBalance(rskAddress));
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskDestinationAddress));
Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method when_registerBtcTransaction_usesMultisigWithWitnessType_afterFork_no_lock_and_refund.
@Test
public void when_registerBtcTransaction_usesMultisigWithWitnessType_afterFork_no_lock_and_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();
Address btcAddress = srcKey1.toAddress(btcParams);
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);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2SHP2WSH, btcAddress, null);
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(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(btcAddress, 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());
}
Aggregations