statics.go 963 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package database
  2. import (
  3. "encoding/json"
  4. )
  5. func SetPeers(newPeers map[string]string, networkName string) bool {
  6. areEqual := PeersAreEqual(newPeers, networkName)
  7. if !areEqual {
  8. jsonData, err := json.Marshal(newPeers)
  9. if err != nil {
  10. return false
  11. }
  12. InsertPeer(networkName, string(jsonData))
  13. return true
  14. }
  15. return !areEqual
  16. }
  17. func GetPeers(networkName string) (map[string]string, error) {
  18. record, err := FetchRecord(PEERS_TABLE_NAME, networkName)
  19. if err != nil {
  20. return nil, err
  21. }
  22. currentDataMap := make(map[string]string)
  23. err = json.Unmarshal([]byte(record), &currentDataMap)
  24. return currentDataMap, err
  25. }
  26. func PeersAreEqual(toCompare map[string]string, networkName string) bool {
  27. currentDataMap, err := GetPeers(networkName)
  28. if err != nil {
  29. return false
  30. }
  31. if len(currentDataMap) != len(toCompare) {
  32. return false
  33. }
  34. for k := range currentDataMap {
  35. if currentDataMap[k] != toCompare[k] {
  36. return false
  37. }
  38. }
  39. return true
  40. }