Skip to content

Commit

Permalink
Implement interface SnapshotPvtdataHashesConsumer for pvtdata store (#…
Browse files Browse the repository at this point in the history
…1908)

Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi authored Sep 23, 2020
1 parent e801c2f commit 5bcb5e0
Show file tree
Hide file tree
Showing 17 changed files with 1,930 additions and 95 deletions.
4 changes: 4 additions & 0 deletions core/chaincode/implicitcollection/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ func MspIDIfImplicitCollection(collectionName string) (isImplicitCollection bool
}
return true, collectionName[len(prefix):]
}

func IsImplicitCollection(collectionName string) bool {
return strings.HasPrefix(collectionName, prefix)
}
6 changes: 6 additions & 0 deletions core/chaincode/implicitcollection/name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ func TestMspIDIfImplicitCollection(t *testing.T) {
})
}
}

func TestIsImplicitCollection(t *testing.T) {
require.True(t, IsImplicitCollection("_implicit_org_"))
require.True(t, IsImplicitCollection("_implicit_org_MyOrg"))
require.False(t, IsImplicitCollection("implicit_org_MyOrg"))
}
13 changes: 12 additions & 1 deletion core/common/privdata/membershipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package privdata
import (
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/implicitcollection"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protoutil"
)
Expand All @@ -20,11 +21,17 @@ type MembershipProvider struct {
mspID string
selfSignedData protoutil.SignedData
IdentityDeserializerFactory func(chainID string) msp.IdentityDeserializer
myImplicitCollectionName string
}

// NewMembershipInfoProvider returns MembershipProvider
func NewMembershipInfoProvider(mspID string, selfSignedData protoutil.SignedData, identityDeserializerFunc func(chainID string) msp.IdentityDeserializer) *MembershipProvider {
return &MembershipProvider{mspID: mspID, selfSignedData: selfSignedData, IdentityDeserializerFactory: identityDeserializerFunc}
return &MembershipProvider{
mspID: mspID,
selfSignedData: selfSignedData,
IdentityDeserializerFactory: identityDeserializerFunc,
myImplicitCollectionName: implicitcollection.NameForOrg(mspID),
}
}

// AmMemberOf checks whether the current peer is a member of the given collection config.
Expand Down Expand Up @@ -56,3 +63,7 @@ func (m *MembershipProvider) AmMemberOf(channelName string, collectionPolicyConf

return true, nil
}

func (m *MembershipProvider) MyImplicitCollectionName() string {
return m.myImplicitCollectionName
}
9 changes: 9 additions & 0 deletions core/common/privdata/membershipinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/policydsl"
"github.com/hyperledger/fabric/core/chaincode/implicitcollection"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protoutil"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -63,6 +64,14 @@ func TestMembershipInfoProvider(t *testing.T) {
require.Nil(t, err)
}

func TestMyImplicitCollectionName(t *testing.T) {
require.Equal(
t,
implicitcollection.NameForOrg("my_org"),
NewMembershipInfoProvider("my_org", protoutil.SignedData{}, nil).MyImplicitCollectionName(),
)
}

