txvalidator_test.go 3.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
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 txvalidator

import (
	"testing"

22
	"github.com/golang/protobuf/proto"
23
	"github.com/hyperledger/fabric/common/ledger/testutil"
Jason Yellick's avatar
Jason Yellick committed
24
	util2 "github.com/hyperledger/fabric/common/util"
25
	"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
26
	"github.com/hyperledger/fabric/core/ledger/util"
27
	mocktxvalidator "github.com/hyperledger/fabric/core/mocks/txvalidator"
28
	"github.com/hyperledger/fabric/core/mocks/validator"
29
	"github.com/hyperledger/fabric/protos/common"
30
	"github.com/hyperledger/fabric/protos/utils"
31
	"github.com/spf13/viper"
32
33
34
35
	"github.com/stretchr/testify/assert"
)

func TestKVLedgerBlockStorage(t *testing.T) {
36
37
38
39
	viper.Set("peer.fileSystemPath", "/tmp/fabric/txvalidatortest")
	ledgermgmt.InitializeTestEnv()
	defer ledgermgmt.CleanupTestEnv()
	ledger, _ := ledgermgmt.CreateLedger("TestLedger")
40
41
	defer ledger.Close()

42
	validator := &txValidator{&mocktxvalidator.Support{LedgerVal: ledger}, &validator.MockVsccValidator{}}
43
44

	bcInfo, _ := ledger.GetBlockchainInfo()
45
	testutil.AssertEquals(t, bcInfo, &common.BlockchainInfo{
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		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()
	block := testutil.ConstructBlock(t, [][]byte{simRes}, true)

	validator.Validate(block)

	txsfltr := util.NewFilterBitArrayFromBytes(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])

	assert.True(t, !txsfltr.IsSet(uint(0)))
	assert.True(t, !txsfltr.IsSet(uint(1)))
	assert.True(t, !txsfltr.IsSet(uint(2)))
}
65
66
67
68
69
70
71
72

func TestNewTxValidator_DuplicateTransactions(t *testing.T) {
	viper.Set("peer.fileSystemPath", "/tmp/fabric/txvalidatortest")
	ledgermgmt.InitializeTestEnv()
	defer ledgermgmt.CleanupTestEnv()
	ledger, _ := ledgermgmt.CreateLedger("TestLedger")
	defer ledger.Close()

73
	validator := &txValidator{&mocktxvalidator.Support{LedgerVal: ledger}, &validator.MockVsccValidator{}}
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

	// Create simeple endorsement transaction
	payload := &common.Payload{
		Header: &common.Header{
			ChainHeader: &common.ChainHeader{
				TxID:    "simple_txID", // Fake txID
				Type:    int32(common.HeaderType_ENDORSER_TRANSACTION),
				ChainID: util2.GetTestChainID(),
			},
		},
		Data: []byte("test"),
	}

	payloadBytes, err := proto.Marshal(payload)

	// Check marshaling didn't fail
	assert.NoError(t, err)

	// Envelope the payload
	envelope := &common.Envelope{
		Payload: payloadBytes,
	}

	envelopeBytes, err := proto.Marshal(envelope)

	// Check marshaling didn't fail
	assert.NoError(t, err)

	block := &common.Block{
		Data: &common.BlockData{
			// Enconde transactions
			Data: [][]byte{envelopeBytes},
		},
	}

	block.Header = &common.BlockHeader{
		Number:   1,
		DataHash: block.Data.Hash(),
	}

	// Initialize metadata
	utils.InitBlockMetadata(block)
	// Commit block to the ledger
	ledger.Commit(block)

	// Validation should invalidate transaction,
	// because it's already committed
	validator.Validate(block)

	txsfltr := util.NewFilterBitArrayFromBytes(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])

	assert.True(t, txsfltr.IsSet(0))
}