updatePeerTip method

void updatePeerTip(
  1. String peerId,
  2. Hash blockHash,
  3. int height,
  4. ChainTipSource source,
)

Update peer's chain tip from any source

Implementation

void updatePeerTip(String peerId, Hash blockHash, int height, ChainTipSource source) {
  if (height < 0) {
    logger.warning('Invalid height $height from peer $peerId');
    return;
  }

  final now = DateTime.now();
  final confidence = _calculateConfidence(source, peerId);

  final newTip = PeerChainTip(
    peerId: peerId,
    blockHash: blockHash,
    height: height,
    reportedAt: now,
    source: source,
    confidence: confidence,
  );

  final oldTip = _peerTips[peerId];
  _peerTips[peerId] = newTip;
  _totalTipUpdates++;

  logger.fine('Updated tip for $peerId: height $height (source: $source)');

  // Track height->hash mappings for reorg detection
  _heightToHashes.putIfAbsent(height, () => []);
  if (!_heightToHashes[height]!.contains(blockHash)) {
    _heightToHashes[height]!.add(blockHash);
  }

  // Check for potential reorganization
  if (oldTip != null && _isPotentialReorganization(oldTip, newTip)) {
    _handlePotentialReorganization(oldTip, newTip);
  }

  // Recalculate consensus
  final oldBestTip = _bestTip;
  _recalculateConsensus();

  // Fire events if consensus changed
  if (_bestTip != oldBestTip) {
    _handleConsensusChange(oldBestTip, _bestTip);
  }
}