Search in sources :

Example 1 with MutableRepository

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

the class RepositoryTest method setUp.

@Before
public void setUp() {
    trieStore = new TrieStoreImpl(new HashMapDB());
    mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
    repository = new MutableRepository(mutableTrie);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) MutableRepository(org.ethereum.db.MutableRepository) HashMapDB(org.ethereum.datasource.HashMapDB) Trie(co.rsk.trie.Trie) Before(org.junit.Before)

Example 2 with MutableRepository

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

the class RepositoryTest method testCode.

@Test
public void testCode() {
    Repository track = repository.startTracking();
    byte[] codeLongerThan32bytes = "this-is-code-because-I-say-it-man".getBytes();
    assertTrue(codeLongerThan32bytes.length > 32);
    track.saveCode(COW, codeLongerThan32bytes);
    byte[] returnedCode = track.getCode(COW);
    assertArrayEquals(codeLongerThan32bytes, returnedCode);
    track.commit();
    // Now try to get the size
    int codeSize = repository.getCodeLength(COW);
    assertEquals(codeLongerThan32bytes.length, codeSize);
    byte[] returnedCode2 = repository.getCode(COW);
    assertArrayEquals(codeLongerThan32bytes, returnedCode2);
    repository.save();
    byte[] prevRoot = repository.getRoot();
    // Use the same store
    // Now we'll create a new repository based on the same store and force
    // this new repository to read all nodes from the store. The results must
    // be the same: lazy evaluation of the value must work.
    Repository repository2 = new MutableRepository(trieStore, trieStore.retrieve(prevRoot).get());
    // Now try to get the size
    codeSize = repository2.getCodeLength(COW);
    assertEquals(codeLongerThan32bytes.length, codeSize);
    returnedCode2 = repository2.getCode(COW);
    assertArrayEquals(codeLongerThan32bytes, returnedCode2);
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Test(org.junit.Test)

Example 3 with MutableRepository

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

the class RepositoryTest method testMultiThread.

// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
    final DataWord cowKey1 = DataWord.valueFromHex("c1");
    final DataWord cowKey2 = DataWord.valueFromHex("c2");
    final byte[] cowVal0 = Hex.decode("c0a0");
    // track
    Repository track2 = repository.startTracking();
    track2.addStorageBytes(COW, cowKey2, cowVal0);
    track2.commit();
    trieStore.flush();
    assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
    final CountDownLatch failSema = new CountDownLatch(2);
    Repository snap = new MutableRepository(trieStore, trieStore.retrieve(repository.getRoot()).get());
    new Thread(() -> {
        try {
            int cnt = 1;
            while (true) {
                Repository snapTrack = snap.startTracking();
                byte[] vcnr = new byte[1];
                vcnr[0] = (byte) (cnt % 128);
                snapTrack.addStorageBytes(COW, cowKey1, vcnr);
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            failSema.countDown();
        }
    }).start();
    new Thread(() -> {
        int cnt = 1;
        try {
            while (true) {
                // track
                Repository track21 = repository.startTracking();
                byte[] cVal = new byte[1];
                cVal[0] = (byte) (cnt % 128);
                track21.addStorageBytes(COW, cowKey1, cVal);
                track21.commit();
                trieStore.flush();
                assertArrayEquals(cVal, repository.getStorageBytes(COW, cowKey1));
                assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            failSema.countDown();
        }
    }).start();
    failSema.await(10, TimeUnit.SECONDS);
    if (failSema.getCount() < 2) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) DataWord(org.ethereum.vm.DataWord) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with MutableRepository

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

the class RepositoryUpdateTest method putNullValueAsDeleteValue.

@Test
public void putNullValueAsDeleteValue() {
    ContractDetailsImpl details = buildContractDetails();
    details.putBytes(DataWord.ONE, new byte[] { 0x01, 0x02, 0x03 });
    details.putBytes(DataWord.ONE, null);
    Repository repo = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(null, new Trie())));
    updateContractDetails(repo, address, details);
    repo.commit();
    byte[] value = repo.getTrie().get(DataWord.ONE.getData());
    Assert.assertNull(value);
    Assert.assertEquals(0, details.getStorageSize());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 5 with MutableRepository

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

the class RepositoryUpdateTest method putDataWordWithoutLeadingZeroes.

@Test
public void putDataWordWithoutLeadingZeroes() {
    ContractDetailsImpl details = buildContractDetails();
    details.put(DataWord.ONE, DataWord.valueOf(42));
    Repository repo = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(null, new Trie())));
    updateContractDetails(repo, address, details);
    byte[] value = repo.getStorageBytes(address, DataWord.ONE);
    Assert.assertNotNull(value);
    Assert.assertEquals(1, value.length);
    Assert.assertEquals(42, value[0]);
    Assert.assertEquals(1, details.getStorageSize());
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

MutableRepository (org.ethereum.db.MutableRepository)40 Test (org.junit.Test)31 Trie (co.rsk.trie.Trie)26 RskAddress (co.rsk.core.RskAddress)21 Repository (org.ethereum.core.Repository)15 TrieStore (co.rsk.trie.TrieStore)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)12 HashMapDB (org.ethereum.datasource.HashMapDB)12 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 BigInteger (java.math.BigInteger)8 Coin (co.rsk.core.Coin)7 DataWord (org.ethereum.vm.DataWord)7 MutableTrieImpl (co.rsk.db.MutableTrieImpl)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)5 MutableTrie (co.rsk.trie.MutableTrie)4 ArrayList (java.util.ArrayList)4 Block (org.ethereum.core.Block)4 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)2 CountDownLatch (java.util.concurrent.CountDownLatch)2