Search in sources :

Example 1 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeSerializationUtilsTest method mockAddress.

private RskAddress mockAddress(String addr) {
    RskAddress mock = PowerMockito.mock(RskAddress.class);
    Mockito.when(mock.getBytes()).thenReturn(Hex.decode(addr));
    return mock;
}
Also used : RskAddress(co.rsk.core.RskAddress)

Example 2 with RskAddress

use of co.rsk.core.RskAddress 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 3 with RskAddress

use of co.rsk.core.RskAddress 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 4 with RskAddress

use of co.rsk.core.RskAddress 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 5 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeStorageProviderTest method mockAddress.

private RskAddress mockAddress(String addr) {
    RskAddress mock = PowerMockito.mock(RskAddress.class);
    when(mock.getBytes()).thenReturn(Hex.decode(addr));
    return mock;
}
Also used : RskAddress(co.rsk.core.RskAddress)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8