Hyperledger Fabric出块配置详解
Hyperledger Fabric的出块主要是Orderer节点负责,出块配置位于创世区块中,支持定时出块、达到一定交易数出块两种条件。出块配置位于configtx.yaml
中,修改出块配置后需要重新生成创世区块。
相关参数
若需要修改fabric的出块机制,则需要调整以下配置参数:
- BatchTimeout:出块超时时间,最长出块间隔(但缓存中必须含有数据才会出块,否则无法出块,即fabric不会强行产生空块)
- MaxMessageCount:区块最大交易数量,当交易数量达到此参数后,会立即出块。
- PreferredMaxBytes:区块首选字节数,正常情况下一个区块中的交易数据大小会小于此参数。
- AbsoluteMaxBytes:区块最大字节数:所有情况下区块的最大允许字节数,超过此参数的交易将无法打包,直接退回。
出块机制和条件
基于上述指标,orderer会在两种条件下打包区块:
1.定时触发:维护以 BatchTimeout 为间隔的闹钟,定时检测缓存中是否还有未出块的交易,如果有则打包出块;
2.新交易触发:当满足条件的新交易与缓存中的交易大小之和与 PreferredMaxBytes 进行比较,超过此限制则将缓存中的交易进行打包,新交易进入缓存。或者交易数目超过MaxMessageCount,也会进行打包。
默认参数配置
fabric的reslease-2.0分支中默认出块配置如下:
# Batch Timeout: The amount of time to wait before creating a batch.
BatchTimeout: 2s
# Batch Size: Controls the number of messages batched into a block.
# The orderer views messages opaquely, but typically, messages may
# be considered to be Fabric transactions. The 'batch' is the group
# of messages in the 'data' field of the block. Blocks will be a few kb
# larger than the batch size, when signatures, hashes, and other metadata
# is applied.
BatchSize:
# Max Message Count: The maximum number of messages to permit in a
# batch. No block will contain more than this number of messages.
MaxMessageCount: 500
# Absolute Max Bytes: The absolute maximum number of bytes allowed for
# the serialized messages in a batch. The maximum block size is this value
# plus the size of the associated metadata (usually a few KB depending
# upon the size of the signing identities). Any transaction larger than
# this value will be rejected by ordering. If the "kafka" OrdererType is
# selected, set 'message.max.bytes' and 'replica.fetch.max.bytes' on
# the Kafka brokers to a value that is larger than this one.
AbsoluteMaxBytes: 10 MB
# Preferred Max Bytes: The preferred maximum number of bytes allowed
# for the serialized messages in a batch. Roughly, this field may be considered
# the best effort maximum size of a batch. A batch will fill with messages
# until this size is reached (or the max message count, or batch timeout is
# exceeded). If adding a new message to the batch would cause the batch to
# exceed the preferred max bytes, then the current batch is closed and written
# to a block, and a new batch containing the new message is created. If a
# message larger than the preferred max bytes is received, then its batch
# will contain only that message. Because messages may be larger than
# preferred max bytes (up to AbsoluteMaxBytes), some batches may exceed
# the preferred max bytes, but will always contain exactly one transaction.
PreferredMaxBytes: 2 MB