Skip to content

Commit

Permalink
feat: export HashSizes for the different TrieHasher hashers via the T…
Browse files Browse the repository at this point in the history
…rieSpec
  • Loading branch information
h5law committed Mar 20, 2024
1 parent 6f0f1e6 commit d58a69b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type PathHasher interface {
type ValueHasher interface {
// HashValue hashes value data to produce the digest stored in leaf node.
HashValue([]byte) []byte
// ValueHashSize returns the length (in bytes) of digests produced by this hasher.
ValueHashSize() int
}

type trieHasher struct {
Expand Down Expand Up @@ -59,10 +61,19 @@ func (ph *pathHasher) PathSize() int {
return ph.hasher.Size()
}

// HashValue hashes the producdes a digest of the data provided by the value hasher
func (vh *valueHasher) HashValue(data []byte) []byte {
return vh.digest(data)
}

// ValueHashSize returns the length (in bytes) of digests produced by the value hasher
func (vh *valueHasher) ValueHashSize() int {
if vh.hasher == nil {
return 0
}
return vh.hasher.Size()
}

func (th *trieHasher) digest(data []byte) []byte {
th.hasher.Write(data)
sum := th.hasher.Sum(nil)
Expand Down
12 changes: 12 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ func newTrieSpec(hasher hash.Hash, sumTrie bool) TrieSpec {
// Spec returns the TrieSpec associated with the given trie
func (spec *TrieSpec) Spec() *TrieSpec { return spec }

// PathHasherSize returns the length (in bytes) of digests produced by the
// path hasher
func (spec *TrieSpec) PathHasherSize() int { return spec.ph.PathSize() }

// ValueHasherSize returns the length (in bytes) of digests produced by the
// value hasher
func (spec *TrieSpec) ValueHasherSize() int { return spec.vh.ValueHashSize() }

// TrieHasherSize returns the length (in bytes) of digests produced by the
// trie hasher
func (spec *TrieSpec) TrieHasherSize() int { return spec.th.hashSize() }

func (spec *TrieSpec) depth() int { return spec.ph.PathSize() * 8 }
func (spec *TrieSpec) digestValue(data []byte) []byte {
if spec.vh == nil {
Expand Down

0 comments on commit d58a69b

Please sign in to comment.