Search in sources :

Example 1 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeSupport method addOneOffLockWhitelistAddress.

/**
 * Adds the given address to the lock whitelist.
 * Returns 1 upon success, or -1 if the address was
 * already in the whitelist.
 * @param addressBase58 the base58-encoded address to add to the whitelist
 * @param maxTransferValue the max amount of satoshis enabled to transfer for this address
 * @return 1 upon success, -1 if the address was already
 * in the whitelist, -2 if address is invalid
 * LOCK_WHITELIST_GENERIC_ERROR_CODE otherwise.
 */
public Integer addOneOffLockWhitelistAddress(Transaction tx, String addressBase58, BigInteger maxTransferValue) {
    try {
        Address address = getParsedAddress(addressBase58);
        Coin maxTransferValueCoin = Coin.valueOf(maxTransferValue.longValueExact());
        return this.addLockWhitelistAddress(tx, new OneOffWhiteListEntry(address, maxTransferValueCoin));
    } catch (AddressFormatException e) {
        logger.warn(INVALID_ADDRESS_FORMAT_MESSAGE, e);
        return LOCK_WHITELIST_INVALID_ADDRESS_FORMAT_ERROR_CODE;
    }
}
Also used : Coin(co.rsk.bitcoinj.core.Coin) AddressFormatException(co.rsk.bitcoinj.core.AddressFormatException) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry)

Example 2 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeSerializationUtils method serializeOneOffLockWhitelist.

/**
 * Serializes the data stored in the Tuple.
 * @param data data MUST be composed of a list of {@link co.rsk.peg.whitelist.OneOffWhiteListEntry} and the value of disableBlockHeight obtained from {@link co.rsk.peg.whitelist.LockWhitelist}
 * @return the serialized data
 */
public static byte[] serializeOneOffLockWhitelist(Pair<List<OneOffWhiteListEntry>, Integer> data) {
    List<OneOffWhiteListEntry> entries = data.getLeft();
    Integer disableBlockHeight = data.getRight();
    int serializationSize = entries.size() * 2 + 1;
    byte[][] serializedLockWhitelist = new byte[serializationSize][];
    for (int i = 0; i < entries.size(); i++) {
        OneOffWhiteListEntry entry = entries.get(i);
        serializedLockWhitelist[2 * i] = RLP.encodeElement(entry.address().getHash160());
        serializedLockWhitelist[2 * i + 1] = RLP.encodeBigInteger(BigInteger.valueOf(entry.maxTransferValue().longValue()));
    }
    serializedLockWhitelist[serializationSize - 1] = RLP.encodeBigInteger(BigInteger.valueOf(disableBlockHeight));
    return RLP.encodeList(serializedLockWhitelist);
}
Also used : BigInteger(java.math.BigInteger) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry)

Example 3 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry 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());
}
Also used : RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) Pair(org.apache.commons.lang3.tuple.Pair) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry 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));
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) RskAddress(co.rsk.core.RskAddress) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) Test(org.junit.Test)

Example 5 with OneOffWhiteListEntry

use of co.rsk.peg.whitelist.OneOffWhiteListEntry in project rskj by rsksmart.

the class BridgeTestPowerMock method getLockWhitelistAddress.

@Test
public void getLockWhitelistAddress() {
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
    bridge.init(null, getGenesisBlock(), createRepository().startTracking(), null, null, null);
    BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
    OneOffWhiteListEntry mockedEntry10 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
    OneOffWhiteListEntry mockedEntry20 = new OneOffWhiteListEntry(new BtcECKey().toAddress(networkParameters), Coin.COIN);
    Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
    when(bridgeSupportMock.getLockWhitelistEntryByIndex(10)).then((InvocationOnMock invocation) -> mockedEntry10);
    when(bridgeSupportMock.getLockWhitelistEntryByIndex(20)).then((InvocationOnMock invocation) -> mockedEntry20);
    Assert.assertEquals(mockedEntry10.address().toBase58(), bridge.getLockWhitelistAddress(new Object[] { BigInteger.valueOf(10) }));
    Assert.assertEquals(mockedEntry20.address().toBase58(), bridge.getLockWhitelistAddress(new Object[] { BigInteger.valueOf(20) }));
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)20 RskAddress (co.rsk.core.RskAddress)17 Test (org.junit.Test)14 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)13 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)12 MutableRepository (org.ethereum.db.MutableRepository)12 Repository (org.ethereum.core.Repository)11 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)9 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)9 BigInteger (java.math.BigInteger)8 InternalTransaction (org.ethereum.vm.program.InternalTransaction)8 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)7 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)7 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Block (org.ethereum.core.Block)7 Transaction (org.ethereum.core.Transaction)7 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)6 ECKey (org.ethereum.crypto.ECKey)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6