Search in sources :

Example 1 with LockWhitelistEntry

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();
}
Also used : BigInteger(java.math.BigInteger) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry)

Example 2 with LockWhitelistEntry

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());
}
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 3 with LockWhitelistEntry

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());
}
Also used : UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) DataWord(org.ethereum.vm.DataWord) BigInteger(java.math.BigInteger) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) 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) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with LockWhitelistEntry

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

Example 5 with LockWhitelistEntry

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()));
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) RskAddress(co.rsk.core.RskAddress) TxSenderAddressType(co.rsk.peg.btcLockSender.BtcLockSender.TxSenderAddressType) Keccak256(co.rsk.crypto.Keccak256) ByteBuffer(java.nio.ByteBuffer) SecureRandom(java.security.SecureRandom) Assert.assertThat(org.junit.Assert.assertThat) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) Is.is(org.hamcrest.core.Is.is) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) Wallet(co.rsk.bitcoinj.wallet.Wallet) BridgeTestNetConstants(co.rsk.config.BridgeTestNetConstants) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) SimpleBlockChain(co.rsk.peg.simples.SimpleBlockChain) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) Script(co.rsk.bitcoinj.script.Script) DigestOutputStream(java.security.DigestOutputStream) Stream(java.util.stream.Stream) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) MessageDigest(java.security.MessageDigest) RunWith(org.junit.runner.RunWith) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) BigIntegers(org.bouncycastle.util.BigIntegers) Hex(org.bouncycastle.util.encoders.Hex) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) HashUtil(org.ethereum.crypto.HashUtil) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Matchers.hasItem(org.hamcrest.Matchers.hasItem) java.io(java.io) InternalTransaction(org.ethereum.vm.program.InternalTransaction) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) SimpleWallet(co.rsk.peg.simples.SimpleWallet) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LockWhitelistEntry (co.rsk.peg.whitelist.LockWhitelistEntry)7 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)6 RskAddress (co.rsk.core.RskAddress)5 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)4 BigInteger (java.math.BigInteger)4 Test (org.junit.Test)4 UnlimitedWhiteListEntry (co.rsk.peg.whitelist.UnlimitedWhiteListEntry)3 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)3 Repository (org.ethereum.core.Repository)3 MutableRepository (org.ethereum.db.MutableRepository)3 DataWord (org.ethereum.vm.DataWord)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 Script (co.rsk.bitcoinj.script.Script)2 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)2 BridgeConstants (co.rsk.config.BridgeConstants)2 Keccak256 (co.rsk.crypto.Keccak256)2 IOException (java.io.IOException)2 java.util (java.util)2