Skip to content

Commit

Permalink
feat: admin function to for sync and peerCount
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 15, 2024
1 parent 4e1b835 commit 30076f7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions node/pkg/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bisonai.com/orakl/node/pkg/admin/config"
"bisonai.com/orakl/node/pkg/admin/feed"
"bisonai.com/orakl/node/pkg/admin/fetcher"
"bisonai.com/orakl/node/pkg/admin/host"
"bisonai.com/orakl/node/pkg/admin/providerUrl"
"bisonai.com/orakl/node/pkg/admin/proxy"
"bisonai.com/orakl/node/pkg/admin/reporter"
Expand Down Expand Up @@ -44,6 +45,7 @@ func Run(bus *bus.MessageBus) error {
wallet.Routes(v1)
providerUrl.Routes(v1)
config.Routes(v1)
host.Routes(v1)

port := os.Getenv("APP_PORT")
if port == "" {
Expand Down
38 changes: 38 additions & 0 deletions node/pkg/admin/host/controller.go
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")
}
12 changes: 12 additions & 0 deletions node/pkg/admin/host/route.go
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)
}
49 changes: 49 additions & 0 deletions node/pkg/admin/tests/host_test.go
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")
}
2 changes: 2 additions & 0 deletions node/pkg/admin/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bisonai.com/orakl/node/pkg/admin/config"
"bisonai.com/orakl/node/pkg/admin/feed"
"bisonai.com/orakl/node/pkg/admin/fetcher"
"bisonai.com/orakl/node/pkg/admin/host"
"bisonai.com/orakl/node/pkg/admin/providerUrl"
"bisonai.com/orakl/node/pkg/admin/proxy"
"bisonai.com/orakl/node/pkg/admin/reporter"
Expand Down Expand Up @@ -67,6 +68,7 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {
reporter.Routes(v1)
providerUrl.Routes(v1)
config.Routes(v1)
host.Routes(v1)
return adminCleanup(testItems), testItems, nil
}

Expand Down
15 changes: 15 additions & 0 deletions node/pkg/admin/tests/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,18 @@ func waitForMessage(t *testing.T, channel <-chan bus.Message, from, to, command
}
}()
}

func waitForMessageWithResponse(t *testing.T, channel <-chan bus.Message, from, to, command string, resp map[string]any) {
go func() {
select {
case msg := <-channel:
if msg.From != from || msg.To != to || msg.Content.Command != command {
t.Errorf("unexpected message: %v", msg)
}
msg.Response <- bus.MessageResponse{Success: true, Args: resp}
case <-time.After(5 * time.Second):
t.Errorf("no message received on channel")
}
}()

}

0 comments on commit 30076f7

Please sign in to comment.