Search in sources :

Example 21 with ContractDetails

use of org.ethereum.db.ContractDetails in project rskj by rsksmart.

the class Program method fullTrace.

public void fullTrace() {
    if (logger.isTraceEnabled() || listener != null) {
        StringBuilder stackData = new StringBuilder();
        for (int i = 0; i < stack.size(); ++i) {
            stackData.append(" ").append(stack.get(i));
            if (i < stack.size() - 1) {
                stackData.append("\n");
            }
        }
        if (stackData.length() > 0) {
            stackData.insert(0, "\n");
        }
        ContractDetails contractDetails = getStorage().getContractDetails(new RskAddress(getOwnerAddress()));
        StringBuilder storageData = new StringBuilder();
        if (contractDetails != null) {
            List<DataWord> storageKeys = new ArrayList<>(contractDetails.getStorage().keySet());
            Collections.sort(storageKeys);
            for (DataWord key : storageKeys) {
                storageData.append(" ").append(key).append(" -> ").append(contractDetails.getStorage().get(key)).append("\n");
            }
            if (storageData.length() > 0) {
                storageData.insert(0, "\n");
            }
        }
        StringBuilder memoryData = new StringBuilder();
        StringBuilder oneLine = new StringBuilder();
        if (memory.size() > 320) {
            memoryData.append("... Memory Folded.... ").append("(").append(memory.size()).append(") bytes");
        } else {
            for (int i = 0; i < memory.size(); ++i) {
                byte value = memory.readByte(i);
                oneLine.append(ByteUtil.oneByteToHexString(value)).append(" ");
                if ((i + 1) % 16 == 0) {
                    String tmp = format("[%4s]-[%4s]", Integer.toString(i - 15, 16), Integer.toString(i, 16)).replace(" ", "0");
                    memoryData.append("").append(tmp).append(" ");
                    memoryData.append(oneLine);
                    if (i < memory.size()) {
                        memoryData.append("\n");
                    }
                    oneLine.setLength(0);
                }
            }
        }
        if (memoryData.length() > 0) {
            memoryData.insert(0, "\n");
        }
        StringBuilder opsString = new StringBuilder();
        for (int i = 0; i < ops.length; ++i) {
            String tmpString = Integer.toString(ops[i] & 0xFF, 16);
            tmpString = tmpString.length() == 1 ? "0" + tmpString : tmpString;
            if (i != pc) {
                opsString.append(tmpString);
            } else {
                opsString.append(" >>").append(tmpString).append("");
            }
        }
        if (pc >= ops.length) {
            opsString.append(" >>");
        }
        if (opsString.length() > 0) {
            opsString.insert(0, "\n ");
        }
        logger.trace(" -- OPS --     {}", opsString);
        logger.trace(" -- STACK --   {}", stackData);
        logger.trace(" -- MEMORY --  {}", memoryData);
        logger.trace(" -- STORAGE -- {}\n", storageData);
        logger.trace("\n  Spent Gas: [{}]/[{}]\n  Left Gas:  [{}]\n", getResult().getGasUsed(), invoke.getGas(), getRemainingGas());
        StringBuilder globalOutput = new StringBuilder("\n");
        if (stackData.length() > 0) {
            stackData.append("\n");
        }
        if (pc != 0) {
            globalOutput.append("[Op: ").append(OpCode.code(lastOp).name()).append("]\n");
        }
        globalOutput.append(" -- OPS --     ").append(opsString).append("\n");
        globalOutput.append(" -- STACK --   ").append(stackData).append("\n");
        globalOutput.append(" -- MEMORY --  ").append(memoryData).append("\n");
        globalOutput.append(" -- STORAGE -- ").append(storageData).append("\n");
        if (getResult().getHReturn() != null) {
            globalOutput.append("\n  HReturn: ").append(Hex.toHexString(getResult().getHReturn()));
        }
        // sophisticated assumption that msg.data != codedata
        // means we are calling the contract not creating it
        byte[] txData = invoke.getDataCopy(DataWord.ZERO, getDataSize());
        if (!Arrays.equals(txData, ops)) {
            globalOutput.append("\n  msg.data: ").append(Hex.toHexString(txData));
        }
        globalOutput.append("\n\n  Spent Gas: ").append(getResult().getGasUsed());
        if (listener != null) {
            listener.output(globalOutput.toString());
        }
    }
}
Also used : RskAddress(co.rsk.core.RskAddress) ContractDetails(org.ethereum.db.ContractDetails)

Example 22 with ContractDetails

use of org.ethereum.db.ContractDetails in project rskj by rsksmart.

the class RepositoryBuilder method build.

