Skip to content

Commit

Permalink
Merge "[FAb-1699] Deterministic hashing for block header"
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborh-da authored and Gerrit Code Review committed Jan 25, 2017
2 parents c6ca34e + e72a671 commit cb39a14
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
31 changes: 27 additions & 4 deletions protos/common/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package common

import (
"encoding/asn1"
"fmt"
"math"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
)
Expand All @@ -38,16 +42,35 @@ func NewBlock(seqNum uint64, previousHash []byte) *Block {
return block
}

// Bytes returns the marshaled representation of the block header.
type asn1Header struct {
Number int64
PreviousHash []byte
DataHash []byte
}

// Bytes returns the ASN.1 marshaled representation of the block header.
func (b *BlockHeader) Bytes() []byte {
data, err := proto.Marshal(b) // XXX this is wrong, protobuf is not the right mechanism to serialize for a hash
asn1Header := asn1Header{
PreviousHash: b.PreviousHash,
DataHash: b.DataHash,
}
if b.Number > uint64(math.MaxInt64) {
panic(fmt.Errorf("Golang does not currently support encoding uint64 to asn1"))
} else {
asn1Header.Number = int64(b.Number)
}
result, err := asn1.Marshal(asn1Header)
if err != nil {
panic("This should never fail and is generally irrecoverable")
// Errors should only arise for types which cannot be encoded, since the
// BlockHeader type is known a-priori to contain only encodable types, an
// error here is fatal and should not be propogated
panic(err)
}
return data
return result
}

// Hash returns the hash of the block header.
// XXX This method will be removed shortly to allow for confgurable hashing algorithms
func (b *BlockHeader) Hash() []byte {
return util.ComputeCryptoHash(b.Bytes())
}
Expand Down
48 changes: 48 additions & 0 deletions protos/common/block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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 common

import (
"math"
"testing"
)

func TestGoodBlockHeaderBytes(t *testing.T) {
goodBlockHeader := &BlockHeader{
Number: 1,
PreviousHash: []byte("foo"),
DataHash: []byte("bar"),
}

_ = goodBlockHeader.Bytes() // Should not panic
}

func TestBadBlockHeaderBytes(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatalf("Should have panicked on block number too high to encode as int64")
}
}()

badBlockHeader := &BlockHeader{
Number: math.MaxUint64,
PreviousHash: []byte("foo"),
DataHash: []byte("bar"),
}

_ = badBlockHeader.Bytes() // Should panic
}
3 changes: 3 additions & 0 deletions protos/common/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions protos/common/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ message Block {
BlockMetadata Metadata = 3;
}

// BlockHeader is the element of the block which forms the block chain
// The block header is hashed using the configured chain hashing algorithm
// over the ASN.1 encoding of the BlockHeader
message BlockHeader {
uint64 Number = 1; // The position in the blockchain
bytes PreviousHash = 2; // The hash of the previous block header
Expand Down

0 comments on commit cb39a14

Please sign in to comment.