Search in sources :

Example 81 with DataWord

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

the class BridgeStorageProviderTest method getLockWhitelist_nonNullBytes.

@Test
public void getLockWhitelist_nonNullBytes() throws IOException {
    List<Integer> calls = new ArrayList<>();
    LockWhitelist whitelistMock = mock(LockWhitelist.class);
    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 new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(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 with the correct AddressBasedAuthorizer
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        return whitelistMock;
    });
    Assert.assertSame(whitelistMock, storageProvider.getLockWhitelist());
    // 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 82 with DataWord

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

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1WrongData.

@Test
public void samplePrecompiledContractMethod1WrongData() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'bytes'}, \n" + "               {'name':'param2','type':'int'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    assertNull(result);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 83 with DataWord

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

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1Ok.

@Test
public void samplePrecompiledContractMethod1Ok() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'bytes'}, \n" + "               {'name':'param2','type':'int'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] bytes = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
    byte[] data = function.encode(111, bytes, 222);
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    Object[] results = function.decodeResult(result);
    assertEquals(new BigInteger("1"), results[0]);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 84 with DataWord

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

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1LargeData.

@Test
public void samplePrecompiledContractMethod1LargeData() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'string'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode(111, StringUtils.leftPad("foobar", 1000000, '*'));
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    Object[] results = function.decodeResult(result);
    assertEquals(new BigInteger("1"), results[0]);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 85 with DataWord

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

the class SamplePrecompiledContractTest method GetBalance.

private int GetBalance(Repository repository) {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[], \n" + "    'name':'GetBalance', \n" + "   'outputs':[{'name':'balance','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode();
    contract.init(null, null, repository, null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    Object[] results = function.decodeResult(result);
    return ((BigInteger) results[0]).intValue();
}
Also used : LogInfo(org.ethereum.vm.LogInfo) CallTransaction(org.ethereum.core.CallTransaction) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord)

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