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.

41 lines
1020 B

5 years ago
  1. package merkletree
  2. import (
  3. "bytes"
  4. "github.com/fatih/color"
  5. common3 "github.com/iden3/go-iden3/common"
  6. )
  7. func (mt *MerkleTree) Insert(key Hash, nodeType byte, indexLength uint32, nodeBytes []byte) error {
  8. // add nodetype at the first byte of the value
  9. var value []byte
  10. value = append(value, nodeType)
  11. indexLengthBytes := common3.Uint32ToBytes(indexLength)
  12. value = append(value, indexLengthBytes[:]...)
  13. value = append(value, nodeBytes[:]...)
  14. err := mt.storage.Put(key[:], value, nil)
  15. if err != nil {
  16. color.Red(err.Error())
  17. return err
  18. }
  19. return nil
  20. }
  21. func (mt *MerkleTree) Get(key Hash) (byte, uint32, []byte, error) {
  22. if bytes.Equal(key[:], EmptyNodeValue[:]) {
  23. return 0, 0, EmptyNodeValue[:], nil
  24. }
  25. value, err := mt.storage.Get(key[:], nil)
  26. if err != nil {
  27. return 0, 0, EmptyNodeValue[:], err
  28. }
  29. // get nodetype of the first byte of the value
  30. nodeType := value[0]
  31. indexLength := common3.BytesToUint32(value[1:5])
  32. nodeBytes := value[5:]
  33. return nodeType, indexLength, nodeBytes, err
  34. }