1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
public synchronized void pushBlock(final BlockCapsule block) throws ValidateSignatureException, ContractValidateException, ContractExeException, UnLinkedBlockException, ValidateScheduleException, AccountResourceInsufficientException, TaposException, TooBigTransactionException, TooBigTransactionResultException, DupTransactionException, TransactionExpirationException, BadNumberBlockException, BadBlockException, NonCommonBlockException, ReceiptCheckErrException, VMIllegalException, ZksnarkException, EventBloomException { long start = System.currentTimeMillis(); List<TransactionCapsule> txs = getVerifyTxs(block); logger.info("Block num: {}, re-push-size: {}, pending-size: {}, " + "block-tx-size: {}, verify-tx-size: {}", block.getNum(), rePushTransactions.size(), pendingTransactions.size(), block.getTransactions().size(), txs.size());
if (CommonParameter.getInstance().getShutdownBlockTime() != null && CommonParameter.getInstance().getShutdownBlockTime() .isSatisfiedBy(new Date(block.getTimeStamp()))) { latestSolidityNumShutDown = block.getNum(); }
try (PendingManager pm = new PendingManager(this)) {
if (!block.generatedByMyself) { if (!block.calcMerkleRoot().equals(block.getMerkleRoot())) { logger.warn( "The merkle root doesn't match, Calc result is " + block.calcMerkleRoot() + " , the headers is " + block.getMerkleRoot()); throw new BadBlockException("The merkle hash is not validated"); } consensus.receiveBlock(block); }
if (block.getTransactions().stream().filter(tran -> isShieldedTransaction(tran.getInstance())) .count() > SHIELDED_TRANS_IN_BLOCK_COUNTS) { throw new BadBlockException( "shielded transaction count > " + SHIELDED_TRANS_IN_BLOCK_COUNTS); }
BlockCapsule newBlock; try { newBlock = this.khaosDb.push(block); } catch (UnLinkedBlockException e) { logger.error( "latestBlockHeaderHash:{}, latestBlockHeaderNumber:{}, latestSolidifiedBlockNum:{}", getDynamicPropertiesStore().getLatestBlockHeaderHash(), getDynamicPropertiesStore().getLatestBlockHeaderNumber(), getDynamicPropertiesStore().getLatestSolidifiedBlockNum()); throw e; }
if (getDynamicPropertiesStore().getLatestBlockHeaderHash() == null) { if (newBlock.getNum() != 0) { return; } } else { if (newBlock.getNum() <= getDynamicPropertiesStore().getLatestBlockHeaderNumber()) { return; }
if (!newBlock .getParentHash() .equals(getDynamicPropertiesStore().getLatestBlockHeaderHash())) { logger.warn( "switch fork! new head num = {}, block id = {}", newBlock.getNum(), newBlock.getBlockId());
logger.warn( "******** before switchFork ******* push block: " + block.toString() + ", new block:" + newBlock.toString() + ", dynamic head num: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + ", dynamic head hash: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderHash() + ", dynamic head timestamp: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp() + ", khaosDb head: " + khaosDb.getHead() + ", khaosDb miniStore size: " + khaosDb.getMiniStore().size() + ", khaosDb unlinkMiniStore size: " + khaosDb.getMiniUnlinkedStore().size());
switchFork(newBlock); logger.info(SAVE_BLOCK + newBlock);
logger.warn( "******** after switchFork ******* push block: " + block.toString() + ", new block:" + newBlock.toString() + ", dynamic head num: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber() + ", dynamic head hash: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderHash() + ", dynamic head timestamp: " + chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp() + ", khaosDb head: " + khaosDb.getHead() + ", khaosDb miniStore size: " + khaosDb.getMiniStore().size() + ", khaosDb unlinkMiniStore size: " + khaosDb.getMiniUnlinkedStore().size());
return; } try (ISession tmpSession = revokingStore.buildSession()) {
long oldSolidNum = chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum(); applyBlock(newBlock, txs); tmpSession.commit(); postBlockTrigger(newBlock); postSolidityTrigger(oldSolidNum, getDynamicPropertiesStore().getLatestSolidifiedBlockNum()); } catch (Throwable throwable) { logger.error(throwable.getMessage(), throwable); khaosDb.removeBlk(block.getBlockId()); throw throwable; } } logger.info(SAVE_BLOCK + newBlock); } if (CollectionUtils.isNotEmpty(ownerAddressSet)) { Set<String> result = new HashSet<>(); for (TransactionCapsule transactionCapsule : rePushTransactions) { filterOwnerAddress(transactionCapsule, result); } for (TransactionCapsule transactionCapsule : pushTransactionQueue) { filterOwnerAddress(transactionCapsule, result); } ownerAddressSet.clear(); ownerAddressSet.addAll(result); }
MetricsUtil.meterMark(MetricsKey.BLOCKCHAIN_BLOCK_PROCESS_TIME, System.currentTimeMillis() - start);
logger.info("pushBlock block number:{}, cost/txs:{}/{}", block.getNum(), System.currentTimeMillis() - start, block.getTransactions().size()); }
|