Search in sources :

Example 6 with DataWord

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

the class SamplePrecompiledContractTest method GetResult.

private int GetResult(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':'GetResult', \n" + "   'outputs':[{'name':'result','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)

Example 7 with DataWord

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

the class SamplePrecompiledContractTest method samplePrecompiledContractIncrementResultOk.

@Test
public void samplePrecompiledContractIncrementResultOk() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[], \n" + "    'name':'IncrementResult', \n" + "   'outputs':[], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    contract.init(null, null, track, null, null, new ArrayList<LogInfo>());
    contract.execute(data);
    track.commit();
    int result = this.GetResult(repository);
    assertEquals(1, result);
}
Also used : Repository(org.ethereum.core.Repository) 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 8 with DataWord

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

the class SamplePrecompiledContractTest method samplePrecompiledContractMethodDoesNotExist.

@Test
public void samplePrecompiledContractMethodDoesNotExist() {
    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':'UnexistentMethod', \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);
    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 9 with DataWord

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

the class Logs method compareToReal.

public List<String> compareToReal(List<LogInfo> logs) {
    List<String> results = new ArrayList<>();
    int i = 0;
    for (LogInfo postLog : this.logs) {
        LogInfo realLog = logs.get(i);
        String postAddress = Hex.toHexString(postLog.getAddress());
        String realAddress = Hex.toHexString(realLog.getAddress());
        if (!postAddress.equals(realAddress)) {
            String formattedString = String.format("Log: %s: has unexpected address, expected address: %s found address: %s", i, postAddress, realAddress);
            results.add(formattedString);
        }
        String postData = Hex.toHexString(postLog.getData());
        String realData = Hex.toHexString(realLog.getData());
        if (!postData.equals(realData)) {
            String formattedString = String.format("Log: %s: has unexpected data, expected data: %s found data: %s", i, postData, realData);
            results.add(formattedString);
        }
        String postBloom = Hex.toHexString(postLog.getBloom().getData());
        String realBloom = Hex.toHexString(realLog.getBloom().getData());
        if (!postData.equals(realData)) {
            String formattedString = String.format("Log: %s: has unexpected bloom, expected bloom: %s found bloom: %s", i, postBloom, realBloom);
            results.add(formattedString);
        }
        List<DataWord> postTopics = postLog.getTopics();
        List<DataWord> realTopics = realLog.getTopics();
        int j = 0;
        for (DataWord postTopic : postTopics) {
            DataWord realTopic = realTopics.get(j);
            if (!postTopic.equals(realTopic)) {
                String formattedString = String.format("Log: %s: has unexpected topic: %s, expected topic: %s found topic: %s", i, j, postTopic, realTopic);
                results.add(formattedString);
            }
            ++j;
        }
        ++i;
    }
    return results;
}
Also used : LogInfo(org.ethereum.vm.LogInfo) ArrayList(java.util.ArrayList) DataWord(org.ethereum.vm.DataWord)

Example 10 with DataWord

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

the class TestRunner method loadRepository.

public Repository loadRepository(Repository track, Map<RskAddress, AccountState> pre) {
    /* 1. Store pre-exist accounts - Pre */
    for (RskAddress addr : pre.keySet()) {
        AccountState accountState = pre.get(addr);
        track.addBalance(addr, accountState.getBalance());
        ((RepositoryTrack) track).setNonce(addr, new BigInteger(1, accountState.getNonce()));
        track.saveCode(addr, accountState.getCode());
        for (DataWord storageKey : accountState.getStorage().keySet()) {
            track.addStorageRow(addr, storageKey, accountState.getStorage().get(storageKey));
        }
    }
    return track;
}
Also used : RskAddress(co.rsk.core.RskAddress) 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