Skip to content

Commit

Permalink
chore: address linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law committed Dec 6, 2023
1 parent 95cf936 commit 54e0721
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 25 deletions.
3 changes: 2 additions & 1 deletion smst.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

var _ SparseMerkleSumTrie = (*SMST)(nil)

// Sparse Merkle Sum Trie object wrapping a Sparse Merkle Trie for custom encoding
// SMST is an object wrapping a Sparse Merkle Trie for custom encoding
type SMST struct {
TrieSpec
*SMT
Expand Down Expand Up @@ -99,6 +99,7 @@ func (smst *SMST) Commit() error {
return smst.SMT.Commit()
}

// Root returns the root hash of the trie with the total sum bytes appended
func (smst *SMST) Root() []byte {
return smst.SMT.Root() // [digest]+[binary sum]
}
Expand Down
10 changes: 3 additions & 7 deletions smst_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ func (smst *SMSTWithStorage) Update(key, value []byte, sum uint64) error {
var sumBz [sumSize]byte
binary.BigEndian.PutUint64(sumBz[:], sum)
value = append(value, sumBz[:]...)
if err := smst.preimages.Set(valueHash, value); err != nil {
return err
}
return nil
return smst.preimages.Set(valueHash, value)
}

// Delete deletes a key from the trie.
Expand All @@ -51,10 +48,9 @@ func (smst *SMSTWithStorage) GetValueSum(key []byte) ([]byte, uint64, error) {
if errors.Is(err, badger.ErrKeyNotFound) {
// If key isn't found, return default value and sum
return defaultValue, 0, nil
} else {
// Otherwise percolate up any other error
return nil, 0, err
}
// Otherwise percolate up any other error
return nil, 0, err
}
var sumBz [sumSize]byte
copy(sumBz[:], value[len(value)-sumSize:])
Expand Down
5 changes: 1 addition & 4 deletions smt_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ func (smt *SMTWithStorage) Update(key, value []byte) error {
return err
}
valueHash := smt.digestValue(value)
if err := smt.preimages.Set(valueHash, value); err != nil {
return err
}
return nil
return smt.preimages.Set(valueHash, value)
}

// Delete deletes a key from the trie.
Expand Down
15 changes: 8 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const (
)

var (
defaultValue []byte = nil
defaultValue []byte
defaultSum [sumSize]byte

// ErrKeyNotPresent is returned when a key is not present in the trie.
ErrKeyNotPresent = errors.New("key already empty")
)

// SparseMerkleTrie represents a Sparse Merkle trie.
// SparseMerkleTrie represents a Sparse Merkle Trie.
type SparseMerkleTrie interface {
// Update inserts a value into the SMT.
Update(key, value []byte) error
Expand All @@ -28,7 +28,7 @@ type SparseMerkleTrie interface {
Get(key []byte) ([]byte, error)
// Root computes the Merkle root digest.
Root() []byte
// Prove computes a Merkle proof of membership or non-membership of a key.
// Prove computes a Merkle proof of inclusion or exclusion of a key.
Prove(key []byte) (*SparseMerkleProof, error)
// ProveClosest computes a Merkle proof of inclusion for a key in the trie which is
// closest to the path provided. It will search for the key with the longest common
Expand All @@ -40,7 +40,7 @@ type SparseMerkleTrie interface {
Spec() *TrieSpec
}

// SparseMerkleSumTrie represents a Sparse Merkle sum trie.
// SparseMerkleSumTrie represents a Sparse Merkle Sum Trie.
type SparseMerkleSumTrie interface {
// Update inserts a value and its sum into the SMST.
Update(key, value []byte, sum uint64) error
Expand All @@ -52,7 +52,7 @@ type SparseMerkleSumTrie interface {
Root() []byte
// Sum computes the total sum of the Merkle trie
Sum() uint64
// Prove computes a Merkle proof of membership or non-membership of a key.
// Prove computes a Merkle proof of inclusion or exclusion of a key.
Prove(key []byte) (*SparseMerkleProof, error)
// ProveClosest computes a Merkle proof of inclusion for a key in the trie which is
// closest to the path provided. It will search for the key with the longest common
Expand All @@ -64,8 +64,8 @@ type SparseMerkleSumTrie interface {
Spec() *TrieSpec
}

// TrieSpec specifies the hashing functions used by a trie instance to encode leaf paths
// and stored values, and the corresponding maximum trie depth.
// TrieSpec specifies the hashing functions used by a trie instance to encode
// leaf paths and stored values, and the corresponding maximum trie depth.
type TrieSpec struct {
th trieHasher
ph PathHasher
Expand All @@ -81,6 +81,7 @@ func newTrieSpec(hasher hash.Hash, sumTrie bool) TrieSpec {
return spec
}

// Spec returns the TrieSpec associated with the given trie
func (spec *TrieSpec) Spec() *TrieSpec { return spec }

func (spec *TrieSpec) depth() int { return spec.ph.PathSize() * 8 }
Expand Down
10 changes: 4 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ func hashSerialization(smt *TrieSpec, data []byte) []byte {
ext := extensionNode{path: path, child: &lazyNode{childHash}}
copy(ext.pathBounds[:], pathBounds)
return smt.hashNode(&ext)
} else {
return smt.th.digest(data)
}
return smt.th.digest(data)
}

// Used for verification of serialized proof data for sum trie nodes
Expand All @@ -194,11 +193,10 @@ func hashSumSerialization(smt *TrieSpec, data []byte) []byte {
ext := extensionNode{path: path, child: &lazyNode{childHash}}
copy(ext.pathBounds[:], pathBounds)
return smt.hashSumNode(&ext)
} else {
digest := smt.th.digest(data)
digest = append(digest, data[len(data)-sumSize:]...)
return digest
}
digest := smt.th.digest(data)
digest = append(digest, data[len(data)-sumSize:]...)
return digest
}

// resolve resolves a lazy node depending on the trie type
Expand Down

0 comments on commit 54e0721

Please sign in to comment.