use of co.rsk.peg.whitelist.LockWhitelistEntry in project rskj by rsksmart.
the class Bridge method getLockWhitelistAddress.
public String getLockWhitelistAddress(Object[] args) {
logger.trace("getLockWhitelistAddress");
int index = ((BigInteger) args[0]).intValue();
LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByIndex(index);
if (entry == null) {
// Empty string is returned when address is not found
return "";
}
return entry.address().toBase58();
}
use of co.rsk.peg.whitelist.LockWhitelistEntry in project rskj by rsksmart.
the class BridgeStorageProviderTest method saveLockWhiteListAfterGetWithData.
@Test
public void saveLockWhiteListAfterGetWithData() {
AtomicReference<Boolean> storageCalled = new AtomicReference<>();
storageCalled.set(Boolean.FALSE);
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
OneOffWhiteListEntry oneOffEntry = new OneOffWhiteListEntry(getBtcAddress("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), Coin.COIN);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig().forBlock(500L));
when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> new byte[] { (byte) 0xaa });
PowerMockito.when(BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
map.put(oneOffEntry.address(), oneOffEntry);
return Pair.of(map, 0);
});
PowerMockito.when(BridgeSerializationUtils.serializeOneOffLockWhitelist(any(Pair.class))).thenReturn(new byte[] { (byte) 0xee });
Mockito.doAnswer((InvocationOnMock invocation) -> {
storageCalled.set(Boolean.TRUE);
return null;
}).when(repositoryMock).addStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))), eq(new byte[] { (byte) 0xee }));
Assert.assertTrue(storageProvider.getLockWhitelist().getSize() > 0);
storageProvider.saveLockWhitelist();
Assert.assertTrue(storageCalled.get());
}
use of co.rsk.peg.whitelist.LockWhitelistEntry in project rskj by rsksmart.
the class BridgeStorageProviderTest method getLockWhitelist_nonNullBytes.
@Test
public void getLockWhitelist_nonNullBytes() {
List<Integer> calls = new ArrayList<>();
LockWhitelist whitelistMock = new LockWhitelist(new HashMap<>());
LockWhitelistEntry oneOffEntry = new OneOffWhiteListEntry(getBtcAddress("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), Coin.COIN);
LockWhitelistEntry unlimitedEntry = new UnlimitedWhiteListEntry(getBtcAddress("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
whitelistMock.put(oneOffEntry.address(), oneOffEntry);
whitelistMock.put(unlimitedEntry.address(), unlimitedEntry);
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);
when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgument(0);
DataWord address = invocation.getArgument(1);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xaa };
});
when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> {
calls.add(0);
RskAddress contractAddress = invocation.getArgument(0);
DataWord address = invocation.getArgument(1);
// Make sure the bytes are got from the correct address in the repo
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
Assert.assertEquals(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
return new byte[] { (byte) 0xbb };
});
PowerMockito.when(BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] data = invocation.getArgument(0);
NetworkParameters parameters = invocation.getArgument(1);
Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
// Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
map.put(oneOffEntry.address(), oneOffEntry);
return Pair.of(map, 0);
});
PowerMockito.when(BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
byte[] unlimitedData = invocation.getArgument(0);
NetworkParameters parameters = invocation.getArgument(1);
Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
// Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xbb }, unlimitedData));
HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
map.put(unlimitedEntry.address(), unlimitedEntry);
return map;
});
Assert.assertEquals(whitelistMock.getAll(), storageProvider.getLockWhitelist().getAll());
// 1 for each call to deserializeFederationOnlyBtcKeys & getStorageBytes (we call getStorageBytes twice)
Assert.assertEquals(4, calls.size());
}
use of co.rsk.peg.whitelist.LockWhitelistEntry in project rskj by rsksmart.
the class BridgeSerializationUtilsTest method serializeDeserializeOneOffLockWhitelistAndDisableBlockHeight.
@Test
public void serializeDeserializeOneOffLockWhitelistAndDisableBlockHeight() {
NetworkParameters btcParams = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
Map<Address, LockWhitelistEntry> whitelist = new HashMap<>();
Address address = BtcECKey.fromPrivate(BigInteger.valueOf(100L)).toAddress(btcParams);
whitelist.put(address, new OneOffWhiteListEntry(address, Coin.COIN));
LockWhitelist originalLockWhitelist = new LockWhitelist(whitelist, 0);
byte[] serializedLockWhitelist = BridgeSerializationUtils.serializeOneOffLockWhitelist(Pair.of(originalLockWhitelist.getAll(OneOffWhiteListEntry.class), originalLockWhitelist.getDisableBlockHeight()));
Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> deserializedLockWhitelist = BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(serializedLockWhitelist, btcParams);
List<Address> originalAddresses = originalLockWhitelist.getAddresses();
List<Address> deserializedAddresses = new ArrayList(deserializedLockWhitelist.getLeft().keySet());
Assert.assertThat(originalAddresses, hasSize(1));
Assert.assertThat(deserializedAddresses, hasSize(1));
Assert.assertThat(originalAddresses, is(deserializedAddresses));
Assert.assertThat(((OneOffWhiteListEntry) originalLockWhitelist.get(originalAddresses.get(0))).maxTransferValue(), is((deserializedLockWhitelist.getLeft().get(deserializedAddresses.get(0))).maxTransferValue()));
}
use of co.rsk.peg.whitelist.LockWhitelistEntry in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method getLockWhitelistMethods.
@Test
public void getLockWhitelistMethods() {
NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
when(mockedWhitelist.getSize()).thenReturn(4);
List<LockWhitelistEntry> entries = Arrays.stream(new Integer[] { 2, 3, 4, 5 }).map(i -> new UnlimitedWhiteListEntry(new Address(parameters, BtcECKey.fromPrivate(BigInteger.valueOf(i)).getPubKeyHash()))).collect(Collectors.toList());
when(mockedWhitelist.getAll()).thenReturn(entries);
for (int i = 0; i < 4; i++) {
when(mockedWhitelist.get(entries.get(i).address())).thenReturn(entries.get(i));
}
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
Assert.assertEquals(4, bridgeSupport.getLockWhitelistSize().intValue());
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(-1));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(4));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(5));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByAddress(new Address(parameters, BtcECKey.fromPrivate(BigInteger.valueOf(-1)).getPubKeyHash()).toBase58()));
for (int i = 0; i < 4; i++) {
Assert.assertEquals(entries.get(i), bridgeSupport.getLockWhitelistEntryByIndex(i));
Assert.assertEquals(entries.get(i), bridgeSupport.getLockWhitelistEntryByAddress(entries.get(i).address().toBase58()));
}
}
Aggregations