kv_ledger_test.go 20.1 KB
Newer Older
manish's avatar
manish committed
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 kvledger

import (
20
	"fmt"
manish's avatar
manish committed
21
22
	"testing"

23
	"github.com/hyperledger/fabric/common/ledger/testutil"
24
	ledgerpackage "github.com/hyperledger/fabric/core/ledger"
25
	"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
26
27
	ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
	"github.com/hyperledger/fabric/protos/common"
28
	"github.com/stretchr/testify/assert"
manish's avatar
manish committed
29
30
31
32
33
)

func TestKVLedgerBlockStorage(t *testing.T) {
	env := newTestEnv(t)
	defer env.cleanup()
34
35
36
	provider, _ := NewProvider()
	defer provider.Close()
	ledger, _ := provider.Create("testLedger")
manish's avatar
manish committed
37
	defer ledger.Close()
38

manish's avatar
manish committed
39
	bcInfo, _ := ledger.GetBlockchainInfo()
40
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
manish's avatar
manish committed
41
42
43
44
45
46
47
48
		Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})

	simulator, _ := ledger.NewTxSimulator()
	simulator.SetState("ns1", "key1", []byte("value1"))
	simulator.SetState("ns1", "key2", []byte("value2"))
	simulator.SetState("ns1", "key3", []byte("value3"))
	simulator.Done()
	simRes, _ := simulator.GetTxSimulationResults()
49
50
	bg := testutil.NewBlockGenerator(t)
	block1 := bg.NextBlock([][]byte{simRes}, false)
51
	ledger.Commit(block1)
manish's avatar
manish committed
52
53

	bcInfo, _ = ledger.GetBlockchainInfo()
54
	block1Hash := block1.Header.Hash()
55
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
manish's avatar
manish committed
56
57
58
59
60
61
62
63
		Height: 1, CurrentBlockHash: block1Hash, PreviousBlockHash: []byte{}})

	simulator, _ = ledger.NewTxSimulator()
	simulator.SetState("ns1", "key1", []byte("value4"))
	simulator.SetState("ns1", "key2", []byte("value5"))
	simulator.SetState("ns1", "key3", []byte("value6"))
	simulator.Done()
	simRes, _ = simulator.GetTxSimulationResults()
64
	block2 := bg.NextBlock([][]byte{simRes}, false)
65
	ledger.Commit(block2)
manish's avatar
manish committed
66
67

	bcInfo, _ = ledger.GetBlockchainInfo()
68
	block2Hash := block2.Header.Hash()
69
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
70
		Height: 2, CurrentBlockHash: block2Hash, PreviousBlockHash: block1.Header.Hash()})
manish's avatar
manish committed
71
72
73
74
75
76
77
78
79
80
81
82
83

	b1, _ := ledger.GetBlockByHash(block1Hash)
	testutil.AssertEquals(t, b1, block1)

	b2, _ := ledger.GetBlockByHash(block2Hash)
	testutil.AssertEquals(t, b2, block2)

	b1, _ = ledger.GetBlockByNumber(1)
	testutil.AssertEquals(t, b1, block1)

	b2, _ = ledger.GetBlockByNumber(2)
	testutil.AssertEquals(t, b2, block2)
}
senthil's avatar
senthil committed
84

85
func TestKVLedgerDBRecovery(t *testing.T) {
86
	ledgertestutil.SetupCoreYAMLConfig("./../../../peer")
senthil's avatar
senthil committed
87
88
	env := newTestEnv(t)
	defer env.cleanup()
89
90
91
	provider, _ := NewProvider()
	defer provider.Close()
	ledger, _ := provider.Create("testLedger")
senthil's avatar
senthil committed
92
93
94
	defer ledger.Close()

	bcInfo, _ := ledger.GetBlockchainInfo()
95
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
senthil's avatar
senthil committed
96
97
98
99
100
101
102
103
104
105
106
107
108
		Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})
	//creating and committing the first block
	simulator, _ := ledger.NewTxSimulator()
	//simulating a transaction
	simulator.SetState("ns1", "key1", []byte("value1"))
	simulator.SetState("ns1", "key2", []byte("value2"))
	simulator.SetState("ns1", "key3", []byte("value3"))
	simulator.Done()
	simRes, _ := simulator.GetTxSimulationResults()
	//generating a block based on the simulation result
	bg := testutil.NewBlockGenerator(t)
	block1 := bg.NextBlock([][]byte{simRes}, false)
	//performing validation of read and write set to find valid transactions
