You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
960 B

package merkletree
import (
"bytes"
"github.com/fatih/color"
)
func (mt *MerkleTree) Insert(key Hash, nodeType byte, indexLength uint32, nodeBytes []byte) error {
// add nodetype at the first byte of the value
var value []byte
value = append(value, nodeType)
indexLengthBytes := Uint32ToBytes(indexLength)
value = append(value, indexLengthBytes[:]...)
value = append(value, nodeBytes[:]...)
err := mt.storage.Put(key[:], value, nil)
if err != nil {
color.Red(err.Error())
return err
}
return nil
}
func (mt *MerkleTree) Get(key Hash) (byte, uint32, []byte, error) {
if bytes.Equal(key[:], EmptyNodeValue[:]) {
return 0, 0, EmptyNodeValue[:], nil
}
value, err := mt.storage.Get(key[:], nil)
if err != nil {
return 0, 0, EmptyNodeValue[:], err
}
// get nodetype of the first byte of the value
nodeType := value[0]
indexLength := BytesToUint32(value[1:5])
nodeBytes := value[5:]
return nodeType, indexLength, nodeBytes, err
}