Skip to content

Commit

Permalink
Merge "Declare ledger dependency for coll membership info"
Browse files Browse the repository at this point in the history
  • Loading branch information
manish-sethi authored and Gerrit Code Review committed Aug 28, 2018
2 parents b9d6267 + 4c38b4f commit f81ca9e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
20 changes: 8 additions & 12 deletions core/common/privdata/membershipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ import (
"github.com/hyperledger/fabric/protos/common"
)

// MembershipInfoProvider interface defines an interface to check whether a peer is eligible to a collection or not
type MembershipInfoProvider interface {
// AmMemberOf checks whether the current peer is a member of the given collection config
AmMemberOf(collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error)
}

type membershipProvider struct {
// MembershipProvider can be used to check whether a peer is eligible to a collection or not
type MembershipProvider struct {
selfSignedData common.SignedData
cf CollectionFilter
channelName string
}

func NewMembershipInfoProvider(channelName string, selfSignedData common.SignedData, filter CollectionFilter) MembershipInfoProvider {
return &membershipProvider{channelName: channelName, selfSignedData: selfSignedData, cf: filter}
// NewMembershipInfoProvider returns MembershipProvider
func NewMembershipInfoProvider(selfSignedData common.SignedData, filter CollectionFilter) *MembershipProvider {
return &MembershipProvider{selfSignedData: selfSignedData, cf: filter}
}

func (m *membershipProvider) AmMemberOf(collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error) {
filt, err := m.cf.AccessFilter(m.channelName, collectionPolicyConfig)
// AmMemberOf checks whether the current peer is a member of the given collection config
func (m *MembershipProvider) AmMemberOf(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error) {
filt, err := m.cf.AccessFilter(channelName, collectionPolicyConfig)
if err != nil {
return false, err
}
Expand Down
8 changes: 4 additions & 4 deletions core/common/privdata/membershipinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func TestMembershipInfoProvider(t *testing.T) {
collectionStore := NewSimpleCollectionStore(&mockStoreSupport{})

// verify membership provider returns true
membershipProvider := NewMembershipInfoProvider("test1", peerSelfSignedData, collectionStore)
res, err := membershipProvider.AmMemberOf(getAccessPolicy([]string{"peer0", "peer1"}))
membershipProvider := NewMembershipInfoProvider(peerSelfSignedData, collectionStore)
res, err := membershipProvider.AmMemberOf("test1", getAccessPolicy([]string{"peer0", "peer1"}))
assert.True(t, res)
assert.Nil(t, err)

// verify membership provider returns false
res, err = membershipProvider.AmMemberOf(getAccessPolicy([]string{"peer2", "peer3"}))
res, err = membershipProvider.AmMemberOf("test1", getAccessPolicy([]string{"peer2", "peer3"}))
assert.False(t, res)
assert.Nil(t, err)

// verify membership provider returns nil and error
res, err = membershipProvider.AmMemberOf(nil)
res, err = membershipProvider.AmMemberOf("test1", nil)
assert.False(t, res)
assert.Error(t, err)
assert.Equal(t, "Collection config policy is nil", err.Error())
Expand Down
8 changes: 8 additions & 0 deletions core/ledger/ledger_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type Initializer struct {
StateListeners []StateListener
DeployedChaincodeInfoProvider DeployedChaincodeInfoProvider
MembershipInfoProvider MembershipInfoProvider
}

// PeerLedgerProvider provides handle to ledger instances
Expand Down Expand Up @@ -439,4 +440,11 @@ type ChaincodeLifecycleDetails struct {
CollectionsRemoved []string // names of the collections that are removed
}

// MembershipInfoProvider is a dependency that is used by ledger to determine whether the current peer is
// a member of a collection. Gossip module is expected to provide the dependency to ledger
type MembershipInfoProvider interface {
// AmMemberOf checks whether the current peer is a member of the given collection
AmMemberOf(channelName string, collectionPolicyConfig *common.CollectionPolicyConfig) (bool, error)
}

//go:generate counterfeiter -o mock/deployed_ccinfo_provider.go -fake-name DeployedChaincodeInfoProvider . DeployedChaincodeInfoProvider
2 changes: 2 additions & 0 deletions core/ledger/ledgermgmt/ledger_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Initializer struct {
CustomTxProcessors customtx.Processors
PlatformRegistry *platforms.Registry
DeployedChaincodeInfoProvider ledger.DeployedChaincodeInfoProvider
MembershipInfoProvider ledger.MembershipInfoProvider
}

// Initialize initializes ledgermgmt
Expand All @@ -64,6 +65,7 @@ func initialize(initializer *Initializer) {
provider.Initialize(&ledger.Initializer{
StateListeners: finalStateListeners,
DeployedChaincodeInfoProvider: initializer.DeployedChaincodeInfoProvider,
MembershipInfoProvider: initializer.MembershipInfoProvider,
})

ledgerProvider = provider
Expand Down

0 comments on commit f81ca9e

Please sign in to comment.