Esempio n. 1
0
func TestLedgerRollbackWithHash(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode0", "key1", []byte("value1"))
	ledger.SetState("chaincode0", "key2", []byte("value2"))
	ledger.SetState("chaincode0", "key3", []byte("value3"))
	ledger.TxFinished("txUuid", true)
	ledger.RollbackTxBatch(0)

	hash0 := ledgerTestWrapper.GetTempStateHash()

	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid")
	ledger.SetState("chaincode1", "key1", []byte("value1"))
	ledger.SetState("chaincode2", "key2", []byte("value2"))
	ledger.SetState("chaincode3", "key3", []byte("value3"))
	ledger.TxFinished("txUuid", true)

	hash1 := ledgerTestWrapper.GetTempStateHash()
	testutil.AssertNotEquals(t, hash1, hash0)

	ledger.RollbackTxBatch(1)
	hash1 = ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, hash1, hash0)
	testutil.AssertNil(t, ledgerTestWrapper.GetState("chaincode1", "key1", false))
}
Esempio n. 2
0
func TestStateImpl_ComputeHash_DB_3(t *testing.T) {
	// simple test... not using custom hasher
	conf = initConfig(DefaultNumBuckets, DefaultMaxGroupingAtEachLevel, fnvHash)
	testDBWrapper.CreateFreshDB(t)
	stateImplTestWrapper := newStateImplTestWrapper(t)
	stateImpl := stateImplTestWrapper.stateImpl
	stateDelta := statemgmt.NewStateDelta()
	stateDelta.Set("chaincode1", "key1", []byte("value1"), nil)
	stateDelta.Set("chaincode2", "key2", []byte("value2"), nil)
	stateDelta.Set("chaincode3", "key3", []byte("value3"), nil)
	stateImpl.PrepareWorkingSet(stateDelta)
	hash1 := stateImplTestWrapper.computeCryptoHash()
	stateImplTestWrapper.persistChangesAndResetInMemoryChanges()

	stateDelta = statemgmt.NewStateDelta()
	stateDelta.Delete("chaincode1", "key1", nil)
	stateDelta.Delete("chaincode2", "key2", nil)
	stateDelta.Delete("chaincode3", "key3", nil)
	stateImpl.PrepareWorkingSet(stateDelta)
	hash2 := stateImplTestWrapper.computeCryptoHash()
	stateImplTestWrapper.persistChangesAndResetInMemoryChanges()
	testutil.AssertNotEquals(t, hash1, hash2)
	testutil.AssertNil(t, hash2)
}
Esempio n. 3
0
func TestApplyDeltaHash(t *testing.T) {

	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	// Block 0
	ledger.BeginTxBatch(0)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincode1", "key1", []byte("value1A"))
	ledger.SetState("chaincode2", "key2", []byte("value2A"))
	ledger.SetState("chaincode3", "key3", []byte("value3A"))
	ledger.TxFinished("txUuid1", true)
	transaction, _ := buildTestTx(t)
	ledger.CommitTxBatch(0, []*protos.Transaction{transaction}, nil, []byte("proof"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode1", "key1", true), []byte("value1A"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode2", "key2", true), []byte("value2A"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode3", "key3", true), []byte("value3A"))

	// Block 1
	ledger.BeginTxBatch(1)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincode1", "key1", []byte("value1B"))
	ledger.SetState("chaincode2", "key2", []byte("value2B"))
	ledger.SetState("chaincode3", "key3", []byte("value3B"))
	ledger.TxFinished("txUuid1", true)
	transaction, _ = buildTestTx(t)
	ledger.CommitTxBatch(1, []*protos.Transaction{transaction}, nil, []byte("proof"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode1", "key1", true), []byte("value1B"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode2", "key2", true), []byte("value2B"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode3", "key3", true), []byte("value3B"))

	// Block 2
	ledger.BeginTxBatch(2)
	ledger.TxBegin("txUuid1")
	ledger.SetState("chaincode1", "key1", []byte("value1C"))
	ledger.SetState("chaincode2", "key2", []byte("value2C"))
	ledger.SetState("chaincode3", "key3", []byte("value3C"))
	ledger.SetState("chaincode4", "key4", []byte("value4C"))
	ledger.TxFinished("txUuid1", true)
	transaction, _ = buildTestTx(t)
	ledger.CommitTxBatch(2, []*protos.Transaction{transaction}, nil, []byte("proof"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode1", "key1", true), []byte("value1C"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode2", "key2", true), []byte("value2C"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode3", "key3", true), []byte("value3C"))
	testutil.AssertEquals(t, ledgerTestWrapper.GetState("chaincode4", "key4", true), []byte("value4C"))

	hash2 := ledgerTestWrapper.GetTempStateHash()

	// Roll backwards once
	delta2 := ledgerTestWrapper.GetStateDelta(2)
	delta2.RollBackwards = true
	ledgerTestWrapper.ApplyStateDelta(1, delta2)

	preHash1 := ledgerTestWrapper.GetTempStateHash()
	testutil.AssertNotEquals(t, preHash1, hash2)

	ledgerTestWrapper.CommitStateDelta(1)

	hash1 := ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, preHash1, hash1)
	testutil.AssertNotEquals(t, hash1, hash2)

	// Roll forwards once
	delta2.RollBackwards = false
	ledgerTestWrapper.ApplyStateDelta(2, delta2)
	preHash2 := ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, preHash2, hash2)
	ledgerTestWrapper.RollbackStateDelta(2)
	preHash2 = ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, preHash2, hash1)
	ledgerTestWrapper.ApplyStateDelta(3, delta2)
	preHash2 = ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, preHash2, hash2)
	ledgerTestWrapper.CommitStateDelta(3)
	preHash2 = ledgerTestWrapper.GetTempStateHash()
	testutil.AssertEquals(t, preHash2, hash2)

}