@ -15,8 +15,8 @@ import (
)
)
const (
const (
// proofFlagsLen is the byte length of the flags in the proof header (first 32
// bytes).
// proofFlagsLen is the byte length of the flags in the proof header
// (first 32 bytes).
proofFlagsLen = 2
proofFlagsLen = 2
// ElemBytesLen is the length of the Hash byte array
// ElemBytesLen is the length of the Hash byte array
ElemBytesLen = 32
ElemBytesLen = 32
@ -45,9 +45,11 @@ var (
// ErrEntryIndexAlreadyExists is used when the entry index already
// ErrEntryIndexAlreadyExists is used when the entry index already
// exists in the tree.
// exists in the tree.
ErrEntryIndexAlreadyExists = errors . New ( "the entry index already exists in the tree" )
ErrEntryIndexAlreadyExists = errors . New ( "the entry index already exists in the tree" )
// ErrNotWritable is used when the MerkleTree is not writable and a write function is called
// ErrNotWritable is used when the MerkleTree is not writable and a
// write function is called
ErrNotWritable = errors . New ( "Merkle Tree not writable" )
ErrNotWritable = errors . New ( "Merkle Tree not writable" )
rootNodeValue = [ ] byte ( "currentroot" )
rootNodeValue = [ ] byte ( "currentroot" )
// HashZero is used at Empty nodes
// HashZero is used at Empty nodes
HashZero = Hash { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
HashZero = Hash { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
)
)
@ -77,7 +79,8 @@ func (h *Hash) BigInt() *big.Int {
return new ( big . Int ) . SetBytes ( common . SwapEndianness ( h [ : ] ) )
return new ( big . Int ) . SetBytes ( common . SwapEndianness ( h [ : ] ) )
}
}
// Bytes returns the []byte representation of the *Hash
// Bytes returns the []byte representation of the *Hash, which always is 32
// bytes length.
func ( h * Hash ) Bytes ( ) [ ] byte {
func ( h * Hash ) Bytes ( ) [ ] byte {
bi := new ( big . Int ) . SetBytes ( common . SwapEndianness ( h [ : ] ) ) . Bytes ( )
bi := new ( big . Int ) . SetBytes ( common . SwapEndianness ( h [ : ] ) ) . Bytes ( )
b := [ 32 ] byte { }
b := [ 32 ] byte { }
@ -85,7 +88,10 @@ func (h *Hash) Bytes() []byte {
return b [ : ]
return b [ : ]
}
}
// NewBigIntFromBytes returns a *big.Int from a byte array, swapping the endianness in the process. This is the intended method to get a *big.Int from a byte array that previously has ben generated by the Hash.Bytes() method.
// NewBigIntFromBytes returns a *big.Int from a byte array, swapping the
// endianness in the process. This is the intended method to get a *big.Int
// from a byte array that previously has ben generated by the Hash.Bytes()
// method.
func NewBigIntFromBytes ( b [ ] byte ) ( * big . Int , error ) {
func NewBigIntFromBytes ( b [ ] byte ) ( * big . Int , error ) {
if len ( b ) != 32 {
if len ( b ) != 32 {
return nil , fmt . Errorf ( "Expected 32 bytes, found %d bytes" , len ( b ) )
return nil , fmt . Errorf ( "Expected 32 bytes, found %d bytes" , len ( b ) )
@ -100,6 +106,18 @@ func NewHashFromBigInt(b *big.Int) *Hash {
return r
return r
}
}
// NewHashFromBytes returns a *Hash from a byte array, swapping the endianness
// in the process. This is the intended method to get a *Hash from a byte array
// that previously has ben generated by the Hash.Bytes() method.
func NewHashFromBytes ( b [ ] byte ) ( * Hash , error ) {
if len ( b ) != 32 {
return nil , fmt . Errorf ( "Expected 32 bytes, found %d bytes" , len ( b ) )
}
var h Hash
copy ( h [ : ] , common . SwapEndianness ( b ) )
return & h , nil
}
// MerkleTree is the struct with the main elements of the MerkleTree
// MerkleTree is the struct with the main elements of the MerkleTree
type MerkleTree struct {
type MerkleTree struct {
sync . RWMutex
sync . RWMutex
@ -109,7 +127,8 @@ type MerkleTree struct {
maxLevels int
maxLevels int
}
}
// NewMerkleTree loads a new Merkletree. If in the sotrage already exists one will open that one, if not, will create a new one.
// NewMerkleTree loads a new Merkletree. If in the sotrage already exists one
// will open that one, if not, will create a new one.
func NewMerkleTree ( storage db . Storage , maxLevels int ) ( * MerkleTree , error ) {
func NewMerkleTree ( storage db . Storage , maxLevels int ) ( * MerkleTree , error ) {
mt := MerkleTree { db : storage , maxLevels : maxLevels , writable : true }
mt := MerkleTree { db : storage , maxLevels : maxLevels , writable : true }
@ -158,7 +177,8 @@ func (mt *MerkleTree) Snapshot(rootKey *Hash) (*MerkleTree, error) {
return & MerkleTree { db : mt . db , maxLevels : mt . maxLevels , rootKey : rootKey , writable : false } , nil
return & MerkleTree { db : mt . db , maxLevels : mt . maxLevels , rootKey : rootKey , writable : false } , nil
}
}
// Add adds a Key & Value into the MerkleTree. Where the `k` determines the path from the Root to the Leaf.
// Add adds a Key & Value into the MerkleTree. Where the `k` determines the
// path from the Root to the Leaf.
func ( mt * MerkleTree ) Add ( k , v * big . Int ) error {
func ( mt * MerkleTree ) Add ( k , v * big . Int ) error {
// verify that the MerkleTree is writable
// verify that the MerkleTree is writable
if ! mt . writable {
if ! mt . writable {