Skip to content

Commit 52c92f5

Browse files
author
Jason Yellick
committed
Factor out byte concatenation to utils
Per feedback from Kostas on https://gerrit.hyperledger.org/r/#/c/3699/ This changeset factors out some byte concatenation which was easilly generalizable. Change-Id: I126286284eac5f1dd0b94a36573eb4c4bacb1302 Signed-off-by: Jason Yellick <[email protected]>
1 parent 289b1a2 commit 52c92f5

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

common/util/utils.go

+18
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,21 @@ func GetTestOrgID() string {
164164
func GetSysCCVersion() string {
165165
return metadata.Version
166166
}
167+
168+
// ConcatenateBytes is useful for combining multiple arrays of bytes, especially for
169+
// signatures or digests over multiple fields
170+
func ConcatenateBytes(data ...[]byte) []byte {
171+
finalLength := 0
172+
for _, slice := range data {
173+
finalLength += len(slice)
174+
}
175+
result := make([]byte, finalLength)
176+
last := 0
177+
for _, slice := range data {
178+
for i := range slice {
179+
result[i+last] = slice[i]
180+
}
181+
last += len(slice)
182+
}
183+
return result
184+
}

common/util/utils_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,27 @@ func TestConcurrencyNotFail(t *testing.T) {
118118
logger.Info("")
119119
}
120120
}
121+
122+
func TestMetadataSignatureBytesNormal(t *testing.T) {
123+
first := []byte("first")
124+
second := []byte("second")
125+
third := []byte("third")
126+
127+
result := ConcatenateBytes(first, second, third)
128+
expected := []byte("firstsecondthird")
129+
if !bytes.Equal(result, expected) {
130+
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
131+
}
132+
}
133+
134+
func TestMetadataSignatureBytesNil(t *testing.T) {
135+
first := []byte("first")
136+
second := []byte(nil)
137+
third := []byte("third")
138+
139+
result := ConcatenateBytes(first, second, third)
140+
expected := []byte("firstthird")
141+
if !bytes.Equal(result, expected) {
142+
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
143+
}
144+
}

orderer/multichain/chainsupport.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package multichain
1919
import (
2020
"github.com/hyperledger/fabric/common/configtx"
2121
"github.com/hyperledger/fabric/common/policies"
22+
"github.com/hyperledger/fabric/common/util"
2223
"github.com/hyperledger/fabric/orderer/common/blockcutter"
2324
"github.com/hyperledger/fabric/orderer/common/broadcast"
2425
"github.com/hyperledger/fabric/orderer/common/deliver"
@@ -193,19 +194,6 @@ func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Block {
193194
return rawledger.CreateNextBlock(cs.ledger, messages)
194195
}
195196

196-
// TODO, factor this out into common util code
197-
func metadataSignatureBytes(value []byte, sigHeader []byte, blockHeader []byte) []byte {
198-
result := make([]byte, len(value)+len(sigHeader)+len(blockHeader))
199-
last := 0
200-
for _, slice := range [][]byte{value, sigHeader, blockHeader} {
201-
for i := range slice {
202-
result[i+last] = slice[i]
203-
}
204-
last += len(slice)
205-
}
206-
return result
207-
}
208-
209197
func (cs *chainSupport) addBlockSignature(block *cb.Block) {
210198
logger.Debugf("%+v", cs)
211199
logger.Debugf("%+v", cs.signer)
@@ -217,7 +205,7 @@ func (cs *chainSupport) addBlockSignature(block *cb.Block) {
217205
// information required beyond the fact that the metadata item is signed.
218206
blockSignatureValue := []byte(nil)
219207

220-
blockSignature.Signature = cs.signer.Sign(metadataSignatureBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes()))
208+
blockSignature.Signature = cs.signer.Sign(util.ConcatenateBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes()))
221209

222210
block.Metadata.Metadata[cb.BlockMetadataIndex_SIGNATURES] = utils.MarshalOrPanic(&cb.Metadata{
223211
Value: blockSignatureValue,
@@ -240,7 +228,7 @@ func (cs *chainSupport) addLastConfigSignature(block *cb.Block) {
240228

241229
lastConfigValue := utils.MarshalOrPanic(&cb.LastConfiguration{Index: cs.lastConfiguration})
242230

243-
lastConfigSignature.Signature = cs.signer.Sign(metadataSignatureBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes()))
231+
lastConfigSignature.Signature = cs.signer.Sign(util.ConcatenateBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes()))
244232

245233
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIGURATION] = utils.MarshalOrPanic(&cb.Metadata{
246234
Value: lastConfigValue,

orderer/multichain/chainsupport_test.go

-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package multichain
1818

1919
import (
20-
"bytes"
2120
"reflect"
2221
"testing"
2322

@@ -87,18 +86,6 @@ func TestCommitConfig(t *testing.T) {
8786
}
8887
}
8988

90-
func TestMetadataSignatureBytes(t *testing.T) {
91-
value := []byte("Value")
92-
signatureHeader := []byte("SignatureHeader")
93-
blockHeader := []byte("BlockHeader")
94-
95-
sigBytes := metadataSignatureBytes(value, signatureHeader, blockHeader)
96-
expected := []byte("ValueSignatureHeaderBlockHeader")
97-
if !bytes.Equal(sigBytes, expected) {
98-
t.Errorf("Did not compute first signature bytes correctly, expected %s, got %s", expected, sigBytes)
99-
}
100-
}
101-
10289
func TestWriteBlockSignatures(t *testing.T) {
10390
ml := &mockLedgerReadWriter{}
10491
cm := &mockconfigtx.Manager{}

orderer/multichain/systemchain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
194194
// Do not include the creation policy
195195
continue
196196
}
197-
remainingBytes = append(remainingBytes, item.ConfigurationItem...)
197+
remainingBytes = util.ConcatenateBytes(remainingBytes, item.ConfigurationItem)
198198
}
199199

200200
configHash := util.ComputeCryptoHash(remainingBytes)

protos/common/signed_data.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package common
1919
import (
2020
"fmt"
2121

22+
"github.com/hyperledger/fabric/common/util"
23+
2224
"github.com/golang/protobuf/proto"
2325
)
2426

@@ -53,7 +55,7 @@ func (sci *SignedConfigurationItem) AsSignedData() ([]*SignedData, error) {
5355
}
5456

5557
result[i] = &SignedData{
56-
Data: append(sci.ConfigurationItem, configSig.SignatureHeader...),
58+
Data: util.ConcatenateBytes(sci.ConfigurationItem, configSig.SignatureHeader),
5759
Identity: sigHeader.Creator,
5860
Signature: configSig.Signature,
5961
}

0 commit comments

Comments
 (0)