Search in sources :

Example 1 with UnlimitedWhiteListEntry

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());
}
Also used : UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) 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) 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 2 with UnlimitedWhiteListEntry

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

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());
}
Also used : MutableRepository(org.ethereum.db.MutableRepository) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) RskAddress(co.rsk.core.RskAddress) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BigInteger(java.math.BigInteger) 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 UnlimitedWhiteListEntry

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

Example 5 with UnlimitedWhiteListEntry

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;
}
Also used : UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) RskAddress(co.rsk.core.RskAddress) RLPList(org.ethereum.util.RLPList)

Aggregations

RskAddress (co.rsk.core.RskAddress)5 UnlimitedWhiteListEntry (co.rsk.peg.whitelist.UnlimitedWhiteListEntry)5 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)4 BigInteger (java.math.BigInteger)4 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)4 MutableRepository (org.ethereum.db.MutableRepository)4 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)3 DataWord (org.ethereum.vm.DataWord)3 LockWhitelistEntry (co.rsk.peg.whitelist.LockWhitelistEntry)2 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)1 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)1 RegTestParams (co.rsk.bitcoinj.params.RegTestParams)1 Script (co.rsk.bitcoinj.script.Script)1 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)1 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)1 Wallet (co.rsk.bitcoinj.wallet.Wallet)1