use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class StateTestRunner method runImpl.
public List<String> runImpl() {
vStats = new ValidationStats();
logger.info("");
trieStore = new TrieStoreImpl(new HashMapDB());
repository = RepositoryBuilder.build(trieStore, stateTestCase.getPre());
logger.info("loaded repository");
transaction = TransactionBuilder.build(stateTestCase.getTransaction());
logger.info("transaction: {}", transaction.toString());
BlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
blockchain = new BlockChainImpl(blockStore, null, null, null, null, new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(trieStore, stateRootHandler), new TransactionExecutorFactory(config, blockStore, null, blockFactory, new ProgramInvokeFactoryImpl(), precompiledContracts, new BlockTxSignatureCache(new ReceivedTxSignatureCache()))), stateRootHandler);
env = EnvBuilder.build(stateTestCase.getEnv());
invokeFactory = new TestProgramInvokeFactory(env);
block = build(env);
block.setStateRoot(repository.getRoot());
block.flushRLP();
blockchain.setStatus(block, block.getCumulativeDifficulty());
// blockchain.setProgramInvokeFactory(invokeFactory);
// blockchain.startTracking();
ProgramResult programResult = executeTransaction(transaction);
trieStore.flush();
List<LogInfo> origLogs = programResult.getLogInfoList();
List<LogInfo> postLogs = LogBuilder.build(stateTestCase.getLogs());
List<String> logsResult = LogsValidator.valid(origLogs, postLogs, vStats);
Repository postRepository = RepositoryBuilder.build(stateTestCase.getPost());
// Balances cannot be validated because has consumption for CALLs differ.
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false, false, vStats);
logger.info("--------- POST Validation---------");
List<String> outputResults = OutputValidator.valid(ByteUtil.toHexString(programResult.getHReturn()), stateTestCase.getOut(), vStats);
List<String> results = new ArrayList<>();
results.addAll(repoResults);
results.addAll(logsResult);
results.addAll(outputResults);
for (String result : results) {
logger.error(result);
}
if ((vStats.storageChecks == 0) && (vStats.logChecks == 0) && (vStats.balancetChecks == 0) && (vStats.outputChecks == 0) && (vStats.blockChecks == 0)) {
// This generally mean that the test didn't check anything
// AccountChecks are considered not indicative of the result of the test
logger.info("IRRELEVANT\n");
}
logger.info("\n\n");
return results;
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class TestRunner method runTestCase.
public List<String> runTestCase(BlockTestCase testCase) {
/* 1 */
// Create genesis + init pre state
ValidationStats vStats = new ValidationStats();
Block genesis = build(testCase.getGenesisBlockHeader(), null, null);
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = RepositoryBuilder.build(trieStore, testCase.getPre());
IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
CompositeEthereumListener listener = new TestCompositeEthereumListener();
KeyValueDataSource ds = new HashMapDB();
ds.init();
ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, blockStore, receiptStore, blockFactory, new ProgramInvokeFactoryImpl(), null, blockTxSignatureCache);
StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
RepositoryLocator repositoryLocator = new RepositoryLocator(trieStore, stateRootHandler);
TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repositoryLocator, null, blockFactory, listener, transactionExecutorFactory, new ReceivedTxSignatureCache(), 10, 100);
BlockChainImpl blockchain = new BlockChainImpl(blockStore, receiptStore, transactionPool, null, new DummyBlockValidator(), new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(trieStore, stateRootHandler), transactionExecutorFactory), stateRootHandler);
blockchain.setNoValidation(true);
blockchain.setStatus(genesis, genesis.getCumulativeDifficulty());
/* 2 */
// Create block traffic list
List<Block> blockTraffic = new ArrayList<>();
for (BlockTck blockTck : testCase.getBlocks()) {
Block block = build(blockTck.getBlockHeader(), blockTck.getTransactions(), blockTck.getUncleHeaders());
Block tBlock = null;
try {
byte[] rlp = parseData(blockTck.getRlp());
tBlock = blockFactory.decodeBlock(rlp);
ArrayList<String> outputSummary = BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader(), null);
if (!outputSummary.isEmpty()) {
for (String output : outputSummary) logger.error("at block {}: {}", Integer.toString(blockTraffic.size()), output);
}
blockTraffic.add(tBlock);
} catch (Exception e) {
System.out.println("*** Exception");
}
}
// Inject blocks to the blockchain execution
for (Block block : blockTraffic) {
ImportResult importResult = blockchain.tryToConnect(block);
logger.debug("{} ~ {} difficulty: {} ::: {}", block.getPrintableHash(), toPrintableHash(block.getParentHash().getBytes()), block.getCumulativeDifficulty(), importResult.toString());
}
// Check state root matches last valid block
List<String> results = new ArrayList<>();
String currRoot = ByteUtil.toHexString(repository.getRoot());
byte[] bestHash = Hex.decode(testCase.getLastblockhash());
String finalRoot = ByteUtil.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());
if (validateStateRoots) {
if (!finalRoot.equals(currRoot)) {
String formattedString = String.format("Root hash doesn't match best: expected: %s current: %s", finalRoot, currRoot);
results.add(formattedString);
}
}
Repository postRepository = RepositoryBuilder.build(testCase.getPostState());
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, validateStateRoots, validateBalances, null);
results.addAll(repoResults);
return results;
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class ReceiptStoreImplFallbackTest method addToExistingOlderReceiptDataViaFallback.
@Test
public void addToExistingOlderReceiptDataViaFallback() {
HashMapDB hashMapDB = new HashMapDB();
ReceiptStore storeV1 = new ReceiptStoreImpl(hashMapDB);
ReceiptStore storeV2 = new ReceiptStoreImplV2(hashMapDB);
TransactionReceipt receipt = createReceipt();
byte[] blockHash1 = Hex.decode("0102030405060708");
byte[] blockHash2 = Hex.decode("0102030405060709");
storeV1.add(blockHash1, 41, receipt);
storeV2.add(blockHash2, 42, receipt);
TransactionInfo result1 = storeV1.get(receipt.getTransaction().getHash().getBytes(), blockHash1).orElse(null);
Assert.assertNotNull(result1);
Assert.assertNotNull(result1.getBlockHash());
Assert.assertArrayEquals(blockHash1, result1.getBlockHash());
Assert.assertEquals(41, result1.getIndex());
Assert.assertArrayEquals(receipt.getEncoded(), result1.getReceipt().getEncoded());
TransactionInfo result2 = storeV1.get(receipt.getTransaction().getHash().getBytes(), blockHash2).orElse(null);
Assert.assertNotNull(result2);
Assert.assertNotNull(result2.getBlockHash());
Assert.assertArrayEquals(blockHash2, result2.getBlockHash());
Assert.assertEquals(42, result2.getIndex());
Assert.assertArrayEquals(receipt.getEncoded(), result2.getReceipt().getEncoded());
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class BlockExecutorTest method generateBlockWithOneTransaction.
private static TestObjects generateBlockWithOneTransaction() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = new MutableRepository(trieStore, new Trie(trieStore));
Repository track = repository.startTracking();
Account account = createAccount("acctest1", track, Coin.valueOf(30000));
Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
track.commit();
Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
BlockExecutor executor = buildBlockExecutor(trieStore);
Transaction tx1 = Transaction.builder().nonce(repository.getNonce(account.getAddress())).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(account2.getAddress()).chainId(CONFIG.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
tx1.sign(account.getEcKey().getPrivKeyBytes());
Transaction tx = tx1;
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
List<BlockHeader> uncles = new ArrayList<>();
// getGenesisBlock() modifies the repository, adding some pre-mined accounts
// Not nice for a getter, but it is what it is :(
Block genesis = BlockChainImplTest.getGenesisBlock(trieStore);
genesis.setStateRoot(repository.getRoot());
// Returns the root state prior block execution but after loading
// some sample accounts (account/account2) and the premined accounts
// in genesis.
byte[] rootPriorExecution = repository.getRoot();
Block block = new BlockGenerator().createChildBlock(genesis, txs, uncles, 1, null);
executor.executeAndFill(block, genesis.getHeader());
repository.save();
return new TestObjects(trieStore, block, genesis, tx, account, rootPriorExecution);
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class BlockValidatorTest method invalidUncleIsAncestor.
@Test
public void invalidUncleIsAncestor() {
IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block uncle1a = blockGenerator.createChildBlock(genesis);
List<BlockHeader> uncles1 = new ArrayList<>();
uncles1.add(uncle1a.getHeader());
uncles1.add(genesis.getHeader());
Block block1 = blockGenerator.createChildBlock(genesis, null, uncles1, 1, null);
store.saveBlock(genesis, TEST_DIFFICULTY, true);
store.saveBlock(uncle1a, TEST_DIFFICULTY, false);
store.saveBlock(block1, TEST_DIFFICULTY, true);
BlockValidatorImpl validator = new BlockValidatorBuilder().addBlockUnclesValidationRule(store).blockStore(store).build();
Assert.assertFalse(validator.isValid(block1));
}
Aggregations