BSC的区块确认机制:快速确认与普通确认

引言

在区块链系统中,区块确认机制是保证交易最终性的关键。BSC(BNB Smart Chain)采用了一种独特的双层确认机制,包括快速确认和普通确认两种模式,让我们深入了解这个机制。

基础概念

1. 确认状态

BSC中的区块有两种确认状态:
  • Justified(已确认):获得超过2/3验证者投票的区块
  • Finalized(已最终确定):一个已确认区块的源区块会被标记为"已最终确定"

2. 投票证明(Attestation)

每个区块可能包含一个投票证明,其结构如下:
 
type VoteAttestation struct {
    Data *VoteData
    VoteAddressSet uint32     // 投票的验证者集合的位图
    AggSignature   BLSSignature  // 聚合签名
}

type VoteData struct {
    SourceNumber uint64      // 源区块高度(已确认的区块)
    SourceHash   common.Hash // 源区块哈希
    TargetNumber uint64      // 目标区块高度(父区块)
    TargetHash   common.Hash // 目标区块哈希
}

确认流程

1. 区块生成

当一个新区块生成时,会:
  • 检查是否可以包含对父区块的Attestation
  • 开始收集对当前区块的投票
  • 通过P2P网络广播

2. 投票收集

投票通过VotePool进行管理:
type VotePool struct {
    curVotes     map[common.Hash]*VoteBox  // 当前区块的投票
    futureVotes  map[common.Hash]*VoteBox  // 未来区块的投票
    votesCh      chan *types.VoteEnvelope  // 投票通道
}
 
func (pool *VotePool) FetchVoteByBlockHash(blockHash common.Hash) []*types.VoteEnvelope {
	pool.mu.RLock()
	defer pool.mu.RUnlock()
	if _, ok := pool.curVotes[blockHash]; ok {
		return pool.curVotes[blockHash].voteMessages
	}
	return nil
}

两种确认模式

1. 快速确认

示例时间轴:
A -> B -> C -> D -> E
        ↑    ↑    ↑
     投票数  投票数  投票数
      75    80    85
    (800ms)(850ms)(750ms)
特点:
  • 投票收集速度快(通常在一个区块间隔内)
  • 确认链连续
  • 延迟低(约1-2个区块时间,1.5s-3s)
  • 需要良好的网络条件

2. 普通确认

示例时间轴:
A -> B -> C -> D -> E -> F -> G
        ↑         ↑         ↑
     投票数     投票数     投票数
      40        67        70
     (5s)      (8s)      (6s)
特点:
  • 投票收集较慢(多个区块间隔)
  • 确认链可能不连续
  • 延迟较高(约4-8个区块时间,6s-12s)
  • 对网络条件要求较低

应用场景

1. 快速确认

适用于高价值交易场景:

2. 普通确认

适用于常规交易场景:
 
if txValue.Cmp(highValueThreshold) > 0 {
    // 等待快速确认
    if header.Number.Uint64() - justifiedNumber <= 2 {
        // 处理高价值交易
    }
}

机制优势

  1. 灵活性:提供了不同速度的确认机制
  1. 安全性:保证了交易的最终确认
  1. 适应性:能够适应不同的网络条件
  1. 实用性:满足不同交易场景的需求

总结

BSC的区块确认机制通过快速确认和普通确认两种模式,巧妙地平衡了效率和安全性。这种设计既能满足高价值交易的快速确认需求,又能在网络条件不佳时保证系统的正常运行。理解这个机制对于开发者和用户都很有帮助,可以根据具体场景选择合适的确认策略。
posted @ 2025-06-11 15:17  若-飞  阅读(75)  评论(0)    收藏  举报