-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAb-1699] Deterministic hashing for block header
https://jira.hyperledger.org/browse/FAB-1699 Currently, we compute the hash of the block header by hashing over the protobuf marshaling of the BlockHeader object. This is not guaranteed to be deterministic, so a reliable marshaling scheme is required. This changeset causes block headers to be marshaled to bytes via ASN.1. Note: Due to an idiosyncracy in the golang ASN.1 implementation, block numbers of MaxInt64 cannot be encoded, and this will result in a panic. This resulting byte slice is used as the parameter to the hashing algorithm. Change-Id: I3b0582e89c17c0d120f3e2d888a2205022f2de18 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Jan 20, 2017
1 parent
051229a
commit e72a671
Showing
4 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters