historyleveldb.go 7.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed 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 historyleveldb

import (
20
	"github.com/hyperledger/fabric/common/flogging"
21
	"github.com/hyperledger/fabric/common/ledger/blkstorage"
22
	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
23
24
	"github.com/hyperledger/fabric/core/ledger"
	"github.com/hyperledger/fabric/core/ledger/kvledger/history/historydb"
manish's avatar
manish committed
25
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
26
27
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
28
	"github.com/hyperledger/fabric/core/ledger/util"
29
	"github.com/hyperledger/fabric/protos/common"
denyeart's avatar
denyeart committed
30
	putils "github.com/hyperledger/fabric/protos/utils"
31
32
)

33
var logger = flogging.MustGetLogger("historyleveldb")
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

var savePointKey = []byte{0x00}
var emptyValue = []byte{}

// HistoryDBProvider implements interface HistoryDBProvider
type HistoryDBProvider struct {
	dbProvider *leveldbhelper.Provider
}

// NewHistoryDBProvider instantiates HistoryDBProvider
func NewHistoryDBProvider() *HistoryDBProvider {
	dbPath := ledgerconfig.GetHistoryLevelDBPath()
	logger.Debugf("constructing HistoryDBProvider dbPath=%s", dbPath)
	dbProvider := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dbPath})
	return &HistoryDBProvider{dbProvider}
}

// GetDBHandle gets the handle to a named database
func (provider *HistoryDBProvider) GetDBHandle(dbName string) (historydb.HistoryDB, error) {
	return newHistoryDB(provider.dbProvider.GetDBHandle(dbName), dbName), nil
}

// Close closes the underlying db
func (provider *HistoryDBProvider) Close() {
	provider.dbProvider.Close()
}

// historyDB implements HistoryDB interface
type historyDB struct {
	db     *leveldbhelper.DBHandle
	dbName string
}

// newHistoryDB constructs an instance of HistoryDB
func newHistoryDB(db *leveldbhelper.DBHandle, dbName string) *historyDB {
	return &historyDB{db, dbName}
}

// Open implements method in HistoryDB interface
func (historyDB *historyDB) Open() error {
	// do nothing because shared db is used
	return nil
}

// Close implements method in HistoryDB interface
func (historyDB *historyDB) Close() {
	// do nothing because shared db is used
}

