Search in sources :

Example 11 with DataWord

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

the class AccountValidator method valid.

public static List<String> valid(RskAddress addr, AccountState expectedState, ContractDetails expectedDetails, AccountState currentState, ContractDetails currentDetails) {
    List<String> results = new ArrayList<>();
    if (currentState == null || currentDetails == null) {
        String formattedString = String.format("Account: %s: expected but doesn't exist", addr);
        results.add(formattedString);
        return results;
    }
    if (expectedState == null || expectedDetails == null) {
        String formattedString = String.format("Account: %s: unexpected account in the repository", addr);
        results.add(formattedString);
        return results;
    }
    Coin expectedBalance = expectedState.getBalance();
    if (!currentState.getBalance().equals(expectedBalance)) {
        String formattedString = String.format("Account: %s: has unexpected balance, expected balance: %s found balance: %s", addr, expectedBalance.toString(), currentState.getBalance().toString());
        results.add(formattedString);
    }
    BigInteger expectedNonce = expectedState.getNonce();
    if (currentState.getNonce().compareTo(expectedNonce) != 0) {
        String formattedString = String.format("Account: %s: has unexpected nonce, expected nonce: %s found nonce: %s", addr, expectedNonce.toString(), currentState.getNonce().toString());
        results.add(formattedString);
    }
    byte[] code = Arrays.equals(currentState.getCodeHash(), EMPTY_DATA_HASH) ? new byte[0] : currentDetails.getCode();
    if (!Arrays.equals(expectedDetails.getCode(), code)) {
        String formattedString = String.format("Account: %s: has unexpected code, expected code: %s found code: %s", addr, Hex.toHexString(expectedDetails.getCode()), Hex.toHexString(currentDetails.getCode()));
        results.add(formattedString);
    }
    // compare storage
    Set<DataWord> currentKeys = currentDetails.getStorage().keySet();
    Set<DataWord> expectedKeys = expectedDetails.getStorage().keySet();
    Set<DataWord> checked = new HashSet<>();
    for (DataWord key : currentKeys) {
        DataWord currentValue = currentDetails.getStorage().get(key);
        DataWord expectedValue = expectedDetails.getStorage().get(key);
        if (expectedValue == null) {
            String formattedString = String.format("Account: %s: has unexpected storage data: %s = %s", addr, key, currentValue);
            results.add(formattedString);
            continue;
        }
        if (!expectedValue.equals(currentValue)) {
            String formattedString = String.format("Account: %s: has unexpected value, for key: %s , expectedValue: %s real value: %s", addr, key.toString(), expectedValue.toString(), currentValue.toString());
            results.add(formattedString);
            continue;
        }
        checked.add(key);
    }
    for (DataWord key : expectedKeys) {
        if (!checked.contains(key)) {
            String formattedString = String.format("Account: %s: doesn't exist expected storage key: %s", addr, key.toString());
            results.add(formattedString);
        }
    }
    return results;
}
Also used : Coin(co.rsk.core.Coin) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord)

Example 12 with DataWord

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

the class LogsValidator method valid.

public static List<String> valid(List<LogInfo> origLogs, List<LogInfo> postLogs) {
    List<String> results = new ArrayList<>();
    int i = 0;
    for (LogInfo postLog : postLogs) {
        if (origLogs == null || origLogs.size() - 1 < i) {
            String formattedString = String.format("Log: %s: was expected but doesn't exist: address: %s", i, Hex.toHexString(postLog.getAddress()));
            results.add(formattedString);
            continue;
        }
        LogInfo realLog = origLogs.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 13 with DataWord

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

the class GasLimitRuleTests method getBlock.

private static Block getBlock(long gasLimitValue) {
    byte[] gasLimit = new DataWord(gasLimitValue).getData();
    BlockHeader header = new BlockHeader(null, null, RskAddress.nullAddress().getBytes(), null, BlockDifficulty.ZERO.getBytes(), 0, gasLimit, 0, 0, null, null, 0);
    return new Block(header);
}
Also used : Block(org.ethereum.core.Block) DataWord(org.ethereum.vm.DataWord) BlockHeader(org.ethereum.core.BlockHeader)

Example 14 with DataWord

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

the class VMExecutionTest method swapnSecondItem.

@Test
public void swapnSecondItem() {
    Program program = executeCode("PUSH1 0x01 PUSH1 0x02 PUSH1 0x00 SWAPN", 4);
    Stack stack = program.getStack();
    Assert.assertEquals(2, stack.size());
    Assert.assertEquals(new DataWord(1), stack.peek());
    Assert.assertEquals(new DataWord(2), stack.get(0));
}
Also used : Program(org.ethereum.vm.program.Program) DataWord(org.ethereum.vm.DataWord) Stack(org.ethereum.vm.program.Stack) Test(org.junit.Test)

Example 15 with DataWord

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

the class VMExecutionTest method testMul.

@Test
public void testMul() {
    Program program = executeCode("PUSH1 3 PUSH1 2 MUL", 3);
    Stack stack = program.getStack();
    Assert.assertEquals(1, stack.size());
    Assert.assertEquals(new DataWord(6), stack.peek());
}
Also used : Program(org.ethereum.vm.program.Program) DataWord(org.ethereum.vm.DataWord) Stack(org.ethereum.vm.program.Stack) 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