3 Star 6 Fork 7

Gitee 极速下载/Hyperledger fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
ledger_mgmt.go 4.22 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ledgermgmt
import (
"errors"
"sync"
"fmt"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
var logger = flogging.MustGetLogger("ledgermgmt")
// ErrLedgerAlreadyOpened is thrown by a CreateLedger call if a ledger with the given id is already opened
var ErrLedgerAlreadyOpened = errors.New("Ledger already opened")
// ErrLedgerMgmtNotInitialized is thrown when ledger mgmt is used before initializing this
var ErrLedgerMgmtNotInitialized = errors.New("ledger mgmt should be initialized before using")
var openedLedgers map[string]ledger.PeerLedger
var ledgerProvider ledger.PeerLedgerProvider
var lock sync.Mutex
var initialized bool
var once sync.Once
// Initialize initializes ledgermgmt
func Initialize() {
once.Do(func() {
initialize()
})
}
func initialize() {
logger.Info("Initializing ledger mgmt")
lock.Lock()
defer lock.Unlock()
initialized = true
openedLedgers = make(map[string]ledger.PeerLedger)
provider, err := kvledger.NewProvider()
if err != nil {
panic(fmt.Errorf("Error in instantiating ledger provider: %s", err))
}
ledgerProvider = provider
logger.Info("ledger mgmt initialized")
}
// CreateLedger creates a new ledger with the given genesis block.
// This function guarantees that the creation of ledger and committing the genesis block would an atomic action
// The chain id retrieved from the genesis block is treated as a ledger id
func CreateLedger(genesisBlock *common.Block) (ledger.PeerLedger, error) {
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
id, err := utils.GetChainIDFromBlock(genesisBlock)
if err != nil {
return nil, err
}
logger.Infof("Creating ledger [%s] with genesis block", id)
l, err := ledgerProvider.Create(genesisBlock)
if err != nil {
return nil, err
}
l = wrapLedger(id, l)
openedLedgers[id] = l
logger.Infof("Created ledger [%s] with genesis block", id)
return l, nil
}
// OpenLedger returns a ledger for the given id
func OpenLedger(id string) (ledger.PeerLedger, error) {
logger.Infof("Opening ledger with id = %s", id)
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
l, ok := openedLedgers[id]
if ok {
return nil, ErrLedgerAlreadyOpened
}
l, err := ledgerProvider.Open(id)
if err != nil {
return nil, err
}
l = wrapLedger(id, l)
openedLedgers[id] = l
logger.Infof("Opened ledger with id = %s", id)
return l, nil
}
// GetLedgerIDs returns the ids of the ledgers created
func GetLedgerIDs() ([]string, error) {
lock.Lock()
defer lock.Unlock()
if !initialized {
return nil, ErrLedgerMgmtNotInitialized
}
return ledgerProvider.List()
}
// Close closes all the opened ledgers and any resources held for ledger management
func Close() {
logger.Infof("Closing ledger mgmt")
lock.Lock()
defer lock.Unlock()
if !initialized {
return
}
for _, l := range openedLedgers {
l.(*closableLedger).closeWithoutLock()
}
ledgerProvider.Close()
openedLedgers = nil
logger.Infof("ledger mgmt closed")
}
func wrapLedger(id string, l ledger.PeerLedger) ledger.PeerLedger {
return &closableLedger{id, l}
}
// closableLedger extends from actual validated ledger and overwrites the Close method
type closableLedger struct {
id string
ledger.PeerLedger
}
// Close closes the actual ledger and removes the entries from opened ledgers map
func (l *closableLedger) Close() {
lock.Lock()
defer lock.Unlock()
l.closeWithoutLock()
}
func (l *closableLedger) closeWithoutLock() {
l.PeerLedger.Close()
delete(openedLedgers, l.id)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/fabric.git
git@gitee.com:mirrors/fabric.git
mirrors
fabric
Hyperledger fabric
v1.0.4

搜索帮助