Skip to content

Commit b973ff7

Browse files
committed
[FAB-9571] Add indirect Validate() MSP caching
The MSP cache, caches the invocation of Validate, however many of the invocations of Validate are invoked indirectly via the identities that the MSP returns via DeserializeIdentity(), and these invocations are not cached, because the bccsp msp's identity simply invokes the bccsp MSP's Validate() which bypasses the cache. Change-Id: If2c47652d080abe43abe5a22854bb13f469cd2dd Signed-off-by: yacovm <yacovm@il.ibm.com>
1 parent 155f5fd commit b973ff7

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

msp/cache/cache.go

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (id *cachedIdentity) SatisfiesPrincipal(principal *pmsp.MSPPrincipal) error
6767
return id.cache.SatisfiesPrincipal(id.Identity, principal)
6868
}
6969

70+
func (id *cachedIdentity) Validate() error {
71+
return id.cache.Validate(id.Identity)
72+
}
73+
7074
func (c *cachedMSP) DeserializeIdentity(serializedIdentity []byte) (msp.Identity, error) {
7175
c.dicMutex.Lock()
7276
id, ok := c.deserializeIdentityCache.Get(string(serializedIdentity))

msp/cache/cache_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,35 @@ func TestValidate(t *testing.T) {
213213
assert.False(t, ok)
214214
}
215215

216+
func TestSatisfiesValidateIndirectCall(t *testing.T) {
217+
mockMSP := &mocks.MockMSP{}
218+
219+
mockIdentity := &mocks.MockIdentity{ID: "Alice"}
220+
mockIdentity.On("Validate").Run(func(_ mock.Arguments) {
221+
panic("shouldn't have invoked the identity method")
222+
})
223+
mockMSP.On("DeserializeIdentity", mock.Anything).Return(mockIdentity, nil).Once()
224+
mockIdentity.On("GetIdentifier").Return(&msp.IdentityIdentifier{Mspid: "MSP", Id: "Alice"})
225+
226+
cache, err := New(mockMSP)
227+
assert.NoError(t, err)
228+
229+
mockMSP.On("Validate", mockIdentity).Return(nil)
230+
231+
// Test that cache returns the correct value, and also use this to prime the cache
232+
err = cache.Validate(mockIdentity)
233+
mockMSP.AssertNumberOfCalls(t, "Validate", 1)
234+
assert.NoError(t, err)
235+
// Get the identity we test the caching on
236+
identity, err := cache.DeserializeIdentity([]byte{1, 2, 3})
237+
assert.NoError(t, err)
238+
// Ensure the identity returned answers what the cached MSP answers.
239+
err = identity.Validate()
240+
assert.NoError(t, err)
241+
// Ensure that although a call to Validate was called, the calls weren't passed on to the backing MSP
242+
mockMSP.AssertNumberOfCalls(t, "Validate", 1)
243+
}
244+
216245
func TestSatisfiesPrincipalIndirectCall(t *testing.T) {
217246
mockMSP := &mocks.MockMSP{}
218247
mockMSPPrincipal := &msp2.MSPPrincipal{PrincipalClassification: msp2.MSPPrincipal_IDENTITY, Principal: []byte{1, 2, 3}}

msp/mocks/mocks.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func (*MockIdentity) GetMSPIdentifier() string {
9696
panic("implement me")
9797
}
9898

99-
func (*MockIdentity) Validate() error {
100-
panic("implement me")
99+
func (m *MockIdentity) Validate() error {
100+
return m.Called().Error(0)
101101
}
102102

103103
func (*MockIdentity) GetOrganizationalUnits() []*msp.OUIdentifier {

0 commit comments

Comments
 (0)