structure chart
Registrar
Registrar is core structure of orderer, which holds all chain, consenters and ledger. It is the only entrance to operate chain.
chains stores all chains;
consenters store 3 kinds of pluggable consenter, they are solo, kafka and etcdraft. you can add your defined consenter.
// Registrar serves as a point of access and control for the individual channel resources.
type Registrar struct {
config localconfig.TopLevel
lock sync.RWMutex
chains map[string]*ChainSupport
consenters map[string]consensus.Consenter
ledgerFactory blockledger.Factory
signer identity.SignerSerializer
blockcutterMetrics *blockcutter.Metrics
systemChannelID string
systemChannel *ChainSupport
templator msgprocessor.ChannelConfigTemplator
callbacks []channelconfig.BundleActor
bccsp bccsp.BCCSP
}
ChainSupport
ChainSupport holds all resources for a particular channel. It namelessly assemble many structures.
// ChainSupport holds the resources for a particular channel.
type ChainSupport struct {
*ledgerResources
msgprocessor.Processor
*BlockWriter
consensus.Chain
cutter blockcutter.Receiver
identity.SignerSerializer
BCCSP bccsp.BCCSP
// NOTE: It makes sense to add this to the ChainSupport since the design of Registrar does not assume
// that there is a single consensus type at this orderer node and therefore the resolution of
// the consensus type too happens only at the ChainSupport level.
consensus.MetadataValidator
}
ConsenterSupport
ConsenterSupport interface is impemented by ChainSupport, is passed into specified consenter chain when Consenter.HandleChain() create a new chain.
// ConsenterSupport provides the resources available to a Consenter implementation.
type ConsenterSupport interface {
identity.SignerSerializer
msgprocessor.Processor
// VerifyBlockSignature verifies a signature of a block with a given optional
// configuration (can be nil).
VerifyBlockSignature([]*protoutil.SignedData, *cb.ConfigEnvelope) error
// BlockCutter returns the block cutting helper for this channel.
BlockCutter() blockcutter.Receiver
// SharedConfig provides the shared config from the channel's current config block.
SharedConfig() channelconfig.Orderer
// ChannelConfig provides the channel config from the channel's current config block.
ChannelConfig() channelconfig.Channel
// CreateNextBlock takes a list of messages and creates the next block based on the block with highest block number committed to the ledger
// Note that either WriteBlock or WriteConfigBlock must be called before invoking this method a second time.
CreateNextBlock(messages []*cb.Envelope) *cb.Block
// Block returns a block with the given number,
// or nil if such a block doesn't exist.
Block(number uint64) *cb.Block
// WriteBlock commits a block to the ledger.
WriteBlock(block *cb.Block, encodedMetadataValue []byte)
// WriteConfigBlock commits a block to the ledger, and applies the config update inside.
WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte)
// Sequence returns the current config sequence.
Sequence() uint64
// ChannelID returns the channel ID this support is associated with.
ChannelID() string
// Height returns the number of blocks in the chain this channel is associated with.
Height() uint64
// Append appends a new block to the ledger in its raw form,
// unlike WriteBlock that also mutates its metadata.
Append(block *cb.Block) error
}
Consenter
HandleChain() is used to create a chain.
// Consenter defines the backing ordering mechanism.
type Consenter interface {
// HandleChain should create and return a reference to a Chain for the given set of resources.
// It will only be invoked for a given chain once per process. In general, errors will be treated
// as irrecoverable and cause system shutdown. See the description of Chain for more details
// The second argument to HandleChain is a pointer to the metadata stored on the `ORDERER` slot of
// the last block committed to the ledger of this Chain. For a new chain, or one which is migrated,
// this metadata will be nil (or contain a zero-length Value), as there is no prior metadata to report.
HandleChain(support ConsenterSupport, metadata *cb.Metadata) (Chain, error)
}
ledgerResources
ledgerResources provide channel config and block ledger resource.
type ledgerResources struct {
*configResources
blockledger.ReadWriter
}
msgprocessor.Processor
msgprocessor.Processor is only implemented by StandardChannel and SystemChannel, but ProcessNormalMsg(), ProcessConfigUpdateMsg() and ProcessConfigMsg() is rewrited by SystemChannel which contain StandardChannel namelessly.
// Processor provides the methods necessary to classify and process any message which
// arrives through the Broadcast interface.
type Processor interface {
// ClassifyMsg inspects the message header to determine which type of processing is necessary
ClassifyMsg(chdr *cb.ChannelHeader) Classification
// ProcessNormalMsg will check the validity of a message based on the current configuration. It returns the current
// configuration sequence number and nil on success, or an error if the message is not valid
ProcessNormalMsg(env *cb.Envelope) (configSeq uint64, err error)
// ProcessConfigUpdateMsg will attempt to apply the config update to the current configuration, and if successful
// return the resulting config message and the configSeq the config was computed from. If the config update message
// is invalid, an error is returned.
ProcessConfigUpdateMsg(env *cb.Envelope) (config *cb.Envelope, configSeq uint64, err error)
// ProcessConfigMsg takes message of type `ORDERER_TX` or `CONFIG`, unpack the ConfigUpdate envelope embedded
// in it, and call `ProcessConfigUpdateMsg` to produce new Config message of the same type as original message.
// This method is used to re-validate and reproduce config message, if it's deemed not to be valid anymore.
ProcessConfigMsg(env *cb.Envelope) (*cb.Envelope, uint64, error)
}
BlockWriter
Blockwriter is used to commit block.
// BlockWriter efficiently writes the blockchain to disk.
// To safely use BlockWriter, only one thread should interact with it.
// BlockWriter will spawn additional committing go routines and handle locking
// so that these other go routines safely interact with the calling one.
type BlockWriter struct {
support blockWriterSupport
registrar *Registrar
lastConfigBlockNum uint64
lastConfigSeq uint64
lastBlock *cb.Block
committingBlock sync.Mutex
}
consensus.Chain
consensus.Chain is implemnted by solo, kafka and etcdraft, which is used by broadcast interface.
// Chain defines a way to inject messages for ordering.
// Note, that in order to allow flexibility in the implementation, it is the responsibility of the implementer
// to take the ordered messages, send them through the blockcutter.Receiver supplied via HandleChain to cut blocks,
// and ultimately write the ledger also supplied via HandleChain. This design allows for two primary flows
// 1. Messages are ordered into a stream, the stream is cut into blocks, the blocks are committed (solo, kafka)
// 2. Messages are cut into blocks, the blocks are ordered, then the blocks are committed (sbft)
type Chain interface {
// Order accepts a message which has been processed at a given configSeq.
// If the configSeq advances, it is the responsibility of the consenter
// to revalidate and potentially discard the message
// The consenter may return an error, indicating the message was not accepted
Order(env *cb.Envelope, configSeq uint64) error
// Configure accepts a message which reconfigures the channel and will
// trigger an update to the configSeq if committed. The configuration must have
// been triggered by a ConfigUpdate message. If the config sequence advances,
// it is the responsibility of the consenter to recompute the resulting config,
// discarding the message if the reconfiguration is no longer valid.
// The consenter may return an error, indicating the message was not accepted
Configure(config *cb.Envelope, configSeq uint64) error
// WaitReady blocks waiting for consenter to be ready for accepting new messages.
// This is useful when consenter needs to temporarily block ingress messages so
// that in-flight messages can be consumed. It could return error if consenter is
// in erroneous states. If this blocking behavior is not desired, consenter could
// simply return nil.
WaitReady() error
// Errored returns a channel which will close when an error has occurred.
// This is especially useful for the Deliver client, who must terminate waiting
// clients when the consenter is not up to date.
Errored() <-chan struct{}
// Start should allocate whatever resources are needed for staying up to date with the chain.
// Typically, this involves creating a thread which reads from the ordering source, passes those
// messages to a block cutter, and writes the resulting blocks to the ledger.
Start()
// Halt frees the resources which were allocated for this Chain.
Halt()
}
cutter
block cutter is used to devide txs into groups, The order of txs in block is confirmed Once cut is over.
// Receiver defines a sink for the ordered broadcast messages
type Receiver interface {
// Ordered should be invoked sequentially as messages are ordered
// Each batch in `messageBatches` will be wrapped into a block.
// `pending` indicates if there are still messages pending in the receiver.
Ordered(msg *cb.Envelope) (messageBatches [][]*cb.Envelope, pending bool)
// Cut returns the current batch and starts a new one
Cut() []*cb.Envelope
}
type receiver struct {
sharedConfigFetcher OrdererConfigFetcher
pendingBatch []*cb.Envelope
pendingBatchSizeBytes uint32
PendingBatchStartTime time.Time
ChannelID string
Metrics *Metrics
}
MetadataValidator
MetadataValidator is used to validate ConsensusMetadata, Now it is only implemented by etcdraft, and used by StandardChannel
// MetadataValidator performs the validation of updates to ConsensusMetadata during config updates to the channel.
// NOTE: We expect the MetadataValidator interface to be optionally implemented by the Consenter implementation.
// If a Consenter does not implement MetadataValidator, we default to using a no-op MetadataValidator.
type MetadataValidator interface {
// ValidateConsensusMetadata determines the validity of a ConsensusMetadata update during config
// updates on the channel.
// Since the ConsensusMetadata is specific to the consensus implementation (independent of the particular
// chain) this validation also needs to be implemented by the specific consensus implementation.
ValidateConsensusMetadata(oldMetadata, newMetadata []byte, newChannel bool) error
}