109
	ledger.Commit(block1)
senthil's avatar
senthil committed
110
111
	bcInfo, _ = ledger.GetBlockchainInfo()
	block1Hash := block1.Header.Hash()
112
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
senthil's avatar
senthil committed
113
114
		Height: 1, CurrentBlockHash: block1Hash, PreviousBlockHash: []byte{}})

115
116
117
118
	//======================================================================================
	//SCENARIO 1: peer fails before committing the second block to state DB
	//and history DB (if exist)
	//======================================================================================
senthil's avatar
senthil committed
119
120
121
122
123
124
125
126
127
128
	simulator, _ = ledger.NewTxSimulator()
	//simulating transaction
	simulator.SetState("ns1", "key1", []byte("value4"))
	simulator.SetState("ns1", "key2", []byte("value5"))
	simulator.SetState("ns1", "key3", []byte("value6"))
	simulator.Done()
	simRes, _ = simulator.GetTxSimulationResults()
	//generating a block based on the simulation result
	block2 := bg.NextBlock([][]byte{simRes}, false)
	//performing validation of read and write set to find valid transactions
manish's avatar
manish committed
129
	ledger.(*kvLedger).txtmgmt.ValidateAndPrepare(block2, true)
130
131
	//writing the validated block to block storage but not committing the transaction
	//to state DB and history DB (if exist)
manish's avatar
manish committed
132
	err := ledger.(*kvLedger).blockStore.AddBlock(block2)
senthil's avatar
senthil committed
133
	//assume that peer fails here before committing the transaction
134
	assert.NoError(t, err)
senthil's avatar
senthil committed
135
136
137

	bcInfo, _ = ledger.GetBlockchainInfo()
	block2Hash := block2.Header.Hash()
138
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
senthil's avatar
senthil committed
139
140
141
142
143
144
145
146
147
148
149
150
		Height: 2, CurrentBlockHash: block2Hash, PreviousBlockHash: block1.Header.Hash()})

	simulator, _ = ledger.NewTxSimulator()
	value, _ := simulator.GetState("ns1", "key1")
	//value for 'key1' should be 'value1' as the last commit failed
	testutil.AssertEquals(t, value, []byte("value1"))
	value, _ = simulator.GetState("ns1", "key2")
	//value for 'key2' should be 'value2' as the last commit failed
	testutil.AssertEquals(t, value, []byte("value2"))
	value, _ = simulator.GetState("ns1", "key3")
	//value for 'key3' should be 'value3' as the last commit failed
	testutil.AssertEquals(t, value, []byte("value3"))
151
	//savepoint in state DB should 1 as the last commit failed
manish's avatar
manish committed
152
	stateDBSavepoint, _ := ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
153
154
155
156
	testutil.AssertEquals(t, stateDBSavepoint, uint64(1))

	if ledgerconfig.IsHistoryDBEnabled() == true {
		qhistory, _ := ledger.NewHistoryQueryExecutor()
157
158
		itr, _ := qhistory.GetHistoryForKey("ns1", "key1")
		//TODO: once GetHistoryForKey() returns values we can compare the values. For now, we just count
159
160
161
162
163
164
165
166
		count := 0
		for {
			if kmod, _ := itr.Next(); kmod == nil {
				break
			}
			count++
		}
		testutil.AssertEquals(t, count, 1)
167

168
		//savepoint in history DB should 1 as the last commit failed
169
		historyDBSavepoint, _ := ledger.(*kvLedger).historyDB.GetBlockNumFromSavepoint()
170
171
		testutil.AssertEquals(t, historyDBSavepoint, uint64(1))
	}
senthil's avatar
senthil committed
172
173
	simulator.Done()
	ledger.Close()
174
	provider.Close()
senthil's avatar
senthil committed
175
176
177

	//we assume here that the peer comes online and calls NewKVLedger to get a handler for the ledger
	//State DB should be recovered before returning from NewKVLedger call
178
179
	provider, _ = NewProvider()
	ledger, _ = provider.Open("testLedger")
