use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSupportTest method assertLockingCap.
private void assertLockingCap(boolean shouldLock, boolean isLockingCapEnabled, Coin lockingCap, Coin amountSentToNewFed, Coin amountSentToOldFed, Coin amountInNewFed, Coin amountInOldFed) throws BlockStoreException, IOException, BridgeIllegalArgumentException {
// Configure if locking cap should be evaluated
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(isLockingCapEnabled);
BridgeConstants bridgeConstants = mock(BridgeConstants.class);
when(bridgeConstants.getLegacyMinimumPeginTxValueInSatoshis()).thenReturn(Coin.SATOSHI);
when(bridgeConstants.getBtcParams()).thenReturn(BridgeRegTestConstants.getInstance().getBtcParams());
when(bridgeConstants.getBtc2RskMinimumAcceptableConfirmations()).thenReturn(1);
when(bridgeConstants.getGenesisFeePerKb()).thenReturn(BridgeRegTestConstants.getInstance().getGenesisFeePerKb());
when(bridgeConstants.getMaxRbtc()).thenReturn(BridgeRegTestConstants.getInstance().getMaxRbtc());
// Configure locking cap
when(bridgeConstants.getInitialLockingCap()).thenReturn(lockingCap);
Repository repository = createRepository();
Federation oldFederation = this.getFederation(bridgeConstants);
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
// We need a random new fed
provider.setNewFederation(this.getFederation(bridgeConstants, Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")))));
// Use genesis fed as old
provider.setOldFederation(oldFederation);
Coin currentFunds = Coin.ZERO;
// Configure existing utxos in both federations
if (amountInOldFed != null) {
UTXO utxo = new UTXO(Sha256Hash.wrap(HashUtil.randomHash()), 0, amountInOldFed, 1, false, new Script(new byte[] {}));
provider.getOldFederationBtcUTXOs().add(utxo);
currentFunds = currentFunds.add(amountInOldFed);
}
if (amountInNewFed != null) {
UTXO utxo = new UTXO(Sha256Hash.wrap(HashUtil.randomHash()), 0, amountInNewFed, 1, false, new Script(new byte[] {}));
provider.getNewFederationBtcUTXOs().add(utxo);
currentFunds = currentFunds.add(amountInNewFed);
}
// Fund bridge in RBTC, substracting the funds that we are indicating were locked in the federations (which means a user locked)
co.rsk.core.Coin bridgeBalance = LIMIT_MONETARY_BASE.subtract(co.rsk.core.Coin.fromBitcoin(currentFunds));
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, bridgeBalance);
// The locking cap shouldn't be surpassed by the initial configuration
Assert.assertFalse(isLockingCapEnabled && currentFunds.isGreaterThan(lockingCap));
Coin lockValue = Coin.ZERO;
int newUtxos = 0;
// Create transaction setting the outputs to the configured values
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
if (amountSentToOldFed != null) {
tx.addOutput(amountSentToOldFed, provider.getOldFederation().getAddress());
lockValue = lockValue.add(amountSentToOldFed);
newUtxos++;
}
if (amountSentToNewFed != null) {
tx.addOutput(amountSentToNewFed, provider.getNewFederation().getAddress());
lockValue = lockValue.add(amountSentToNewFed);
newUtxos++;
}
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 registerHeader = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
Block executionBlock = Mockito.mock(Block.class);
when(executionBlock.getNumber()).thenReturn(10L);
// Get the tx sender public key
byte[] data = tx.getInput(0).getScriptSig().getChunks().get(1).data;
BtcECKey senderBtcKey = BtcECKey.fromPublicOnly(data);
// Whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
Address address = senderBtcKey.toAddress(bridgeConstants.getBtcParams());
whitelist.put(address, new OneOffWhiteListEntry(address, lockValue));
// The address is whitelisted
Assert.assertThat(whitelist.isWhitelisted(address), is(true));
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, new BtcLockSenderProvider(), new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
// Simulate blockchain
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, registerHeader, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
Transaction rskTx = mock(Transaction.class);
Keccak256 hash = new Keccak256(HashUtil.keccak256(new byte[] {}));
when(rskTx.getHash()).thenReturn(hash);
// Try to register tx
bridgeSupport.registerBtcTransaction(rskTx, tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
bridgeSupport.save();
// If the address is no longer whitelisted, it means it was consumed, whether the lock was rejected by lockingCap or not
Assert.assertThat(whitelist.isWhitelisted(address), is(false));
// The Btc transaction should have been processed
assertTrue(bridgeSupport.isBtcTxHashAlreadyProcessed(tx.getHash()));
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(shouldLock ? lockValue : Coin.ZERO);
RskAddress srcKeyRskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey.getPrivKey()).getAddress());
// Verify amount was locked
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(srcKeyRskAddress));
Assert.assertEquals(bridgeBalance.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
if (!shouldLock) {
// Release tx should have been created directly to the signatures stack
BtcTransaction releaseTx = provider.getReleaseTransactionSet().getEntries().iterator().next().getTransaction();
Assert.assertNotNull(releaseTx);
// returns the funds to the sender
Assert.assertEquals(1, releaseTx.getOutputs().size());
Assert.assertEquals(address, releaseTx.getOutputs().get(0).getAddressFromP2PKHScript(bridgeConstants.getBtcParams()));
Assert.assertEquals(lockValue, releaseTx.getOutputs().get(0).getValue().add(releaseTx.getFee()));
// Uses the same UTXO(s)
Assert.assertEquals(newUtxos, releaseTx.getInputs().size());
for (int i = 0; i < newUtxos; i++) {
TransactionInput input = releaseTx.getInput(i);
Assert.assertEquals(tx.getHash(), input.getOutpoint().getHash());
Assert.assertEquals(i, input.getOutpoint().getIndex());
}
}
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method registerBtcTransactionLockTxWhitelisted.
@Test
public void registerBtcTransactionLockTxWhitelisted() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
List<BtcECKey> federation1Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")) });
federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation federation1 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams);
List<BtcECKey> federation2Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")) });
federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation federation2 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, btcParams);
Repository repository = createRepository();
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
Block executionBlock = Mockito.mock(Block.class);
Mockito.when(executionBlock.getNumber()).thenReturn(10L);
Repository track = repository.startTracking();
// First transaction goes only to the first federation
BtcTransaction tx1 = new BtcTransaction(btcParams);
tx1.addOutput(Coin.COIN.multiply(5), federation1.getAddress());
BtcECKey srcKey1 = new BtcECKey();
tx1.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
// Second transaction goes only to the second federation
BtcTransaction tx2 = new BtcTransaction(btcParams);
tx2.addOutput(Coin.COIN.multiply(10), federation2.getAddress());
BtcECKey srcKey2 = new BtcECKey();
tx2.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey2));
// Third transaction has one output to each federation
// Lock is expected to be done accordingly and utxos assigned accordingly as well
BtcTransaction tx3 = new BtcTransaction(btcParams);
tx3.addOutput(Coin.COIN.multiply(2), federation1.getAddress());
tx3.addOutput(Coin.COIN.multiply(3), federation2.getAddress());
BtcECKey srcKey3 = new BtcECKey();
tx3.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey3));
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, bridgeConstants, activationsBeforeForks);
provider.setNewFederation(federation1);
provider.setOldFederation(federation2);
// Whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
Address address1 = srcKey1.toAddress(btcParams);
Address address2 = srcKey2.toAddress(btcParams);
Address address3 = srcKey3.toAddress(btcParams);
whitelist.put(address1, new OneOffWhiteListEntry(address1, Coin.COIN.multiply(5)));
whitelist.put(address2, new OneOffWhiteListEntry(address2, Coin.COIN.multiply(10)));
whitelist.put(address3, new OneOffWhiteListEntry(address3, Coin.COIN.multiply(2).add(Coin.COIN.multiply(3))));
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(tx1.getHash());
hashes.add(tx2.getHash());
hashes.add(tx3.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 3);
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());
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx2.bitcoinSerialize(), height, pmt.bitcoinSerialize());
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx3.bitcoinSerialize(), height, pmt.bitcoinSerialize());
bridgeSupport.save();
track.commit();
Assert.assertThat(whitelist.isWhitelisted(address1), is(false));
Assert.assertThat(whitelist.isWhitelisted(address2), is(false));
Assert.assertThat(whitelist.isWhitelisted(address3), is(false));
co.rsk.core.Coin amountToHaveBeenCreditedToSrc1 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
co.rsk.core.Coin amountToHaveBeenCreditedToSrc2 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(10, 0));
co.rsk.core.Coin amountToHaveBeenCreditedToSrc3 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = amountToHaveBeenCreditedToSrc1.add(amountToHaveBeenCreditedToSrc2).add(amountToHaveBeenCreditedToSrc3);
RskAddress srcKey1RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey1.getPrivKey()).getAddress());
RskAddress srcKey2RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey2.getPrivKey()).getAddress());
RskAddress srcKey3RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey3.getPrivKey()).getAddress());
Assert.assertEquals(amountToHaveBeenCreditedToSrc1, repository.getBalance(srcKey1RskAddress));
Assert.assertEquals(amountToHaveBeenCreditedToSrc2, repository.getBalance(srcKey2RskAddress));
Assert.assertEquals(amountToHaveBeenCreditedToSrc3, repository.getBalance(srcKey3RskAddress));
Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
Assert.assertEquals(2, provider2.getNewFederationBtcUTXOs().size());
Assert.assertEquals(2, provider2.getOldFederationBtcUTXOs().size());
Assert.assertEquals(Coin.COIN.multiply(5), provider2.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(Coin.COIN.multiply(2), provider2.getNewFederationBtcUTXOs().get(1).getValue());
Assert.assertEquals(Coin.COIN.multiply(10), provider2.getOldFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(Coin.COIN.multiply(3), provider2.getOldFederationBtcUTXOs().get(1).getValue());
Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider2.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider2.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
Assert.assertTrue(provider2.getHeightIfBtcTxhashIsAlreadyProcessed(tx2.getHash()).isPresent());
Assert.assertTrue(provider2.getHeightIfBtcTxhashIsAlreadyProcessed(tx3.getHash()).isPresent());
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry 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.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveLockWhitelist.
@Test
public void saveLockWhitelist() {
LockWhitelist whitelistMock = mock(LockWhitelist.class);
List<Integer> storageBytesCalls = new ArrayList<>();
List<Integer> serializeCalls = new ArrayList<>();
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
// Overriding activation to make sure it serializes the unlimited whitelist data
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
// Mock the One-Off serialization
PowerMockito.when(BridgeSerializationUtils.serializeOneOffLockWhitelist(any(Pair.class))).then((InvocationOnMock invocation) -> {
Pair<List<OneOffWhiteListEntry>, Integer> data = invocation.getArgument(0);
Assert.assertEquals(whitelistMock.getAll(OneOffWhiteListEntry.class), data.getLeft());
Assert.assertSame(whitelistMock.getDisableBlockHeight(), data.getRight());
serializeCalls.add(0);
return Hex.decode("ccdd");
});
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageBytesCalls.add(0);
RskAddress contractAddress = invocation.getArgument(0);
DataWord address = invocation.getArgument(1);
byte[] data = invocation.getArgument(2);
// Make sure the bytes are set to the correct address in the repo and that what's saved is what was serialized
Assert.assertTrue(Arrays.equals(Hex.decode("aabbccdd"), contractAddress.getBytes()));
Assert.assertEquals(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(Hex.decode("ccdd"), data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))), any(byte[].class));
// Mock the Unlimited serialization
PowerMockito.when(BridgeSerializationUtils.serializeUnlimitedLockWhitelist(any(List.class))).then((InvocationOnMock invocation) -> {
List<UnlimitedWhiteListEntry> unlimitedWhiteListEntries = invocation.getArgument(0);
Assert.assertEquals(whitelistMock.getAll(UnlimitedWhiteListEntry.class), unlimitedWhiteListEntries);
serializeCalls.add(0);
return Hex.decode("bbcc");
});
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageBytesCalls.add(0);
RskAddress contractAddress = invocation.getArgument(0);
DataWord address = invocation.getArgument(1);
byte[] data = invocation.getArgument(2);
// Make sure the bytes are set to the correct address in the repo and that what's saved is what was serialized
Assert.assertTrue(Arrays.equals(Hex.decode("aabbccdd"), contractAddress.getBytes()));
Assert.assertEquals(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
Assert.assertTrue(Arrays.equals(Hex.decode("bbcc"), data));
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8))), any(byte[].class));
storageProvider.saveLockWhitelist();
// Shouldn't have tried to save nor serialize anything
Assert.assertEquals(0, storageBytesCalls.size());
Assert.assertEquals(0, serializeCalls.size());
Whitebox.setInternalState(storageProvider, "lockWhitelist", whitelistMock);
storageProvider.saveLockWhitelist();
Assert.assertEquals(2, storageBytesCalls.size());
Assert.assertEquals(2, serializeCalls.size());
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_accepts_lock_tx_without_witness_after_rskip_143_activation.
@Test
public void registerBtcTransaction_accepts_lock_tx_without_witness_after_rskip_143_activation() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
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(10);
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(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(10)));
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddress, rskAddress), new PeginInstructionsProvider(), mock(Block.class), mockFactory, activations);
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
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());
}
Aggregations