[types] improve HashID
make a HashID from hash of bytes
compare two HashIDs
package types
import (
+ "bytes"
+
sha256 "github.com/minio/sha256-simd"
"crypto/sha512"
@@ -22,6 +24,12 @@ type AccountID HashID
// TxID is a HashID to identify a transaction
type TxID HashID
+// GetHashID make a HashID from hash of bytes
+func GetHashID(bytes []byte) HashID {
+ hash := TrieHasher(bytes)
+ return ToHashID(hash)
+}
+
// ToHashID make a HashID from bytes
func ToHashID(hash []byte) HashID {
buf := HashID{}
@@ -32,6 +40,11 @@ func (id HashID) String() string {
return enc.ToString(id[:])
}
+// Compare returns an integer comparing two HashIDs as byte slices.
+func (id HashID) Compare(alt HashID) int {
+ return bytes.Compare(id[:], alt[:])
+}
+
// ToBlockID make a BlockID from bytes
func ToBlockID(blockHash []byte) BlockID {
return BlockID(ToHashID(blockHash))
@@ -50,8 +63,7 @@ func (id TxID) String() string {
// ToAccountID make a AccountHash from bytes
func ToAccountID(account []byte) AccountID {
- accountHash := TrieHasher(account)
- return AccountID(ToHashID(accountHash))
+ return AccountID(GetHashID(account))
}
func (id AccountID) String() string {
return HashID(id).String()