senthil's avatar
senthil committed
180
181
182
183
184
185
186
187
188
189
	simulator, _ = ledger.NewTxSimulator()
	value, _ = simulator.GetState("ns1", "key1")
	//value for 'key1' should be 'value4' after recovery
	testutil.AssertEquals(t, value, []byte("value4"))
	value, _ = simulator.GetState("ns1", "key2")
	//value for 'key2' should be 'value5' after recovery
	testutil.AssertEquals(t, value, []byte("value5"))
	value, _ = simulator.GetState("ns1", "key3")
	//value for 'key3' should be 'value6' after recovery
	testutil.AssertEquals(t, value, []byte("value6"))
190
	//savepoint in state DB should 2 after recovery
manish's avatar
manish committed
191
	stateDBSavepoint, _ = ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
192
193
194
195
	testutil.AssertEquals(t, stateDBSavepoint, uint64(2))

	if ledgerconfig.IsHistoryDBEnabled() == true {
		qhistory, _ := ledger.NewHistoryQueryExecutor()
196
		itr, _ := qhistory.GetHistoryForKey("ns1", "key1")
197
198
199
200
201
202
203
204
		count := 0
		for {
			if kmod, _ := itr.Next(); kmod == nil {
				break
			}
			count++
		}
		testutil.AssertEquals(t, count, 2)
205

206
		//savepoint in history DB should 2 after recovery
207
		historyDBSavepoint, _ := ledger.(*kvLedger).historyDB.GetBlockNumFromSavepoint()
208
209
210
211
212
213
214
215
		testutil.AssertEquals(t, historyDBSavepoint, uint64(2))
	}
	simulator.Done()

	//======================================================================================
	//SCENARIO 2: peer fails after committing the third block to state DB
	//but before committing to history DB (if exist)
	//======================================================================================
216

217
218
219
220
221
222
223
224
225
226
	simulator, _ = ledger.NewTxSimulator()
	//simulating transaction
	simulator.SetState("ns1", "key1", []byte("value7"))
	simulator.SetState("ns1", "key2", []byte("value8"))
	simulator.SetState("ns1", "key3", []byte("value9"))
	simulator.Done()
	simRes, _ = simulator.GetTxSimulationResults()
	//generating a block based on the simulation result
	block3 := bg.NextBlock([][]byte{simRes}, false)
	//performing validation of read and write set to find valid transactions
manish's avatar
manish committed
227
	ledger.(*kvLedger).txtmgmt.ValidateAndPrepare(block3, true)
228
	//writing the validated block to block storage
manish's avatar
manish committed
229
	err = ledger.(*kvLedger).blockStore.AddBlock(block3)
230
	//committing the transaction to state DB
manish's avatar
manish committed
231
	err = ledger.(*kvLedger).txtmgmt.Commit()
232
233
234
235
236
237
	//assume that peer fails here after committing the transaction to state DB but before
	//history DB
	assert.NoError(t, err)

	bcInfo, _ = ledger.GetBlockchainInfo()
	block3Hash := block3.Header.Hash()
238
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
239
240
241
242
243
244
245
246
247
248
249
250
251
		Height: 3, CurrentBlockHash: block3Hash, PreviousBlockHash: block2.Header.Hash()})

	simulator, _ = ledger.NewTxSimulator()
	value, _ = simulator.GetState("ns1", "key1")
	//value for 'key1' should be 'value7'
	testutil.AssertEquals(t, value, []byte("value7"))
	value, _ = simulator.GetState("ns1", "key2")
	//value for 'key2' should be 'value8'
	testutil.AssertEquals(t, value, []byte("value8"))
	value, _ = simulator.GetState("ns1", "key3")
	//value for 'key3' should be 'value9'
	testutil.AssertEquals(t, value, []byte("value9"))
	//savepoint in state DB should 3
manish's avatar
manish committed
252
	stateDBSavepoint, _ = ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
