Search in sources :

Example 16 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class RepositoryTrack method addBalance.

@Override
public Coin addBalance(RskAddress addr, Coin value) {
    synchronized (repository) {
        AccountState accountState = getAccountState(addr);
        if (accountState == null) {
            accountState = createAccount(addr);
        }
        getContractDetails(addr).setDirty(true);
        Coin newBalance = accountState.addToBalance(value);
        logger.trace("adding to balance addr: [{}], balance: [{}], delta: [{}]", addr, newBalance, value);
        return newBalance;
    }
}
Also used : Coin(co.rsk.core.Coin) AccountState(org.ethereum.core.AccountState)

Example 17 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class RepositoryTrack method getAccountState.

@Override
public AccountState getAccountState(RskAddress addr) {
    synchronized (repository) {
        AccountState accountState = cacheAccounts.get(addr);
        if (accountState == null) {
            repository.loadAccount(addr, cacheAccounts, cacheDetails);
            accountState = cacheAccounts.get(addr);
        }
        return accountState;
    }
}
Also used : AccountState(org.ethereum.core.AccountState)

Example 18 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class RepositoryTrack method increaseNonce.

@Override
public BigInteger increaseNonce(RskAddress addr) {
    synchronized (repository) {
        AccountState accountState = getAccountState(addr);
        if (accountState == null) {
            accountState = createAccount(addr);
        }
        getContractDetails(addr).setDirty(true);
        BigInteger saveNonce = accountState.getNonce();
        accountState.incrementNonce();
        logger.trace("increase nonce addr: [{}], from: [{}], to: [{}]", addr, saveNonce, accountState.getNonce());
        return accountState.getNonce();
    }
}
Also used : BigInteger(java.math.BigInteger) AccountState(org.ethereum.core.AccountState)

Example 19 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class RepositoryTrack method createAccount.

@Override
public AccountState createAccount(RskAddress addr) {
    synchronized (repository) {
        logger.trace("createAccount: [{}]", addr);
        AccountState accountState = new AccountState();
        cacheAccounts.put(addr, accountState);
        ContractDetails contractDetails = new ContractDetailsCacheImpl(null);
        contractDetails.setDirty(true);
        cacheDetails.put(addr, contractDetails);
        return accountState;
    }
}
Also used : AccountState(org.ethereum.core.AccountState)

Example 20 with AccountState

use of org.ethereum.core.AccountState in project rskj by rsksmart.

the class NetworkStateExporterTest method testContracts.

@Test
public void testContracts() throws Exception {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    String address1String = "1000000000000000000000000000000000000000";
    RskAddress addr1 = new RskAddress(address1String);
    repository.createAccount(addr1);
    repository.addBalance(addr1, Coin.valueOf(1L));
    repository.increaseNonce(addr1);
    ContractDetails contractDetails = new co.rsk.db.ContractDetailsImpl(config);
    contractDetails.setCode(new byte[] { 1, 2, 3, 4 });
    contractDetails.put(DataWord.ZERO, DataWord.ONE);
    contractDetails.putBytes(DataWord.ONE, new byte[] { 5, 6, 7, 8 });
    repository.updateContractDetails(addr1, contractDetails);
    AccountState accountState = repository.getAccountState(addr1);
    accountState.setStateRoot(contractDetails.getStorageHash());
    repository.updateAccountState(addr1, accountState);
    Map result = writeAndReadJson(repository);
    Assert.assertEquals(1, result.keySet().size());
    Map address1Value = (Map) result.get(address1String);
    Assert.assertEquals(3, address1Value.keySet().size());
    Assert.assertEquals("1", address1Value.get("balance"));
    Assert.assertEquals("1", address1Value.get("nonce"));
    Map contract = (Map) address1Value.get("contract");
    Assert.assertEquals(2, contract.keySet().size());
    Assert.assertEquals("01020304", contract.get("code"));
    Map data = (Map) contract.get("data");
    Assert.assertEquals(2, data.keySet().size());
    Assert.assertEquals("01", data.get(Hex.toHexString(DataWord.ZERO.getData())));
    Assert.assertEquals("05060708", data.get(Hex.toHexString(DataWord.ONE.getData())));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) AccountState(org.ethereum.core.AccountState) HashMapDB(org.ethereum.datasource.HashMapDB) HashMap(java.util.HashMap) Map(java.util.Map) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

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