public BlockStore buildBlockStore(BranchId branchId) { return new BlockStore(getDbSource(branchId + "/blocks")); }
/** * Gets block by hash. * * @param hash the hash * @return the block by hash */ public BlockHusk getBlockByHash(Sha3Hash hash) { return blockStore.get(hash); }
public BlockHusk getBlockByIndex(long idx) { return blockStore.getBlockByIndex(idx); }
@Test public void shouldBeGotBlock() { LevelDbDataSource ds = new LevelDbDataSource(StoreTestUtils.getTestPath(), "block-store-test"); BlockStore blockStore = new BlockStore(ds); BlockHusk blockHuskFixture = BlockChainTestUtils.genesisBlock(); blockStore.put(blockHuskFixture.getHash(), blockHuskFixture); BlockHusk foundBlockHusk = blockStore.get(blockHuskFixture.getHash()); StoreTestUtils.clearTestDb(); Assertions.assertThat(foundBlockHusk).isEqualTo(blockHuskFixture); Assertions.assertThat(blockStore.get(foundBlockHusk.getHash())).isEqualTo(foundBlockHusk); } }
public BlockChain(Branch branch, BlockHusk genesisBlock, BlockStore blockStore, TransactionStore transactionStore, MetaStore metaStore, Runtime runtime) { this.branch = branch; this.genesisBlock = genesisBlock; this.blockStore = blockStore; this.transactionStore = transactionStore; this.metaStore = metaStore; this.runtime = runtime; this.stateStore = runtime.getStateStore(); this.transactionReceiptStore = runtime.getTransactionReceiptStore(); // Empty blockChain if (!blockStore.contains(genesisBlock.getHash())) { initGenesis(); } else { loadTransaction(); } }
public long size() { return blockStore.getBlockchainTransactionSize(); }
public BlockHusk getBlockByIndex(long index) { byte[] indexKey = blockIndexKey(index); byte[] blockHash = db.get(indexKey); if (blockHash == null) { return null; } byte[] blockData = db.get(blockHash); if (blockData == null) { return null; } return new BlockHusk(blockData); }
public void close() { this.blockStore.close(); this.transactionStore.close(); this.metaStore.close(); // TODO refactoring this.stateStore.close(); this.transactionReceiptStore.close(); }
BlockStore(DbSource<byte[], byte[]> dbSource) { this.db = dbSource.init(); // get Transaction Size transactionSize = getBlockchainTransactionSize(); }
public void addBlock(BlockHusk block) { // Add BlockIndex and Add Block Data long index = block.getIndex(); byte[] indexKey = blockIndexKey(index); // store block index db.put(indexKey, block.getHash().getBytes()); // store block data db.put(block.getHash().getBytes(), block.getData()); // add block Transaction size transactionSize += block.getBodySize(); db.put("TRANSACTION_SIZE".getBytes(), Longs.toByteArray(transactionSize)); }
private void loadTransaction() { // load recent 1000 block // Start Block and End Block Long bestBlock = metaStore.getBestBlock(); Long loadStart = bestBlock > 1000 ? bestBlock - 1000 : 0; for (long i = loadStart; i <= bestBlock; i++) { // recent block load and update Cache BlockHusk block = blockStore.getBlockByIndex(i); transactionStore.updateCache(block.getBody()); // set Last Best Block if (i == bestBlock) { this.prevBlock = block; } } }
private void executeBlock() { // Run Block // GET BEST BLOCK long bestBlock = metaStore.getBestBlock(); long lastExecuteBlock = metaStore.getLastExecuteBlockIndex(); // TODO Validate Block will be stored if (bestBlock > lastExecuteBlock) { runExecute = true; while (lastExecuteBlock < bestBlock) { lastExecuteBlock++; BlockHusk block = store.getBlockByIndex(lastExecuteBlock); if (block == null) { throw new FailedOperationException("Blockchain is not wired"); } // Block Execute // TODO get block execute root state runtime.invokeBlock(block); // Set Next ExecuteBlock metaStore.setLastExecuteBlock(block); log.info("Block "+block.getIndex()+" Execute Complite"); } runExecute = false; } } }