// Commit implements method in HistoryDB interface
func (historyDB *historyDB) Commit(block *common.Block) error {

	blockNo := block.Header.Number
	//Set the starting tranNo to 0
	var tranNo uint64

	dbBatch := leveldbhelper.NewUpdateBatch()

92
93
	logger.Debugf("Channel [%s]: Updating history database for blockNo [%v] with [%d] transactions",
		historyDB.dbName, blockNo, len(block.Data.Data))
94

95
96
97
98
99
100
101
102
103
	// Get the invalidation byte array for the block
	txsFilter := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
	// Initialize txsFilter if it does not yet exist (e.g. during testing, for genesis block, etc)
	if len(txsFilter) == 0 {
		txsFilter = util.NewTxValidationFlags(len(block.Data.Data))
		block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter
	}

	// write each tran's write set to history db
104
105
	for _, envBytes := range block.Data.Data {

106
		// If the tran is marked as invalid, skip it
107
		if txsFilter.IsInvalid(int(tranNo)) {
108
109
			logger.Debugf("Channel [%s]: Skipping history write for invalid transaction number %d",
				historyDB.dbName, tranNo)
110
			tranNo++
111
112
113
			continue
		}

denyeart's avatar
denyeart committed
114
		env, err := putils.GetEnvelopeFromBlock(envBytes)
115
116
117
118
		if err != nil {
			return err
		}

denyeart's avatar
denyeart committed
119
120
		payload, err := putils.GetPayload(env)
		if err != nil {
121
122
123
			return err
		}

124
125
126
127
128
129
		chdr, err := putils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
		if err != nil {
			return err
		}

		if common.HeaderType(chdr.Type) == common.HeaderType_ENDORSER_TRANSACTION {
130

denyeart's avatar
denyeart committed
131
132
133
134
135
			// extract actions from the envelope message
			respPayload, err := putils.GetActionFromEnvelope(envBytes)
			if err != nil {
				return err
			}
136

denyeart's avatar
denyeart committed
137
			//preparation for extracting RWSet from transaction
manish's avatar
manish committed
138
			txRWSet := &rwsetutil.TxRwSet{}
denyeart's avatar
denyeart committed
139
140
141

			// Get the Result from the Action and then Unmarshal
			// it into a TxReadWriteSet using custom unmarshalling
manish's avatar
manish committed
142
			if err = txRWSet.FromProtoBytes(respPayload.Results); err != nil {
denyeart's avatar
denyeart committed
143
144
145
146
				return err
			}
			// for each transaction, loop through the namespaces and writesets
			// and add a history record for each write
manish's avatar
manish committed
147
			for _, nsRWSet := range txRWSet.NsRwSets {
denyeart's avatar
denyeart committed
148
				ns := nsRWSet.NameSpace
149

manish's avatar
manish committed
150
				for _, kvWrite := range nsRWSet.KvRwSet.Writes {
denyeart's avatar
denyeart committed
151
					writeKey := kvWrite.Key
152

denyeart's avatar
denyeart committed
153
154
155
156
157
158
					//composite key for history records is in the form ns~key~blockNo~tranNo
					compositeHistoryKey := historydb.ConstructCompositeHistoryKey(ns, writeKey, blockNo, tranNo)

					// No value is required, write an empty byte array (emptyValue) since Put() of nil is not allowed
					dbBatch.Put(compositeHistoryKey, emptyValue)
				}
159
			}
denyeart's avatar
denyeart committed
160
161

		} else {
162
			logger.Debugf("Skipping transaction [%d] since it is not an endorsement transaction\n", tranNo)
163
		}
164
		tranNo++
165
166
167
168
169
170
171
	}

	// add savepoint for recovery purpose
	height := version.NewHeight(blockNo, tranNo)
	dbBatch.Put(savePointKey, height.ToBytes())

	// write the block's history records and savepoint to LevelDB
172
173
	// Setting snyc to true as a precaution, false may be an ok optimization after further testing.
	if err := historyDB.db.WriteBatch(dbBatch, true); err != nil {
174
175
176
		return err
	}

177
	logger.Debugf("Channel [%s]: Updates committed to history database for blockNo [%v]", historyDB.dbName, blockNo)
178
179
180
181
	return nil
}

// NewHistoryQueryExecutor implements method in HistoryDB interface
182
183
func (historyDB *historyDB) NewHistoryQueryExecutor(blockStore blkstorage.BlockStore) (ledger.HistoryQueryExecutor, error) {
	return &LevelHistoryDBQueryExecutor{historyDB, blockStore}, nil
184
185
186
}

// GetBlockNumFromSavepoint implements method in HistoryDB interface
187
func (historyDB *historyDB) GetLastSavepoint() (*version.Height, error) {
188
189
	versionBytes, err := historyDB.db.Get(savePointKey)
	if err != nil || versionBytes == nil {
190
		return nil, err
191
192
	}
	height, _ := version.NewHeightFromBytes(versionBytes)
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	return height, nil
}

// ShouldRecover implements method in interface kvledger.Recoverer
func (historyDB *historyDB) ShouldRecover(lastAvailableBlock uint64) (bool, uint64, error) {
	if !ledgerconfig.IsHistoryDBEnabled() {
		return false, 0, nil
	}
	savepoint, err := historyDB.GetLastSavepoint()
	if err != nil {
		return false, 0, err
	}
	if savepoint == nil {
		return true, 0, nil
	}
	return savepoint.BlockNum != lastAvailableBlock, savepoint.BlockNum + 1, nil
}

// CommitLostBlock implements method in interface kvledger.Recoverer
212
213
func (historyDB *historyDB) CommitLostBlock(blockAndPvtdata *ledger.BlockAndPvtData) error {
	block := blockAndPvtdata.Block
214
215
216
217
	if err := historyDB.Commit(block); err != nil {
		return err
	}
	return nil
218
}