Skip to content

Commit 49199e3

Browse files
author
Jason Yellick
committed
FAB-13949 Make DeserializeMetadata return sentinal
Today, DeserializeMetadata accepts a parameter whether to return an error on missing or not. This is an odd pattern, and it should really return a sentinal indicating whether or not the metadata was found. Change-Id: Iea76701864a293b9d893b46bc12cc4d7a4ac7ac6 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent 9f7c523 commit 49199e3

File tree

5 files changed

+28
-35
lines changed

5 files changed

+28
-35
lines changed

core/chaincode/lifecycle/deployedcc_infoprovider.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ func (l *Lifecycle) ChaincodeInNewLifecycle(chaincodeName string, qe ledger.Simp
7878
return true, state, nil
7979
}
8080

81-
metadata, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state, false)
81+
metadata, ok, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state)
8282
if err != nil {
8383
return false, nil, errors.WithMessage(err, fmt.Sprintf("could not deserialize metadata for chaincode %s", chaincodeName))
8484
}
8585

86-
if metadata.Datatype == "" {
87-
// If the type is unset, then fallback to the legacy definition
86+
if !ok {
8887
return false, state, nil
8988
}
9089

core/chaincode/lifecycle/legacy_lifecycle.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ func (l *Lifecycle) ChaincodeDefinition(chaincodeName string, qe ledger.SimpleQu
6262
Namespace: LifecycleNamespace,
6363
SimpleQueryExecutor: qe,
6464
}
65-
metadata, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state, false)
65+
metadata, ok, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state)
6666
if err != nil {
6767
return nil, errors.WithMessage(err, fmt.Sprintf("could not deserialize metadata for chaincode %s", chaincodeName))
6868
}
6969

70-
if metadata.Datatype == "" {
71-
// If the type is unset, then fallback to the legacy definition
70+
if !ok {
7271
return l.LegacyImpl.ChaincodeDefinition(chaincodeName, qe)
7372
}
7473

@@ -102,13 +101,12 @@ func (l *Lifecycle) ChaincodeContainerInfo(chaincodeName string, qe ledger.Simpl
102101
Namespace: LifecycleNamespace,
103102
SimpleQueryExecutor: qe,
104103
}
105-
metadata, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state, false)
104+
metadata, ok, err := l.Serializer.DeserializeMetadata(NamespacesName, chaincodeName, state)
106105
if err != nil {
107106
return nil, errors.WithMessage(err, fmt.Sprintf("could not deserialize metadata for chaincode %s", chaincodeName))
108107
}
109108

110-
if metadata.Datatype == "" {
111-
// If the type is unset, then fallback to the legacy definition
109+
if !ok {
112110
return l.LegacyImpl.ChaincodeContainerInfo(chaincodeName, qe)
113111
}
114112

core/chaincode/lifecycle/lifecycle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ var _ = Describe("Lifecycle", func() {
494494

495495
It("returns an error", func() {
496496
_, err := l.QueryChaincodeDefinition("cc-name", fakePublicState)
497-
Expect(err).To(MatchError("could not deserialize namespace cc-name as chaincode: could not unmarshal metadata for namespace namespaces/cc-name: no existing serialized message found"))
497+
Expect(err).To(MatchError("could not deserialize namespace cc-name as chaincode: metadata for namespace namespaces/cc-name does not exist"))
498498
})
499499
})
500500
})

core/chaincode/lifecycle/serializer.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ func (s *Serializer) Serialize(namespace, name string, structure interface{}, st
107107
return errors.WithMessage(err, fmt.Sprintf("structure for namespace %s/%s is not serializable", namespace, name))
108108
}
109109

110-
metadata, err := s.DeserializeMetadata(namespace, name, state, false)
110+
metadata, ok, err := s.DeserializeMetadata(namespace, name, state)
111111
if err != nil {
112112
return errors.WithMessage(err, fmt.Sprintf("could not deserialize metadata for namespace %s/%s", namespace, name))
113113
}
114+
if !ok {
115+
metadata = &lb.StateMetadata{}
116+
}
114117

