-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: admin function to for
sync
and peerCount
- Loading branch information
1 parent
4e1b835
commit 30076f7
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package host | ||
|
||
import ( | ||
"bisonai.com/orakl/node/pkg/admin/utils" | ||
"bisonai.com/orakl/node/pkg/bus" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func getPeerCount(c *fiber.Ctx) error { | ||
msg, err := utils.SendMessage(c, bus.LIBP2P, bus.GET_PEER_COUNT, nil) | ||
if err != nil { | ||
log.Error().Err(err).Str("Player", "Admin").Msg("failed to send message to libp2p helper") | ||
return c.Status(fiber.StatusInternalServerError).SendString("failed to get peer count: " + err.Error()) | ||
} | ||
resp := <-msg.Response | ||
if !resp.Success { | ||
log.Error().Str("Player", "Admin").Msg("failed to get peer count: " + resp.Args["error"].(string)) | ||
return c.Status(fiber.StatusInternalServerError).SendString("failed to get peer count: " + resp.Args["error"].(string)) | ||
} | ||
|
||
return c.JSON(resp.Args) | ||
} | ||
|
||
func sync(c *fiber.Ctx) error { | ||
msg, err := utils.SendMessage(c, bus.LIBP2P, bus.SYNC, nil) | ||
if err != nil { | ||
log.Error().Err(err).Str("Player", "Admin").Msg("failed to send message to libp2p helper") | ||
return c.Status(fiber.StatusInternalServerError).SendString("failed to sync libp2p host: " + err.Error()) | ||
} | ||
resp := <-msg.Response | ||
if !resp.Success { | ||
log.Error().Str("Player", "Admin").Msg("failed to sync libp2p host") | ||
return c.Status(fiber.StatusInternalServerError).SendString("failed to sync libp2p host: " + resp.Args["error"].(string)) | ||
} | ||
|
||
return c.SendString("libp2p synced") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package host | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func Routes(router fiber.Router) { | ||
host := router.Group("/host") | ||
|
||
host.Get("/peercount", getPeerCount) | ||
host.Post("/sync", sync) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//nolint:all | ||
package tests | ||
|
||
import ( | ||
"context" | ||
|
||
"testing" | ||
|
||
"bisonai.com/orakl/node/pkg/bus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetPeerCount(t *testing.T) { | ||
ctx := context.Background() | ||
cleanup, testItems, err := setup(ctx) | ||
if err != nil { | ||
t.Fatalf("error setting up test: %v", err) | ||
} | ||
defer cleanup() | ||
|
||
channel := testItems.mb.Subscribe(bus.LIBP2P) | ||
waitForMessageWithResponse(t, channel, bus.ADMIN, bus.LIBP2P, bus.GET_PEER_COUNT, map[string]any{"Count": 1}) | ||
|
||
result, err := GetRequest[struct{ Count int }](testItems.app, "/api/v1/host/peercount", nil) | ||
if err != nil { | ||
t.Fatalf("error getting peercount: %v", err) | ||
} | ||
|
||
assert.Equal(t, 1, result.Count) | ||
} | ||
|
||
func TestSync(t *testing.T) { | ||
ctx := context.Background() | ||
cleanup, testItems, err := setup(ctx) | ||
if err != nil { | ||
t.Fatalf("error setting up test: %v", err) | ||
} | ||
defer cleanup() | ||
|
||
channel := testItems.mb.Subscribe(bus.LIBP2P) | ||
waitForMessage(t, channel, bus.ADMIN, bus.LIBP2P, bus.SYNC) | ||
|
||
result, err := RawPostRequest(testItems.app, "/api/v1/host/sync", nil) | ||
if err != nil { | ||
t.Fatalf("error sync libp2p host: %v", err) | ||
} | ||
|
||
assert.Equal(t, string(result), "libp2p synced") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters