Skip to content

Commit 22348a2

Browse files
authored
Add config sequence to batch (#241)
- add config sequence - some refactoring and clean-up Signed-off-by: Yoav Tock <tock@il.ibm.com>
1 parent d2f3627 commit 22348a2

File tree

5 files changed

+90
-46
lines changed

5 files changed

+90
-46
lines changed

node/ledger/assembler_ledger_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,8 @@ func createBatchesAndOrdInfo(t *testing.T, num int) ([]types.Batch, []*state.Ord
314314
seq := seqArray[sIdx][pIdx]
315315
seqArray[sIdx][pIdx] = seq + 1
316316

317-
fb, err := node_ledger.NewFabricBatchFromRequests(
318-
party, shard, types.BatchSequence(seq), batchedRequests, nil)
319-
require.NoError(t, err)
317+
fb := node_ledger.NewFabricBatchFromRequests(shard, party, types.BatchSequence(seq), batchedRequests, 0, nil)
318+
require.NotNil(t, fb)
320319

321320
transactionCount += len(batchedRequests)
322321

node/ledger/assembler_ledger_util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func TestAssemblerBlockMetadataToFromBytes(t *testing.T) {
2222
[]byte("tx1-1"), []byte("tx2"),
2323
}
2424

25-
fb, err := node_ledger.NewFabricBatchFromRequests(1, 2, 3, batchedRequests, []byte("bogus"))
26-
assert.NoError(t, err)
25+
fb := node_ledger.NewFabricBatchFromRequests(2, 1, 3, batchedRequests, 0, []byte("bogus"))
26+
assert.NotNil(t, fb)
2727

2828
oi := &state.OrderingInformation{
2929
BlockHeader: &state.BlockHeader{

node/ledger/batch_encoding.go

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import (
1818
"github.com/pkg/errors"
1919
)
2020

21-
// BlockMetadataIndex_PartyShard is one location after the last entry that Fabric uses, which evaluates to 5.
22-
// It includes the primary PartyID and ShardID.
23-
var BlockMetadataIndex_PartyShard = len(common.BlockMetadataIndex_name)
21+
// BlockMetadataIndex_PartyShardConfigSequence is the location where we encode the ShardID, primary PartyID, and config sequence.
22+
// We reuse the location of the orderer metadata.
23+
const BlockMetadataIndex_PartyShardConfigSequence = common.BlockMetadataIndex_ORDERER
2424

25-
// FabricBatch is a types.Batch encoded in a Fabric block
25+
// The size of the partyShardConfigSequenceSize metadata: shard uint16 + party uint16 + config sequence uint64
26+
const partyShardConfigSequenceSize = 2 + 2 + 8
27+
28+
// FabricBatch is a types.Batch encoded in a Fabric block.
2629
type FabricBatch common.Block
2730

2831
func (b *FabricBatch) Digest() []byte {
@@ -35,48 +38,63 @@ func (b *FabricBatch) Requests() types.BatchedRequests {
3538

3639
// Primary returns the PartyID if encoded correctly, or 0.
3740
func (b *FabricBatch) Primary() types.PartyID {
38-
m := (*common.Block)(b).GetMetadata().GetMetadata()
39-
if len(m) <= BlockMetadataIndex_PartyShard {
40-
return 0
41-
}
42-
43-
buff := m[BlockMetadataIndex_PartyShard]
44-
if len(buff) < 4 {
41+
buff := ordererMetadata((*common.Block)(b).GetMetadata().GetMetadata())
42+
if len(buff) == 0 {
4543
return 0
4644
}
4745

48-
return types.PartyID(binary.BigEndian.Uint16(buff[:2]))
46+
return types.PartyID(binary.BigEndian.Uint16(buff[2:4]))
4947
}
5048

5149
// Shard returns the ShardID if encoded correctly, or 0.
5250
func (b *FabricBatch) Shard() types.ShardID {
53-
m := (*common.Block)(b).GetMetadata().GetMetadata()
54-
if len(m) <= BlockMetadataIndex_PartyShard {
51+
buff := ordererMetadata((*common.Block)(b).GetMetadata().GetMetadata())
52+
if len(buff) == 0 {
5553
return 0
5654
}
5755

58-
buff := m[BlockMetadataIndex_PartyShard]
59-
if len(buff) < 4 {
56+
return types.ShardID(binary.BigEndian.Uint16(buff[0:2]))
57+
}
58+
59+
// ConfigSequence returns the ConfigSequence if encoded correctly, or 0.
60+
func (b *FabricBatch) ConfigSequence() types.ConfigSequence {
61+
buff := ordererMetadata((*common.Block)(b).GetMetadata().GetMetadata())
62+
if len(buff) == 0 {
6063
return 0
6164
}
6265

63-
return types.ShardID(binary.BigEndian.Uint16(buff[2:]))
66+
return types.ConfigSequence(binary.BigEndian.Uint64(buff[4:]))
67+
}
68+
69+
func ordererMetadata(m [][]byte) []byte {
70+
if len(m) <= int(BlockMetadataIndex_PartyShardConfigSequence) {
71+
return nil
72+
}
73+
74+
buff := m[BlockMetadataIndex_PartyShardConfigSequence]
75+
if len(buff) < partyShardConfigSequenceSize {
76+
return nil
77+
}
78+
79+
return buff
6480
}
6581

6682
func (b *FabricBatch) Seq() types.BatchSequence {
6783
return types.BatchSequence((*common.Block)(b).GetHeader().GetNumber())
6884
}
6985

7086
func NewFabricBatchFromRequests(
71-
partyID types.PartyID,
7287
shardID types.ShardID,
88+
partyID types.PartyID,
7389
seq types.BatchSequence,
7490
batchedRequests types.BatchedRequests,
91+
configSeq types.ConfigSequence,
7592
prevHash []byte,
76-
) (*FabricBatch, error) { // TODO remove the error
77-
buff := make([]byte, 4)
78-
binary.BigEndian.PutUint16(buff[:2], uint16(partyID))
79-
binary.BigEndian.PutUint16(buff[2:], uint16(shardID))
93+
) *FabricBatch {
94+
buff := make([]byte, partyShardConfigSequenceSize)
95+
binary.BigEndian.PutUint16(buff[:2], uint16(shardID))
96+
binary.BigEndian.PutUint16(buff[2:4], uint16(partyID))
97+
binary.BigEndian.PutUint64(buff[4:], uint64(configSeq))
8098

8199
block := &common.Block{
82100
Header: &common.BlockHeader{
@@ -88,11 +106,11 @@ func NewFabricBatchFromRequests(
88106
Data: batchedRequests,
89107
},
90108
Metadata: &common.BlockMetadata{
91-
Metadata: [][]byte{{}, {}, {}, {}, {}, buff},
109+
Metadata: [][]byte{{}, {}, {}, buff, {}},
92110
},
93111
}
94112

95-
return (*FabricBatch)(block), nil
113+
return (*FabricBatch)(block)
96114
}
97115

98116
func NewFabricBatchFromBlock(block *common.Block) (*FabricBatch, error) {
@@ -110,13 +128,13 @@ func NewFabricBatchFromBlock(block *common.Block) (*FabricBatch, error) {
110128
}
111129

112130
m := block.GetMetadata().GetMetadata()
113-
if len(m) <= BlockMetadataIndex_PartyShard {
114-
return nil, errors.New("missing shard party metadata")
131+
if len(m) <= int(BlockMetadataIndex_PartyShardConfigSequence) {
132+
return nil, errors.New("missing orderer metadata")
115133
}
116134

117-
buff := m[BlockMetadataIndex_PartyShard]
118-
if len(buff) < 4 {
119-
return nil, errors.New("bad shard party metadata")
135+
buff := m[BlockMetadataIndex_PartyShardConfigSequence]
136+
if len(buff) < partyShardConfigSequenceSize {
137+
return nil, errors.New("bad orderer metadata")
120138
}
121139

122140
batch := (*FabricBatch)(block)

node/ledger/batch_encoding_test.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ SPDX-License-Identifier: Apache-2.0
77
package ledger_test
88

99
import (
10+
"bytes"
1011
"testing"
1112

1213
"github.com/hyperledger/fabric-protos-go-apiv2/common"
13-
"github.com/stretchr/testify/assert"
14-
1514
"github.com/hyperledger/fabric-x-orderer/common/types"
15+
"github.com/hyperledger/fabric-x-orderer/node/consensus/state"
1616
"github.com/hyperledger/fabric-x-orderer/node/ledger"
17-
17+
"github.com/stretchr/testify/assert"
1818
"github.com/stretchr/testify/require"
1919
)
2020

@@ -120,29 +120,38 @@ func TestNewFabricBatchFromBlock(t *testing.T) {
120120
expectedErr: "empty block metadata",
121121
},
122122
{
123-
name: "missing shard party metadata 2",
123+
name: "missing orderer metadata",
124124
block: &common.Block{
125125
Header: header,
126126
Data: data,
127-
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}, {}, {}}},
127+
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}}},
128128
},
129-
expectedErr: "missing shard party metadata",
129+
expectedErr: "missing orderer metadata",
130130
},
131131
{
132-
name: "bad shard party metadata",
132+
name: "bad orderer metadata",
133133
block: &common.Block{
134134
Header: header,
135135
Data: data,
136136
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}, {}, {}, {}}},
137137
},
138-
expectedErr: "bad shard party metadata",
138+
expectedErr: "bad orderer metadata",
139+
},
140+
{
141+
name: "bad orderer metadata 2",
142+
block: &common.Block{
143+
Header: header,
144+
Data: data,
145+
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}, {0x01}, {}, {}}},
146+
},
147+
expectedErr: "bad orderer metadata",
139148
},
140149
{
141150
name: "good",
142151
block: &common.Block{
143152
Header: header,
144153
Data: data,
145-
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}, {}, {}, {0x01, 0x02, 0x03, 0x04}}},
154+
Metadata: &common.BlockMetadata{Metadata: [][]byte{{}, {}, {}, {0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05}, {}}},
146155
},
147156
expectedErr: "",
148157
},
@@ -156,11 +165,29 @@ func TestNewFabricBatchFromBlock(t *testing.T) {
156165
assert.NotNil(t, fb)
157166
assert.NoError(t, err)
158167
assert.Equal(t, types.BatchSequence(7), fb.Seq())
159-
assert.Equal(t, types.PartyID(0x102), fb.Primary())
160-
assert.Equal(t, types.ShardID(0x304), fb.Shard())
168+
assert.Equal(t, types.ShardID(0x102), fb.Shard())
169+
assert.Equal(t, types.PartyID(0x304), fb.Primary())
170+
assert.Equal(t, types.ConfigSequence(0x05), fb.ConfigSequence())
161171
assert.Len(t, fb.Requests(), 2)
162172
assert.Equal(t, header.DataHash, fb.Digest())
163173
}
164174
})
165175
}
166176
}
177+
178+
func TestNewFabricBatchFromRequests(t *testing.T) {
179+
bReqs := types.BatchedRequests([][]byte{{0x08}, {0x09}})
180+
fb := ledger.NewFabricBatchFromRequests(2, 3, 4, bReqs, 5, []byte{0x06})
181+
require.NotNil(t, fb)
182+
require.Equal(t, types.ShardID(2), fb.Shard())
183+
require.Equal(t, types.PartyID(3), fb.Primary())
184+
require.Equal(t, types.BatchSequence(4), fb.Seq())
185+
require.True(t, bytes.Equal(bReqs.Digest(), fb.Digest()))
186+
require.Equal(t, types.ConfigSequence(5), fb.ConfigSequence())
187+
require.Equal(t, bReqs, fb.Requests())
188+
189+
require.True(t, bytes.Equal([]byte{0x06}, fb.Header.GetPreviousHash()))
190+
191+
require.Equal(t, "Sh,Pr,Sq,Dg: <2,3,4,f99be8ba3f263229e64cd89aded97556d208a7650bfd06be5979fbf748f94cbe>", types.BatchIDToString(fb))
192+
require.True(t, types.BatchIDEqual(fb, state.NewAvailableBatch(3, 2, 4, bReqs.Digest())))
193+
}

node/ledger/batch_ledger_part.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func newBatchLedgerPart(
7171
func (b *BatchLedgerPart) Append(seq types.BatchSequence, batchedRequests types.BatchedRequests) {
7272
b.logger.Debugf("Party %d, Shard: %d, is appending batch with sequence %d of size %d bytes, from Primary: %d", b.partyID, b.shardID, seq, batchedRequests.SizeBytes(), b.primaryPartyID)
7373

74-
block, _ := NewFabricBatchFromRequests(b.primaryPartyID, b.shardID, seq, batchedRequests, b.prevHash)
74+
block := NewFabricBatchFromRequests(b.shardID, b.primaryPartyID, seq, batchedRequests, 0, b.prevHash)
7575

7676
// Note: We do this only because we reuse the Fabric ledger, we don't really need a hash chain here.
7777
b.prevHash = protoutil.BlockHeaderHash(block.Header)

0 commit comments

Comments
 (0)