use of org.ethereum.core.AccountState in project rskj by rsksmart.
the class RepositoryImplTest method createAccount.
@Test
public void createAccount() {
RepositoryImpl repository = new RepositoryImpl(config);
AccountState accState = repository.createAccount(randomAccountAddress());
Assert.assertNotNull(accState);
Assert.assertEquals(BigInteger.ZERO, accState.getNonce());
Assert.assertEquals(BigInteger.ZERO, accState.getBalance().asBigInteger());
Assert.assertFalse(Arrays.equals(emptyHash.getBytes(), repository.getRoot()));
}
use of org.ethereum.core.AccountState 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.core.AccountState 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.core.AccountState in project rskj by rsksmart.
the class VMComplexTest method test1.
// TODO #POC9
@Ignore
// contract call recursive
@Test
public void test1() {
/**
* #The code will run
* ------------------
*
* a = contract.storage[999]
* if a > 0:
* contract.storage[999] = a - 1
*
* # call to contract: 77045e71a7a2c50903d88e564cd72fab11e82051
* send((tx.gas / 10 * 8), 0x77045e71a7a2c50903d88e564cd72fab11e82051, 0)
* else:
* stop
*/
int expectedGas = 436;
DataWord key1 = new DataWord(999);
DataWord value1 = new DataWord(3);
// Set contract into Database
String callerAddr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826";
String contractAddr = "77045e71a7a2c50903d88e564cd72fab11e82051";
String code = "6103e75460005260006000511115630000004c576001600051036103e755600060006000600060007377045e71a7a2c50903d88e564cd72fab11e820516008600a5a0402f1630000004c00565b00";
RskAddress contractAddrB = new RskAddress(contractAddr);
RskAddress callerAddrB = new RskAddress(callerAddr);
byte[] codeB = Hex.decode(code);
byte[] codeKey = HashUtil.keccak256(codeB);
AccountState accountState = new AccountState();
accountState.setCodeHash(codeKey);
ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl();
pi.setOwnerAddress(contractAddrB);
Repository repository = pi.getRepository();
repository.createAccount(callerAddrB);
final BigInteger value = new BigInteger("100000000000000000000");
repository.addBalance(callerAddrB, new Coin(value));
repository.createAccount(contractAddrB);
repository.saveCode(contractAddrB, codeB);
repository.addStorageRow(contractAddrB, key1, value1);
// Play the program
VM vm = getSubject();
Program program = getProgram(codeB, pi);
try {
while (!program.isStopped()) vm.step(program);
} catch (RuntimeException e) {
program.setRuntimeFailure(e);
}
System.out.println();
System.out.println("============ Results ============");
Coin balance = repository.getBalance(callerAddrB);
System.out.println("*** Used gas: " + program.getResult().getGasUsed());
System.out.println("*** Contract Balance: " + balance);
// todo: assert caller balance after contract exec
repository.close();
assertEquals(expectedGas, program.getResult().getGasUsed());
}
use of org.ethereum.core.AccountState 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