use of co.rsk.peg.whitelist.UnlimitedWhiteListEntry 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.UnlimitedWhiteListEntry 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.UnlimitedWhiteListEntry in project rskj by rsksmart.
the class BridgeTestPowerMock method getLockWhitelistEntryByAddressAfterRskip87Fork.
@Test
public void getLockWhitelistEntryByAddressAfterRskip87Fork() throws Exception {
byte[] result;
Transaction mockedTransaction;
doReturn(true).when(activationConfig).isActive(eq(RSKIP87), anyLong());
doReturn(false).when(activationConfig).isActive(eq(RSKIP88), anyLong());
Repository repository = createRepository();
Repository track = repository.startTracking();
Address mockedAddressForUnlimited = new BtcECKey().toAddress(networkParameters);
Address mockedAddressForOneOff = new BtcECKey().toAddress(networkParameters);
BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForUnlimited.toBase58())).then((InvocationOnMock invocation) -> new UnlimitedWhiteListEntry(mockedAddressForUnlimited));
when(bridgeSupportMock.getLockWhitelistEntryByAddress(mockedAddressForOneOff.toBase58())).then((InvocationOnMock invocation) -> new OneOffWhiteListEntry(mockedAddressForOneOff, Coin.COIN));
mockedTransaction = mock(Transaction.class);
when(mockedTransaction.isLocalCallTransaction()).thenReturn(true);
BridgeSupportFactory bridgeSupportFactoryMock = mock(BridgeSupportFactory.class);
when(bridgeSupportFactoryMock.newInstance(any(), any(), any(), any())).thenReturn(bridgeSupportMock);
Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactoryMock);
bridge.init(mockedTransaction, getGenesisBlock(), track, null, null, null);
// Get the unlimited whitelist address
result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { mockedAddressForUnlimited.toBase58() }));
BigInteger decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
Assert.assertEquals(0, decodedResult.longValue());
// Get the one-off whitelist address
result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { mockedAddressForOneOff.toBase58() }));
decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
Assert.assertEquals(Coin.COIN.value, decodedResult.longValue());
// Try fetch an unexisting address
result = bridge.execute(Bridge.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.encode(new Object[] { (new BtcECKey().toAddress(networkParameters)).toBase58() }));
decodedResult = (BigInteger) BridgeMethods.GET_LOCK_WHITELIST_ENTRY_BY_ADDRESS.getFunction().decodeResult(result)[0];
Assert.assertEquals(-1, decodedResult.longValue());
}
use of co.rsk.peg.whitelist.UnlimitedWhiteListEntry 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()));
}
}
use of co.rsk.peg.whitelist.UnlimitedWhiteListEntry in project rskj by rsksmart.
the class BridgeSerializationUtils method deserializeUnlimitedLockWhitelistEntries.
public static Map<Address, UnlimitedWhiteListEntry> deserializeUnlimitedLockWhitelistEntries(byte[] data, NetworkParameters parameters) {
if (data == null) {
return new HashMap<>();
}
RLPList unlimitedWhitelistEntriesRlpList = (RLPList) RLP.decode2(data).get(0);
int unlimitedWhitelistEntriesSerializedAddressesSize = unlimitedWhitelistEntriesRlpList.size();
Map<Address, UnlimitedWhiteListEntry> entries = new HashMap<>(unlimitedWhitelistEntriesSerializedAddressesSize);
for (int j = 0; j < unlimitedWhitelistEntriesSerializedAddressesSize; j++) {
byte[] hash160 = unlimitedWhitelistEntriesRlpList.get(j).getRLPData();
Address address = new Address(parameters, hash160);
entries.put(address, new UnlimitedWhiteListEntry(address));
}
return entries;
}
Aggregations