to update the chaining score after block delete and merge processed
the blockScore value is important for significance evaluation
434350
public static void updateScore(FatCatParameters params, AFPChain afpChain)
	{
		int     i, j, bknow, bkold, g1, g2;


		afpChain.setConn(0d);
		afpChain.setDVar(0d);

		int blockNum = afpChain.getBlockNum();
		int alignScoreUpdate = 0;
		double[] blockScore = afpChain.getBlockScore();
		int[] blockGap = afpChain.getBlockGap();
		int[] blockSize =afpChain.getBlockSize();
		int[] afpChainList = afpChain.getAfpChainList();
		List<AFP>afpSet = afpChain.getAfpSet();
		int[] block2Afp = afpChain.getBlock2Afp();

		double torsionPenalty = params.getTorsionPenalty();


		bkold = 0;
		for(i = 0; i < blockNum; i ++)  {
			blockScore[i] = 0;
			blockGap[i] = 0;
			for(j = 0; j < blockSize[i]; j ++)      {
				bknow = afpChainList[block2Afp[i] + j];
				if(j == 0)      {
					blockScore[i] = afpSet.get(bknow).getScore();
				}
				else    {
					AFPChainer.afpPairConn(bkold, bknow, params, afpChain); //note: j, i
					Double conn = afpChain.getConn();
					blockScore[i] += afpSet.get(bknow).getScore() + conn;
					g1 = afpSet.get(bknow).getP1() - afpSet.get(bkold).getP1() - afpSet.get(bkold).getFragLen();
					g2 = afpSet.get(bknow).getP2() - afpSet.get(bkold).getP2() - afpSet.get(bkold).getFragLen();
					blockGap[i] += (g1 > g2)?g1:g2;
				}
				bkold = bknow;
			}
			alignScoreUpdate += blockScore[i];
		}
		if(blockNum >= 2)       {
			alignScoreUpdate += (blockNum - 1) * torsionPenalty;
		}

		afpChain.setBlockGap(blockGap);
		afpChain.setAlignScoreUpdate(alignScoreUpdate);
		afpChain.setBlockScore(blockScore);
		afpChain.setBlockSize(blockSize);
		afpChain.setAfpChainList(afpChainList);
		afpChain.setBlock2Afp(block2Afp);
	}
1103880
def add_next_block(prev_block, data)
      if valid_block?(prev_block)
        blockchain << next_block(data)
      else
        raise InvalidBlockError
      end
    end
391037
void hashUpdate(final long hash0, final long hash1) {
    int col = Long.numberOfLeadingZeros(hash1);
    if (col < fiCol) { return; } // important speed optimization
    if (col > 63) { col = 63; } // clip so that 0 <= col <= 63
    final long c = numCoupons;
    if (c == 0) { promoteEmptyToSparse(this); }
    final long k = 1L << lgK;
    final int row = (int) (hash0 & (k - 1L));
    int rowCol = (row << 6) | col;

    // Avoid the hash table's "empty" value which is (2^26 -1, 63) (all ones) by changing it
    // to the pair (2^26 - 2, 63), which effectively merges the two cells.
    // This case is *extremely* unlikely, but we might as well handle it.
    // It can't happen at all if lgK (or maxLgK) < 26.
    if (rowCol == -1) { rowCol ^= (1 << 6); } //set the LSB of row to 0

    if ((c << 5) < (3L * k)) { updateSparse(this, rowCol); }
    else { updateWindowed(this, rowCol); }
  }
476728
def _cleanup_processing_chain(self):
        """"""
        self.log("Removing stale processing chain entries.")
        self._processing_chain = [x for x in self._processing_chain
                                  if x.remember_between_connections]
