forked from hyperledger-archives/fabric
/
blockchain_indexes.go
executable file
·222 lines (186 loc) · 6.91 KB
/
blockchain_indexes.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package ledger
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/op/go-logging"
"github.com/openblockchain/obc-peer/openchain/db"
"github.com/openblockchain/obc-peer/protos"
"github.com/tecbot/gorocksdb"
)
var indexLogger = logging.MustGetLogger("indexes")
var prefixBlockHashKey = byte(1)
var prefixTxUUIDKey = byte(2)
var prefixAddressBlockNumCompositeKey = byte(3)
type blockchainIndexer interface {
isSynchronous() bool
start(blockchain *blockchain) error
createIndexesSync(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error
createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error
fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error)
fetchTransactionIndexByUUID(txUUID string) (uint64, uint64, error)
stop()
}
// Implementation for sync indexer
type blockchainIndexerSync struct {
}
func newBlockchainIndexerSync() *blockchainIndexerSync {
return &blockchainIndexerSync{}
}
func (indexer *blockchainIndexerSync) isSynchronous() bool {
return true
}
func (indexer *blockchainIndexerSync) start(blockchain *blockchain) error {
return nil
}
func (indexer *blockchainIndexerSync) createIndexesSync(
block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
return addIndexDataForPersistence(block, blockNumber, blockHash, writeBatch)
}
func (indexer *blockchainIndexerSync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error {
return fmt.Errorf("Method not applicable")
}
func (indexer *blockchainIndexerSync) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) {
return fetchBlockNumberByBlockHashFromDB(blockHash)
}
func (indexer *blockchainIndexerSync) fetchTransactionIndexByUUID(txUUID string) (uint64, uint64, error) {
return fetchTransactionIndexByUUIDFromDB(txUUID)
}
func (indexer *blockchainIndexerSync) stop() {
return
}
// Functions for persisting and retrieving index data
func addIndexDataForPersistence(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
openchainDB := db.GetDBHandle()
cf := openchainDB.IndexesCF
// add blockhash -> blockNumber
indexLogger.Debug("Indexing block number [%d] by hash = [%x]", blockNumber, blockHash)
writeBatch.PutCF(cf, encodeBlockHashKey(blockHash), encodeBlockNumber(blockNumber))
addressToTxIndexesMap := make(map[string][]uint64)
addressToChaincodeIDsMap := make(map[string][]*protos.ChaincodeID)
transactions := block.GetTransactions()
for txIndex, tx := range transactions {
// add TxUUID -> (blockNumber,indexWithinBlock)
writeBatch.PutCF(cf, encodeTxUUIDKey(tx.Uuid), encodeBlockNumTxIndex(blockNumber, uint64(txIndex)))
txExecutingAddress := getTxExecutingAddress(tx)
addressToTxIndexesMap[txExecutingAddress] = append(addressToTxIndexesMap[txExecutingAddress], uint64(txIndex))
switch tx.Type {
case protos.Transaction_CHAINCODE_NEW, protos.Transaction_CHAINCODE_UPDATE:
authroizedAddresses, chaincodeID := getAuthorisedAddresses(tx)
for _, authroizedAddress := range authroizedAddresses {
addressToChaincodeIDsMap[authroizedAddress] = append(addressToChaincodeIDsMap[authroizedAddress], chaincodeID)
}
}
}
for address, txsIndexes := range addressToTxIndexesMap {
writeBatch.PutCF(cf, encodeAddressBlockNumCompositeKey(address, blockNumber), encodeListTxIndexes(txsIndexes))
}
return nil
}
func fetchBlockNumberByBlockHashFromDB(blockHash []byte) (uint64, error) {
blockNumberBytes, err := db.GetDBHandle().GetFromIndexesCF(encodeBlockHashKey(blockHash))
if err != nil {
return 0, err
}
blockNumber := decodeBlockNumber(blockNumberBytes)
return blockNumber, nil
}
func fetchTransactionIndexByUUIDFromDB(txUUID string) (uint64, uint64, error) {
blockNumTxIndexBytes, err := db.GetDBHandle().GetFromIndexesCF(encodeTxUUIDKey(txUUID))
if err != nil {
return 0, 0, err
}
if blockNumTxIndexBytes == nil {
return 0, 0, ErrResourceNotFound
}
return decodeBlockNumTxIndex(blockNumTxIndexBytes)
}
func getTxExecutingAddress(tx *protos.Transaction) string {
// TODO Fetch address form tx
return "address1"
}
func getAuthorisedAddresses(tx *protos.Transaction) ([]string, *protos.ChaincodeID) {
// TODO fetch address from chaincode deployment tx
// TODO this method should also return error
data := tx.ChaincodeID
cID := &protos.ChaincodeID{}
err := proto.Unmarshal(data, cID)
if err != nil {
return nil, nil
}
return []string{"address1", "address2"}, cID
}
// functions for encoding/decoding db keys/values for index data
// encode / decode BlockNumber
func encodeBlockNumber(blockNumber uint64) []byte {
return proto.EncodeVarint(blockNumber)
}
func decodeBlockNumber(blockNumberBytes []byte) (blockNumber uint64) {
blockNumber, _ = proto.DecodeVarint(blockNumberBytes)
return
}
// encode / decode BlockNumTxIndex
func encodeBlockNumTxIndex(blockNumber uint64, txIndexInBlock uint64) []byte {
b := proto.NewBuffer([]byte{})
b.EncodeVarint(blockNumber)
b.EncodeVarint(txIndexInBlock)
return b.Bytes()
}
func decodeBlockNumTxIndex(bytes []byte) (blockNum uint64, txIndex uint64, err error) {
b := proto.NewBuffer(bytes)
blockNum, err = b.DecodeVarint()
if err != nil {
return
}
txIndex, err = b.DecodeVarint()
if err != nil {
return
}
return
}
// encode BlockHashKey
func encodeBlockHashKey(blockHash []byte) []byte {
return prependKeyPrefix(prefixBlockHashKey, blockHash)
}
// encode TxUUIDKey
func encodeTxUUIDKey(txUUID string) []byte {
return prependKeyPrefix(prefixTxUUIDKey, []byte(txUUID))
}
func encodeAddressBlockNumCompositeKey(address string, blockNumber uint64) []byte {
b := proto.NewBuffer([]byte{prefixAddressBlockNumCompositeKey})
b.EncodeRawBytes([]byte(address))
b.EncodeVarint(blockNumber)
return b.Bytes()
}
func encodeListTxIndexes(listTx []uint64) []byte {
b := proto.NewBuffer([]byte{})
for i := range listTx {
b.EncodeVarint(listTx[i])
}
return b.Bytes()
}
func encodeChaincodeID(c *protos.ChaincodeID) []byte {
// TODO serialize chaincodeID
return []byte{}
}
func prependKeyPrefix(prefix byte, key []byte) []byte {
modifiedKey := []byte{}
modifiedKey = append(modifiedKey, prefix)
modifiedKey = append(modifiedKey, key...)
return modifiedKey
}