github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/txmgmt/rwsetutil/rwset_proto_util.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package rwsetutil 18 19 import ( 20 "github.com/golang/protobuf/proto" 21 "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version" 22 "github.com/hyperledger/fabric/protos/ledger/rwset" 23 "github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset" 24 ) 25 26 // TxRwSet acts as a proxy of 'rwset.TxReadWriteSet' proto message and helps constructing Read-write set specifically for KV data model 27 type TxRwSet struct { 28 NsRwSets []*NsRwSet 29 } 30 31 // NsRwSet encapsulates 'kvrwset.KVRWSet' proto message for a specific name space (chaincode) 32 type NsRwSet struct { 33 NameSpace string 34 KvRwSet *kvrwset.KVRWSet 35 } 36 37 // ToProtoBytes constructs TxReadWriteSet proto message and serializes using protobuf Marshal 38 func (txRwSet *TxRwSet) ToProtoBytes() ([]byte, error) { 39 protoTxRWSet := &rwset.TxReadWriteSet{} 40 protoTxRWSet.DataModel = rwset.TxReadWriteSet_KV 41 for _, nsRwSet := range txRwSet.NsRwSets { 42 protoNsRwSet := &rwset.NsReadWriteSet{} 43 protoNsRwSet.Namespace = nsRwSet.NameSpace 44 protoRwSetBytes, err := proto.Marshal(nsRwSet.KvRwSet) 45 if err != nil { 46 return nil, err 47 } 48 protoNsRwSet.Rwset = protoRwSetBytes 49 protoTxRWSet.NsRwset = append(protoTxRWSet.NsRwset, protoNsRwSet) 50 } 51 protoTxRwSetBytes, err := proto.Marshal(protoTxRWSet) 52 if err != nil { 53 return nil, err 54 } 55 return protoTxRwSetBytes, nil 56 } 57 58 // FromProtoBytes deserializes protobytes into TxReadWriteSet proto message and populates 'TxRwSet' 59 func (txRwSet *TxRwSet) FromProtoBytes(protoBytes []byte) error { 60 protoTxRwSet := &rwset.TxReadWriteSet{} 61 if err := proto.Unmarshal(protoBytes, protoTxRwSet); err != nil { 62 return err 63 } 64 protoNsRwSets := protoTxRwSet.GetNsRwset() 65 var nsRwSet *NsRwSet 66 for _, protoNsRwSet := range protoNsRwSets { 67 nsRwSet = &NsRwSet{} 68 nsRwSet.NameSpace = protoNsRwSet.Namespace 69 protoRwSetBytes := protoNsRwSet.Rwset 70 71 protoKvRwSet := &kvrwset.KVRWSet{} 72 if err := proto.Unmarshal(protoRwSetBytes, protoKvRwSet); err != nil { 73 return err 74 } 75 nsRwSet.KvRwSet = protoKvRwSet 76 txRwSet.NsRwSets = append(txRwSet.NsRwSets, nsRwSet) 77 } 78 return nil 79 } 80 81 // NewKVRead helps constructing proto message kvrwset.KVRead 82 func NewKVRead(key string, version *version.Height) *kvrwset.KVRead { 83 return &kvrwset.KVRead{Key: key, Version: newProtoVersion(version)} 84 } 85 86 // NewVersion helps converting proto message kvrwset.Version to version.Height 87 func NewVersion(protoVersion *kvrwset.Version) *version.Height { 88 if protoVersion == nil { 89 return nil 90 } 91 return version.NewHeight(protoVersion.BlockNum, protoVersion.TxNum) 92 } 93 94 func newProtoVersion(height *version.Height) *kvrwset.Version { 95 if height == nil { 96 return nil 97 } 98 return &kvrwset.Version{BlockNum: height.BlockNum, TxNum: height.TxNum} 99 } 100 101 func newKVWrite(key string, value []byte) *kvrwset.KVWrite { 102 return &kvrwset.KVWrite{Key: key, IsDelete: value == nil, Value: value} 103 }