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.

47 lines
1.2 KiB

  1. package merkle
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. type Tree interface {
  7. Size() (size int)
  8. Height() (height int8)
  9. Has(key []byte) (has bool)
  10. Proof(key []byte) (value []byte, proof []byte, exists bool) // TODO make it return an index
  11. Get(key []byte) (index int, value []byte, exists bool)
  12. GetByIndex(index int) (key []byte, value []byte)
  13. Set(key []byte, value []byte) (updated bool)
  14. Remove(key []byte) (value []byte, removed bool)
  15. HashWithCount() (hash []byte, count int)
  16. Hash() (hash []byte)
  17. Save() (hash []byte)
  18. Load(hash []byte)
  19. Copy() Tree
  20. Iterate(func(key []byte, value []byte) (stop bool)) (stopped bool)
  21. IterateRange(start []byte, end []byte, ascending bool, fx func(key []byte, value []byte) (stop bool)) (stopped bool)
  22. }
  23. type Hasher interface {
  24. Hash() []byte
  25. }
  26. //-----------------------------------------------------------------------
  27. // NOTE: these are duplicated from go-wire so we dont need go-wire as a dep
  28. func encodeByteSlice(w io.Writer, bz []byte) (err error) {
  29. err = encodeVarint(w, int64(len(bz)))
  30. if err != nil {
  31. return
  32. }
  33. _, err = w.Write(bz)
  34. return
  35. }
  36. func encodeVarint(w io.Writer, i int64) (err error) {
  37. var buf [10]byte
  38. n := binary.PutVarint(buf[:], i)
  39. _, err = w.Write(buf[0:n])
  40. return
  41. }