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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值