115118
existingKeys := map[string][]byte{}
116119
for _, existingField := range metadata.Fields {
@@ -275,10 +278,13 @@ func (s *Serializer) Deserialize(namespace, name string, structure interface{},
275278
return errors.WithMessage(err, fmt.Sprintf("could not deserialize namespace %s/%s to unserializable type %T", namespace, name, structure))
276279
}
277280

278-
metadata, err := s.DeserializeMetadata(namespace, name, state, true)
281+
metadata, ok, err := s.DeserializeMetadata(namespace, name, state)
279282
if err != nil {
280283
return errors.Wrapf(err, "could not unmarshal metadata for namespace %s/%s", namespace, name)
281284
}
285+
if !ok {
286+
return errors.Errorf("metadata for namespace %s/%s does not exist", namespace, name)
287+
}
282288

283289
typeName := value.Type().Name()
284290
if typeName != metadata.Datatype {
@@ -329,22 +335,22 @@ func (s *Serializer) Deserialize(namespace, name string, structure interface{},
329335
return nil
330336
}
331337

332-
func (s *Serializer) DeserializeMetadata(namespace, name string, state ReadableState, failOnMissing bool) (*lb.StateMetadata, error) {
338+
func (s *Serializer) DeserializeMetadata(namespace, name string, state ReadableState) (*lb.StateMetadata, bool, error) {
333339
metadataBin, err := state.GetState(fmt.Sprintf("%s/metadata/%s", namespace, name))
334340
if err != nil {
335-
return nil, errors.WithMessage(err, fmt.Sprintf("could not query metadata for namespace %s/%s", namespace, name))
341+
return nil, false, errors.WithMessage(err, fmt.Sprintf("could not query metadata for namespace %s/%s", namespace, name))
336342
}
337-
if metadataBin == nil && failOnMissing {
338-
return nil, errors.Errorf("no existing serialized message found")
343+
if metadataBin == nil {
344+
return nil, false, nil
339345
}
340346

341347
metadata := &lb.StateMetadata{}
342348
err = proto.Unmarshal(metadataBin, metadata)
343349
if err != nil {
344-
return nil, errors.Wrapf(err, "could not unmarshal metadata for namespace %s/%s", namespace, name)
350+
return nil, false, errors.Wrapf(err, "could not unmarshal metadata for namespace %s/%s", namespace, name)
345351
}
346352

347-
return metadata, nil
353+
return metadata, true, nil
348354
}
349355

350356
func (s *Serializer) DeserializeField(namespace, name, field string, state ReadableState) (*lb.StateData, error) {

core/chaincode/lifecycle/serializer_test.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ var _ = Describe("Serializer", func() {
534534
It("fails", func() {
535535
testStruct := &TestStruct{}
536536
err := s.Deserialize("namespaces", "fake", testStruct, fakeState)
537-
Expect(err).To(MatchError("could not unmarshal metadata for namespace namespaces/fake: no existing serialized message found"))
537+
Expect(err).To(MatchError("metadata for namespace namespaces/fake does not exist"))
538538
})
539539
})
540540

@@ -868,8 +868,9 @@ var _ = Describe("Serializer", func() {
868868
})
869869

870870
It("deserializes the metadata", func() {
871-
result, err := s.DeserializeMetadata("namespaces", "fake", fakeState, true)
871+
result, ok, err := s.DeserializeMetadata("namespaces", "fake", fakeState)
872872
Expect(err).NotTo(HaveOccurred())
873+
Expect(ok).To(BeTrue())
873874
Expect(proto.Equal(result, &lb.StateMetadata{Datatype: "TestDatatype"})).To(BeTrue())
874875

875876
Expect(fakeState.GetStateCallCount()).To(Equal(1))
@@ -882,7 +883,7 @@ var _ = Describe("Serializer", func() {
882883
})
883884

884885
It("wraps and returns the error", func() {
885-
_, err := s.DeserializeMetadata("namespaces", "fake", fakeState, true)
886+
_, _, err := s.DeserializeMetadata("namespaces", "fake", fakeState)
886887
Expect(err).To(MatchError("could not query metadata for namespace namespaces/fake: get-state-error"))
887888
})
888889
})
@@ -893,20 +894,9 @@ var _ = Describe("Serializer", func() {
893894
})
894895

895896
It("returns an error", func() {
896-
_, err := s.DeserializeMetadata("namespaces", "fake", fakeState, true)
897-
Expect(err).To(MatchError("no existing serialized message found"))
898-
})
899-
})
900-
901-
Context("when GetState returns nil and missing is allowed", func() {
902-
BeforeEach(func() {
903-
fakeState.GetStateReturns(nil, nil)
904-
})
905-
906-
It("returns an empty metadata", func() {
907-
metadata, err := s.DeserializeMetadata("namespaces", "fake", fakeState, false)
897+
_, ok, err := s.DeserializeMetadata("namespaces", "fake", fakeState)
908898
Expect(err).NotTo(HaveOccurred())
909-
Expect(proto.Equal(metadata, &lb.StateMetadata{})).To(BeTrue())
899+
Expect(ok).To(BeFalse())
910900
})
911901
})
912902

@@ -916,7 +906,7 @@ var _ = Describe("Serializer", func() {
916906
})
917907

918908
It("returns an error", func() {
919-
_, err := s.DeserializeMetadata("namespaces", "fake", fakeState, true)
909+
_, _, err := s.DeserializeMetadata("namespaces", "fake", fakeState)
920910
Expect(err).To(MatchError("could not unmarshal metadata for namespace namespaces/fake: unexpected EOF"))
921911
})
922912
})

0 commit comments

Comments
 (0)