use of co.rsk.peg.whitelist.LockWhitelist 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.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginBtc_before_rskip_170_activation.
@Test
public void eventLoggerLogPeginBtc_before_rskip_170_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(false);
BridgeEventLogger mockedEventLogger = mock(BridgeEventLogger.class);
BridgeStorageProvider mockBridgeStorageProvider = mock(BridgeStorageProvider.class);
when(mockBridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(any(Sha256Hash.class))).thenReturn(Optional.empty());
LockWhitelist lockWhitelist = mock(LockWhitelist.class);
when(lockWhitelist.isWhitelistedFor(any(Address.class), any(Coin.class), any(int.class))).thenReturn(true);
when(mockBridgeStorageProvider.getLockWhitelist()).thenReturn(lockWhitelist);
when(mockBridgeStorageProvider.getNewFederation()).thenReturn(bridgeConstants.getGenesisFederation());
Block executionBlock = mock(Block.class);
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStoreFactory.newInstance(any(Repository.class), any(), any(), any())).thenReturn(btcBlockStore);
// Create transaction
Coin lockValue = Coin.COIN;
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
tx.addOutput(lockValue, mockBridgeStorageProvider.getNewFederation().getAddress());
BtcECKey srcKey = new BtcECKey();
tx.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey));
// Create header and PMT
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), bits, hashes, 1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock btcBlock = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<>());
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, btcBlock, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, atLeastOnce()).logLockBtc(any(RskAddress.class), any(BtcTransaction.class), any(Address.class), any(Coin.class));
verify(mockedEventLogger, never()).logPeginBtc(any(RskAddress.class), any(BtcTransaction.class), any(Coin.class), anyInt());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginBtc_after_rskip_170_activation.
@Test
public void eventLoggerLogPeginBtc_after_rskip_170_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
BridgeEventLogger mockedEventLogger = mock(BridgeEventLogger.class);
BridgeStorageProvider mockBridgeStorageProvider = mock(BridgeStorageProvider.class);
when(mockBridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(any(Sha256Hash.class))).thenReturn(Optional.empty());
LockWhitelist lockWhitelist = mock(LockWhitelist.class);
when(lockWhitelist.isWhitelistedFor(any(Address.class), any(Coin.class), any(int.class))).thenReturn(true);
when(mockBridgeStorageProvider.getLockWhitelist()).thenReturn(lockWhitelist);
when(mockBridgeStorageProvider.getNewFederation()).thenReturn(bridgeConstants.getGenesisFederation());
Block executionBlock = mock(Block.class);
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStoreFactory.newInstance(any(Repository.class), any(), any(), any())).thenReturn(btcBlockStore);
// Create transaction
Coin lockValue = Coin.COIN;
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
tx.addOutput(lockValue, mockBridgeStorageProvider.getNewFederation().getAddress());
BtcECKey srcKey = new BtcECKey();
tx.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey));
// Create header and PMT
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), bits, hashes, 1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock btcBlock = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<>());
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, btcBlock, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, never()).logLockBtc(any(RskAddress.class), any(BtcTransaction.class), any(Address.class), any(Coin.class));
verify(mockedEventLogger, atLeastOnce()).logPeginBtc(any(RskAddress.class), any(BtcTransaction.class), any(Coin.class), anyInt());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginRejectionEvents_before_rskip_181_activation.
@Test
public void eventLoggerLogPeginRejectionEvents_before_rskip_181_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP181)).thenReturn(false);
BridgeEventLogger mockedEventLogger = mock(BridgeEventLogger.class);
BridgeStorageProvider mockBridgeStorageProvider = mock(BridgeStorageProvider.class);
when(mockBridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(any(Sha256Hash.class))).thenReturn(Optional.empty());
LockWhitelist lockWhitelist = mock(LockWhitelist.class);
when(lockWhitelist.isWhitelistedFor(any(Address.class), any(Coin.class), any(int.class))).thenReturn(true);
when(mockBridgeStorageProvider.getLockWhitelist()).thenReturn(lockWhitelist);
when(mockBridgeStorageProvider.getNewFederation()).thenReturn(bridgeConstants.getGenesisFederation());
Block executionBlock = mock(Block.class);
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStoreFactory.newInstance(any(Repository.class), any(), any(), any())).thenReturn(btcBlockStore);
// Create transaction
Coin lockValue = Coin.COIN;
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
tx.addOutput(lockValue, mockBridgeStorageProvider.getNewFederation().getAddress());
BtcECKey srcKey = new BtcECKey();
tx.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey));
// Create header and PMT
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), bits, hashes, 1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock btcBlock = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<>());
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, btcBlock, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any(BtcTransaction.class))).thenReturn(Optional.empty());
PeginInstructionsProvider peginInstructionsProvider = mock(PeginInstructionsProvider.class);
when(peginInstructionsProvider.buildPeginInstructions(any(BtcTransaction.class))).thenReturn(Optional.empty());
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(btcLockSenderProvider).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, never()).logRejectedPegin(any(BtcTransaction.class), any(RejectedPeginReason.class));
verify(mockedEventLogger, never()).logUnrefundablePegin(any(BtcTransaction.class), any(UnrefundablePeginReason.class));
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_sending_segwit_tx_twice_locks_just_once.
@Test
public void registerBtcTransaction_sending_segwit_tx_twice_locks_just_once() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock mockedActivations = mock(ActivationConfig.ForBlock.class);
when(mockedActivations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
BtcTransaction txWithWitness = new BtcTransaction(btcParams);
// first input spends P2PKH
BtcECKey srcKey1 = new BtcECKey();
txWithWitness.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
// second input spends P2SH-P2PWKH (actually, just has a witness doesn't matter if it truly spends a witness for the test's sake)
txWithWitness.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
txWithWitness.setWitness(0, txWit);
List<BtcECKey> fedKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
Federation fed = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(fedKeys), Instant.ofEpochMilli(1000L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
txWithWitness.addOutput(Coin.COIN.multiply(5), fed.getAddress());
// Create the pmt without witness and calculate the block merkle root
byte[] bits = new byte[1];
bits[0] = 0x3f;
PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash()), 1);
Sha256Hash merkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
PartialMerkleTree pmtWithWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash(true)), 1);
Sha256Hash witnessMerkleRoot = pmtWithWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getNewFederation()).thenReturn(fed);
when(provider.getCoinbaseInformation(registerHeader.getHash())).thenReturn(new CoinbaseInformation(witnessMerkleRoot));
when(provider.getLockWhitelist()).thenReturn(new LockWhitelist(new HashMap<>(), 0));
when(provider.getLockingCap()).thenReturn(Coin.FIFTY_COINS);
// mock an actual store for the processed txs
HashMap<Sha256Hash, Long> processedTxs = new HashMap<>();
doAnswer(a -> {
processedTxs.put(a.getArgument(0), a.getArgument(1));
return null;
}).when(provider).setHeightBtcTxhashAlreadyProcessed(any(), anyLong());
doAnswer(a -> Optional.ofNullable(processedTxs.get(a.getArgument(0)))).when(provider).getHeightIfBtcTxhashIsAlreadyProcessed(any());
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
Block executionBlock = mock(Block.class);
when(executionBlock.getNumber()).thenReturn(666L);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(provider).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(mockFactory).withActivations(mockedActivations).build();
int height = 30;
mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
// Tx is locked
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithWitness.bitcoinSerialize(), height, pmtWithWitness.bitcoinSerialize());
verify(provider, never()).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(true), executionBlock.getNumber());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(false), executionBlock.getNumber());
BtcTransaction txWithoutWitness = new BtcTransaction(btcParams, txWithWitness.bitcoinSerialize());
txWithoutWitness.setWitness(0, null);
assertFalse(txWithoutWitness.hasWitness());
// Tx is NOT locked again!
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithoutWitness.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithoutWitness.getHash(), executionBlock.getNumber());
assertNotEquals(txWithWitness.getHash(true), txWithoutWitness.getHash());
}
Aggregations