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());
}
}
}
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;
}
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;
}
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;
}
Aggregations