-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathblock_serialization_test.go
More file actions
128 lines (111 loc) · 3.52 KB
/
block_serialization_test.go
File metadata and controls
128 lines (111 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
127
128
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package blkstorage
import (
"testing"
"github.com/hyperledger/fabric-protos-go-apiv2/common"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protoutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
)
func TestBlockSerialization(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
// malformed Payload
block.Data.Data[1] = protoutil.MarshalOrPanic(&common.Envelope{
Signature: []byte{1, 2, 3},
Payload: []byte("Malformed Payload"),
})
// empty TxID
block.Data.Data[2] = protoutil.MarshalOrPanic(&common.Envelope{
Signature: []byte{1, 2, 3},
Payload: protoutil.MarshalOrPanic(&common.Payload{
Header: &common.Header{
ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{
TxId: "",
}),
},
}),
})
bb, _ := serializeBlock(block)
deserializedBlock, err := deserializeBlock(bb)
require.NoError(t, err)
require.Equal(t, block, deserializedBlock)
}
func TestSerializedBlockInfo(t *testing.T) {
c := &testutilTxIDComputator{
t: t,
malformedTxNums: map[int]struct{}{},
}
t.Run("txID is present in all transaction", func(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
testSerializedBlockInfo(t, block, c)
})
t.Run("txID is not present in one of the transactions", func(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
// empty txid for txNum 2
block.Data.Data[1] = protoutil.MarshalOrPanic(&common.Envelope{
Payload: protoutil.MarshalOrPanic(&common.Payload{
Header: &common.Header{
ChannelHeader: protoutil.MarshalOrPanic(&common.ChannelHeader{
TxId: "",
}),
SignatureHeader: protoutil.MarshalOrPanic(&common.SignatureHeader{
Creator: []byte("fake user"),
Nonce: []byte("fake nonce"),
}),
},
}),
})
testSerializedBlockInfo(t, block, c)
})
t.Run("malformed tx-envelope for one of the transactions", func(t *testing.T) {
block := testutil.ConstructTestBlock(t, 1, 10, 100)
// malformed Payload for
block.Data.Data[1] = protoutil.MarshalOrPanic(&common.Envelope{
Payload: []byte("Malformed Payload"),
})
c.reset()
c.malformedTxNums[1] = struct{}{}
testSerializedBlockInfo(t, block, c)
})
}
func testSerializedBlockInfo(t *testing.T, block *common.Block, c *testutilTxIDComputator) {
bb, info := serializeBlock(block)
infoFromBB, err := extractSerializedBlockInfo(bb)
require.NoError(t, err)
require.Equal(t, info, infoFromBB)
require.Equal(t, len(block.Data.Data), len(info.txOffsets))
for txIndex, txEnvBytes := range block.Data.Data {
txid := c.computeExpectedTxID(txIndex, txEnvBytes)
indexInfo := info.txOffsets[txIndex]
indexTxID := indexInfo.txID
indexOffset := indexInfo.loc
require.Equal(t, indexTxID, txid)
b := bb[indexOffset.offset:]
length, num := protowire.ConsumeVarint(b)
if num < 0 {
length, num = 0, 0
}
txEnvBytesFromBB := b[num : num+int(length)]
require.Equal(t, txEnvBytes, txEnvBytesFromBB)
}
}
type testutilTxIDComputator struct {
t *testing.T
malformedTxNums map[int]struct{}
}
func (c *testutilTxIDComputator) computeExpectedTxID(txNum int, txEnvBytes []byte) string {
txid, err := protoutil.GetOrComputeTxIDFromEnvelope(txEnvBytes)
if _, ok := c.malformedTxNums[txNum]; ok {
require.Error(c.t, err)
} else {
require.NoError(c.t, err)
}
return txid
}
func (c *testutilTxIDComputator) reset() {
c.malformedTxNums = map[int]struct{}{}
}