use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class ReceiptStoreImplTest method addTwoTransactionsAndGetTransactionByDescendantBlocks.
@Test
public void addTwoTransactionsAndGetTransactionByDescendantBlocks() {
World world = new World();
Block genesis = world.getBlockChain().getBestBlock();
Block block1a = new BlockBuilder().difficulty(10).parent(genesis).build();
Block block1b = new BlockBuilder().difficulty(block1a.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
Block block2a = new BlockBuilder().parent(block1a).build();
Block block2b = new BlockBuilder().parent(block1b).build();
Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1a));
Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2a));
Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block2b));
ReceiptStore store = new ReceiptStoreImpl(new HashMapDB());
TransactionReceipt receipt0 = createReceipt();
byte[] blockHash0 = Hex.decode("010203040506070809");
store.add(block1a.getHash().getBytes(), 3, receipt0);
TransactionReceipt receipt = createReceipt();
byte[] blockHash = Hex.decode("0102030405060708");
store.add(block1b.getHash().getBytes(), 42, receipt);
TransactionInfo result = store.get(receipt.getTransaction().getHash().getBytes(), block2a.getHash().getBytes(), world.getBlockChain().getBlockStore());
Assert.assertNotNull(result.getBlockHash());
Assert.assertArrayEquals(block1a.getHash().getBytes(), result.getBlockHash());
Assert.assertEquals(3, result.getIndex());
Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
result = store.get(receipt.getTransaction().getHash().getBytes(), block2b.getHash().getBytes(), world.getBlockChain().getBlockStore());
Assert.assertNotNull(result.getBlockHash());
Assert.assertArrayEquals(block1b.getHash().getBytes(), result.getBlockHash());
Assert.assertEquals(42, result.getIndex());
Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
result = store.get(receipt.getTransaction().getHash().getBytes(), genesis.getHash().getBytes(), world.getBlockChain().getBlockStore());
Assert.assertNull(result);
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class ReceiptStoreImplTest method getUnknownTransactionByBlock.
@Test
public void getUnknownTransactionByBlock() {
ReceiptStore store = new ReceiptStoreImpl(new HashMapDB());
TransactionReceipt receipt = createReceipt();
byte[] blockHash = Hex.decode("010203040506070809");
TransactionInfo result = store.get(receipt.getTransaction().getHash().getBytes(), blockHash, null);
Assert.assertNull(result);
}
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
Block genesis = BlockBuilder.build(testCase.getGenesisBlockHeader(), null, null);
Repository repository = RepositoryBuilder.build(testCase.getPre());
IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
EthereumListener listener = new CompositeEthereumListener();
KeyValueDataSource ds = new HashMapDB();
ds.init();
ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, null, null, new DummyBlockValidator());
// BlockchainImpl blockchain = new BlockchainImpl(blockStore, repository, wallet, adminInfo, listener,
// new CommonConfig().parentHeaderValidator(), receiptStore);
blockchain.setNoValidation(true);
TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
blockchain.setBestBlock(genesis);
blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
blockchain.setTransactionPool(transactionPool);
/* 2 */
// Create block traffic list
List<Block> blockTraffic = new ArrayList<>();
for (BlockTck blockTck : testCase.getBlocks()) {
Block block = BlockBuilder.build(blockTck.getBlockHeader(), blockTck.getTransactions(), blockTck.getUncleHeaders());
setNewStateRoot = !((blockTck.getTransactions() == null) && (blockTck.getUncleHeaders() == null) && (blockTck.getBlockHeader() == null));
Block tBlock = null;
try {
byte[] rlp = parseData(blockTck.getRlp());
tBlock = new Block(rlp);
ArrayList<String> outputSummary = BlockHeaderValidator.valid(tBlock.getHeader(), block.getHeader());
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.getShortHash(), shortHash(block.getParentHash().getBytes()), block.getCumulativeDifficulty(), importResult.toString());
}
// Check state root matches last valid block
List<String> results = new ArrayList<>();
String currRoot = Hex.toHexString(repository.getRoot());
byte[] bestHash = Hex.decode(testCase.getLastblockhash());
String finalRoot = Hex.toHexString(blockStore.getBlockByHash(bestHash).getStateRoot());
/*
if (!blockchain.byTest) // If this comes from ETH, it won't match
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, false);
results.addAll(repoResults);
return results;
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class StateTestRunner method runImpl.
public List<String> runImpl() {
logger.info("");
repository = RepositoryBuilder.build(stateTestCase.getPre());
logger.info("loaded repository");
transaction = TransactionBuilder.build(stateTestCase.getTransaction());
logger.info("transaction: {}", transaction.toString());
BlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
blockchain = new BlockChainImpl(config, repository, blockStore, null, null, null, null, null);
env = EnvBuilder.build(stateTestCase.getEnv());
invokeFactory = new TestProgramInvokeFactory(env);
block = BlockBuilder.build(env);
block.setStateRoot(repository.getRoot());
block.flushRLP();
blockchain.setBestBlock(block);
// blockchain.setProgramInvokeFactory(invokeFactory);
// blockchain.startTracking();
ProgramResult programResult = executeTransaction(transaction);
repository.flushNoReconnect();
List<LogInfo> origLogs = programResult.getLogInfoList();
List<LogInfo> postLogs = LogBuilder.build(stateTestCase.getLogs());
List<String> logsResult = LogsValidator.valid(origLogs, postLogs);
Repository postRepository = RepositoryBuilder.build(stateTestCase.getPost());
List<String> repoResults = RepositoryValidator.valid(repository, postRepository, false);
logger.info("--------- POST Validation---------");
List<String> outputResults = OutputValidator.valid(Hex.toHexString(programResult.getHReturn()), stateTestCase.getOut());
List<String> results = new ArrayList<>();
results.addAll(repoResults);
results.addAll(logsResult);
results.addAll(outputResults);
for (String result : results) {
logger.error(result);
}
logger.info("\n\n");
return results;
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class Web3ImplLogsTest method getWeb3WithContractInvoke.
private Web3Impl getWeb3WithContractInvoke() {
ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
World world = new World(receiptStore);
Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
Block genesis = world.getBlockByName("g00");
Transaction tx;
tx = getContractTransaction(acc1);
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
BlockChainImpl blockChain = world.getBlockChain();
Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
byte[] contractAddress = tx.getContractAddress().getBytes();
Transaction tx2 = getContractTransactionWithInvoke(acc1, contractAddress);
List<Transaction> tx2s = new ArrayList<>();
tx2s.add(tx2);
Block block2 = new BlockBuilder(world).parent(block1).transactions(tx2s).build();
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2));
TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), receiptStore, null, null, 10, 100);
Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool, receiptStore);
web3.personal_newAccountWithSeed("default");
web3.personal_newAccountWithSeed("notDefault");
return web3;
}
Aggregations