Browse Source

Fix names and update README

feature_chain_module
p4u 6 years ago
parent
commit
a504f46141
2 changed files with 28 additions and 28 deletions
  1. +8
    -8
      tree/README.md
  2. +20
    -20
      tree/tree.go

+ 8
- 8
tree/README.md

@ -1,22 +1,22 @@
#### Dvote Tree
## dvote Tree
Implementation of dvote tree structure. Currently based on iden3 merkle tree. Implementation of dvote tree structure. Currently based on iden3 merkle tree.
Example of usage: Example of usage:
``` ```
T := tree.Tree {namespace: "vocdoni"}
if T.init() != nil { fmt.Println("Cannot create tree database") }
err := T.addClaim([]byte("Hello you!"))
T := tree.Tree
if T.Init() != nil { fmt.Println("Cannot create tree database") }
err := T.AddClaim([]byte("Hello you!"))
if err != nil { if err != nil {
fmt.Println("Claim already exist") fmt.Println("Claim already exist")
} }
mpHex, err := T.genProof([]byte("Hello you!"))
mpHex, err := T.GenProof([]byte("Hello you!"))
fmt.Println(mpHex) fmt.Println(mpHex)
fmt.Println(T.checkProof([]byte("Hello you!"), mpHex))
T.close()
fmt.Println(T.CheckProof([]byte("Hello you!"), mpHex))
T.Close()
``` ```
### To-Do
#### To-Do
+ Add export/import methods + Add export/import methods

+ 20
- 20
tree/tree.go

@ -9,19 +9,19 @@ import (
) )
type Tree struct { type Tree struct {
namespace string
storage string
tree *merkletree.MerkleTree
Namespace string
Storage string
Tree *merkletree.MerkleTree
} }
func (t *Tree) init() error {
if len(t.storage) < 1 {
func (t *Tree) Init() error {
if len(t.Storage) < 1 {
usr, err := user.Current() usr, err := user.Current()
if err == nil { if err == nil {
t.storage = usr.HomeDir + "/.dvote/Tree"
} else { t.storage = "./dvoteTree" }
t.Storage = usr.HomeDir + "/.dvote/Tree"
} else { t.Storage = "./dvoteTree" }
} }
mtdb, err := db.NewLevelDbStorage(t.storage, false)
mtdb, err := db.NewLevelDbStorage(t.Storage, false)
if err != nil { if err != nil {
return err return err
} }
@ -29,22 +29,22 @@ func (t *Tree) init() error {
if err != nil { if err != nil {
return err return err
} }
t.tree = mt
t.Tree = mt
return nil return nil
} }
func (t *Tree) close() {
defer t.tree.Storage().Close()
func (t *Tree) Close() {
defer t.Tree.Storage().Close()
} }
func (t *Tree) addClaim(data []byte) error {
claim := mkcore.NewGenericClaim(t.namespace, "default", data, nil)
return t.tree.Add(claim)
func (t *Tree) AddClaim(data []byte) error {
claim := mkcore.NewGenericClaim(t.Namespace, "default", data, nil)
return t.Tree.Add(claim)
} }
func (t *Tree) genProof(data []byte) (string, error) {
claim := mkcore.NewGenericClaim(t.namespace, "default", data, nil)
mp, err := t.tree.GenerateProof(claim.Hi())
func (t *Tree) GenProof(data []byte) (string, error) {
claim := mkcore.NewGenericClaim(t.Namespace, "default", data, nil)
mp, err := t.Tree.GenerateProof(claim.Hi())
if err!=nil { if err!=nil {
return "", err return "", err
} }
@ -52,11 +52,11 @@ func (t *Tree) genProof(data []byte) (string, error) {
return mpHex, nil return mpHex, nil
} }
func (t *Tree) checkProof(data []byte, mpHex string) (bool, error) {
func (t *Tree) CheckProof(data []byte, mpHex string) (bool, error) {
mp, err := common3.HexToBytes(mpHex) mp, err := common3.HexToBytes(mpHex)
if err != nil { if err != nil {
return false, err return false, err
} }
claim := mkcore.NewGenericClaim(t.namespace, "default", data, nil)
return merkletree.CheckProof(t.tree.Root(), mp, claim.Hi(), claim.Ht(), t.tree.NumLevels()), nil
claim := mkcore.NewGenericClaim(t.Namespace, "default", data, nil)
return merkletree.CheckProof(t.Tree.Root(), mp, claim.Hi(), claim.Ht(), t.Tree.NumLevels()), nil
} }

Loading…
Cancel
Save