202944
@GuardedBy("workerInfo")
  private void processWorkerRemovedBlocks(MasterWorkerInfo workerInfo,
      Collection<Long> removedBlockIds) {
    for (long removedBlockId : removedBlockIds) {
      try (LockResource lr = lockBlock(removedBlockId)) {
        Optional<BlockMeta> block = mBlockStore.getBlock(removedBlockId);
        if (block.isPresent()) {
          LOG.info("Block {} is removed on worker {}.", removedBlockId, workerInfo.getId());
          mBlockStore.removeLocation(removedBlockId, workerInfo.getId());
          if (mBlockStore.getLocations(removedBlockId).size() == 0) {
            mLostBlocks.add(removedBlockId);
          }
        }
        // Remove the block even if its metadata has been deleted already.
        workerInfo.removeBlock(removedBlockId);
      }
    }
  }
1332222
def merge_blocks(a_blocks, b_blocks):
    
    # Check sentinels for sequence length.
    assert a_blocks[-1][2] == b_blocks[-1][2] == 0 # sentinel size is 0
    assert a_blocks[-1] == b_blocks[-1]
    combined_blocks = sorted(list(set(a_blocks + b_blocks)))
    # Check for overlaps.
    i = j = 0
    for a, b, size in combined_blocks:
        assert i <= a
        assert j <= b
        i = a + size
        j = b + size
    return combined_blocks
360808
private static void mergeBlocks(List< Block > blocks) {
        for (int i = 1; i < blocks.size(); i++) {
            Block b1 = blocks.get(i - 1);
            Block b2 = blocks.get(i);
            if ((b1.mode == b2.mode) &&
                (b1.mode != EncodingMode.NUM || b1.length + b2.length <= MAX_NUMERIC_COMPACTION_BLOCK_SIZE)) {
                b1.length += b2.length;
                blocks.remove(i);
                i--;
            }
        }
    }
255897
public void set(long index, Block value)
    {
        Block currentValue = array.get(index);
        if (currentValue != null) {
            currentValue.retainedBytesForEachPart((object, size) -> {
                if (currentValue == object) {
                    // track instance size separately as the reference count for an instance is always 1
                    sizeOfBlocks -= size;
                    return;
                }
                if (trackedObjects.decrementAndGet(object) == 0) {
                    // decrement the size only when it is the last reference
                    sizeOfBlocks -= size;
                }
            });
        }
        if (value != null) {
            value.retainedBytesForEachPart((object, size) -> {
                if (value == object) {
                    // track instance size separately as the reference count for an instance is always 1
                    sizeOfBlocks += size;
                    return;
                }
                if (trackedObjects.incrementAndGet(object) == 1) {
                    // increment the size only when it is the first reference
                    sizeOfBlocks += size;
                }
            });
        }
        array.set(index, value);
    }
1074497
private void processOverReplicatedBlocksAsync() {

    // blocks should not be scheduled for deletion during safemode
    if (isInSafeMode()) {
      return;
    }
    
    if (delayOverreplicationMonitorTime > now()) {
      LOG.info("Overreplication monitor delayed for "
          + ((delayOverreplicationMonitorTime - now()) / 1000) + " seconds");
      return;
    }
    nameNode.clearOutstandingNodes();
    
    final int nodes = heartbeats.size();
    List<Block> blocksToProcess = new ArrayList<Block>(Math.min(
        overReplicatedBlocks.size(),
        ReplicationConfigKeys.overreplicationWorkMultiplier * nodes));
    
    for (int i = 0; i < ReplicationConfigKeys.overreplicationWorkMultiplier; i++) {
      writeLock();
      try {
        NameNode.getNameNodeMetrics().numOverReplicatedBlocks.set(overReplicatedBlocks.size());
        overReplicatedBlocks.pollNToList(nodes, blocksToProcess);
        if (overReplicatedBlocks.isEmpty()) {
          break;
        }
      } finally {
        writeUnlock();
      }
    }

    for (Block block : blocksToProcess) {
      if (NameNode.stateChangeLog.isDebugEnabled()) {
        NameNode.stateChangeLog
            .debug("BLOCK* NameSystem.processOverReplicatedBlocksAsync: " + block);
      }
      if (block instanceof OverReplicatedBlock) {
        OverReplicatedBlock opb = (OverReplicatedBlock)block;
        processOverReplicatedBlock(block, (short) -1, opb.addedNode, opb.delNodeHint);
      } else {
        processOverReplicatedBlock(block, (short) -1, null, null);
      }
        
    }
  }