public static Repository build(Map<String, AccountTck> accounts) {
    HashMap<RskAddress, AccountState> stateBatch = new HashMap<>();
    HashMap<RskAddress, ContractDetails> detailsBatch = new HashMap<>();
    for (String address : accounts.keySet()) {
        RskAddress addr = new RskAddress(address);
        AccountTck accountTCK = accounts.get(address);
        AccountBuilder.StateWrap stateWrap = AccountBuilder.build(accountTCK);
        AccountState state = stateWrap.getAccountState();
        ContractDetails details = stateWrap.getContractDetails();
        stateBatch.put(addr, state);
        ContractDetailsCacheImpl detailsCache = new ContractDetailsCacheImpl(details);
        detailsCache.setDirty(true);
        detailsBatch.put(addr, detailsCache);
    }
    RepositoryImpl repositoryDummy = new RepositoryImpl(new RskSystemProperties(), new TrieStoreImpl(new HashMapDB()));
    Repository track = repositoryDummy.startTracking();
    track.updateBatch(stateBatch, detailsBatch);
    track.commit();
    return repositoryDummy;
}
Also used : AccountTck(org.ethereum.jsontestsuite.model.AccountTck) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) HashMap(java.util.HashMap) AccountState(org.ethereum.core.AccountState) HashMapDB(org.ethereum.datasource.HashMapDB) ContractDetails(org.ethereum.db.ContractDetails) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) ContractDetailsCacheImpl(org.ethereum.db.ContractDetailsCacheImpl) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 23 with ContractDetails

use of org.ethereum.db.ContractDetails in project rskj by rsksmart.

the class RepositoryValidator method valid.

public static List<String> valid(Repository currentRepository, Repository postRepository, boolean validateRootHash) {
    List<String> results = new ArrayList<>();
    Set<RskAddress> currentKeys = currentRepository.getAccountsKeys();
    Set<RskAddress> expectedKeys = postRepository.getAccountsKeys();
    if (expectedKeys.size() != currentKeys.size()) {
        String out = String.format("The size of the repository is invalid \n expected: %d, \n current: %d", expectedKeys.size(), currentKeys.size());
        results.add(out);
    }
    for (RskAddress addr : currentKeys) {
        AccountState state = currentRepository.getAccountState(addr);
        ContractDetails details = currentRepository.getContractDetails(addr);
        AccountState postState = postRepository.getAccountState(addr);
        ContractDetails postDetails = postRepository.getContractDetails(addr);
        List<String> accountResult = AccountValidator.valid(addr, postState, postDetails, state, details);
        results.addAll(accountResult);
    }
    Set<RskAddress> expectedButAbsent = ByteUtil.difference(expectedKeys, currentKeys);
    for (RskAddress addr : expectedButAbsent) {
        String formattedString = String.format("Account: %s: expected but doesn't exist", addr);
        results.add(formattedString);
    }
    // Compare roots
    String postRoot = Hex.toHexString(postRepository.getRoot());
    String currRoot = Hex.toHexString(currentRepository.getRoot());
    if (validateRootHash && !postRoot.equals(currRoot)) {
        String formattedString = String.format("Root hash doesn't match: expected: %s current: %s", postRoot, currRoot);
        results.add(formattedString);
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) RskAddress(co.rsk.core.RskAddress) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails)

Example 24 with ContractDetails

use of org.ethereum.db.ContractDetails in project rskj by rsksmart.

the class NetworkStateExporter method createAccountNode.

private ObjectNode createAccountNode(ObjectNode mainNode, RskAddress addr, Repository frozenRepository) {
    ObjectNode accountNode = mainNode.objectNode();
    AccountState accountState = frozenRepository.getAccountState(addr);
    Coin balance = accountState.getBalance();
    accountNode.put("balance", balance.asBigInteger().toString());
    BigInteger nonce = accountState.getNonce();
    accountNode.put("nonce", nonce.toString());
    ContractDetails contractDetails = frozenRepository.getContractDetails(addr);
    if (!contractDetails.isNullObject() && !PrecompiledContracts.REMASC_ADDR.equals(addr)) {
        accountNode.set("contract", createContractNode(contractDetails, accountNode));
    }
    return accountNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BigInteger(java.math.BigInteger) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails)

Aggregations

ContractDetails (org.ethereum.db.ContractDetails)24 Test (org.junit.Test)13 DataWord (org.ethereum.vm.DataWord)9 Repository (org.ethereum.core.Repository)8 RskAddress (co.rsk.core.RskAddress)7 AccountState (org.ethereum.core.AccountState)7 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)6 HashMapDB (org.ethereum.datasource.HashMapDB)6 ProgramResult (org.ethereum.vm.program.ProgramResult)5 TestContract (co.rsk.util.TestContract)4 BigInteger (java.math.BigInteger)4 Block (org.ethereum.core.Block)4 CallTransaction (org.ethereum.core.CallTransaction)4 Coin (co.rsk.core.Coin)3 TrieStore (co.rsk.trie.TrieStore)3 HashMap (java.util.HashMap)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 VmConfig (co.rsk.config.VmConfig)2 RepositoryImpl (co.rsk.db.RepositoryImpl)2 ArrayList (java.util.ArrayList)2