Search in sources :

Example 1 with DataWord

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

the class BridgeStorageProviderTest method getNewFederation.

@Test
public void getNewFederation() throws IOException {
    List<Integer> calls = new ArrayList<>();
    Context contextMock = mock(Context.class);
    Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
    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 get 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("newFederation".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(Context.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgumentAt(0, byte[].class);
        Context btcContext = invocation.getArgumentAt(1, Context.class);
        // Make sure we're deserializing what just came from the repo with the correct BTC context
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        Assert.assertEquals(contextMock, btcContext);
        return newFederation;
    });
    Assert.assertEquals(newFederation, storageProvider.getNewFederation());
    Assert.assertEquals(newFederation, storageProvider.getNewFederation());
    // 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 2 with DataWord

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

the class BridgeStorageProviderTest method getFederationElection_nonNullBytes.

@Test
public void getFederationElection_nonNullBytes() 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 new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeElection(any(byte[].class), any(AddressBasedAuthorizer.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgumentAt(0, byte[].class);
        AddressBasedAuthorizer authorizer = invocation.getArgumentAt(1, AddressBasedAuthorizer.class);
        // Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        Assert.assertEquals(authorizerMock, authorizer);
        return electionMock;
    });
    Assert.assertSame(electionMock, storageProvider.getFederationElection(authorizerMock));
    // 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 3 with DataWord

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

the class BridgeStorageProviderTest method saveNewFederation.

@Test
public void saveNewFederation() throws IOException {
    Federation newFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(BigInteger.valueOf(100)) }), Instant.ofEpochMilli(1000), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    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.serializeFederation(any(Federation.class))).then((InvocationOnMock invocation) -> {
        Federation federation = invocation.getArgumentAt(0, Federation.class);
        Assert.assertEquals(newFederation, federation);
        serializeCalls.add(0);
        return new byte[] { (byte) 0xbb };
    });
    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("newFederation".getBytes(StandardCharsets.UTF_8)), address);
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xbb }, data));
        return null;
    }).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
    storageProvider.saveNewFederation();
    // Shouldn't have tried to save nor serialize anything
    Assert.assertEquals(0, storageBytesCalls.size());
    Assert.assertEquals(0, serializeCalls.size());
    storageProvider.setNewFederation(newFederation);
    storageProvider.saveNewFederation();
    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 4 with DataWord

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

the class BridgeStorageProviderTest method saveLockWhitelist.

@Test
public void saveLockWhitelist() throws IOException {
    LockWhitelist whitelistMock = mock(LockWhitelist.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.serializeLockWhitelist(any(LockWhitelist.class))).then((InvocationOnMock invocation) -> {
        LockWhitelist whitelist = invocation.getArgumentAt(0, LockWhitelist.class);
        Assert.assertSame(whitelistMock, whitelist);
        serializeCalls.add(0);
        return Hex.decode("ccdd");
    });
    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(Hex.decode("aabbccdd"), contractAddress.getBytes()));
        Assert.assertEquals(new DataWord("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        Assert.assertTrue(Arrays.equals(Hex.decode("ccdd"), data));
        return null;
    }).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), 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(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 5 with DataWord

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

the class BridgeStorageProviderTest method getLockWhitelist_nullBytes.

@Test
public void getLockWhitelist_nullBytes() throws IOException {
    List<Integer> calls = new ArrayList<>();
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Context contextMock = mock(Context.class);
    when(contextMock.getParams()).thenReturn(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
    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("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        return null;
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        return null;
    });
    LockWhitelist result = storageProvider.getLockWhitelist();
    Assert.assertNotNull(result);
    Assert.assertEquals(0, result.getSize().intValue());
    // 1 for each call to deserializeFederation & getStorageBytes
    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