253
254
255
256
	testutil.AssertEquals(t, stateDBSavepoint, uint64(3))

	if ledgerconfig.IsHistoryDBEnabled() == true {
		qhistory, _ := ledger.NewHistoryQueryExecutor()
257
258
		itr, _ := qhistory.GetHistoryForKey("ns1", "key1")
		//TODO: once GetHistoryForKey() returns values we can compare the values. For now, we just count
259
260
261
262
263
264
265
266
		count := 0
		for {
			if kmod, _ := itr.Next(); kmod == nil {
				break
			}
			count++
		}
		testutil.AssertEquals(t, count, 2)
267

268
		//savepoint in history DB should 2 as the last commit failed
269
		historyDBSavepoint, _ := ledger.(*kvLedger).historyDB.GetBlockNumFromSavepoint()
270
271
272
273
274
275
276
277
278
279
280
		testutil.AssertEquals(t, historyDBSavepoint, uint64(2))
	}
	simulator.Done()
	ledger.Close()
	provider.Close()

	//we assume here that the peer comes online and calls NewKVLedger to get a handler for the ledger
	//history DB should be recovered before returning from NewKVLedger call
	provider, _ = NewProvider()
	ledger, _ = provider.Open("testLedger")
	simulator, _ = ledger.NewTxSimulator()
manish's avatar
manish committed
281
	stateDBSavepoint, _ = ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
282
283
284
285
	testutil.AssertEquals(t, stateDBSavepoint, uint64(3))

	if ledgerconfig.IsHistoryDBEnabled() == true {
		qhistory, _ := ledger.NewHistoryQueryExecutor()
286
		itr, _ := qhistory.GetHistoryForKey("ns1", "key1")
287
288
289
290
291
292
293
294
		count := 0
		for {
			if kmod, _ := itr.Next(); kmod == nil {
				break
			}
			count++
		}
		testutil.AssertEquals(t, count, 3)
295

296
		//savepoint in history DB should 3 after recovery
297
		historyDBSavepoint, _ := ledger.(*kvLedger).historyDB.GetBlockNumFromSavepoint()
298
299
300
301
302
		testutil.AssertEquals(t, historyDBSavepoint, uint64(3))
	}
	simulator.Done()

	//Rare scenario
303

304
305
306
307
308
309
310
311
312
313
314
315
316
317
	//======================================================================================
	//SCENARIO 3: peer fails after committing the fourth block to history DB (if exist)
	//but before committing to state DB
	//======================================================================================
	simulator, _ = ledger.NewTxSimulator()
	//simulating transaction
	simulator.SetState("ns1", "key1", []byte("value10"))
	simulator.SetState("ns1", "key2", []byte("value11"))
	simulator.SetState("ns1", "key3", []byte("value12"))
	simulator.Done()
	simRes, _ = simulator.GetTxSimulationResults()
	//generating a block based on the simulation result
	block4 := bg.NextBlock([][]byte{simRes}, false)
	//performing validation of read and write set to find valid transactions
manish's avatar
manish committed
318
	ledger.(*kvLedger).txtmgmt.ValidateAndPrepare(block4, true)
319
320
	//writing the validated block to block storage but fails to commit to state DB but
	//successfully commits to history DB (if exists)
manish's avatar
manish committed
321
	err = ledger.(*kvLedger).blockStore.AddBlock(block4)
322
	if ledgerconfig.IsHistoryDBEnabled() == true {
323
		err = ledger.(*kvLedger).historyDB.Commit(block4)
324
325
326
327
328
	}
	assert.NoError(t, err)

	bcInfo, _ = ledger.GetBlockchainInfo()
	block4Hash := block4.Header.Hash()
329
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
330
331
332
333
334
335
336
337
338
339
340
341
342
		Height: 4, CurrentBlockHash: block4Hash, PreviousBlockHash: block3.Header.Hash()})

	simulator, _ = ledger.NewTxSimulator()
	value, _ = simulator.GetState("ns1", "key1")
	//value for 'key1' should be 'value7' as the last commit to State DB failed
	testutil.AssertEquals(t, value, []byte("value7"))
	value, _ = simulator.GetState("ns1", "key2")
	//value for 'key2' should be 'value8' as the last commit to State DB failed
	testutil.AssertEquals(t, value, []byte("value8"))
	value, _ = simulator.GetState("ns1", "key3")
	//value for 'key3' should be 'value9' as the last commit to State DB failed
	testutil.AssertEquals(t, value, []byte("value9"))
	//savepoint in state DB should 3 as the last commit failed
