Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Minor improvements:
Browse files Browse the repository at this point in the history
- remove obsolete key param in updateWithSideNodes
- remove redundant parentheses
- properly format example code and make explicit that Update returns to params
  • Loading branch information
liamsi committed Sep 16, 2021
1 parent 5f13c0f commit ce2ab73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,33 @@ A Go library that implements a Sparse Merkle tree for a key-value map. The tree
```go
package main

import(
"fmt"
"crypto/sha256"
"github.com/celestiaorg/smt"
import (
"crypto/sha256"
"fmt"

"github.com/celestiaorg/smt"
)

func main() {
// Initialise two new key-value store to store the nodes and values of the tree
nodeStore := smt.NewSimpleMap()
valueStore := smt.NewSimpleMap()
// Initialise the tree
tree := smt.NewSparseMerkleTree(nodeStore, valueStore, sha256.New())

// Update the key "foo" with the value "bar"
tree.Update([]byte("foo"), []byte("bar"))

// Generate a Merkle proof for foo=bar
proof, _ := tree.Prove([]byte("foo"))
root := tree.Root() // We also need the current tree root for the proof

// Verify the Merkle proof for foo=bar
if smt.VerifyProof(proof, root, []byte("foo"), []byte("bar"), sha256.New()) {
fmt.Println("Proof verification succeeded.")
} else {
fmt.Println("Proof verification failed.")
}
// Initialise two new key-value store to store the nodes and values of the tree
nodeStore := smt.NewSimpleMap()
valueStore := smt.NewSimpleMap()
// Initialise the tree
tree := smt.NewSparseMerkleTree(nodeStore, valueStore, sha256.New())

// Update the key "foo" with the value "bar"
_, _ = tree.Update([]byte("foo"), []byte("bar"))

// Generate a Merkle proof for foo=bar
proof, _ := tree.Prove([]byte("foo"))
root := tree.Root() // We also need the current tree root for the proof

// Verify the Merkle proof for foo=bar
if smt.VerifyProof(proof, root, []byte("foo"), []byte("bar"), sha256.New()) {
fmt.Println("Proof verification succeeded.")
} else {
fmt.Println("Proof verification failed.")
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (smt *SparseMerkleTree) UpdateForRoot(key []byte, value []byte, root []byte

} else {
// Insert or update operation.
newRoot, err = smt.updateWithSideNodes(key, path, value, sideNodes, pathNodes, oldLeafData)
newRoot, err = smt.updateWithSideNodes(path, value, sideNodes, pathNodes, oldLeafData)
}
return newRoot, err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (smt *SparseMerkleTree) deleteWithSideNodes(path []byte, sideNodes [][]byte
return currentHash, nil
}

func (smt *SparseMerkleTree) updateWithSideNodes(key, path []byte, value []byte, sideNodes [][]byte, pathNodes [][]byte, oldLeafData []byte) ([]byte, error) {
func (smt *SparseMerkleTree) updateWithSideNodes(path []byte, value []byte, sideNodes [][]byte, pathNodes [][]byte, oldLeafData []byte) ([]byte, error) {
valueHash := smt.th.digest(value)
currentHash, currentData := smt.th.digestLeaf(path, valueHash)
if err := smt.nodes.Set(currentHash, currentData); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func getBitAtFromMSB(data []byte, position int) int {
// setBitAtFromMSB sets the bit at an offset from the most significant bit
func setBitAtFromMSB(data []byte, position int) {
n := int(data[position/8])
n |= (1 << (8 - 1 - uint(position)%8))
n |= 1 << (8 - 1 - uint(position)%8)
data[position/8] = byte(n)
}

Expand Down

0 comments on commit ce2ab73

Please sign in to comment.