use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class SamplePrecompiledContractTest method samplePrecompiledContractAddBalanceOk.
@Test
public void samplePrecompiledContractAddBalanceOk() {
DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
String funcJson = "{\n" + " 'constant':false, \n" + " 'inputs':[], \n" + " 'name':'AddBalance', \n" + " 'outputs':[], \n" + " 'type':'function' \n" + "}\n";
funcJson = funcJson.replaceAll("'", "\"");
CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
byte[] data = function.encode();
Repository repository = new RepositoryImpl(config);
contract.init(null, null, repository, null, null, new ArrayList<LogInfo>());
contract.execute(data);
int balance = this.GetBalance(repository);
assertEquals(50000, balance);
}
use of org.ethereum.vm.DataWord 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.vm.DataWord in project rskj by rsksmart.
the class ContractDetailsCacheImpl method getStorageHash.
@Override
public byte[] getStorageHash() {
// todo: unsupported
Trie storageTrie = new TrieImpl(null, true);
for (DataWord key : storage.keySet()) {
DataWord value = storage.get(key);
storageTrie = storageTrie.put(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
}
for (DataWord key : bytesStorage.keySet()) {
byte[] value = bytesStorage.get(key);
storageTrie = storageTrie.put(key.getData(), RLP.encodeElement(value));
}
return storageTrie.getHash().getBytes();
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class ContractDetailsCacheImpl method commit.
public void commit() {
if (origContract == null) {
return;
}
for (DataWord key : storage.keySet()) {
origContract.put(key, storage.get(key));
}
for (DataWord key : bytesStorage.keySet()) {
origContract.putBytes(key, bytesStorage.get(key));
}
origContract.setCode(code);
origContract.setDirty(this.dirty || origContract.isDirty());
}
use of org.ethereum.vm.DataWord in project rskj by rsksmart.
the class ContractDetailsImplTest method syncStorageWithExternalStorage.
@Test
public void syncStorageWithExternalStorage() {
TrieStore store = new TrieStoreImpl(new HashMapDB());
Trie trie = new TrieImpl(store, false);
byte[] accountAddress = randomAddress();
ContractDetailsImpl details = new ContractDetailsImpl(config, accountAddress, trie, null);
int nkeys = IN_MEMORY_STORAGE_LIMIT;
for (int k = 1; k <= nkeys + 1; k++) details.put(new DataWord(k), new DataWord(k * 2));
Assert.assertTrue(details.hasExternalStorage());
details.syncStorage();
int ssize = details.getStorageSize();
details = new ContractDetailsImpl(config, details.getEncoded());
Assert.assertEquals(ssize, details.getStorageSize());
for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details.get(new DataWord(k)));
ContractDetailsImpl clone = new ContractDetailsImpl(config, details.getEncoded());
Assert.assertNotNull(clone);
Assert.assertTrue(clone.hasExternalStorage());
Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(clone.get(new DataWord(k)));
for (int k = 1; k <= nkeys + 1; k++) clone.put(new DataWord(k), new DataWord(k * 3));
Assert.assertTrue(clone.hasExternalStorage());
Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
ContractDetailsImpl snapshot = (ContractDetailsImpl) clone.getSnapshotTo(clone.getStorageHash());
Assert.assertTrue(snapshot.hasExternalStorage());
Assert.assertEquals(clone.getStorageSize(), snapshot.getStorageSize());
}
Aggregations