func getAccessPolicy(signers []string) *peer.CollectionPolicyConfig {
var data [][]byte
for _, signer := range signers {
Expand Down
86 changes: 86 additions & 0 deletions core/ledger/confighistory/confighistorytest/confighistory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package confighistorytest

import (
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/confighistory"
"github.com/hyperledger/fabric/core/ledger/mock"
)

type Mgr struct {
*confighistory.Mgr
mockCCInfoProvider *mock.DeployedChaincodeInfoProvider
}

func NewMgr(dbPath string) (*Mgr, error) {
mockCCInfoProvider := &mock.DeployedChaincodeInfoProvider{}
configHistory, err := confighistory.NewMgr(dbPath, mockCCInfoProvider)
if err != nil {
return nil, err
}
return &Mgr{
Mgr: configHistory,
mockCCInfoProvider: mockCCInfoProvider,
}, nil
}

func (m *Mgr) Setup(ledgerID, namespace string, configHistory map[uint64][]*peer.StaticCollectionConfig) error {
for committingBlk, config := range configHistory {
m.mockCCInfoProvider.UpdatedChaincodesReturns(
[]*ledger.ChaincodeLifecycleInfo{
{
Name: namespace,
},
}, nil,
)

m.mockCCInfoProvider.ChaincodeInfoReturns(
&ledger.DeployedChaincodeInfo{
Name: namespace,
ExplicitCollectionConfigPkg: BuildCollConfigPkg(config),
},
nil,
)

err := m.HandleStateUpdates(
&ledger.StateUpdateTrigger{
LedgerID: ledgerID,
CommittingBlockNum: committingBlk,
},
)
defer m.StateCommitDone(ledgerID)
if err != nil {
return err
}
}
return nil
}

func (m *Mgr) Close() {
m.Mgr.Close()
}

func BuildCollConfigPkg(staticCollectionConfigs []*peer.StaticCollectionConfig) *peer.CollectionConfigPackage {
if len(staticCollectionConfigs) == 0 {
return nil
}
pkg := &peer.CollectionConfigPackage{
Config: []*peer.CollectionConfig{},
}
for _, c := range staticCollectionConfigs {
pkg.Config = append(pkg.Config,
&peer.CollectionConfig{
Payload: &peer.CollectionConfig_StaticCollectionConfig{
StaticCollectionConfig: c,
},
},
)
}
return pkg
}
107 changes: 107 additions & 0 deletions core/ledger/confighistory/confighistorytest/confighistory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package confighistorytest

import (
"fmt"
"io/ioutil"
"math"
"os"
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/stretchr/testify/require"
)

func TestConfigHistory(t *testing.T) {
testDir, err := ioutil.TempDir("", "confighitory-")
require.NoError(t, err)
defer os.RemoveAll(testDir)

mgr, err := NewMgr(testDir)
require.NoError(t, err)
defer mgr.Close()

sampleConfigHistoryNS1 := map[uint64][]*peer.StaticCollectionConfig{
300: {{Name: "coll1"}, {Name: "coll2"}, {Name: "coll3"}},
20: {{Name: "coll1"}, {Name: "coll2"}},
}

sampleConfigHistoryNS2 := map[uint64][]*peer.StaticCollectionConfig{
400: {{Name: "coll4"}},
}

require.NoError(t, mgr.Setup("ledger-1", "ns1", sampleConfigHistoryNS1))
require.NoError(t, mgr.Setup("ledger-1", "ns2", sampleConfigHistoryNS2))

r := mgr.GetRetriever("ledger-1")

testcases := []struct {
inputNS string
inputBlkNum uint64

outputConfig *peer.CollectionConfigPackage
outputBlkNum uint64
}{
{
inputNS: "ns1",
inputBlkNum: math.MaxUint64,
outputConfig: BuildCollConfigPkg(sampleConfigHistoryNS1[300]),
outputBlkNum: 300,
},

{
inputNS: "ns1",
inputBlkNum: 300,
outputConfig: BuildCollConfigPkg(sampleConfigHistoryNS1[20]),
outputBlkNum: 20,
},

{
inputNS: "ns1",
inputBlkNum: 20,
outputConfig: nil,
outputBlkNum: 0,
},

{
inputNS: "ns2",
inputBlkNum: math.MaxUint64,
outputConfig: BuildCollConfigPkg(sampleConfigHistoryNS2[400]),
outputBlkNum: 400,
},

{
inputNS: "ns2",
inputBlkNum: 200,
outputConfig: nil,
outputBlkNum: 0,
},
}

for i, c := range testcases {
t.Run(fmt.Sprintf("testcase-%d", i), func(t *testing.T) {
collectionConfigInfo, err := r.MostRecentCollectionConfigBelow(c.inputBlkNum, c.inputNS)
require.NoError(t, err)

if c.outputConfig == nil {
require.Nil(t, c.outputConfig)
require.Equal(t, uint64(0), c.outputBlkNum)
return
}

require.Equal(t, c.outputBlkNum, collectionConfigInfo.CommittingBlockNum)
require.True(t,
proto.Equal(
collectionConfigInfo.CollectionConfig,
c.outputConfig,
),
)
})
}
}
2 changes: 2 additions & 0 deletions core/ledger/ledger_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ type ChaincodeLifecycleDetails struct {
type MembershipInfoProvider interface {
// AmMemberOf checks whether the current peer is a member of the given collection
AmMemberOf(channelName string, collectionPolicyConfig *peer.CollectionPolicyConfig) (bool, error)
// MyImplicitCollectionName returns the name of the implicit collection for the current peer
MyImplicitCollectionName() string
}

type HealthCheckRegistry interface {
Expand Down
64 changes: 64 additions & 0 deletions core/ledger/mock/membership_info_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion core/ledger/pvtdatapolicy/btlpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ func (p *LSCCBasedBTLPolicy) GetExpiringBlock(namesapce string, collection strin
if err != nil {
return 0, err
}
return ComputeExpiringBlock(namesapce, collection, committingBlock, btl), nil
}

func ComputeExpiringBlock(namesapce, collection string, committingBlock, btl uint64) uint64 {
expiryBlk := committingBlock + btl + uint64(1)
if expiryBlk <= committingBlock { // committingBlk + btl overflows uint64-max
expiryBlk = math.MaxUint64
}
return expiryBlk, nil
return expiryBlk
}

type collectionInfoProvider interface {
Expand Down
Loading

0 comments on commit 5bcb5e0

Please sign in to comment.