Browse Source

parser: present block hashes in big endian order

v2_protobufs
George Tankersley 6 years ago
parent
commit
f42dea2b1e
  1. 7
      parser/block_header.go
  2. 4
      parser/block_header_test.go

7
parser/block_header.go

@ -150,6 +150,7 @@ func parseNBits(b []byte) *big.Int {
return new(big.Int).SetBytes(targetBytes)
}
// GetHash returns the bytes of a block hash in big-endian order.
func (hdr *blockHeader) GetHash() []byte {
if hdr.cachedHash != nil {
return hdr.cachedHash
@ -165,6 +166,12 @@ func (hdr *blockHeader) GetHash() []byte {
digest := sha256.Sum256(serializedHeader)
digest = sha256.Sum256(digest[:])
// Reverse byte order
for i := 0; i < len(digest)/2; i++ {
j := len(digest) - 1 - i
digest[i], digest[j] = digest[j], digest[i]
}
hdr.cachedHash = digest[:]
return hdr.cachedHash
}

4
parser/block_header_test.go

@ -126,9 +126,9 @@ func TestBlockHeader(t *testing.T) {
hash := blockHeader.GetHash()
// This is not necessarily true for anything but our current test cases.
for _, b := range hash[28:] {
for _, b := range hash[:4] {
if b != 0 {
t.Errorf("Hash lacked trailing zeros")
t.Errorf("Hash lacked leading zeros: %x", hash)
}
}
}

Loading…
Cancel
Save