fabric 2.0, BlockLedger

The code is in common\ledger, only used by Orderer for ledger management.

ledgerFactory

It is an interface to Get/Create a ledger. ChannelId <==> ledger Id

// Factory retrieves or creates new ledgers by channelID
type Factory interface {
	// GetOrCreate gets an existing ledger (if it exists)
	// or creates it if it does not
	GetOrCreate(channelID string) (ReadWriter, error)

	// ChannelIDs returns the channel IDs the Factory is aware of
	ChannelIDs() []string

	// Close releases all resources acquired by the factory
	Close()
}

fileLedgerFactory

It implements the ledger factory.

type fileLedgerFactory struct {
	blkstorageProvider blkstorage.BlockStoreProvider
	ledgers            map[string]blockledger.ReadWriter
	mutex              sync.Mutex
}

// GetOrCreate gets an existing ledger (if it exists) or creates it if it does not
func (flf *fileLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) {
	flf.mutex.Lock()
	defer flf.mutex.Unlock()

	key := chainID
	// check cache
	ledger, ok := flf.ledgers[key]
	if ok {
		return ledger, nil
	}
	// open fresh
	blockStore, err := flf.blkstorageProvider.OpenBlockStore(key)
	if err != nil {
		return nil, err
	}
	ledger = NewFileLedger(blockStore)
	flf.ledgers[key] = ledger
	return ledger, nil
}

// New creates a new ledger factory
func New(directory string, metricsProvider metrics.Provider) (blockledger.Factory, error) {
	p, err := fsblkstorage.NewProvider(
		fsblkstorage.NewConf(directory, -1),
		&blkstorage.IndexConfig{
			AttrsToIndex: []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockNum}},
		metricsProvider,
	)
	if err != nil {
		return nil, err
	}
	return &fileLedgerFactory{
		blkstorageProvider: p,
		ledgers:            make(map[string]blockledger.ReadWriter),
	}, nil
}

As we can see fileLedgerFactory internally creates a fsBlockStoreProvider, which will be used to create fsBlockStore for all channels.

fsBlockStoreProvider is created with blkstorage.IndexableAttrBlockNum

It means only BlockNum will be indexed in underlying DB. Thus, when indexing block, only BlockNum index will be created in DB. As a result, there are 4 kinds of index, in the case of Orderer ledger, only getBlockLocByBlockNum works. Apparently Orderer doesn’t need more indices in DB…

  • getBlockLocByBlockNum
  • getBlockLocByHash(blockHash []byte)
  • getTxIDVal(txID string)
  • getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章