Exemple #1
0
func TestInvalidOrderDelta(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"))

	delta := ledgerTestWrapper.GetStateDelta(1)

	err := ledger.CommitStateDelta(1)
	testutil.AssertError(t, err, "Expected error commiting delta")

	err = ledger.RollbackTxBatch(1)
	testutil.AssertError(t, err, "Expected error rolling back delta")

	ledgerTestWrapper.ApplyStateDelta(2, delta)

	err = ledger.ApplyStateDelta(3, delta)
	testutil.AssertError(t, err, "Expected error applying delta")

	err = ledger.CommitStateDelta(3)
	testutil.AssertError(t, err, "Expected error applying delta")

	err = ledger.RollbackStateDelta(3)
	testutil.AssertError(t, err, "Expected error applying delta")

}
Exemple #2
0
func TestLedgerDifferentID(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger
	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)
	transaction, _ := buildTestTx(t)
	err := ledger.CommitTxBatch(2, []*protos.Transaction{transaction}, nil, []byte("prrof"))
	testutil.AssertError(t, err, "ledger should throw error for wrong batch ID")
}
Exemple #3
0
func TestVerifyChain(t *testing.T) {
	ledgerTestWrapper := createFreshDBAndTestLedgerWrapper(t)
	ledger := ledgerTestWrapper.ledger

	// Build a big blockchain
	for i := 0; i < 100; i++ {
		ledger.BeginTxBatch(i)
		ledger.TxBegin("txUuid" + strconv.Itoa(i))
		ledger.SetState("chaincode"+strconv.Itoa(i), "key"+strconv.Itoa(i), []byte("value"+strconv.Itoa(i)))
		ledger.TxFinished("txUuid"+strconv.Itoa(i), true)
		transaction, _ := buildTestTx(t)
		ledger.CommitTxBatch(i, []*protos.Transaction{transaction}, nil, []byte("proof"))
	}

	// Verify the chain
	for lowBlock := uint64(0); lowBlock < ledger.GetBlockchainSize()-1; lowBlock++ {
		testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(ledger.GetBlockchainSize()-1, lowBlock), uint64(0))
	}
	for highBlock := ledger.GetBlockchainSize() - 1; highBlock > 0; highBlock-- {
		testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(highBlock, 0), uint64(0))
	}

	// Add bad blocks and test
	badBlock := protos.NewBlock(nil, nil)
	badBlock.PreviousBlockHash = []byte("evil")
	for i := uint64(0); i < ledger.GetBlockchainSize(); i++ {
		goodBlock := ledgerTestWrapper.GetBlockByNumber(i)
		ledger.PutRawBlock(badBlock, i)
		for lowBlock := uint64(0); lowBlock < ledger.GetBlockchainSize()-1; lowBlock++ {
			if i >= lowBlock {
				expected := uint64(i + 1)
				if i == ledger.GetBlockchainSize()-1 {
					expected--
				}
				testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(ledger.GetBlockchainSize()-1, lowBlock), expected)
			} else {
				testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(ledger.GetBlockchainSize()-1, lowBlock), uint64(0))
			}
		}
		for highBlock := ledger.GetBlockchainSize() - 1; highBlock > 0; highBlock-- {
			if i <= highBlock {
				expected := uint64(i + 1)
				if i == highBlock {
					expected--
				}
				testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(highBlock, 0), expected)
			} else {
				testutil.AssertEquals(t, ledgerTestWrapper.VerifyChain(highBlock, 0), uint64(0))
			}
		}
		ledgerTestWrapper.PutRawBlock(goodBlock, i)
	}

	// Test edge cases
	_, err := ledger.VerifyChain(2, 10)
	testutil.AssertError(t, err, "Expected error as high block is less than low block")
	_, err = ledger.VerifyChain(2, 2)
	testutil.AssertError(t, err, "Expected error as high block is equal to low block")
	_, err = ledger.VerifyChain(0, 100)
	testutil.AssertError(t, err, "Expected error as high block is out of bounds")
}