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