use of co.rsk.peg.whitelist.OneOffWhiteListEntry 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.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeSerializationUtils method deserializeOneOffLockWhitelistAndDisableBlockHeight.
public static Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> deserializeOneOffLockWhitelistAndDisableBlockHeight(byte[] data, NetworkParameters parameters) {
if (data == null || data.length == 0) {
return null;
}
RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
int serializedAddressesSize = rlpList.size() - 1;
// serialized addresses size must be even - key, value pairs expected in sequence
if (serializedAddressesSize % 2 != 0) {
throw new RuntimeException("deserializeLockWhitelist: expected an even number of addresses, but odd given");
}
HashMap<Address, OneOffWhiteListEntry> entries = new HashMap<>(serializedAddressesSize / 2);
for (int i = 0; i < serializedAddressesSize; i = i + 2) {
byte[] hash160 = rlpList.get(i).getRLPData();
byte[] maxTransferValueData = rlpList.get(i + 1).getRLPData();
Address address = new Address(parameters, hash160);
entries.put(address, new OneOffWhiteListEntry(address, Coin.valueOf(safeToBigInteger(maxTransferValueData).longValueExact())));
}
int disableBlockHeight = safeToBigInteger(rlpList.get(serializedAddressesSize).getRLPData()).intValueExact();
return Pair.of(entries, disableBlockHeight);
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry 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.OneOffWhiteListEntry in project rskj by rsksmart.
the class BridgeStorageProvider method getLockWhitelist.
public LockWhitelist getLockWhitelist() {
if (lockWhitelist != null) {
return lockWhitelist;
}
Pair<HashMap<Address, OneOffWhiteListEntry>, Integer> oneOffWhitelistAndDisableBlockHeightData = safeGetFromRepository(LOCK_ONE_OFF_WHITELIST_KEY, data -> BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(data, networkParameters));
if (oneOffWhitelistAndDisableBlockHeightData == null) {
lockWhitelist = new LockWhitelist(new HashMap<>());
return lockWhitelist;
}
Map<Address, LockWhitelistEntry> whitelistedAddresses = new HashMap<>();
whitelistedAddresses.putAll(oneOffWhitelistAndDisableBlockHeightData.getLeft());
if (activations.isActive(RSKIP87)) {
whitelistedAddresses.putAll(safeGetFromRepository(LOCK_UNLIMITED_WHITELIST_KEY, data -> BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(data, networkParameters)));
}
lockWhitelist = new LockWhitelist(whitelistedAddresses, oneOffWhitelistAndDisableBlockHeightData.getRight());
return lockWhitelist;
}
use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.
the class Bridge method getLockWhitelistEntryByAddress.
public long getLockWhitelistEntryByAddress(Object[] args) {
logger.trace("getLockWhitelistEntryByAddress");
String addressBase58;
try {
addressBase58 = (String) args[0];
} catch (Exception e) {
logger.warn("Exception in getLockWhitelistEntryByAddress", e);
return LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE;
}
LockWhitelistEntry entry = bridgeSupport.getLockWhitelistEntryByAddress(addressBase58);
if (entry == null) {
// Empty string is returned when address is not found
return LOCK_WHITELIST_ENTRY_NOT_FOUND_CODE;
}
return entry.getClass() == OneOffWhiteListEntry.class ? ((OneOffWhiteListEntry) entry).maxTransferValue().getValue() : LOCK_WHITELIST_UNLIMITED_MODE_CODE;
}
Aggregations