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.

26 lines
541 B

  1. package merkle
  2. type SimpleMap struct {
  3. kvz KVPairs
  4. }
  5. func NewSimpleMap() *SimpleMap {
  6. return &SimpleMap{
  7. kvz: nil,
  8. }
  9. }
  10. func (sm *SimpleMap) Set(k string, o interface{}) {
  11. sm.kvz = append(sm.kvz, KVPair{Key: k, Value: o})
  12. }
  13. // Merkle root hash of items sorted by key.
  14. // NOTE: Behavior is undefined when key is duplicate.
  15. func (sm *SimpleMap) Hash() []byte {
  16. sm.kvz.Sort()
  17. kvPairsH := make([]Hashable, 0, len(sm.kvz))
  18. for _, kvp := range sm.kvz {
  19. kvPairsH = append(kvPairsH, kvp)
  20. }
  21. return SimpleHashFromHashables(kvPairsH)
  22. }