Search in sources :

Example 76 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method getReleaseRequestQueue.

@Test
public void getReleaseRequestQueue() throws IOException {
    List<Integer> calls = new ArrayList<>();
    ReleaseRequestQueue requestQueueMock = mock(ReleaseRequestQueue.class);
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        // 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(new DataWord("releaseRequestQueue".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeReleaseRequestQueue(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgumentAt(0, byte[].class);
        NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
        Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
        // Make sure we're deserializing what just came from the repo
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        return requestQueueMock;
    });
    Assert.assertSame(requestQueueMock, storageProvider.getReleaseRequestQueue());
    // 1 for each call to deserializeFederation & getStorageBytes
    Assert.assertEquals(2, calls.size());
}
Also used : BigInteger(java.math.BigInteger) Repository(org.ethereum.core.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 77 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method saveFederationElection.

@Test
public void saveFederationElection() throws IOException {
    ABICallElection electionMock = mock(ABICallElection.class);
    List<Integer> storageBytesCalls = new ArrayList<>();
    List<Integer> serializeCalls = new ArrayList<>();
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    PowerMockito.when(BridgeSerializationUtils.serializeElection(any(ABICallElection.class))).then((InvocationOnMock invocation) -> {
        ABICallElection election = invocation.getArgumentAt(0, ABICallElection.class);
        Assert.assertSame(electionMock, election);
        serializeCalls.add(0);
        return Hex.decode("aabb");
    });
    Mockito.doAnswer((InvocationOnMock invocation) -> {
        storageBytesCalls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        byte[] data = invocation.getArgumentAt(2, byte[].class);
        // 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(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
        Assert.assertEquals(new DataWord("federationElection".getBytes(StandardCharsets.UTF_8)), address);
        Assert.assertTrue(Arrays.equals(Hex.decode("aabb"), data));
        return null;
    }).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
    storageProvider.saveFederationElection();
    // Shouldn't have tried to save nor serialize anything
    Assert.assertEquals(0, storageBytesCalls.size());
    Assert.assertEquals(0, serializeCalls.size());
    Whitebox.setInternalState(storageProvider, "federationElection", electionMock);
    storageProvider.saveFederationElection();
    Assert.assertEquals(1, storageBytesCalls.size());
    Assert.assertEquals(1, serializeCalls.size());
}
Also used : BigInteger(java.math.BigInteger) Repository(org.ethereum.core.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 78 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method createInstance.

@Test
public void createInstance() throws IOException {
    Repository repository = new RepositoryImpl(config);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Map<Sha256Hash, Long> processed = provider.getBtcTxHashesAlreadyProcessed();
    Assert.assertNotNull(processed);
    Assert.assertTrue(processed.isEmpty());
    ReleaseRequestQueue releaseRequestQueue = provider.getReleaseRequestQueue();
    Assert.assertNotNull(releaseRequestQueue);
    Assert.assertEquals(0, releaseRequestQueue.getEntries().size());
    ReleaseTransactionSet releaseTransactionSet = provider.getReleaseTransactionSet();
    Assert.assertNotNull(releaseTransactionSet);
    Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
    SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
    Assert.assertNotNull(signatures);
    Assert.assertTrue(signatures.isEmpty());
    List<UTXO> utxos = provider.getNewFederationBtcUTXOs();
    Assert.assertNotNull(utxos);
    Assert.assertTrue(utxos.isEmpty());
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 79 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method getReleaseTransactionSet.

@Test
public void getReleaseTransactionSet() throws IOException {
    List<Integer> calls = new ArrayList<>();
    ReleaseTransactionSet transactionSetMock = mock(ReleaseTransactionSet.class);
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        // 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(new DataWord("releaseTransactionSet".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeReleaseTransactionSet(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgumentAt(0, byte[].class);
        NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
        Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
        // Make sure we're deserializing what just came from the repo
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        return transactionSetMock;
    });
    Assert.assertSame(transactionSetMock, storageProvider.getReleaseTransactionSet());
    // 1 for each call to deserializeFederation & getStorageBytes
    Assert.assertEquals(2, calls.size());
}
Also used : BigInteger(java.math.BigInteger) Repository(org.ethereum.core.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 80 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method createSaveAndRecreateInstance.

@Test
public void createSaveAndRecreateInstance() throws IOException {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    provider0.getBtcTxHashesAlreadyProcessed();
    provider0.getReleaseRequestQueue();
    provider0.getReleaseTransactionSet();
    provider0.getRskTxsWaitingForSignatures();
    provider0.getNewFederationBtcUTXOs();
    provider0.getOldFederationBtcUTXOs();
    provider0.save();
    track.commit();
    track = repository.startTracking();
    RskAddress contractAddress = PrecompiledContracts.BRIDGE_ADDR;
    Assert.assertNotNull(repository.getContractDetails(contractAddress));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("btcTxHashesAP".getBytes())));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("releaseRequestQueue".getBytes())));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("releaseTransactionSet".getBytes())));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("rskTxsWaitingFS".getBytes())));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("newFederationBtcUTXOs".getBytes())));
    Assert.assertNotNull(repository.getStorageBytes(contractAddress, new DataWord("oldFederationBtcUTXOs".getBytes())));
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Map<Sha256Hash, Long> processed = provider.getBtcTxHashesAlreadyProcessed();
    Assert.assertNotNull(processed);
    Assert.assertTrue(processed.isEmpty());
    ReleaseRequestQueue releaseRequestQueue = provider.getReleaseRequestQueue();
    Assert.assertNotNull(releaseRequestQueue);
    Assert.assertEquals(0, releaseRequestQueue.getEntries().size());
    ReleaseTransactionSet releaseTransactionSet = provider.getReleaseTransactionSet();
    Assert.assertNotNull(releaseTransactionSet);
    Assert.assertEquals(0, releaseTransactionSet.getEntries().size());
    SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
    Assert.assertNotNull(signatures);
    Assert.assertTrue(signatures.isEmpty());
    List<UTXO> newUtxos = provider.getNewFederationBtcUTXOs();
    Assert.assertNotNull(newUtxos);
    Assert.assertTrue(newUtxos.isEmpty());
    List<UTXO> oldUtxos = provider.getOldFederationBtcUTXOs();
    Assert.assertNotNull(oldUtxos);
    Assert.assertTrue(oldUtxos.isEmpty());
}
Also used : DataWord(org.ethereum.vm.DataWord) Keccak256(co.rsk.crypto.Keccak256) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Repository (org.ethereum.core.Repository)136 Test (org.junit.Test)109 RskAddress (co.rsk.core.RskAddress)59 DataWord (org.ethereum.vm.DataWord)43 BigInteger (java.math.BigInteger)31 RepositoryImpl (co.rsk.db.RepositoryImpl)25 Coin (co.rsk.core.Coin)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 HashMapDB (org.ethereum.datasource.HashMapDB)12 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Transaction (org.ethereum.core.Transaction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)10 ArrayList (java.util.ArrayList)10 Program (org.ethereum.vm.program.Program)10 Block (org.ethereum.core.Block)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 RskSystemProperties (co.rsk.config.RskSystemProperties)8 IOException (java.io.IOException)8 List (java.util.List)8