Search in sources :

Example 1 with ContractDetails

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

the class ContractDetailsImplTest method getSnapshot.

@Test
public void getSnapshot() {
    ContractDetailsImpl details = new ContractDetailsImpl(config);
    byte[] initialRoot = details.getStorageHash();
    List<DataWord> keys = new ArrayList<>();
    keys.add(DataWord.ZERO);
    keys.add(DataWord.ONE);
    List<DataWord> values = new ArrayList<>();
    values.add(new DataWord(42));
    values.add(new DataWord(144));
    details.setStorage(keys, values);
    byte[] root = details.getStorageHash();
    List<DataWord> keys2 = new ArrayList<>();
    keys2.add(new DataWord(2));
    keys2.add(new DataWord(3));
    List<DataWord> values2 = new ArrayList<>();
    values2.add(new DataWord(1));
    values2.add(new DataWord(2));
    details.setStorage(keys2, values2);
    ContractDetails result = details.getSnapshotTo(root);
    Assert.assertEquals(new DataWord(42), result.get(DataWord.ZERO));
    Assert.assertEquals(new DataWord(144), result.get(DataWord.ONE));
    Assert.assertEquals(null, result.get(new DataWord(2)));
    Assert.assertEquals(null, result.get(new DataWord(3)));
    ContractDetails result2 = details.getSnapshotTo(initialRoot);
    Assert.assertEquals(null, result2.get(DataWord.ZERO));
    Assert.assertEquals(null, result2.get(DataWord.ONE));
    Assert.assertEquals(null, result2.get(new DataWord(2)));
    Assert.assertEquals(null, result2.get(new DataWord(3)));
}
Also used : DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 2 with ContractDetails

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

the class ContractDetailsImplTest method externalStorageTransition.

@Test
public void externalStorageTransition() {
    byte[] address = randomAddress();
    byte[] code = randomBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    HashMapDB externalStorage = new HashMapDB();
    ContractDetailsImpl original = new ContractDetailsImpl(config, address, new TrieImpl(new TrieStoreImpl(externalStorage), true), code);
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT - 1; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    ContractDetails deserialized = new ContractDetailsImpl(config, original.getEncoded());
    // adds keys for in-memory storage limit overflow
    for (int i = 0; i < 10; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        deserialized.put(key, value);
    }
    deserialized.syncStorage();
    deserialized = new ContractDetailsImpl(config, deserialized.getEncoded());
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    Assert.assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        Assert.assertEquals(elements.get(key), storage.get(key));
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) TrieImpl(co.rsk.trie.TrieImpl) DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) HashMapDB(org.ethereum.datasource.HashMapDB) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 3 with ContractDetails

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

the class RepositoryImplForTesting method addStorageBytes.

@Override
public synchronized void addStorageBytes(RskAddress addr, DataWord key, byte[] value) {
    super.addStorageBytes(addr, key, value);
    AccountState accountState = getAccountState(addr);
    ContractDetails details = getDetailsDataStore().get(addr);
    accountState.setStateRoot(details.getStorageHash());
    updateAccountState(addr, accountState);
}
Also used : AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails)

Example 4 with ContractDetails

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

the class ContractDetailsMapper method mapFromContract.

public ContractDetails mapFromContract(Contract contract) {
    ContractDetails contractDetails;
    contractDetails = new ContractDetailsImpl(config);
    if (contract.getCode() != null) {
        contractDetails.setCode(Hex.decode(contract.getCode()));
    }
    for (String key : contract.getData().keySet()) {
        String value = contract.getData().get(key);
        contractDetails.putBytes(new DataWord(Hex.decode(key)), Hex.decode(value));
    }
    return contractDetails;
}
Also used : ContractDetailsImpl(co.rsk.db.ContractDetailsImpl) DataWord(org.ethereum.vm.DataWord) ContractDetails(org.ethereum.db.ContractDetails)

Example 5 with ContractDetails

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

the class GenesisLoader method generatePreMine.

private static Map<RskAddress, InitialAddressState> generatePreMine(RskSystemProperties config, BigInteger initialNonce, Map<String, AllocatedAccount> alloc) {
    Map<RskAddress, InitialAddressState> premine = new HashMap<>();
    ContractDetailsMapper detailsMapper = new ContractDetailsMapper(config);
    for (Map.Entry<String, AllocatedAccount> accountEntry : alloc.entrySet()) {
        if (!StringUtils.equals("00", accountEntry.getKey())) {
            Coin balance = new Coin(new BigInteger(accountEntry.getValue().getBalance()));
            BigInteger nonce;
            if (accountEntry.getValue().getNonce() != null) {
                nonce = new BigInteger(accountEntry.getValue().getNonce());
            } else {
                nonce = initialNonce;
            }
            AccountState acctState = new AccountState(nonce, balance);
            ContractDetails contractDetails = null;
            Contract contract = accountEntry.getValue().getContract();
            if (contract != null) {
                contractDetails = detailsMapper.mapFromContract(contract);
                if (contractDetails.getCode() != null) {
                    acctState.setCodeHash(Keccak256Helper.keccak256(contractDetails.getCode()));
                }
                acctState.setStateRoot(contractDetails.getStorageHash());
            }
            premine.put(new RskAddress(accountEntry.getKey()), new InitialAddressState(acctState, contractDetails));
        }
    }
    return premine;
}
Also used : HashMap(java.util.HashMap) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails) Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ContractDetails (org.ethereum.db.ContractDetails)11 AccountState (org.ethereum.core.AccountState)6 DataWord (org.ethereum.vm.DataWord)5 Test (org.junit.Test)4 RskAddress (co.rsk.core.RskAddress)3 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 Repository (org.ethereum.core.Repository)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 Coin (co.rsk.core.Coin)1 ContractDetailsImpl (co.rsk.db.ContractDetailsImpl)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 TrieImpl (co.rsk.trie.TrieImpl)1 TrieStore (co.rsk.trie.TrieStore)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1