use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupport method setLockWhitelistDisableBlockDelay.
/**
* Sets a delay in the BTC best chain to disable lock whitelist
* @param tx current RSK transaction
* @param disableBlockDelayBI block since current BTC best chain height to disable lock whitelist
* @return 1 if it was successful, -1 if a delay was already set, -2 if disableBlockDelay contains an invalid value
*/
public Integer setLockWhitelistDisableBlockDelay(Transaction tx, BigInteger disableBlockDelayBI) throws IOException, BlockStoreException {
if (!isLockWhitelistChangeAuthorized(tx)) {
return LOCK_WHITELIST_GENERIC_ERROR_CODE;
}
LockWhitelist lockWhitelist = provider.getLockWhitelist();
if (lockWhitelist.isDisableBlockSet()) {
return -1;
}
int disableBlockDelay = disableBlockDelayBI.intValueExact();
int bestChainHeight = getBtcBlockchainBestChainHeight();
if (disableBlockDelay + bestChainHeight <= bestChainHeight) {
return -2;
}
lockWhitelist.setDisableBlockHeight(bestChainHeight + disableBlockDelay);
return 1;
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeStorageProviderTest method getLockWhitelist_nullBytes.
@Test
public void getLockWhitelist_nullBytes() {
List<Integer> calls = new ArrayList<>();
PowerMockito.mockStatic(BridgeSerializationUtils.class);
Repository repositoryMock = mock(Repository.class);
BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).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) 0xee };
});
PowerMockito.when(BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
calls.add(0);
return null;
});
PowerMockito.when(BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
// THIS ONE WON'T BE CALLED BECAUSE ONEOFF IS EMPTY
calls.add(0);
Assert.fail("As we don't have data for one-off, we shouldn't have called deserialize unlimited");
return null;
});
LockWhitelist result = storageProvider.getLockWhitelist();
Assert.assertNotNull(result);
Assert.assertEquals(0, result.getSize().intValue());
// 1 for each call to deserializeFederationOnlyBtcKeys & getStorageBytes
Assert.assertEquals(2, calls.size());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSerializationUtilsTest method serializeLockWhitelist.
@Test
public void serializeLockWhitelist() throws Exception {
byte[][] addressesBytes = new byte[][] { BtcECKey.fromPrivate(BigInteger.valueOf(100)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(200)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(300)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(400)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(500)).getPubKeyHash(), BtcECKey.fromPrivate(BigInteger.valueOf(600)).getPubKeyHash() };
Coin maxToTransfer = Coin.CENT;
LockWhitelist lockWhitelist = new LockWhitelist(Arrays.stream(addressesBytes).map(bytes -> new Address(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), bytes)).collect(Collectors.toMap(Function.identity(), k -> new OneOffWhiteListEntry(k, maxToTransfer))), 0);
byte[] result = BridgeSerializationUtils.serializeOneOffLockWhitelist(Pair.of(lockWhitelist.getAll(OneOffWhiteListEntry.class), lockWhitelist.getDisableBlockHeight()));
StringBuilder expectedBuilder = new StringBuilder();
expectedBuilder.append("f897");
Arrays.stream(addressesBytes).sorted(UnsignedBytes.lexicographicalComparator()).forEach(bytes -> {
expectedBuilder.append("94");
expectedBuilder.append(ByteUtil.toHexString(bytes));
expectedBuilder.append("83");
expectedBuilder.append(ByteUtil.toHexString(BigInteger.valueOf(maxToTransfer.value).toByteArray()));
});
expectedBuilder.append("80");
String expected = expectedBuilder.toString();
Assert.assertEquals(expected, ByteUtil.toHexString(result));
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method setLockWhitelistDisableBlockDelay_maxIntValueDisableBlockBI.
@Test
public void setLockWhitelistDisableBlockDelay_maxIntValueDisableBlockBI() throws IOException, BlockStoreException {
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);
when(mockedWhitelist.isDisableBlockSet()).thenReturn(false);
int bestChainHeight = 10;
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
StoredBlock storedBlock = mock(StoredBlock.class);
when(storedBlock.getHeight()).thenReturn(bestChainHeight);
when(btcBlockStore.getChainHead()).thenReturn(storedBlock);
BtcBlock btcBlock = mock(BtcBlock.class);
doReturn(Sha256Hash.of(Hex.decode("aa"))).when(btcBlock).getHash();
doReturn(btcBlock).when(storedBlock).getHeader();
BridgeSupport bridgeSupport = getBridgeSupportWithMocksAndBtcBlockstoreForWhitelistTests(mockedWhitelist, btcBlockStore);
BigInteger disableBlockDelayBI = BigInteger.valueOf(Integer.MAX_VALUE);
Assert.assertEquals(-2, bridgeSupport.setLockWhitelistDisableBlockDelay(mockedTx, disableBlockDelayBI).intValue());
verify(mockedWhitelist, never()).put(any(), any());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method setLockWhitelistDisableBlockDelay_notAuthorized.
@Test
public void setLockWhitelistDisableBlockDelay_notAuthorized() throws IOException, BlockStoreException {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = Hex.decode("0000000000000000000000000000000000aabbcc");
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
BigInteger disableBlockDelayBI = BigInteger.valueOf(100);
Assert.assertEquals(-10, bridgeSupport.setLockWhitelistDisableBlockDelay(mockedTx, disableBlockDelayBI).intValue());
verify(mockedWhitelist, never()).put(any(), any());
}
Aggregations