Search in sources :

Example 26 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class RepositoryImplTest method flushNoReconnect.

@Test
public void flushNoReconnect() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    RepositoryImpl repository = new RepositoryImpl(config, store);
    RskAddress accAddress = randomAccountAddress();
    byte[] initialRoot = repository.getRoot();
    repository.createAccount(accAddress);
    repository.flushNoReconnect();
    Assert.assertTrue(repository.isExist(accAddress));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RskAddress(co.rsk.core.RskAddress) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TrieImplHashTest(co.rsk.trie.TrieImplHashTest) Test(org.junit.Test)

Example 27 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class RepositoryTest method testMultiThread.

// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    final Repository repository = new RepositoryImpl(config, store);
    final byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    final DataWord cowKey1 = new DataWord("c1");
    final DataWord cowKey2 = new DataWord("c2");
    final byte[] cowVal0 = Hex.decode("c0a0");
    // track
    Repository track2 = repository.startTracking();
    track2.addStorageBytes(COW, cowKey2, cowVal0);
    track2.commit();
    repository.flush();
    ContractDetails cowDetails = repository.getContractDetails(COW);
    assertArrayEquals(cowVal0, cowDetails.getBytes(cowKey2));
    final CountDownLatch failSema = new CountDownLatch(1);
    new Thread(() -> {
        try {
            int cnt = 1;
            while (true) {
                // To Review, not needed?
                repository.flush();
                Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
                byte[] vcnr = new byte[1];
                vcnr[0] = (byte) (cnt % 128);
                snap.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();
                repository.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() == 0) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) CountDownLatch(java.util.concurrent.CountDownLatch) TrieStore(co.rsk.trie.TrieStore) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 28 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class RepositoryTest method test16.

@Test
public void test16() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
    byte[] cowKey1 = "key-c-1".getBytes();
    byte[] cowValue1 = "val-c-1".getBytes();
    byte[] horseKey1 = "key-h-1".getBytes();
    byte[] horseValue1 = "val-h-1".getBytes();
    byte[] cowKey2 = "key-c-2".getBytes();
    byte[] cowValue2 = "val-c-2".getBytes();
    byte[] horseKey2 = "key-h-2".getBytes();
    byte[] horseValue2 = "val-h-2".getBytes();
    // changes level_1
    Repository track1 = repository.startTracking();
    track1.addStorageBytes(COW, new DataWord(cowKey1), cowValue1);
    track1.addStorageBytes(HORSE, new DataWord(horseKey1), horseValue1);
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
    track2.addStorageBytes(HORSE, new DataWord(horseKey2), horseValue2);
    assertArrayEquals(cowValue1, track2.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track2.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, track2.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, track2.getStorageBytes(HORSE, new DataWord(horseKey2)));
    track2.commit();
    // leaving level_2
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, track1.getStorageBytes(HORSE, new DataWord(horseKey2)));
    track1.commit();
    // leaving level_1
    assertArrayEquals(cowValue1, repository.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, repository.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, repository.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, repository.getStorageBytes(HORSE, new DataWord(horseKey2)));
    repository.close();
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 29 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class Web3ImplTest method eth_sendTransaction.

@Test
public void eth_sendTransaction() {
    BigInteger nonce = BigInteger.ONE;
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Web3Impl web3 = createWeb3(world, receiptStore);
    // **** Initializes data ******************
    String addr1 = web3.personal_newAccountWithSeed("sampleSeed1");
    String addr2 = web3.personal_newAccountWithSeed("sampleSeed2");
    String toAddress = addr2;
    BigInteger value = BigInteger.valueOf(7);
    BigInteger gasPrice = BigInteger.valueOf(8);
    BigInteger gasLimit = BigInteger.valueOf(9);
    String data = "0xff";
    // ***** Executes the transaction *******************
    Web3.CallArguments args = new Web3.CallArguments();
    args.from = addr1;
    args.to = addr2;
    args.data = data;
    args.gas = TypeConverter.toJsonHex(gasLimit);
    args.gasPrice = TypeConverter.toJsonHex(gasPrice);
    args.value = value.toString();
    args.nonce = nonce.toString();
    String txHash = null;
    try {
        txHash = web3.eth_sendTransaction(args);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // ***** Verifies tx hash
    Transaction tx = Transaction.create(config, toAddress.substring(2), value, nonce, gasPrice, gasLimit, args.data);
    tx.sign(wallet.getAccount(new RskAddress(addr1)).getEcKey().getPrivKeyBytes());
    String expectedHash = tx.getHash().toJsonString();
    Assert.assertTrue("Method is not creating the expected transaction", expectedHash.compareTo(txHash) == 0);
}
Also used : HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) JsonRpcInvalidParamException(org.ethereum.rpc.exception.JsonRpcInvalidParamException) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) BigInteger(java.math.BigInteger) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 30 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class Web3ImplTest method getTransactionReceipt.

@Test
public void getTransactionReceipt() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Web3Impl web3 = createWeb3(world, receiptStore);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String hashString = tx.getHash().toHexString();
    TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString);
    org.junit.Assert.assertNotNull(tr);
    org.junit.Assert.assertEquals("0x" + hashString, tr.transactionHash);
    String trxFrom = TypeConverter.toJsonHex(tx.getSender().getBytes());
    org.junit.Assert.assertEquals(trxFrom, tr.from);
    String trxTo = TypeConverter.toJsonHex(tx.getReceiveAddress().getBytes());
    org.junit.Assert.assertEquals(trxTo, tr.to);
    String blockHashString = "0x" + block1.getHash();
    org.junit.Assert.assertEquals(blockHashString, tr.blockHash);
    String blockNumberAsHex = "0x" + Long.toHexString(block1.getNumber());
    org.junit.Assert.assertEquals(blockNumberAsHex, tr.blockNumber);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionReceiptDTO(org.ethereum.rpc.dto.TransactionReceiptDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)141 Test (org.junit.Test)130 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)28 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)23 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)22 SimpleBlock (co.rsk.peg.simples.SimpleBlock)18 ArrayList (java.util.ArrayList)18 RepositoryImpl (co.rsk.db.RepositoryImpl)15 DataWord (org.ethereum.vm.DataWord)14 Repository (org.ethereum.core.Repository)12 RskAddress (co.rsk.core.RskAddress)11 TrieStore (co.rsk.trie.TrieStore)10 BlockBuilder (co.rsk.test.builders.BlockBuilder)9 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)8 ReceiptStore (org.ethereum.db.ReceiptStore)8 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)8 Keccak256 (co.rsk.crypto.Keccak256)7 World (co.rsk.test.World)7 TrieImpl (co.rsk.trie.TrieImpl)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6