Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taras Woronjanski committed Feb 7, 2024
1 parent 95ff86b commit 7ec0c5a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
6 changes: 5 additions & 1 deletion fixed_merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ func DeserializeMerkleTree(data SerializedTreeState, hashFn HashFunction) (*Merk

out := &MerkleTree{
BaseTree: &BaseTree{
levels: data.Levels,
levels: data.GetLevels(),
layers: layers,
zeros: zeros,
hashFn: hashFn,
},
}
// check against root
if !out.Root().Cmp(data.GetRoot()) {
return nil, fmt.Errorf("root mismatch")
}
out.zeroElement = out.zeros[0]
return out, nil
}
47 changes: 37 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
package fMerkleTree

import "encoding/hex"
import (
"bytes"
"encoding/hex"
"math/big"
)

type Element []byte

func (e Element) Hex() string {
return hex.EncodeToString(e)
}

func (e Element) BigInt() *big.Int {
return big.NewInt(0).SetBytes(e)
}

func (e Element) Cmp(x Element) bool {
return bytes.Compare(e, x) == 0
}

type HashFunction func(left Element, right Element) []byte

type ComparatorFunction func(left Element, right Element) bool

type SerializedTreeState struct {
Levels int `db:"levels"`
Layers []byte `db:"layers"`
Zeros []byte `db:"zeros"`
ID int `db:"id"`
type SerializedTreeState interface {
GetLevels() int
GetRoot() Element
GetLayers() ([][]Element, error)
GetZeros() ([]Element, error)
}

type serializedTreeState struct {
Root Element `db:"root"`
Levels int `db:"levels"`
Layers []byte `db:"layers"`
Zeros []byte `db:"zeros"`
ID int `db:"id"`
}

func (st *serializedTreeState) GetRoot() Element {
return st.Root
}

func (st *serializedTreeState) GetLevels() int {
return st.Levels
}

func (st SerializedTreeState) GetLayers() ([][]Element, error) {
func (st *serializedTreeState) GetLayers() ([][]Element, error) {
var out [][]Element
return out, GobDecode(st.Layers, &out)
}

func (st SerializedTreeState) GetZeros() ([]Element, error) {
func (st *serializedTreeState) GetZeros() ([]Element, error) {
var out []Element
return out, GobDecode(st.Zeros, &out)
}

func NewSerializedTreeState(tree *MerkleTree) (SerializedTreeState, error) {
var out SerializedTreeState
out.Levels = tree.levels
out := &serializedTreeState{Levels: tree.levels, Root: tree.Root()}
var err error
out.Layers, err = GobEncode(tree.layers)
if err != nil {
Expand Down

0 comments on commit 7ec0c5a

Please sign in to comment.