manish's avatar
manish committed
343
	stateDBSavepoint, _ = ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
344
345
346
347
	testutil.AssertEquals(t, stateDBSavepoint, uint64(3))

	if ledgerconfig.IsHistoryDBEnabled() == true {
		qhistory, _ := ledger.NewHistoryQueryExecutor()
348
349
		itr, _ := qhistory.GetHistoryForKey("ns1", "key1")
		//TODO: once GetHistoryForKey() returns values we can compare the values. For now, we just count
350
351
352
353
354
355
356
357
358
		count := 0
		for {
			if kmod, _ := itr.Next(); kmod == nil {
				break
			}
			count++
		}
		testutil.AssertEquals(t, count, 4)
		//savepoint in history DB should 4
359
		historyDBSavepoint, _ := ledger.(*kvLedger).historyDB.GetBlockNumFromSavepoint()
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
		testutil.AssertEquals(t, historyDBSavepoint, uint64(4))
	}
	simulator.Done()
	ledger.Close()
	provider.Close()

	//we assume here that the peer comes online and calls NewKVLedger to get a handler for the ledger
	//state DB should be recovered before returning from NewKVLedger call
	provider, _ = NewProvider()
	ledger, _ = provider.Open("testLedger")
	simulator, _ = ledger.NewTxSimulator()
	value, _ = simulator.GetState("ns1", "key1")
	//value for 'key1' should be 'value10' after state DB recovery
	testutil.AssertEquals(t, value, []byte("value10"))
	value, _ = simulator.GetState("ns1", "key2")
	//value for 'key2' should be 'value11' after state DB recovery
	testutil.AssertEquals(t, value, []byte("value11"))
	value, _ = simulator.GetState("ns1", "key3")
	//value for 'key3' should be 'value12' after state DB recovery
	testutil.AssertEquals(t, value, []byte("value12"))
	//savepoint in state DB should 4 after the recovery
manish's avatar
manish committed
381
	stateDBSavepoint, _ = ledger.(*kvLedger).txtmgmt.GetBlockNumFromSavepoint()
382
	testutil.AssertEquals(t, stateDBSavepoint, uint64(4))
senthil's avatar
senthil committed
383
384
	simulator.Done()
}
385
386
387
388

func TestLedgerWithCouchDbEnabledWithBinaryAndJSONData(t *testing.T) {

	//call a helper method to load the core.yaml
389
	ledgertestutil.SetupCoreYAMLConfig("./../../../peer")
390
391
392
393
394
395

	logger.Debugf("TestLedgerWithCouchDbEnabledWithBinaryAndJSONData  IsCouchDBEnabled()value: %v , IsHistoryDBEnabled()value: %v\n",
		ledgerconfig.IsCouchDBEnabled(), ledgerconfig.IsHistoryDBEnabled())

	env := newTestEnv(t)
	defer env.cleanup()
396
397
398
	provider, _ := NewProvider()
	defer provider.Close()
	ledger, _ := provider.Create("testLedger")
399
400
401
	defer ledger.Close()

	bcInfo, _ := ledger.GetBlockchainInfo()
402
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
403
404
405
406
407
		Height: 0, CurrentBlockHash: nil, PreviousBlockHash: nil})

	simulator, _ := ledger.NewTxSimulator()
	simulator.SetState("ns1", "key4", []byte("value1"))
	simulator.SetState("ns1", "key5", []byte("value2"))
