Skip to content

Commit

Permalink
fix: update table column name (lib_id -> host_id)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Mar 18, 2024
1 parent cc51210 commit 326f9da
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion node/migrations/000006_peers.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ CREATE TABLE IF NOT EXISTS peers (
id SERIAL PRIMARY KEY,
ip TEXT NOT NULL UNIQUE,
port INTEGER NOT NULL,
lib_id TEXT NOT NULL UNIQUE
host_id TEXT NOT NULL UNIQUE
);
2 changes: 1 addition & 1 deletion node/pkg/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func RefreshJob(ctx context.Context) error {
}

for _, p := range peers {
connectionUrl := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", p.Ip, p.Port, p.LibId)
connectionUrl := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", p.Ip, p.Port, p.HostId)
isAlive, liveCheckErr := libp2p.IsHostAlive(ctx, h, connectionUrl)
if liveCheckErr != nil {
log.Error().Err(liveCheckErr).Msg("Failed to check peer")
Expand Down
26 changes: 13 additions & 13 deletions node/pkg/boot/peer/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
)

type PeerModel struct {
Id int64 `db:"id" json:"id"`
Ip string `db:"ip" json:"ip"`
Port int `db:"port" json:"port"`
LibId string `db:"lib_id" json:"lib_id"`
Id int64 `db:"id" json:"id"`
Ip string `db:"ip" json:"ip"`
Port int `db:"port" json:"port"`
HostId string `db:"host_id" json:"host_id"`
}

type PeerInsertModel struct {
Ip string `db:"ip" json:"ip" validate:"required"`
Port int `db:"port" json:"port" validate:"required"`
LibId string `db:"lib_id" json:"lib_id" validate:"required"`
Ip string `db:"ip" json:"ip" validate:"required"`
Port int `db:"port" json:"port" validate:"required"`
HostId string `db:"host_id" json:"host_id" validate:"required"`
}

func insert(c *fiber.Ctx) error {
Expand All @@ -34,9 +34,9 @@ func insert(c *fiber.Ctx) error {
}

result, err := db.QueryRow[PeerModel](c.Context(), UpsertPeer, map[string]any{
"ip": payload.Ip,
"port": payload.Port,
"lib_id": payload.LibId})
"ip": payload.Ip,
"port": payload.Port,
"host_id": payload.HostId})
if err != nil {
log.Error().Err(err).Msg("Failed to execute insert query")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to execute insert query")
Expand Down Expand Up @@ -74,9 +74,9 @@ func sync(c *fiber.Ctx) error {
}

_, err = db.QueryRow[PeerModel](c.Context(), InsertPeer, map[string]any{
"ip": payload.Ip,
"port": payload.Port,
"lib_id": payload.LibId})
"ip": payload.Ip,
"port": payload.Port,
"host_id": payload.HostId})
if err != nil {
log.Error().Err(err).Msg("Failed to execute insert query")
return c.Status(fiber.StatusInternalServerError).SendString("Failed to execute insert query")
Expand Down
6 changes: 3 additions & 3 deletions node/pkg/boot/peer/queries.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package peer

const (
InsertPeer = `INSERT INTO peers (ip, port, lib_id) VALUES (@ip, @port, @lib_id) RETURNING *;`
InsertPeer = `INSERT INTO peers (ip, port, host_id) VALUES (@ip, @port, @host_id) RETURNING *;`

UpsertPeer = `
INSERT INTO peers (ip, port, lib_id) VALUES (@ip, @port, @lib_id)
ON CONFLICT (ip) DO UPDATE SET port = @port, lib_id = @lib_id RETURNING *;
INSERT INTO peers (ip, port, host_id) VALUES (@ip, @port, @host_id)
ON CONFLICT (ip) DO UPDATE SET port = @port, host_id = @host_id RETURNING *;
`

GetPeer = `SELECT * FROM peers;`
Expand Down
4 changes: 3 additions & 1 deletion node/pkg/boot/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"context"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -46,11 +47,12 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {
func insertSampleData(ctx context.Context) (*TmpData, error) {
var tmpData = new(TmpData)

tmpPeer, err := db.QueryRow[peer.PeerModel](ctx, peer.InsertPeer, map[string]any{"ip": "127.0.0.1", "port": 10000, "lib_id": "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLabc"})
tmpPeer, err := db.QueryRow[peer.PeerModel](ctx, peer.InsertPeer, map[string]any{"ip": "127.0.0.1", "port": 10000, "host_id": "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLabc"})
if err != nil {
return nil, err
}
tmpData.peer = tmpPeer
fmt.Println(tmpPeer)
return tmpData, nil
}

Expand Down
35 changes: 22 additions & 13 deletions node/pkg/boot/tests/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"bisonai.com/orakl/node/pkg/db"
"bisonai.com/orakl/node/pkg/libp2p"
_peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
)

Expand All @@ -25,9 +26,9 @@ func TestPeerInsert(t *testing.T) {
defer cleanup()

mockPeer1 := peer.PeerInsertModel{
Ip: "127.0.0.2",
Port: 10002,
LibId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLdef",
Ip: "127.0.0.2",
Port: 10002,
HostId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLdef",
}

readResultBefore, err := adminTests.GetRequest[[]peer.PeerModel](testItems.app, "/api/v1/peer", nil)
Expand Down Expand Up @@ -79,15 +80,15 @@ func TestSync(t *testing.T) {
defer cleanup()

mockPeer1 := peer.PeerInsertModel{
Ip: "127.0.0.2",
Port: 10002,
LibId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLdef",
Ip: "127.0.0.2",
Port: 10002,
HostId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLdef",
}

mockPeer2 := peer.PeerInsertModel{
Ip: "127.0.0.3",
Port: 10003,
LibId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLghi",
Ip: "127.0.0.3",
Port: 10003,
HostId: "12DGKooWM8vWWqGPWWNCVPqb4tfqGrzx45W257GDBSeYbDSSLghi",
}

syncResult, err := adminTests.PostRequest[[]peer.PeerModel](testItems.app, "/api/v1/peer/sync", mockPeer1)
Expand Down Expand Up @@ -129,7 +130,15 @@ func TestRefresh(t *testing.T) {
Addrs: h.Addrs(),
}

addr := pi.Addrs[len(pi.Addrs)-1]
var addr multiaddr.Multiaddr
for _, a := range pi.Addrs {
if strings.Contains(a.String(), "127.0.0.1") {
continue
}
addr = a
break
}

splitted := strings.Split(addr.String(), "/")
if len(splitted) < 5 {
t.Fatalf("error splitting address: %v", splitted)
Expand All @@ -143,9 +152,9 @@ func TestRefresh(t *testing.T) {
}

res, err := adminTests.PostRequest[peer.PeerModel](testItems.app, "/api/v1/peer", peer.PeerInsertModel{
Ip: ip,
Port: portInt,
LibId: h.ID().String(),
Ip: ip,
Port: portInt,
HostId: h.ID().String(),
})
if err != nil {
t.Fatalf("error inserting peer: %v", err)
Expand Down

0 comments on commit 326f9da

Please sign in to comment.