Search in sources :

Example 86 with DataWord

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);
}
Also used : Repository(org.ethereum.core.Repository) LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 87 with DataWord

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;
}
Also used : ContractDetailsImpl(co.rsk.db.ContractDetailsImpl) DataWord(org.ethereum.vm.DataWord) ContractDetails(org.ethereum.db.ContractDetails)

Example 88 with DataWord

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();
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) DataWord(org.ethereum.vm.DataWord) Trie(co.rsk.trie.Trie)

Example 89 with DataWord

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());
}
Also used : DataWord(org.ethereum.vm.DataWord)

Example 90 with DataWord

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());
}
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) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

DataWord (org.ethereum.vm.DataWord)133 Test (org.junit.Test)88 Repository (org.ethereum.core.Repository)41 RskAddress (co.rsk.core.RskAddress)25 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)22 BigInteger (java.math.BigInteger)19 LogInfo (org.ethereum.vm.LogInfo)17 HashMapDB (org.ethereum.datasource.HashMapDB)14 Program (org.ethereum.vm.program.Program)13 Stack (org.ethereum.vm.program.Stack)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 RepositoryImpl (co.rsk.db.RepositoryImpl)9 ContractDetails (org.ethereum.db.ContractDetails)9 Trie (co.rsk.trie.Trie)8 CallTransaction (org.ethereum.core.CallTransaction)8 TrieImpl (co.rsk.trie.TrieImpl)7 TrieStore (co.rsk.trie.TrieStore)7 Coin (co.rsk.core.Coin)5