Skip to content

Commit

Permalink
Factor out byte concatenation to utils
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jan 11, 2017
1 parent 289b1a2 commit 52c92f5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
18 changes: 18 additions & 0 deletions common/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,21 @@ func GetTestOrgID() string {
func GetSysCCVersion() string {
return metadata.Version
}

// ConcatenateBytes is useful for combining multiple arrays of bytes, especially for
// signatures or digests over multiple fields
func ConcatenateBytes(data ...[]byte) []byte {
finalLength := 0
for _, slice := range data {
finalLength += len(slice)
}
result := make([]byte, finalLength)
last := 0
for _, slice := range data {
for i := range slice {
result[i+last] = slice[i]
}
last += len(slice)
}
return result
}
24 changes: 24 additions & 0 deletions common/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,27 @@ func TestConcurrencyNotFail(t *testing.T) {
logger.Info("")
}
}

func TestMetadataSignatureBytesNormal(t *testing.T) {
first := []byte("first")
second := []byte("second")
third := []byte("third")

result := ConcatenateBytes(first, second, third)
expected := []byte("firstsecondthird")
if !bytes.Equal(result, expected) {
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
}
}

func TestMetadataSignatureBytesNil(t *testing.T) {
first := []byte("first")
second := []byte(nil)
third := []byte("third")

result := ConcatenateBytes(first, second, third)
expected := []byte("firstthird")
if !bytes.Equal(result, expected) {
t.Errorf("Did not concatenate bytes correctly, expected %s, got %s", expected, result)
}
}
18 changes: 3 additions & 15 deletions orderer/multichain/chainsupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package multichain
import (
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/orderer/common/blockcutter"
"github.com/hyperledger/fabric/orderer/common/broadcast"
"github.com/hyperledger/fabric/orderer/common/deliver"
Expand Down Expand Up @@ -193,19 +194,6 @@ func (cs *chainSupport) CreateNextBlock(messages []*cb.Envelope) *cb.Block {
return rawledger.CreateNextBlock(cs.ledger, messages)
}

// TODO, factor this out into common util code
func metadataSignatureBytes(value []byte, sigHeader []byte, blockHeader []byte) []byte {
result := make([]byte, len(value)+len(sigHeader)+len(blockHeader))
last := 0
for _, slice := range [][]byte{value, sigHeader, blockHeader} {
for i := range slice {
result[i+last] = slice[i]
}
last += len(slice)
}
return result
}

func (cs *chainSupport) addBlockSignature(block *cb.Block) {
logger.Debugf("%+v", cs)
logger.Debugf("%+v", cs.signer)
Expand All @@ -217,7 +205,7 @@ func (cs *chainSupport) addBlockSignature(block *cb.Block) {
// information required beyond the fact that the metadata item is signed.
blockSignatureValue := []byte(nil)

blockSignature.Signature = cs.signer.Sign(metadataSignatureBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes()))
blockSignature.Signature = cs.signer.Sign(util.ConcatenateBytes(blockSignatureValue, blockSignature.SignatureHeader, block.Header.Bytes()))

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

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

lastConfigSignature.Signature = cs.signer.Sign(metadataSignatureBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes()))
lastConfigSignature.Signature = cs.signer.Sign(util.ConcatenateBytes(lastConfigValue, lastConfigSignature.SignatureHeader, block.Header.Bytes()))

block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIGURATION] = utils.MarshalOrPanic(&cb.Metadata{
Value: lastConfigValue,
Expand Down
13 changes: 0 additions & 13 deletions orderer/multichain/chainsupport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package multichain

import (
"bytes"
"reflect"
"testing"

Expand Down Expand Up @@ -87,18 +86,6 @@ func TestCommitConfig(t *testing.T) {
}
}

func TestMetadataSignatureBytes(t *testing.T) {
value := []byte("Value")
signatureHeader := []byte("SignatureHeader")
blockHeader := []byte("BlockHeader")

sigBytes := metadataSignatureBytes(value, signatureHeader, blockHeader)
expected := []byte("ValueSignatureHeaderBlockHeader")
if !bytes.Equal(sigBytes, expected) {
t.Errorf("Did not compute first signature bytes correctly, expected %s, got %s", expected, sigBytes)
}
}

func TestWriteBlockSignatures(t *testing.T) {
ml := &mockLedgerReadWriter{}
cm := &mockconfigtx.Manager{}
Expand Down
2 changes: 1 addition & 1 deletion orderer/multichain/systemchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
// Do not include the creation policy
continue
}
remainingBytes = append(remainingBytes, item.ConfigurationItem...)
remainingBytes = util.ConcatenateBytes(remainingBytes, item.ConfigurationItem)
}

configHash := util.ComputeCryptoHash(remainingBytes)
Expand Down
4 changes: 3 additions & 1 deletion protos/common/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package common
import (
"fmt"

"github.com/hyperledger/fabric/common/util"

"github.com/golang/protobuf/proto"
)

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

result[i] = &SignedData{
Data: append(sci.ConfigurationItem, configSig.SignatureHeader...),
Data: util.ConcatenateBytes(sci.ConfigurationItem, configSig.SignatureHeader),
Identity: sigHeader.Creator,
Signature: configSig.Signature,
}
Expand Down

0 comments on commit 52c92f5

Please sign in to comment.