github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/consensus/consensus.go (about)

     1  /*
     2  Copyright hechain. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package consensus
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/channelconfig"
    11  	"github.com/hechain20/hechain/internal/pkg/identity"
    12  	"github.com/hechain20/hechain/orderer/common/blockcutter"
    13  	"github.com/hechain20/hechain/orderer/common/msgprocessor"
    14  	"github.com/hechain20/hechain/protoutil"
    15  	cb "github.com/hyperledger/fabric-protos-go/common"
    16  )
    17  
    18  // Consenter defines the backing ordering mechanism.
    19  type Consenter interface {
    20  	// HandleChain should create and return a reference to a Chain for the given set of resources.
    21  	// It will only be invoked for a given chain once per process.  In general, errors will be treated
    22  	// as irrecoverable and cause system shutdown.  See the description of Chain for more details
    23  	// The second argument to HandleChain is a pointer to the metadata stored on the `ORDERER` slot of
    24  	// the last block committed to the ledger of this Chain. For a new chain, or one which is migrated,
    25  	// this metadata will be nil (or contain a zero-length Value), as there is no prior metadata to report.
    26  	HandleChain(support ConsenterSupport, metadata *cb.Metadata) (Chain, error)
    27  }
    28  
    29  // ClusterConsenter defines methods implemented by cluster-type consenters.
    30  type ClusterConsenter interface {
    31  	// IsChannelMember inspects the join block and detects whether it implies that this orderer is a member of the
    32  	// channel. It returns true if the orderer is a member of the consenters set, and false if it is not. The method
    33  	// also inspects the consensus type metadata for validity. It returns an error if membership cannot be determined
    34  	// due to errors processing the block.
    35  	IsChannelMember(joinBlock *cb.Block) (bool, error)
    36  	// RemoveInactiveChainRegistry stops and removes the inactive chain registry.
    37  	// This is used when removing the system channel.
    38  	RemoveInactiveChainRegistry()
    39  }
    40  
    41  // MetadataValidator performs the validation of updates to ConsensusMetadata during config updates to the channel.
    42  // NOTE: We expect the MetadataValidator interface to be optionally implemented by the Consenter implementation.
    43  //       If a Consenter does not implement MetadataValidator, we default to using a no-op MetadataValidator.
    44  type MetadataValidator interface {
    45  	// ValidateConsensusMetadata determines the validity of a ConsensusMetadata update during config
    46  	// updates on the channel.
    47  	// Since the ConsensusMetadata is specific to the consensus implementation (independent of the particular
    48  	// chain) this validation also needs to be implemented by the specific consensus implementation.
    49  	ValidateConsensusMetadata(oldOrdererConfig, newOrdererConfig channelconfig.Orderer, newChannel bool) error
    50  }
    51  
    52  // Chain defines a way to inject messages for ordering.
    53  // Note, that in order to allow flexibility in the implementation, it is the responsibility of the implementer
    54  // to take the ordered messages, send them through the blockcutter.Receiver supplied via HandleChain to cut blocks,
    55  // and ultimately write the ledger also supplied via HandleChain.  This design allows for two primary flows
    56  // 1. Messages are ordered into a stream, the stream is cut into blocks, the blocks are committed (solo, kafka)
    57  // 2. Messages are cut into blocks, the blocks are ordered, then the blocks are committed (sbft)
    58  type Chain interface {
    59  	// Order accepts a message which has been processed at a given configSeq.
    60  	// If the configSeq advances, it is the responsibility of the consenter
    61  	// to revalidate and potentially discard the message
    62  	// The consenter may return an error, indicating the message was not accepted
    63  	Order(env *cb.Envelope, configSeq uint64) error
    64  
    65  	// Configure accepts a message which reconfigures the channel and will
    66  	// trigger an update to the configSeq if committed.  The configuration must have
    67  	// been triggered by a ConfigUpdate message. If the config sequence advances,
    68  	// it is the responsibility of the consenter to recompute the resulting config,
    69  	// discarding the message if the reconfiguration is no longer valid.
    70  	// The consenter may return an error, indicating the message was not accepted
    71  	Configure(config *cb.Envelope, configSeq uint64) error
    72  
    73  	// WaitReady blocks waiting for consenter to be ready for accepting new messages.
    74  	// This is useful when consenter needs to temporarily block ingress messages so
    75  	// that in-flight messages can be consumed. It could return error if consenter is
    76  	// in erroneous states. If this blocking behavior is not desired, consenter could
    77  	// simply return nil.
    78  	WaitReady() error
    79  
    80  	// Errored returns a channel which will close when an error has occurred.
    81  	// This is especially useful for the Deliver client, who must terminate waiting
    82  	// clients when the consenter is not up to date.
    83  	Errored() <-chan struct{}
    84  
    85  	// Start should allocate whatever resources are needed for staying up to date with the chain.
    86  	// Typically, this involves creating a thread which reads from the ordering source, passes those
    87  	// messages to a block cutter, and writes the resulting blocks to the ledger.
    88  	Start()
    89  
    90  	// Halt frees the resources which were allocated for this Chain.
    91  	Halt()
    92  }
    93  
    94  //go:generate counterfeiter -o mocks/mock_consenter_support.go . ConsenterSupport
    95  
    96  // ConsenterSupport provides the resources available to a Consenter implementation.
    97  type ConsenterSupport interface {
    98  	identity.SignerSerializer
    99  	msgprocessor.Processor
   100  
   101  	// VerifyBlockSignature verifies a signature of a block with a given optional
   102  	// configuration (can be nil).
   103  	VerifyBlockSignature([]*protoutil.SignedData, *cb.ConfigEnvelope) error
   104  
   105  	// BlockCutter returns the block cutting helper for this channel.
   106  	BlockCutter() blockcutter.Receiver
   107  
   108  	// SharedConfig provides the shared config from the channel's current config block.
   109  	SharedConfig() channelconfig.Orderer
   110  
   111  	// ChannelConfig provides the channel config from the channel's current config block.
   112  	ChannelConfig() channelconfig.Channel
   113  
   114  	// CreateNextBlock takes a list of messages and creates the next block based on the block with highest block number committed to the ledger
   115  	// Note that either WriteBlock or WriteConfigBlock must be called before invoking this method a second time.
   116  	CreateNextBlock(messages []*cb.Envelope) *cb.Block
   117  
   118  	// Block returns a block with the given number,
   119  	// or nil if such a block doesn't exist.
   120  	Block(number uint64) *cb.Block
   121  
   122  	// WriteBlock commits a block to the ledger.
   123  	WriteBlock(block *cb.Block, encodedMetadataValue []byte)
   124  
   125  	// WriteConfigBlock commits a block to the ledger, and applies the config update inside.
   126  	WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte)
   127  
   128  	// Sequence returns the current config sequence.
   129  	Sequence() uint64
   130  
   131  	// ChannelID returns the channel ID this support is associated with.
   132  	ChannelID() string
   133  
   134  	// Height returns the number of blocks in the chain this channel is associated with.
   135  	Height() uint64
   136  
   137  	// Append appends a new block to the ledger in its raw form,
   138  	// unlike WriteBlock that also mutates its metadata.
   139  	Append(block *cb.Block) error
   140  }
   141  
   142  // NoOpMetadataValidator implements a MetadataValidator that always returns nil error irrespecttive of the inputs.
   143  type NoOpMetadataValidator struct{}
   144  
   145  // ValidateConsensusMetadata determines the validity of a ConsensusMetadata update during config updates
   146  // on the channel, and it always returns nil error for the NoOpMetadataValidator implementation.
   147  func (n NoOpMetadataValidator) ValidateConsensusMetadata(oldChannelConfig, newChannelConfig channelconfig.Orderer, newChannel bool) error {
   148  	return nil
   149  }