Skip to content

Commit

Permalink
Moved KVS functionality behind interfaces and grouped them based on d…
Browse files Browse the repository at this point in the history
…omains for decoupling

Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Jan 15, 2025
1 parent e458304 commit 09cb7d7
Show file tree
Hide file tree
Showing 26 changed files with 571 additions and 372 deletions.
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
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
}
95 changes: 23 additions & 72 deletions platform/common/services/sig/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +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
GetExisting(ids ...string) []string
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 @@ -91,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 @@ -151,72 +143,43 @@ 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 {
return len(o.AreMe(identity)) > 0
}

func (o *Service) AreMe(identities ...view.Identity) []string {
idHashes := make([]string, len(identities))
for i, id := range identities {
idHashes[i] = id.UniqueID()
}
result := collections.NewSet[string]()
notFound := make([]string, 0)
notFound := make([]view.Identity, 0)
// check local cache
o.mutex.RLock()
for _, idHash := range idHashes {
if _, ok := o.signers[idHash]; ok {
result.Add(idHash)
for _, id := range identities {
if _, ok := o.signers[id.UniqueID()]; ok {
result.Add(id.UniqueID())
} else {
notFound = append(notFound, idHash)
notFound = append(notFound, id)
}
}
o.mutex.RUnlock()
if len(notFound) == 0 || o.kvs == nil {
if len(notFound) == 0 || o.signerKVS == nil {
return result.ToSlice()
}
// check kvs
keys := make([]string, len(notFound))
for i, idHash := range notFound {
key, err := kvs.CreateCompositeKey("sigService", []string{"signer", idHash})
if err != nil {
logger.Errorf("failed creating composite key: %v", err)

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())
}
keys[i] = key
}

result.Add(o.kvs.GetExisting(keys...)...)

return result.ToSlice()
}

Expand Down Expand Up @@ -341,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

0 comments on commit 09cb7d7

Please sign in to comment.