This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
store.go
280 lines (222 loc) · 7.78 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cdbblkstorage
import (
"encoding/hex"
"fmt"
"path/filepath"
"strconv"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/ledger/blkstorage"
"github.com/hyperledger/fabric/common/ledger/snapshot"
"github.com/pkg/errors"
)
type store struct {
ledgerID string
blockStore couchDB
txnStore couchDB
attachTxn bool
}
func newStore(ledgerID string, blockStore, txnStore couchDB) *store {
return &store{
ledgerID: ledgerID,
blockStore: blockStore,
txnStore: txnStore,
attachTxn: false,
}
}
func (s *store) RetrieveBlockByHash(blockHash []byte) (*common.Block, error) {
logger.Debugf("[%s] Retrieving block from store for hash", s.ledgerID)
blockHashHex := hex.EncodeToString(blockHash)
const queryFmt = `
{
"selector": {
"` + blockHeaderField + `.` + blockHashField + `": {
"$eq": "%s"
}
},
"use_index": ["_design/` + blockHashIndexDoc + `", "` + blockHashIndexName + `"]
}`
block, err := retrieveBlockQuery(s.blockStore, fmt.Sprintf(queryFmt, blockHashHex))
if err != nil {
// note: allow ErrNotFoundInIndex to pass through
return nil, err
}
return block, nil
}
func (s *store) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
logger.Debugf("[%s] Retrieving block [%d] from store", s.ledgerID, blockNum)
id := blockNumberToKey(blockNum)
doc, _, err := s.blockStore.ReadDoc(id)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("retrieval of block [%d] from couchDB failed", blockNum))
}
if doc == nil {
return nil, blkstorage.ErrNotFoundInIndex
}
block, err := couchDocToBlock(doc)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("unmarshal of block [%d] from couchDB failed", blockNum))
}
return block, nil
}
func (s *store) RetrieveTxByID(txID string) (*common.Envelope, error) {
logger.Debugf("[%s] Retrieving transaction [%s] from store", s.ledgerID, txID)
doc, _, err := s.txnStore.ReadDoc(txID)
if err != nil {
// note: allow ErrNotFoundInIndex to pass through
logger.Debugf("[%s] Error retrieving transaction [%s] from store: %s", s.ledgerID, txID, err)
return nil, err
}
if doc == nil {
logger.Debugf("[%s] Transaction [%s] not found", s.ledgerID, txID)
return nil, blkstorage.ErrNotFoundInIndex
}
// If this transaction includes the envelope as a valid attachment then can return immediately.
if len(doc.Attachments) > 0 {
attachedEnv, e := couchAttachmentsToTxnEnvelope(doc.Attachments)
if e == nil {
return attachedEnv, nil
}
logger.Debugf("transaction [%s] has attachment but failed to be extracted into envelope", err)
}
// Otherwise, we need to extract the transaction from the block document.
block, err := s.RetrieveBlockByTxID(txID)
if err != nil {
// note: allow ErrNotFoundInIndex to pass through
return nil, err
}
return extractTxnEnvelopeFromBlock(block, txID)
}
// RetrieveBlockByTxID returns a block for a given transaction ID
func (s *store) RetrieveBlockByTxID(txID string) (*common.Block, error) {
logger.Debugf("[%s] Retrieving block from store for transaction ID [%s]", s.ledgerID, txID)
blockHash, err := s.retrieveBlockHashByTxID(txID)
if err != nil {
return nil, err
}
return s.RetrieveBlockByHash(blockHash)
}
// RetrieveTxValidationCodeByTxID returns a TX validation code for a given transaction ID
func (s *store) RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) {
logger.Debugf("[%s] Retrieving TxValidationCode from store for transaction ID [%s]", s.ledgerID, txID)
jsonResult, err := retrieveJSONQuery(s.txnStore, txID)
if err != nil {
// note: allow ErrNotFoundInIndex to pass through
return peer.TxValidationCode(-1), err
}
txnValidationCodeStoredUT, ok := jsonResult[txnValidationCode]
if !ok {
return peer.TxValidationCode_INVALID_OTHER_REASON, errors.Errorf("validation code was not found for transaction ID [%s]", txID)
}
txnValidationCodeStored, ok := txnValidationCodeStoredUT.(string)
if !ok {
return peer.TxValidationCode_INVALID_OTHER_REASON, errors.Errorf("validation code has invalid type for transaction ID [%s]", txID)
}
const sizeOfTxValidationCode = 32
txnValidationCode, err := strconv.ParseInt(txnValidationCodeStored, txnValidationCodeBase, sizeOfTxValidationCode)
if err != nil {
return peer.TxValidationCode_INVALID_OTHER_REASON, errors.Wrapf(err, "validation code was invalid for transaction ID [%s]", txID)
}
return peer.TxValidationCode(txnValidationCode), nil
}
func (s *store) retrieveBlockHashByTxID(txID string) ([]byte, error) {
jsonResult, err := retrieveJSONQuery(s.txnStore, txID)
if err != nil {
// note: allow ErrNotFoundInIndex to pass through
logger.Errorf("retrieving transaction document from DB failed : %s", err)
return nil, err
}
blockHashStoredUT, ok := jsonResult[txnBlockHashField]
if !ok {
return nil, errors.Errorf("block hash was not found for transaction ID [%s]", txID)
}
blockHashStored, ok := blockHashStoredUT.(string)
if !ok {
return nil, errors.Errorf("block hash has invalid type for transaction ID [%s]", txID)
}
blockHash, err := hex.DecodeString(blockHashStored)
if err != nil {
return nil, errors.Wrapf(err, "block hash was invalid for transaction ID [%s]", txID)
}
return blockHash, nil
}
func (s *store) store(block *common.Block) error {
err := s.storeBlock(block)
if err != nil {
return err
}
return s.storeTransactions(block)
}
func (s *store) storeBlock(block *common.Block) error {
doc, err := blockToCouchDoc(block)
if err != nil {
return errors.WithMessage(err, "converting block to couchDB document failed")
}
logger.Debugf("[%s] Storing block [%d]", s.ledgerID, block.Header.Number)
id := blockNumberToKey(block.GetHeader().GetNumber())
rev, err := s.blockStore.SaveDoc(id, "", doc)
if err != nil {
return errors.WithMessage(err, "adding block to couchDB failed")
}
logger.Debugf("block added to couchDB [%d, %s]", block.GetHeader().GetNumber(), rev)
return nil
}
func (s *store) storeTransactions(block *common.Block) error {
docs, err := blockToTxnCouchDocs(block, s.attachTxn)
if err != nil {
return errors.WithMessage(err, "converting block to couchDB txn documents failed")
}
if len(docs) == 0 {
return nil
}
logger.Debugf("[%s] Storing transactions for block [%d]", s.ledgerID, block.Header.Number)
_, err = s.txnStore.BatchUpdateDocuments(docs)
if err != nil {
return errors.WithMessage(err, "adding block to couchDB failed")
}
logger.Debugf("block transactions added to couchDB [%d]", block.GetHeader().GetNumber())
return nil
}
func (s *store) exportTxIDs(dir string, newHashFunc snapshot.NewHashFunc) ([]byte, uint64, error) {
// Get everything from the DB
// TODO: Is it practical to be returning all rows from a database?
results, _, err := s.txnStore.QueryDocuments(`{"selector": {}}`)
if err != nil {
return nil, 0, err
}
var numTxIDs uint64 = 0
var dataFile *snapshot.FileWriter
for _, r := range results {
if numTxIDs == 0 { // first iteration, create the data file
fileName := filepath.Join(dir, snapshotDataFileName)
dataFile, err = snapshot.CreateFile(fileName, snapshotFileFormat, newHashFunc)
if err != nil {
return nil, 0, err
}
logger.Debugf("[%s] Created file [%s]", s.ledgerID, fileName)
defer func() {
if err = dataFile.Close(); err != nil {
logger.Warnf("Error closing datafile: %s", err)
}
}()
}
logger.Debugf("[%s] Adding TxID [%s]", s.ledgerID, r.ID)
if e := dataFile.EncodeString(r.ID); e != nil {
return nil, 0, e
}
numTxIDs++
}
if dataFile == nil {
logger.Infof("[%s] No data file created", s.ledgerID)
return nil, 0, nil
}
dataHash, err := dataFile.Done()
if err != nil {
return nil, 0, err
}
return dataHash, numTxIDs, nil
}