use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method addLockWhitelistAddress_addFails.
@Test
public void addLockWhitelistAddress_addFails() {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = ECKey.fromPublicOnly(Hex.decode(// Public key hex of the authorized whitelist admin in regtest, taken from BridgeRegTestConstants
"04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad")).getAddress();
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
ArgumentCaptor<Address> argument = ArgumentCaptor.forClass(Address.class);
when(mockedWhitelist.isWhitelisted(any(Address.class))).thenReturn(true);
Assert.assertEquals(-1, bridgeSupport.addOneOffLockWhitelistAddress(mockedTx, "mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", BigInteger.valueOf(Coin.COIN.getValue())).intValue());
verify(mockedWhitelist).isWhitelisted(argument.capture());
Assert.assertThat(argument.getValue().toBase58(), is("mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN"));
}
use of co.rsk.peg.whitelist.LockWhitelist 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.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method addLockWhitelistAddress_ok.
@Test
public void addLockWhitelistAddress_ok() {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = ECKey.fromPublicOnly(Hex.decode(// Public key hex of the authorized whitelist admin in regtest, taken from BridgeRegTestConstants
"04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad")).getAddress();
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
when(mockedWhitelist.put(any(Address.class), any(OneOffWhiteListEntry.class))).then((InvocationOnMock m) -> {
Address address = m.<Address>getArgument(0);
Assert.assertEquals("mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", address.toBase58());
return true;
});
Assert.assertEquals(1, bridgeSupport.addOneOffLockWhitelistAddress(mockedTx, "mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", BigInteger.valueOf(Coin.COIN.getValue())).intValue());
}
use of co.rsk.peg.whitelist.LockWhitelist 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.LockWhitelist 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());
}
Aggregations