Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple KVS implementation #727

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions docs/fabric/fabricdev/core/fabricdev/channelprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import (
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/events"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/hash"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/kvs"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/metrics"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/trace"
)

type provider struct {
kvss *kvs.KVS
envelopeKVS driver.EnvelopeKVS
metadataKVS driver.MetadataKVS
endorseTxKVS driver.EndorseTxKVS
publisher events.Publisher
hasher hash.Hasher
newVault generic.VaultConstructor
Expand All @@ -45,7 +46,9 @@ type provider struct {
}

func NewChannelProvider(
kvss *kvs.KVS,
envelopeKVS driver.EnvelopeKVS,
metadataKVS driver.MetadataKVS,
endorseTxKVS driver.EndorseTxKVS,
publisher events.Publisher,
hasher hash.Hasher,
tracerProvider trace.TracerProvider,
Expand All @@ -58,7 +61,9 @@ func NewChannelProvider(
acceptedHeaderTypes []common.HeaderType,
) *provider {
return &provider{
kvss: kvss,
envelopeKVS: envelopeKVS,
metadataKVS: metadataKVS,
endorseTxKVS: endorseTxKVS,
publisher: publisher,
hasher: hasher,
newVault: newVault,
Expand Down Expand Up @@ -90,9 +95,9 @@ func (p *provider) NewChannel(nw driver.FabricNetworkService, channelName string
return nil, err
}

envelopeService := transaction.NewEnvelopeService(p.kvss, nw.Name(), channelName)
transactionService := transaction.NewEndorseTransactionService(p.kvss, nw.Name(), channelName)
metadataService := transaction.NewMetadataService(p.kvss, nw.Name(), channelName)
envelopeService := transaction.NewEnvelopeService(p.envelopeKVS, nw.Name(), channelName)
transactionService := transaction.NewEndorseTransactionService(p.endorseTxKVS, nw.Name(), channelName)
metadataService := transaction.NewMetadataService(p.metadataKVS, nw.Name(), channelName)
peerService := services.NewClientFactory(nw.ConfigService(), nw.LocalMembership().DefaultSigningIdentity())

// Fabric finality
Expand Down
7 changes: 6 additions & 1 deletion docs/fabric/fabricdev/core/fabricdev/driver/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Provider struct {
}

func NewProvider(
envelopeKVS fdriver.EnvelopeKVS,
metadataKVS fdriver.MetadataKVS,
endorseTxKVS fdriver.EndorseTxKVS,
configProvider config.Provider,
metricsProvider metrics.Provider,
endpointService identity.EndpointService,
Expand All @@ -65,7 +68,9 @@ func NewProvider(
return &Provider{
configProvider: configProvider,
channelProvider: fabricdev.NewChannelProvider(
kvss,
envelopeKVS,
metadataKVS,
endorseTxKVS,
publisher,
hasher,
tracerProvider,
Expand Down
8 changes: 8 additions & 0 deletions docs/fabric/fabricdev/sdk/fabricdev/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/driver/config"
mspdriver "github.com/hyperledger-labs/fabric-smart-client/platform/fabric/core/generic/msp/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
vdriver "github.com/hyperledger-labs/fabric-smart-client/platform/view/driver"
dbdriver "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/events"
Expand All @@ -24,6 +25,10 @@ import (

func NewDriver(in struct {
dig.In

EnvelopeKVS driver.EnvelopeKVS
MetadataKVS driver.MetadataKVS
EndorseTxKVS driver.EndorseTxKVS
ConfigProvider config.Provider
MetricsProvider metrics.Provider
EndpointService vdriver.EndpointService
Expand All @@ -39,6 +44,9 @@ func NewDriver(in struct {
d := core.NamedDriver{
Name: "fabricdev",
Driver: fdevdriver.NewProvider(
in.EnvelopeKVS,
in.MetadataKVS,
in.EndorseTxKVS,
in.ConfigProvider,
in.MetricsProvider,
in.EndpointService,
Expand Down
2 changes: 1 addition & 1 deletion integration/nwo/fabric/topology/core_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fabric:
{{ FabricName }}:
default: {{ DefaultNetwork }}
driver: {{ Driver }}
mspCacheSize: 3
mspCacheSize: 500
defaultMSP: {{ Peer.DefaultIdentity }}
msps: {{ range Peer.Identities }}
- id: {{ .ID }}
Expand Down
50 changes: 50 additions & 0 deletions platform/common/driver/kvs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package driver

import (
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)

type SignerEntry struct {
Signer Signer
DebugStack []byte
}

type SignerKVS interface {
GetSigner(id view.Identity) (*SignerEntry, error)
FilterExistingSigners(ids ...view.Identity) ([]view.Identity, error)
PutSigner(id view.Identity, entry *SignerEntry) error
}

type AuditInfoKVS interface {
GetAuditInfo(id view.Identity) ([]byte, error)
PutAuditInfo(id view.Identity, info []byte) error
}

type BindingKVS interface {
GetBinding(ephemeral view.Identity) (view.Identity, error)
PutBinding(ephemeral, longTerm view.Identity) error
}

type MetadataKVS[K any, M any] interface {
GetMetadata(key K) (M, error)
ExistMetadata(key K) (bool, error)
PutMetadata(key K, transientMap M) error
}

type EnvelopeKVS[K any] interface {
GetEnvelope(key K) ([]byte, error)
ExistsEnvelope(key K) (bool, error)
PutEnvelope(key K, env []byte) error
}

type EndorseTxKVS[K any] interface {
GetEndorseTx(key K) ([]byte, error)
ExistsEndorseTx(key K) (bool, error)
PutEndorseTx(key K, etx []byte) error
}
3 changes: 3 additions & 0 deletions platform/common/driver/sigservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ type SigService interface {

// IsMe returns true if a signer was ever registered for the passed identity
IsMe(identity view.Identity) bool

// AreMe returns the hashes of the passed identities that have a signer registered before
AreMe(identities ...view.Identity) []string
}

// AuditRegistry models a repository of identities' audit information
Expand Down
100 changes: 35 additions & 65 deletions platform/common/services/sig/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,41 @@ import (
"runtime/debug"
"sync"

driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/kvs"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)

var logger = logging.MustGetLogger("common-sdk.sig")

type KVS interface {
Exists(id string) bool
Put(id string, state interface{}) error
Get(id string, state interface{}) error
}

type VerifierEntry struct {
Verifier driver.Verifier
DebugStack []byte
}

type SignerEntry struct {
Signer driver.Signer
DebugStack []byte
}
type SignerEntry = driver2.SignerEntry

type Service struct {
deserializer Deserializer
kvs KVS
signerKVS driver2.SignerKVS
auditInfoKVS driver2.AuditInfoKVS

mutex sync.RWMutex
signers map[string]SignerEntry
verifiers map[string]VerifierEntry
}

func NewService(deserializer Deserializer, kvs KVS) *Service {
func NewService(deserializer Deserializer, auditInfoKVS driver2.AuditInfoKVS, signerKVS driver2.SignerKVS) *Service {
return &Service{
signerKVS: signerKVS,
auditInfoKVS: auditInfoKVS,
signers: map[string]SignerEntry{},
verifiers: map[string]VerifierEntry{},
deserializer: deserializer,
kvs: kvs,
}
}

Expand Down Expand Up @@ -89,8 +83,8 @@ func (o *Service) RegisterSigner(identity view.Identity, signer driver.Signer, v
o.signers[idHash] = entry
o.mutex.Unlock()

if o.kvs != nil {
if err := o.registerSigner(idHash, &entry); err != nil {
if o.signerKVS != nil {
if err := o.signerKVS.PutSigner(identity, &entry); err != nil {
o.deleteSigner(idHash)
return errors.Wrap(err, "failed to store entry in kvs for the passed signer")
}
Expand Down Expand Up @@ -149,56 +143,44 @@ func (o *Service) RegisterVerifier(identity view.Identity, verifier driver.Verif
}

func (o *Service) RegisterAuditInfo(identity view.Identity, info []byte) error {
k := kvs.CreateCompositeKeyOrPanic(
"fsc.platform.view.sig",
[]string{
identity.String(),
},
)
if err := o.kvs.Put(k, info); err != nil {
return err
}
return nil
return o.auditInfoKVS.PutAuditInfo(identity, info)
}

func (o *Service) GetAuditInfo(identity view.Identity) ([]byte, error) {
k := kvs.CreateCompositeKeyOrPanic(
"fsc.platform.view.sig",
[]string{
identity.String(),
},
)

if !o.kvs.Exists(k) {
return nil, nil
}
var res []byte
if err := o.kvs.Get(k, &res); err != nil {
return nil, err
}
return res, nil
return o.auditInfoKVS.GetAuditInfo(identity)
}

func (o *Service) IsMe(identity view.Identity) bool {
idHash := identity.UniqueID()
return len(o.AreMe(identity)) > 0
}

func (o *Service) AreMe(identities ...view.Identity) []string {
result := collections.NewSet[string]()
notFound := make([]view.Identity, 0)
// check local cache
o.mutex.RLock()
_, ok := o.signers[idHash]
for _, id := range identities {
if _, ok := o.signers[id.UniqueID()]; ok {
result.Add(id.UniqueID())
} else {
notFound = append(notFound, id)
}
}
o.mutex.RUnlock()
if ok {
return true
if len(notFound) == 0 || o.signerKVS == nil {
return result.ToSlice()
}
// check kvs
if o.kvs != nil {
k, err := kvs.CreateCompositeKey("sigService", []string{"signer", idHash})
if err != nil {
return false
}
if o.kvs.Exists(k) {
return true

if existing, err := o.signerKVS.FilterExistingSigners(notFound...); err != nil {
logger.Errorf("failed getting existing signers: %v", err)
} else {
for _, id := range existing {
result.Add(id.UniqueID())
}
}
return false

return result.ToSlice()
}

func (o *Service) Info(id view.Identity) string {
Expand Down Expand Up @@ -322,18 +304,6 @@ func (o *Service) GetSigningIdentity(identity view.Identity) (driver.SigningIden
}, nil
}

func (o *Service) registerSigner(id string, signer *SignerEntry) error {
k, err := kvs.CreateCompositeKey("sigService", []string{"signer", id})
if err != nil {
return errors.Wrap(err, "failed to create composite key to store entry in kvs")
}
err = o.kvs.Put(k, signer)
if err != nil {
return errors.Wrap(err, "failed to store entry in kvs for the passed signer")
}
return nil
}

func (o *Service) deleteSigner(id string) {
o.mutex.Lock()
defer o.mutex.Unlock()
Expand Down
19 changes: 12 additions & 7 deletions platform/fabric/core/generic/channelprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
driver2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/events"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/hash"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/kvs"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/metrics"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/pkg/errors"
Expand Down Expand Up @@ -69,7 +68,9 @@ type ChannelProvider interface {
}

type provider struct {
kvss *kvs.KVS
envelopeKVS driver.EnvelopeKVS
metadataKVS driver.MetadataKVS
endorserTxKVS driver.EndorseTxKVS
publisher events.Publisher
hasher hash.Hasher
newVault VaultConstructor
Expand All @@ -87,7 +88,9 @@ type provider struct {
}

func NewChannelProvider(
kvss *kvs.KVS,
envelopeKVS driver.EnvelopeKVS,
metadataKVS driver.MetadataKVS,
endorserTxKVS driver.EndorseTxKVS,
publisher events.Publisher,
hasher hash.Hasher,
tracerProvider trace.TracerProvider,
Expand All @@ -104,7 +107,9 @@ func NewChannelProvider(
acceptedHeaderTypes []common.HeaderType,
) *provider {
return &provider{
kvss: kvss,
envelopeKVS: envelopeKVS,
metadataKVS: metadataKVS,
endorserTxKVS: endorserTxKVS,
publisher: publisher,
hasher: hasher,
newVault: newVault,
Expand Down Expand Up @@ -141,9 +146,9 @@ func (p *provider) NewChannel(nw driver.FabricNetworkService, channelName string
return nil, err
}

envelopeService := transaction.NewEnvelopeService(p.kvss, nw.Name(), channelName)
transactionService := transaction.NewEndorseTransactionService(p.kvss, nw.Name(), channelName)
metadataService := transaction.NewMetadataService(p.kvss, nw.Name(), channelName)
envelopeService := transaction.NewEnvelopeService(p.envelopeKVS, nw.Name(), channelName)
transactionService := transaction.NewEndorseTransactionService(p.endorserTxKVS, nw.Name(), channelName)
metadataService := transaction.NewMetadataService(p.metadataKVS, nw.Name(), channelName)
peerService := services.NewClientFactory(nw.ConfigService(), nw.LocalMembership().DefaultSigningIdentity())

// Fabric finality
Expand Down
Loading
Loading