|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 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 ledgerstorage |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/hyperledger/fabric/common/ledger/blkstorage" |
| 24 | + "github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage" |
| 25 | + "github.com/hyperledger/fabric/core/ledger" |
| 26 | + "github.com/hyperledger/fabric/core/ledger/ledgerconfig" |
| 27 | + "github.com/hyperledger/fabric/core/ledger/pvtdatastorage" |
| 28 | + "github.com/hyperledger/fabric/protos/common" |
| 29 | +) |
| 30 | + |
| 31 | +// Provider encapusaltes two providers 1) block store provider and 2) and pvt data store provider |
| 32 | +type Provider struct { |
| 33 | + blkStoreProvider blkstorage.BlockStoreProvider |
| 34 | + pvtdataStoreProvider pvtdatastorage.Provider |
| 35 | +} |
| 36 | + |
| 37 | +// Store encapsulates two stores 1) block store and pvt data store |
| 38 | +type Store struct { |
| 39 | + blkstorage.BlockStore |
| 40 | + pvtdataStore pvtdatastorage.Store |
| 41 | + rwlock *sync.RWMutex |
| 42 | +} |
| 43 | + |
| 44 | +// NewProvider returns the handle to the provider |
| 45 | +func NewProvider() *Provider { |
| 46 | + // Initialize the block storage |
| 47 | + attrsToIndex := []blkstorage.IndexableAttr{ |
| 48 | + blkstorage.IndexableAttrBlockHash, |
| 49 | + blkstorage.IndexableAttrBlockNum, |
| 50 | + blkstorage.IndexableAttrTxID, |
| 51 | + blkstorage.IndexableAttrBlockNumTranNum, |
| 52 | + blkstorage.IndexableAttrBlockTxID, |
| 53 | + blkstorage.IndexableAttrTxValidationCode, |
| 54 | + } |
| 55 | + indexConfig := &blkstorage.IndexConfig{AttrsToIndex: attrsToIndex} |
| 56 | + blockStoreProvider := fsblkstorage.NewProvider( |
| 57 | + fsblkstorage.NewConf(ledgerconfig.GetBlockStorePath(), ledgerconfig.GetMaxBlockfileSize()), |
| 58 | + indexConfig) |
| 59 | + |
| 60 | + pvtStoreProvider := pvtdatastorage.NewProvider() |
| 61 | + return &Provider{blockStoreProvider, pvtStoreProvider} |
| 62 | +} |
| 63 | + |
| 64 | +// Open opens the store |
| 65 | +func (p *Provider) Open(ledgerid string) (*Store, error) { |
| 66 | + var blockStore blkstorage.BlockStore |
| 67 | + var pvtdataStore pvtdatastorage.Store |
| 68 | + var err error |
| 69 | + if blockStore, err = p.blkStoreProvider.OpenBlockStore(ledgerid); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + if pvtdataStore, err = p.pvtdataStoreProvider.OpenStore(ledgerid); err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + store := &Store{blockStore, pvtdataStore, &sync.RWMutex{}} |
| 76 | + if err := store.init(); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + return store, nil |
| 80 | +} |
| 81 | + |
| 82 | +// Close closes the provider |
| 83 | +func (p *Provider) Close() { |
| 84 | + p.blkStoreProvider.Close() |
| 85 | + p.pvtdataStoreProvider.Close() |
| 86 | +} |
| 87 | + |
| 88 | +// CommitWithPvtData commits the block and the corresponding pvt data in an atomic operation |
| 89 | +func (s *Store) CommitWithPvtData(blockAndPvtdata *ledger.BlockAndPvtData) error { |
| 90 | + s.rwlock.Lock() |
| 91 | + defer s.rwlock.Unlock() |
| 92 | + var pvtdata []*ledger.TxPvtData |
| 93 | + for _, v := range blockAndPvtdata.BlockPvtData { |
| 94 | + pvtdata = append(pvtdata, v) |
| 95 | + } |
| 96 | + if err := s.pvtdataStore.Prepare(blockAndPvtdata.Block.Header.Number, pvtdata); err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + if err := s.AddBlock(blockAndPvtdata.Block); err != nil { |
| 100 | + s.pvtdataStore.Rollback() |
| 101 | + return err |
| 102 | + } |
| 103 | + return s.pvtdataStore.Commit() |
| 104 | +} |
| 105 | + |
| 106 | +// GetPvtDataAndBlockByNum returns the block and the corresponding pvt data. |
| 107 | +// The pvt data is filtered by the list of 'collections' supplied |
| 108 | +func (s *Store) GetPvtDataAndBlockByNum(blockNum uint64, filter ledger.PvtNsCollFilter) (*ledger.BlockAndPvtData, error) { |
| 109 | + s.rwlock.RLock() |
| 110 | + defer s.rwlock.RUnlock() |
| 111 | + |
| 112 | + var block *common.Block |
| 113 | + var pvtdata []*ledger.TxPvtData |
| 114 | + var err error |
| 115 | + if block, err = s.RetrieveBlockByNumber(blockNum); err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + if pvtdata, err = s.GetPvtDataByNum(blockNum, filter); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + return &ledger.BlockAndPvtData{Block: block, BlockPvtData: constructPvtdataMap(pvtdata)}, nil |
| 122 | +} |
| 123 | + |
| 124 | +// GetPvtDataByNum returns only the pvt data corresponding to the given block number |
| 125 | +// The pvt data is filtered by the list of 'ns/collections' supplied in the filter |
| 126 | +// A nil filter does not filter any results |
| 127 | +func (s *Store) GetPvtDataByNum(blockNum uint64, filter ledger.PvtNsCollFilter) ([]*ledger.TxPvtData, error) { |
| 128 | + s.rwlock.RLock() |
| 129 | + defer s.rwlock.RUnlock() |
| 130 | + |
| 131 | + var pvtdata []*ledger.TxPvtData |
| 132 | + var err error |
| 133 | + if pvtdata, err = s.pvtdataStore.GetPvtDataByBlockNum(blockNum, filter); err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + return pvtdata, nil |
| 137 | +} |
| 138 | + |
| 139 | +// init checks whether the block storage and pvt data store are in sync |
| 140 | +// this is called when the store instance is constructed and handed over for the use. |
| 141 | +// this check whether there is a pending batch (possibly from a previous system crash) |
| 142 | +// of pvt data that was not committed. If a pending batch exists, the check is made |
| 143 | +// whether the associated block was successfully committed in the block storage (before the crash) |
| 144 | +// or not. If the block was committed, the private data batch is committed |
| 145 | +// otherwise, the pvt data batch is rolledback |
| 146 | +func (s *Store) init() error { |
| 147 | + var pendingPvtbatch bool |
| 148 | + var err error |
| 149 | + if pendingPvtbatch, err = s.pvtdataStore.HasPendingBatch(); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + if !pendingPvtbatch { |
| 153 | + return nil |
| 154 | + } |
| 155 | + var bcInfo *common.BlockchainInfo |
| 156 | + var pvtdataStoreHt uint64 |
| 157 | + |
| 158 | + if bcInfo, err = s.GetBlockchainInfo(); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + if pvtdataStoreHt, err = s.pvtdataStore.LastCommittedBlockHeight(); err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + if bcInfo.Height == pvtdataStoreHt { |
| 166 | + return s.pvtdataStore.Rollback() |
| 167 | + } |
| 168 | + |
| 169 | + if bcInfo.Height == pvtdataStoreHt+1 { |
| 170 | + return s.pvtdataStore.Commit() |
| 171 | + } |
| 172 | + |
| 173 | + return fmt.Errorf("This is not expected. blockStoreHeight=%d, pvtdataStoreHeight=%d", bcInfo.Height, pvtdataStoreHt) |
| 174 | +} |
| 175 | + |
| 176 | +func constructPvtdataMap(pvtdata []*ledger.TxPvtData) map[uint64]*ledger.TxPvtData { |
| 177 | + if pvtdata == nil { |
| 178 | + return nil |
| 179 | + } |
| 180 | + m := make(map[uint64]*ledger.TxPvtData) |
| 181 | + for _, pvtdatum := range pvtdata { |
| 182 | + m[pvtdatum.SeqInBlock] = pvtdatum |
| 183 | + } |
| 184 | + return m |
| 185 | +} |
0 commit comments