TransactionStore extends ObjectDataSource<List<TransactionInfo>> { public TransactionInfo get(byte[] txHash, byte[] blockHash) { List<TransactionInfo> existingInfos = get(txHash); for (TransactionInfo info : existingInfos) { if (FastByteComparisons.equal(info.getBlockHash(), blockHash)) { return info; } } return null; } TransactionStore(Source<byte[], byte[]> src); boolean put(TransactionInfo tx); TransactionInfo get(byte[] txHash, byte[] blockHash); @PreDestroy void close();  }
@Test public void simpleTest() { String contractSrc = "contract Adder {" + " function add(int a, int b) returns (int) {return a + b;}" + "}"; HashMapDB<byte[]> txDb = new HashMapDB<>(); StandaloneBlockchain bc = new StandaloneBlockchain(); bc.getBlockchain().withTransactionStore(new TransactionStore(txDb)); SolidityContract contract = bc.submitNewContract(contractSrc); bc.createBlock(); contract.callFunction("add", 555, 222); Block b2 = bc.createBlock(); contract.callFunction("add", 333, 333); Block b3 = bc.createBlock(); Transaction tx1 = b2.getTransactionsList().get(0); TransactionInfo tx1Info = bc.getBlockchain().getTransactionInfo(tx1.getHash()); byte[] executionResult = tx1Info.getReceipt().getExecutionResult(); Assert.assertArrayEquals(DataWord.of(777).getData(), executionResult); System.out.println(txDb.keys().size()); bc.getBlockchain().flush(); System.out.println(txDb.keys().size()); TransactionStore txStore = new TransactionStore(txDb); TransactionInfo tx1Info_ = txStore.get(tx1.getHash()).get(0); executionResult = tx1Info_.getReceipt().getExecutionResult(); Assert.assertArrayEquals(DataWord.of(777).getData(), executionResult); TransactionInfo highIndex = new TransactionInfo(tx1Info.getReceipt(), tx1Info.getBlockHash(), 255); TransactionInfo highIndexCopy = new TransactionInfo(highIndex.getEncoded()); Assert.assertArrayEquals(highIndex.getBlockHash(), highIndexCopy.getBlockHash()); Assert.assertEquals(highIndex.getIndex(), highIndexCopy.getIndex()); }
@Test public void forkTest() { String contractSrc = "contract Adder {" + " int public lastResult;" + " function add(int a, int b) returns (int) {lastResult = a + b; return lastResult; }" + "}"; HashMapDB txDb = new HashMapDB(); StandaloneBlockchain bc = new StandaloneBlockchain(); TransactionStore transactionStore = new TransactionStore(txDb); bc.getBlockchain().withTransactionStore(transactionStore); SolidityContract contract = bc.submitNewContract(contractSrc); Block b1 = bc.createBlock(); contract.callFunction("add", 555, 222); Block b2 = bc.createBlock(); Transaction tx1 = b2.getTransactionsList().get(0); TransactionInfo txInfo = bc.getBlockchain().getTransactionInfo(tx1.getHash()); Assert.assertTrue(Arrays.equals(txInfo.getBlockHash(), b2.getHash())); Block b2_ = bc.createForkBlock(b1); contract.callFunction("add", 555, 222); Block b3_ = bc.createForkBlock(b2_); TransactionInfo txInfo_ = bc.getBlockchain().getTransactionInfo(tx1.getHash()); Assert.assertTrue(Arrays.equals(txInfo_.getBlockHash(), b3_.getHash())); Block b3 = bc.createForkBlock(b2); Block b4 = bc.createForkBlock(b3); txInfo = bc.getBlockchain().getTransactionInfo(tx1.getHash()); Assert.assertTrue(Arrays.equals(txInfo.getBlockHash(), b2.getHash())); }