8591
def mul_block(self, index, val):
        """"""
        self._prepare_cache_slice(index)
        self.msinds[self.cache_slice] *= val
652304
private void verifyFirstBlock() {
    BlockScanInfo block = null;
    synchronized (this) {
      if ( blockInfoSet.size() > 0 ) {
        block = blockInfoSet.first();
      }
    }
    
    if ( block != null ) {
      verifyBlock(block);
      processedBlocks.add(block.block.getBlockId());
    }
  }
944096
private void removeBlockFromAddedBlocks(long blockId) {
    Iterator<Entry<String, List<Long>>> iterator = mAddedBlocks.entrySet().iterator();
    while (iterator.hasNext()) {
      Entry<String, List<Long>> entry = iterator.next();
      List<Long> blockList = entry.getValue();
      if (blockList.contains(blockId)) {
        blockList.remove(blockId);
        if (blockList.isEmpty()) {
          iterator.remove();
        }
        // exit the loop when already find and remove block id from mAddedBlocks
        break;
      }
    }
  }
58831
def chain_update(self, block, receipts):
        

        block_events = BlockEventExtractor(block).extract([
            EventSubscription(event_type="sawtooth/block-commit")])
        receipt_events = ReceiptEventExtractor(receipts).extract([
            EventSubscription(event_type="identity/update")])

        for event in block_events:
            forked = self._handle_block_commit(event)
            if forked:
                return

        for event in receipt_events:
            if event.event_type == "identity/update":
                self._handle_txn_commit(event)
147763
protected void processMisReplicatedBlocks() {
    writeLock();
    try {
      if (this.initializedReplQueues) {
        return;
      }
      String logPrefix = "Processing mis-replicated blocks: ";
      long nrInvalid = 0;
      
      // clear queues
      neededReplications.clear();
      overReplicatedBlocks.clear();
      raidEncodingTasks.clear();
      
      int totalBlocks = blocksMap.size();
      
      // shutdown initial block report executor
      setupInitialBlockReportExecutor(true);
      
      // simply return if there are no blocks
      if (totalBlocks == 0) {
        // indicate that we are populating replication queues
        setInitializedReplicationQueues(true);
        LOG.info(logPrefix + "Blocks map empty. Nothing to do.");
        return;
      }
      
      AtomicLong count = new AtomicLong(0);
      
      // shutdown initial block report thread executor
      setupInitialBlockReportExecutor(true);
      
      FLOG.info(logPrefix + "Starting, total number of blocks: " + totalBlocks);
      long start = now();
      
      // have one thread process at least 1M blocks
      // no more than configured number of threads
      // at least one thread if the number of blocks < 1M
      int numShards = Math.min(parallelProcessingThreads,
          ((totalBlocks + parallelRQblocksPerShard - 1) / parallelRQblocksPerShard));
      
      // get shard iterators
      List<Iterator<BlockInfo>> iterators = blocksMap
          .getBlocksIterarors(numShards);

      // sanity, as the number can be lower in corner cases
      numShards = iterators.size();
      
      FLOG.info(logPrefix + "Using " + numShards + " threads");
      
      List<MisReplicatedBlocksWorker> misReplicationWorkers 
        = new ArrayList<MisReplicatedBlocksWorker>();
      List<Thread> misReplicationThreads = new ArrayList<Thread>();
      
      // workers will synchronize on this object to insert into 
      // replication queues
      Object lock = new Object();
      
      // initialize mis-replication worker threads
      for (int i = 0; i < numShards; i++) {
        MisReplicatedBlocksWorker w = new MisReplicatedBlocksWorker(lock, i, count,
            iterators.get(i), totalBlocks);
        Thread t = new Thread(w);
        t.start();
        misReplicationWorkers.add(w);
        misReplicationThreads.add(t);
      }
      
      // join all threads
      for (Thread t : misReplicationThreads) {
        try {
          t.join();
        } catch (InterruptedException e) { 
          Thread.currentThread().interrupt();
        }
      }
      
      // get number of invalidated blocks
      for (MisReplicatedBlocksWorker w : misReplicationWorkers) {
        nrInvalid += w.nrInvalid;
      }
      
      // update safeblock count (finalized blocks)
      this.blocksSafe = blocksMap.size() - getMissingBlocksCount();
      // update safeblock count (under construction blocks)
      for (MisReplicatedBlocksWorker w : misReplicationWorkers) {
        this.blocksSafe -= w.missingUnderConstructionBlocks;
      }
      
      // indicate that we are populating replication queues
      setInitializedReplicationQueues(true);

      long stop = now();
      
      FLOG.info(logPrefix + "Total number of blocks = " + blocksMap.size());
      FLOG.info(logPrefix + "Number of invalid blocks = " + nrInvalid);
      FLOG.info(logPrefix + "Number of under-replicated blocks = " +
          neededReplications.size());
      FLOG.info(logPrefix + "Number of  over-replicated blocks = " +
          overReplicatedBlocks.size());
      FLOG.info(logPrefix + "Number of safe blocks = " + this.blocksSafe);
      FLOG.info(logPrefix + "Finished in " + (stop - start) + " ms");
    } finally {
      writeUnlock();
    }
  }
