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

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