6 Star 47 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cache
import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/msp"
pmsp "github.com/hyperledger/fabric/protos/msp"
"github.com/pkg/errors"
)
const (
deserializeIdentityCacheSize = 100
validateIdentityCacheSize = 100
satisfiesPrincipalCacheSize = 100
)
var mspLogger = flogging.MustGetLogger("msp")
func New(o msp.MSP) (msp.MSP, error) {
mspLogger.Debugf("Creating Cache-MSP instance")
if o == nil {
return nil, errors.Errorf("Invalid passed MSP. It must be different from nil.")
}
theMsp := &cachedMSP{MSP: o}
theMsp.deserializeIdentityCache = newSecondChanceCache(deserializeIdentityCacheSize)
theMsp.satisfiesPrincipalCache = newSecondChanceCache(satisfiesPrincipalCacheSize)
theMsp.validateIdentityCache = newSecondChanceCache(validateIdentityCacheSize)
return theMsp, nil
}
type cachedMSP struct {
msp.MSP
// cache for DeserializeIdentity.
deserializeIdentityCache *secondChanceCache
// cache for validateIdentity
validateIdentityCache *secondChanceCache
// basically a map of principals=>identities=>stringified to booleans
// specifying whether this identity satisfies this principal
satisfiesPrincipalCache *secondChanceCache
}
type cachedIdentity struct {
msp.Identity
cache *cachedMSP
}
func (id *cachedIdentity) SatisfiesPrincipal(principal *pmsp.MSPPrincipal) error {
return id.cache.SatisfiesPrincipal(id.Identity, principal)
}
func (id *cachedIdentity) Validate() error {
return id.cache.Validate(id.Identity)
}
func (c *cachedMSP) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
id, ok := c.deserializeIdentityCache.get(string(serializedIdentity))
if ok {
return &cachedIdentity{
cache: c,
Identity: id.(msp.Identity),
}, nil
}
id, err := c.MSP.DeserializeIdentity(serializedIdentity)
if err == nil {
c.deserializeIdentityCache.add(string(serializedIdentity), id)
return &cachedIdentity{
cache: c,
Identity: id.(msp.Identity),
}, nil
}
return nil, err
}
func (c *cachedMSP) Setup(config *pmsp.MSPConfig) error {
c.cleanCash()
return c.MSP.Setup(config)
}
func (c *cachedMSP) Validate(id msp.Identity) error {
identifier := id.GetIdentifier()
key := string(identifier.Mspid + ":" + identifier.Id)
_, ok := c.validateIdentityCache.get(key)
if ok {
// cache only stores if the identity is valid.
return nil
}
err := c.MSP.Validate(id)
if err == nil {
c.validateIdentityCache.add(key, true)
}
return err
}
func (c *cachedMSP) SatisfiesPrincipal(id msp.Identity, principal *pmsp.MSPPrincipal) error {
identifier := id.GetIdentifier()
identityKey := string(identifier.Mspid + ":" + identifier.Id)
principalKey := string(principal.PrincipalClassification) + string(principal.Principal)
key := identityKey + principalKey
v, ok := c.satisfiesPrincipalCache.get(key)
if ok {
if v == nil {
return nil
}
return v.(error)
}
err := c.MSP.SatisfiesPrincipal(id, principal)
c.satisfiesPrincipalCache.add(key, err)
return err
}
func (c *cachedMSP) cleanCash() error {
c.deserializeIdentityCache = newSecondChanceCache(deserializeIdentityCacheSize)
c.satisfiesPrincipalCache = newSecondChanceCache(satisfiesPrincipalCacheSize)
c.validateIdentityCache = newSecondChanceCache(validateIdentityCacheSize)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v1.4.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385