Search in sources :

Example 76 with DataWord

use of org.ethereum.vm.DataWord 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 DataWord

use of org.ethereum.vm.DataWord 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 DataWord

use of org.ethereum.vm.DataWord 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 79 with DataWord

use of org.ethereum.vm.DataWord 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)

Example 80 with DataWord

use of org.ethereum.vm.DataWord in project rskj by rsksmart.

the class BridgeStorageProviderTest method getFederationElection_nullBytes.

@Test
public void getFederationElection_nullBytes() throws IOException {
    List<Integer> calls = new ArrayList<>();
    AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
    ABICallElection electionMock = mock(ABICallElection.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("federationElection".getBytes(StandardCharsets.UTF_8)), address);
        return null;
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeElection(any(byte[].class), any(AddressBasedAuthorizer.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        return null;
    });
    ABICallElection result = storageProvider.getFederationElection(authorizerMock);
    Assert.assertSame(authorizerMock, Whitebox.getInternalState(result, "authorizer"));
    Assert.assertEquals(0, result.getVotes().size());
    // getStorageBytes is the only one called (can't be the other way around)
    Assert.assertEquals(1, 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)

Aggregations

DataWord (org.ethereum.vm.DataWord)133 Test (org.junit.Test)88 Repository (org.ethereum.core.Repository)41 RskAddress (co.rsk.core.RskAddress)25 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)22 BigInteger (java.math.BigInteger)19 LogInfo (org.ethereum.vm.LogInfo)17 HashMapDB (org.ethereum.datasource.HashMapDB)14 Program (org.ethereum.vm.program.Program)13 Stack (org.ethereum.vm.program.Stack)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 RepositoryImpl (co.rsk.db.RepositoryImpl)9 ContractDetails (org.ethereum.db.ContractDetails)9 Trie (co.rsk.trie.Trie)8 CallTransaction (org.ethereum.core.CallTransaction)8 TrieImpl (co.rsk.trie.TrieImpl)7 TrieStore (co.rsk.trie.TrieStore)7 Coin (co.rsk.core.Coin)5