Search in sources :

Example 36 with AccountState

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()));
}
Also used : AccountState(org.ethereum.core.AccountState) TrieImplHashTest(co.rsk.trie.TrieImplHashTest) Test(org.junit.Test)

Example 37 with AccountState

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;
}
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 38 with AccountState

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;
}
Also used : ArrayList(java.util.ArrayList) RskAddress(co.rsk.core.RskAddress) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails)

Example 39 with AccountState

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());
}
Also used : Program(org.ethereum.vm.program.Program) AccountState(org.ethereum.core.AccountState) Coin(co.rsk.core.Coin) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 40 with AccountState

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BigInteger(java.math.BigInteger) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails)

Aggregations

AccountState (org.ethereum.core.AccountState)46 Test (org.junit.Test)20 RskAddress (co.rsk.core.RskAddress)12 BigInteger (java.math.BigInteger)11 Coin (co.rsk.core.Coin)10 Transaction (org.ethereum.core.Transaction)10 Repository (org.ethereum.core.Repository)7 ContractDetails (org.ethereum.db.ContractDetails)7 Program (org.ethereum.vm.program.Program)5 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)5 Ignore (org.junit.Ignore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)3 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ECKey (org.ethereum.crypto.ECKey)3 RepositoryImpl (co.rsk.db.RepositoryImpl)2 TxsPerAccount (co.rsk.net.handler.TxsPerAccount)2 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)2 HashMapDB (org.ethereum.datasource.HashMapDB)2