private void addInternalBlock(Block block, BigInteger cummDifficulty, boolean mainChain) { List<BlockInfo> blockInfos = block.getNumber() >= index.size() ? new ArrayList<BlockInfo>() : index.get((int) block.getNumber()); BlockInfo blockInfo = new BlockInfo(); blockInfo.setCummDifficulty(cummDifficulty); blockInfo.setHash(block.getHash()); blockInfo.setMainChain( mainChain); // FIXME:maybe here I should force reset main chain for all uncles on that level blockInfos.add(blockInfo); index.set((int) block.getNumber(), blockInfos); blocks.put(block.getHash(), block); }
@Override public BigInteger getTotalDifficultyForHash(byte[] hash) { Block block = this.getBlockByHash(hash); if (block == null) return ZERO; Long level = block.getNumber(); List<BlockInfo> blockInfos = index.get(level.intValue()); for (BlockInfo blockInfo : blockInfos) if (areEqual(blockInfo.getHash(), hash)) { return blockInfo.cummDifficulty; } return ZERO; }
@Override public Block createForkBlock(Block parent) { try { List<Transaction> txes = new ArrayList<>(); Map<ByteArrayWrapper, Long> nonces = new HashMap<>(); Repository repoSnapshot = getBlockchain().getRepository().getSnapshotTo(parent.getStateRoot()); for (PendingTx tx : submittedTxes) { ByteArrayWrapper senderW = new ByteArrayWrapper(tx.sender.getAddress()); Long nonce = nonces.get(senderW); if (nonce == null) { BigInteger bcNonce = repoSnapshot.getNonce(tx.sender.getAddress()); nonce = bcNonce.longValue(); } nonces.put(senderW, nonce + 1); byte[] toAddress = tx.targetContract != null ? tx.targetContract.getAddress() : tx.toAddress; Transaction transaction = new Transaction( ByteUtil.longToBytesNoLeadZeroes(nonce), ByteUtil.longToBytesNoLeadZeroes(gasPrice), ByteUtil.longToBytesNoLeadZeroes(gasLimit), toAddress, ByteUtil.bigIntegerToBytes(tx.value), tx.data); transaction.sign(tx.sender.getPrivKeyBytes()); if (tx.createdContract != null) { tx.createdContract.setAddress(transaction.getContractAddress()); } txes.add(transaction); } Block b = getBlockchain().createNewBlock(parent, txes, Collections.EMPTY_LIST); Ethash.getForBlock(b.getNumber()).mineLight(b).get(); ImportResult importResult = getBlockchain().tryToConnect(b); if (importResult != ImportResult.IMPORTED_BEST && importResult != ImportResult.IMPORTED_NOT_BEST) { throw new RuntimeException( "Invalid block import result " + importResult + " for block " + b); } submittedTxes.clear(); return b; } catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e); } }
@Override public void reBranch(Block forkBlock) { Block bestBlock = getBestBlock(); long maxLevel = Math.max(bestBlock.getNumber(), forkBlock.getNumber()); // 1. First ensure that you are one the save level long currentLevel = maxLevel; Block forkLine = forkBlock; if (forkBlock.getNumber() > bestBlock.getNumber()) { while (currentLevel > bestBlock.getNumber()) { List<BlockInfo> blocks = getBlockInfoForLevel(currentLevel); BlockInfo blockInfo = getBlockInfoForHash(blocks, forkLine.getHash()); if (blockInfo != null) { blockInfo.setMainChain(true); setBlockInfoForLevel(currentLevel, blocks); } forkLine = getBlockByHash(forkLine.getParentHash()); --currentLevel; } } Block bestLine = bestBlock; if (bestBlock.getNumber() > forkBlock.getNumber()) { while (currentLevel > forkBlock.getNumber()) { List<BlockInfo> blocks = getBlockInfoForLevel(currentLevel); BlockInfo blockInfo = getBlockInfoForHash(blocks, bestLine.getHash()); if (blockInfo != null) { blockInfo.setMainChain(false); setBlockInfoForLevel(currentLevel, blocks); } bestLine = getBlockByHash(bestLine.getParentHash()); --currentLevel; } } // 2. Loop back on each level until common block while (!bestLine.isEqual(forkLine)) { List<BlockInfo> levelBlocks = getBlockInfoForLevel(currentLevel); BlockInfo bestInfo = getBlockInfoForHash(levelBlocks, bestLine.getHash()); if (bestInfo != null) { bestInfo.setMainChain(false); setBlockInfoForLevel(currentLevel, levelBlocks); } BlockInfo forkInfo = getBlockInfoForHash(levelBlocks, forkLine.getHash()); if (forkInfo != null) { forkInfo.setMainChain(true); setBlockInfoForLevel(currentLevel, levelBlocks); } bestLine = getBlockByHash(bestLine.getParentHash()); forkLine = getBlockByHash(forkLine.getParentHash()); --currentLevel; } }