408
	simulator.SetState("ns1", "key6", []byte("{\"shipmentID\":\"161003PKC7300\",\"customsInvoice\":{\"methodOfTransport\":\"GROUND\",\"invoiceNumber\":\"00091622\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
409
410
411
412
413
414
	simulator.SetState("ns1", "key7", []byte("{\"shipmentID\":\"161003PKC7600\",\"customsInvoice\":{\"methodOfTransport\":\"AIR MAYBE\",\"invoiceNumber\":\"00091624\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
	simulator.Done()
	simRes, _ := simulator.GetTxSimulationResults()
	bg := testutil.NewBlockGenerator(t)
	block1 := bg.NextBlock([][]byte{simRes}, false)

415
	ledger.Commit(block1)
416
417
418

	bcInfo, _ = ledger.GetBlockchainInfo()
	block1Hash := block1.Header.Hash()
419
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
420
421
		Height: 1, CurrentBlockHash: block1Hash, PreviousBlockHash: []byte{}})

422
	simulationResults := [][]byte{}
423
424
425
426
427
428
429
430
	simulator, _ = ledger.NewTxSimulator()
	simulator.SetState("ns1", "key4", []byte("value3"))
	simulator.SetState("ns1", "key5", []byte("{\"shipmentID\":\"161003PKC7500\",\"customsInvoice\":{\"methodOfTransport\":\"AIR FREIGHT\",\"invoiceNumber\":\"00091623\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
	simulator.SetState("ns1", "key6", []byte("value4"))
	simulator.SetState("ns1", "key7", []byte("{\"shipmentID\":\"161003PKC7600\",\"customsInvoice\":{\"methodOfTransport\":\"GROUND\",\"invoiceNumber\":\"00091624\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
	simulator.SetState("ns1", "key8", []byte("{\"shipmentID\":\"161003PKC7700\",\"customsInvoice\":{\"methodOfTransport\":\"SHIP\",\"invoiceNumber\":\"00091625\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
	simulator.Done()
	simRes, _ = simulator.GetTxSimulationResults()
431
432
433
	simulationResults = append(simulationResults, simRes)
	//add a 2nd transaction
	simulator2, _ := ledger.NewTxSimulator()
434
	simulator2.SetState("ns1", "key7", []byte("{\"shipmentID\":\"161003PKC7600\",\"customsInvoice\":{\"methodOfTransport\":\"TRAIN\",\"invoiceNumber\":\"00091624\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
435
436
437
438
439
440
441
	simulator2.SetState("ns1", "key9", []byte("value5"))
	simulator2.SetState("ns1", "key10", []byte("{\"shipmentID\":\"261003PKC8000\",\"customsInvoice\":{\"methodOfTransport\":\"DONKEY\",\"invoiceNumber\":\"00091626\"},\"weightUnitOfMeasure\":\"KGM\",\"volumeUnitOfMeasure\": \"CO\",\"dimensionUnitOfMeasure\":\"CM\",\"currency\":\"USD\"}"))
	simulator2.Done()
	simRes2, _ := simulator2.GetTxSimulationResults()
	simulationResults = append(simulationResults, simRes2)

	block2 := bg.NextBlock(simulationResults, false)
442
	ledger.Commit(block2)
443
444
445

	bcInfo, _ = ledger.GetBlockchainInfo()
	block2Hash := block2.Header.Hash()
446
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
447
448
449
450
451
452
453
454
455
456
457
458
459
460
		Height: 2, CurrentBlockHash: block2Hash, PreviousBlockHash: block1.Header.Hash()})

	b1, _ := ledger.GetBlockByHash(block1Hash)
	testutil.AssertEquals(t, b1, block1)

	b2, _ := ledger.GetBlockByHash(block2Hash)
	testutil.AssertEquals(t, b2, block2)

	b1, _ = ledger.GetBlockByNumber(1)
	testutil.AssertEquals(t, b1, block1)

	b2, _ = ledger.GetBlockByNumber(2)
	testutil.AssertEquals(t, b2, block2)

461
	//Similar test has been pushed down to historyleveldb_test.go as well
462
	if ledgerconfig.IsHistoryDBEnabled() == true {
463
		logger.Debugf("History is enabled\n")
464
465
466
		qhistory, err := ledger.NewHistoryQueryExecutor()
		testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to retrieve history database executor"))

467
468
		itr, err2 := qhistory.GetHistoryForKey("ns1", "key7")
		testutil.AssertNoError(t, err2, fmt.Sprintf("Error upon GetHistoryForKey"))
469
470
471
472
473
474
475

		count := 0
		for {
			kmod, _ := itr.Next()
			if kmod == nil {
				break
			}
476
477
478
			txid := kmod.(*ledgerpackage.KeyModification).TxID
			//v := kmod.(*ledger.KeyModification).Value TODO value not populated yet
			t.Logf("Retrieved history record for key=key7 at TxId=%s", txid)
479
480
481
			count++
		}
		testutil.AssertEquals(t, count, 3)
482
		// TODO add assertions for exact history values once it is populated
483
484
	}
}