822601
void clearReplicationQueues() {
    writeLock();
    try {
      synchronized (neededReplications) {
        neededReplications.clear();
      }
      underReplicatedBlocksCount = 0;

      corruptReplicas.clear();
      corruptReplicaBlocksCount = 0;

      overReplicatedBlocks.clear();
      raidEncodingTasks.clear();

      excessReplicateMap = new HashMap<String, LightWeightHashSet<Block>>();
      excessBlocksCount = 0;
    } finally {
      writeUnlock();
    }
  }
904330
private void processDestination(FastQueue<AssociatedIndex> matches, int numDestination,
									FastQueue<AssociatedIndex> output ) {
		//set up data structures
		scores.resize(numDestination);
		solutions.resize(numDestination);
		for( int i =0; i < numDestination; i++ ) {
			solutions.data[i] = -1;
		}

		// select best matches
		for( int i = 0; i < matches.size(); i++ ) {
			AssociatedIndex a = matches.get(i);
			int found = solutions.data[a.dst];
			if( found != -1 ) {
				if( found == -2 ) {
					// the best solution is invalid because two or more had the same score, see if this is better
					double bestScore = scores.data[a.dst];
					int result = type.compareTo(bestScore,a.fitScore);
					if( result < 0 ) {
						// found a better one, use this now
						solutions.data[a.dst] = i;
						scores.data[a.dst] = a.fitScore;
					}
				} else {
					// see if this new score is better than the current best
					AssociatedIndex currentBest = matches.get( found );
					int result = type.compareTo(currentBest.fitScore,a.fitScore);
					if( result < 0 ) {
						// found a better one, use this now
						solutions.data[a.dst] = i;
						scores.data[a.dst] = a.fitScore;
					} else if( result == 0 ) {
						// two solutions have the same score
						solutions.data[a.dst] = -2;
					}
				}
			} else {
				// no previous match found
				solutions.data[a.dst] = i;
				scores.data[a.dst] = i;
			}
		}

		output.reset();
		for( int i =0; i < numDestination; i++ ) {
			int index = solutions.data[i];
			if( index >= 0 ) {
				output.add( matches.get(index) );
			}
		}
	}
485977
function increaseBlockRecursively(provider, count, total, callback) {
  // if the count hits the total, stop the recursion
  if (count >= total) {
    callback(null, true);
  } else {
    // else mine a block
    provider.sendAsync({
      method: 'evm_mine',
    }, (mineError) => {
      if (mineError) {
        callback(`while mining to increase block: ${JSON.stringify(mineError)}`, null);
      } else {
        increaseBlockRecursively(provider, count + 1, total, callback);
      }
    });
  }
}