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