Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 9af1aca

Browse files
committed
Refactor and fix moderator panic
1 parent bd6a206 commit 9af1aca

File tree

9 files changed

+78
-31
lines changed

9 files changed

+78
-31
lines changed

api/endpoints.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,6 @@ func gatewayAllowedPath(path, method string) bool {
258258
}
259259

260260
func blockingStartupMiddleware(i *jsonAPIHandler, w http.ResponseWriter, r *http.Request, requestFunc func(w http.ResponseWriter, r *http.Request)) {
261-
<-i.node.DHT.BootstrapChan
261+
i.node.Service.WaitForReady()
262262
requestFunc(w, r)
263263
}

api/jsonapi.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/hex"
99
"encoding/json"
1010
"fmt"
11-
1211
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
1312
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
1413
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
@@ -857,7 +856,8 @@ func (i *jsonAPIHandler) POSTSettings(w http.ResponseWriter, r *http.Request) {
857856
i.node.BanManager.SetBlockedIds(blockedIds)
858857
}
859858
if settings.StoreModerators != nil {
860-
go i.node.NotifyModerators(*settings.StoreModerators)
859+
modsToAdd, modsToDelete := extractModeratorChanges(*settings.StoreModerators, nil)
860+
go i.node.NotifyModerators(modsToAdd, modsToDelete)
861861
if err := i.node.SetModeratorsOnListings(*settings.StoreModerators); err != nil {
862862
ErrorResponse(w, http.StatusInternalServerError, err.Error())
863863
}
@@ -895,7 +895,7 @@ func (i *jsonAPIHandler) PUTSettings(w http.ResponseWriter, r *http.Request) {
895895
ErrorResponse(w, http.StatusBadRequest, err.Error())
896896
return
897897
}
898-
_, err = i.node.Datastore.Settings().Get()
898+
currentSettings, err := i.node.Datastore.Settings().Get()
899899
if err != nil {
900900
ErrorResponse(w, http.StatusNotFound, "Settings is not yet set. Use POST.")
901901
return
@@ -912,7 +912,8 @@ func (i *jsonAPIHandler) PUTSettings(w http.ResponseWriter, r *http.Request) {
912912
i.node.BanManager.SetBlockedIds(blockedIds)
913913
}
914914
if settings.StoreModerators != nil {
915-
go i.node.NotifyModerators(*settings.StoreModerators)
915+
modsToAdd, modsToDelete := extractModeratorChanges(*settings.StoreModerators, currentSettings.StoreModerators)
916+
go i.node.NotifyModerators(modsToAdd, modsToDelete)
916917
if err := i.node.SetModeratorsOnListings(*settings.StoreModerators); err != nil {
917918
ErrorResponse(w, http.StatusInternalServerError, err.Error())
918919
}
@@ -957,6 +958,11 @@ func (i *jsonAPIHandler) PATCHSettings(w http.ResponseWriter, r *http.Request) {
957958
}
958959
return
959960
}
961+
currentSettings, err := i.node.Datastore.Settings().Get()
962+
if err != nil {
963+
ErrorResponse(w, http.StatusNotFound, "Settings is not yet set. Use POST.")
964+
return
965+
}
960966
if err = validateSMTPSettings(settings); err != nil {
961967
ErrorResponse(w, http.StatusBadRequest, err.Error())
962968
return
@@ -966,7 +972,8 @@ func (i *jsonAPIHandler) PATCHSettings(w http.ResponseWriter, r *http.Request) {
966972
return
967973
}
968974
if settings.StoreModerators != nil {
969-
go i.node.NotifyModerators(*settings.StoreModerators)
975+
modsToAdd, modsToDelete := extractModeratorChanges(*settings.StoreModerators, currentSettings.StoreModerators)
976+
go i.node.NotifyModerators(modsToAdd, modsToDelete)
970977
if err := i.node.SetModeratorsOnListings(*settings.StoreModerators); err != nil {
971978
ErrorResponse(w, http.StatusInternalServerError, err.Error())
972979
}

api/utils.go

+28
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,31 @@ func convertOrderStates(states []int) []pb.OrderState {
5959
}
6060
return orderStates
6161
}
62+
63+
func extractModeratorChanges(newModList []string, currentModList *[]string) (toAdd, toDelete []string) {
64+
currentModMap := make(map[string]bool)
65+
if currentModList != nil {
66+
for _, mod := range *currentModList {
67+
currentModMap[mod] = true
68+
}
69+
}
70+
71+
newModMap := make(map[string]bool)
72+
for _, mod := range newModList {
73+
newModMap[mod] = true
74+
}
75+
76+
for _, mod := range newModList {
77+
if !currentModMap[mod] {
78+
toAdd = append(toAdd, mod)
79+
}
80+
}
81+
82+
for mod := range currentModMap {
83+
if !newModMap[mod] {
84+
toDelete = append(toDelete, mod)
85+
}
86+
}
87+
88+
return toAdd, toDelete
89+
}

api/utils_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package api
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_extractModeratorChanges(t *testing.T) {
8+
currentModList := []string{"a", "b", "c"}
9+
newModList := []string{"a", "x", "c"}
10+
11+
toAdd, toDelete := extractModeratorChanges(newModList, &currentModList)
12+
if len(toAdd) != 1 {
13+
t.Errorf("Test_extractModeratorChanges returned incorrect number of additions: expected %d got %d", 1, len(toAdd))
14+
}
15+
if len(toDelete) != 1 {
16+
t.Errorf("Test_extractModeratorChanges returned incorrect number of deletions: expected %d got %d", 1, len(toDelete))
17+
}
18+
19+
if toAdd[0] != "x" {
20+
t.Errorf("Test_extractModeratorChanges returned incorrect addition: expected a got %s", toAdd[0])
21+
}
22+
if toDelete[0] != "b" {
23+
t.Errorf("Test_extractModeratorChanges returned incorrect deletion: expected b got %s", toDelete[0])
24+
}
25+
}

cmd/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ func (x *Start) Execute(args []string) error {
713713
}()
714714
}
715715
}
716-
<-core.Node.DHT.BootstrapChan
717716
core.Node.Service = service.New(core.Node, sqliteDB)
717+
core.Node.Service.WaitForReady()
718718

719719
core.Node.StartMessageRetriever()
720720
core.Node.StartPointerRepublisher()

core/moderation.go

+3-23
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,12 @@ func (n *OpenBazaarNode) SetModeratorsOnListings(moderators []string) error {
262262
}
263263

264264
// NotifyModerators - notify moderators(peers)
265-
func (n *OpenBazaarNode) NotifyModerators(moderators []string) error {
266-
settings, err := n.Datastore.Settings().Get()
267-
if err != nil {
268-
return err
269-
}
270-
currentMods := make(map[string]bool)
271-
if settings.StoreModerators != nil {
272-
for _, mod := range *settings.StoreModerators {
273-
currentMods[mod] = true
274-
}
275-
}
276-
var addedMods []string
277-
for _, mod := range moderators {
278-
if !currentMods[mod] {
279-
addedMods = append(addedMods, mod)
280-
} else {
281-
delete(currentMods, mod)
282-
}
283-
}
284-
285-
removedMods := currentMods
286-
265+
func (n *OpenBazaarNode) NotifyModerators(addedMods, removedMods []string) error {
266+
n.Service.WaitForReady()
287267
for _, mod := range addedMods {
288268
go n.SendModeratorAdd(mod)
289269
}
290-
for mod := range removedMods {
270+
for _, mod := range removedMods {
291271
go n.SendModeratorRemove(mod)
292272
}
293273
return nil

mobile/node.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ func (n *Node) Start() error {
383383
}()
384384
}
385385
}
386-
<-n.OpenBazaarNode.DHT.BootstrapChan
387386
n.OpenBazaarNode.Service = service.New(n.OpenBazaarNode, n.OpenBazaarNode.Datastore)
387+
n.OpenBazaarNode.Service.WaitForReady()
388388
MR := ret.NewMessageRetriever(ret.MRConfig{
389389
Db: n.OpenBazaarNode.Datastore,
390390
IPFSNode: n.OpenBazaarNode.IpfsNode,

net/networkservice.go

+3
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ type NetworkService interface {
3030

3131
// Disconnect from the given peer
3232
DisconnectFromPeer(p peer.ID) error
33+
34+
// Block until the service is available
35+
WaitForReady()
3336
}

net/service/service.go

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func New(node *core.OpenBazaarNode, datastore repo.Datastore) *OpenBazaarService
5454
return service
5555
}
5656

57+
func (service *OpenBazaarService) WaitForReady() {
58+
<-service.node.DHT.BootstrapChan
59+
}
60+
5761
func (service *OpenBazaarService) DisconnectFromPeer(p peer.ID) error {
5862
log.Debugf("Disconnecting from %s", p.Pretty())
5963
service.senderlk.Lock()

0 commit comments

Comments
 (0)