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.

39 lines
1.1 KiB

  1. package merkle
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. // Tree is a Merkle tree interface.
  7. type Tree interface {
  8. Size() (size int)
  9. Height() (height int8)
  10. Has(key []byte) (has bool)
  11. Proof(key []byte) (value []byte, proof []byte, exists bool) // TODO make it return an index
  12. Get(key []byte) (index int, value []byte, exists bool)
  13. GetByIndex(index int) (key []byte, value []byte)
  14. Set(key []byte, value []byte) (updated bool)
  15. Remove(key []byte) (value []byte, removed bool)
  16. HashWithCount() (hash []byte, count int)
  17. Hash() (hash []byte)
  18. Save() (hash []byte)
  19. Load(hash []byte)
  20. Copy() Tree
  21. Iterate(func(key []byte, value []byte) (stop bool)) (stopped bool)
  22. IterateRange(start []byte, end []byte, ascending bool, fx func(key []byte, value []byte) (stop bool)) (stopped bool)
  23. }
  24. //-----------------------------------------------------------------------
  25. // Uvarint length prefixed byteslice
  26. func encodeByteSlice(w io.Writer, bz []byte) (err error) {
  27. var buf [binary.MaxVarintLen64]byte
  28. n := binary.PutUvarint(buf[:], uint64(len(bz)))
  29. _, err = w.Write(buf[0:n])
  30. if err != nil {
  31. return
  32. }
  33. _, err = w.Write(bz)
  34. return
  35. }