Skip to content

Commit

Permalink
Add sync status (kvdb stats) p2p endpoints to auth, tns and patrick (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samyfodil authored Jun 5, 2024
1 parent e8236cd commit 8bf90b9
Show file tree
Hide file tree
Showing 26 changed files with 580 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore

./pkg/vm-orbit/tests/e2e/go/fixtures/*.wasm
./pkg/vm-orbit/tests/e2e/go/fixtures/testPlugin*
pkg/vm-orbit/tests/e2e/go/fixtures/*.wasm
pkg/vm-orbit/tests/e2e/go/fixtures/testPlugin*

./services/hoarder/testGIT
./services/monkey/build/
Expand Down
57 changes: 26 additions & 31 deletions clients/p2p/auth/inject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,36 @@ import (

"github.com/taubyte/http/helpers"
commonIface "github.com/taubyte/tau/core/common"
authIface "github.com/taubyte/tau/core/services/auth"
"github.com/taubyte/tau/dream"
"github.com/taubyte/tau/services/auth/acme/store"
"gotest.tools/v3/assert"
)

var testDir = "testdir"
var testDir = "testdir" // TODO: use temp

func injectCert(t *testing.T, client authIface.Client) []byte {
cert, key, err := helpers.GenerateCert("*.pass.com")
assert.NilError(t, err)

var p bytes.Buffer
err = pem.Encode(&p, &pem.Block{
Type: "PRIVATE KEY",
Bytes: key,
})
assert.NilError(t, err)

err = pem.Encode(&p, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
assert.NilError(t, err)

err = client.InjectStaticCertificate("*.pass.com", []byte(cert))
assert.NilError(t, err)

return cert
}

func TestInject(t *testing.T) {
defer os.Remove(testDir)
Expand Down Expand Up @@ -45,39 +69,10 @@ func TestInject(t *testing.T) {
return
}

cert, key, err := helpers.GenerateCert("*.pass.com")
if err != nil {
t.Error(err)
return
}

var p bytes.Buffer
err = pem.Encode(&p, &pem.Block{
Type: "PRIVATE KEY",
Bytes: key,
})
if err != nil {
t.Error(err)
return
}

err = pem.Encode(&p, &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
})
if err != nil {
t.Error(err)
return
}

auth, err := simple.Auth()
assert.NilError(t, err)

err = auth.InjectStaticCertificate("*.pass.com", []byte(cert))
if err != nil {
t.Error(err)
return
}
cert := injectCert(t, auth)

newStore, err := store.New(u.Context(), simple.PeerNode(), testDir, err)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions clients/p2p/auth/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package auth

import (
"errors"
"fmt"

"github.com/taubyte/p2p/streams/command"
kvdbIface "github.com/taubyte/tau/core/kvdb"
iface "github.com/taubyte/tau/core/services/auth"
"github.com/taubyte/tau/pkg/kvdb"
)

func (c *Client) Stats() iface.Stats {
return (*Stats)(c)
}

func (h *Stats) Database() (kvdbIface.Stats, error) {
response, err := h.client.Send("stats", command.Body{"action": "db"})
if err != nil {
return nil, fmt.Errorf("sending stats.db request failed with %w", err)
}

idata, err := response.Get("stats")
if err != nil {
return nil, fmt.Errorf("getting stats from response failed with %w", err)
}

data, ok := idata.([]byte)
if !ok {
return nil, errors.New("incorrect stats type")
}

s := kvdb.NewStats()
err = s.Decode(data)
if err != nil {
return nil, fmt.Errorf("decoding stats failed with %w", err)
}

return s, nil
}
49 changes: 49 additions & 0 deletions clients/p2p/auth/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package auth_test

import (
"os"
"testing"

commonIface "github.com/taubyte/tau/core/common"
"github.com/taubyte/tau/core/kvdb"
"github.com/taubyte/tau/dream"
"gotest.tools/v3/assert"
)

func TestStats(t *testing.T) {
defer os.Remove(testDir)

u := dream.New(dream.UniverseConfig{Name: t.Name()})
defer u.Stop()

err := u.StartWithConfig(&dream.Config{
Services: map[string]commonIface.ServiceConfig{
"auth": {},
},
Simples: map[string]dream.SimpleConfig{
"client": {
Clients: dream.SimpleConfigClients{
Auth: &commonIface.ClientConfig{},
}.Compat(),
},
},
})
assert.NilError(t, err)

simple, err := u.Simple("client")
assert.NilError(t, err)

auth, err := simple.Auth()
assert.NilError(t, err)

// make sure database has a head
injectCert(t, auth)

stats, err := auth.Stats().Database()
assert.NilError(t, err)

assert.Equal(t, stats.Type(), kvdb.TypeCRDT)

assert.Equal(t, len(stats.Heads()), 1)

}
2 changes: 2 additions & 0 deletions clients/p2p/auth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Client struct {
client *client.Client
}

type Stats Client

type Hooks Client

type Projects Client
Expand Down
6 changes: 6 additions & 0 deletions clients/p2p/monkey/tests/patrick_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

"github.com/fxamacker/cbor/v2"
"github.com/taubyte/p2p/peer"
kvdbIface "github.com/taubyte/tau/core/kvdb"
"github.com/taubyte/tau/core/services/patrick"
"github.com/taubyte/tau/pkg/kvdb"
patrickSpecs "github.com/taubyte/tau/pkg/specs/patrick"
)

Expand All @@ -18,6 +20,10 @@ func (s *starfish) Close() {
s.Jobs = nil
}

func (s *starfish) DatabaseStats() (kvdbIface.Stats, error) {
return kvdb.NewStats(), nil
}

func (s *starfish) AddJob(t *testing.T, peerC peer.Node, job *patrick.Job) error {
job_bytes, err := cbor.Marshal(job)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions clients/p2p/patrick/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package patrick

import (
"errors"
"fmt"

"github.com/taubyte/p2p/streams/command"

kvdbIface "github.com/taubyte/tau/core/kvdb"
"github.com/taubyte/tau/pkg/kvdb"
)

func (client *Client) DatabaseStats() (kvdbIface.Stats, error) {
response, err := client.Send("stats", command.Body{"action": "db"})
if err != nil {
return nil, fmt.Errorf("sending stats.db request failed with %w", err)
}

idata, err := response.Get("stats")
if err != nil {
return nil, fmt.Errorf("getting stats from response failed with %w", err)
}

data, ok := idata.([]byte)
if !ok {
return nil, errors.New("incorrect stats type")
}

s := kvdb.NewStats()
err = s.Decode(data)
if err != nil {
return nil, fmt.Errorf("decoding stats failed with %w", err)
}

return s, nil
}
44 changes: 44 additions & 0 deletions clients/p2p/patrick/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package patrick_test

import (
"testing"

commonIface "github.com/taubyte/tau/core/common"
"github.com/taubyte/tau/core/kvdb"
"github.com/taubyte/tau/dream"
"gotest.tools/v3/assert"

_ "github.com/taubyte/tau/services/patrick"
)

func TestStats(t *testing.T) {
u := dream.New(dream.UniverseConfig{Name: t.Name()})
defer u.Stop()

err := u.StartWithConfig(&dream.Config{
Services: map[string]commonIface.ServiceConfig{
"patrick": {},
},
Simples: map[string]dream.SimpleConfig{
"client": {
Clients: dream.SimpleConfigClients{
Patrick: &commonIface.ClientConfig{},
}.Compat(),
},
},
})
assert.NilError(t, err)

simple, err := u.Simple("client")
assert.NilError(t, err)

pat, err := simple.Patrick()
assert.NilError(t, err)

stats, err := pat.DatabaseStats()
assert.NilError(t, err)

assert.Equal(t, stats.Type(), kvdb.TypeCRDT)

assert.Equal(t, len(stats.Heads()), 0)
}
40 changes: 40 additions & 0 deletions clients/p2p/tns/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tns

import (
"errors"
"fmt"

"github.com/taubyte/p2p/streams/command"
kvdbIface "github.com/taubyte/tau/core/kvdb"
iface "github.com/taubyte/tau/core/services/tns"
"github.com/taubyte/tau/pkg/kvdb"
)

func (c *Client) Stats() iface.Stats {
return (*Stats)(c)
}

func (h *Stats) Database() (kvdbIface.Stats, error) {
response, err := h.client.Send("stats", command.Body{"action": "db"})
if err != nil {
return nil, fmt.Errorf("sending stats.db request failed with %w", err)
}

idata, err := response.Get("stats")
if err != nil {
return nil, fmt.Errorf("getting stats from response failed with %w", err)
}

data, ok := idata.([]byte)
if !ok {
return nil, errors.New("incorrect stats type")
}

s := kvdb.NewStats()
err = s.Decode(data)
if err != nil {
return nil, fmt.Errorf("decoding stats failed with %w", err)
}

return s, nil
}
43 changes: 43 additions & 0 deletions clients/p2p/tns/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tns_test

import (
"testing"

commonIface "github.com/taubyte/tau/core/common"
"github.com/taubyte/tau/core/kvdb"
"github.com/taubyte/tau/dream"
"gotest.tools/v3/assert"
)

func TestStats(t *testing.T) {
u := dream.New(dream.UniverseConfig{Name: t.Name()})
defer u.Stop()

err := u.StartWithConfig(&dream.Config{
Services: map[string]commonIface.ServiceConfig{
"tns": {},
},
Simples: map[string]dream.SimpleConfig{
"client": {
Clients: dream.SimpleConfigClients{
TNS: &commonIface.ClientConfig{},
}.Compat(),
},
},
})
assert.NilError(t, err)

simple, err := u.Simple("client")
assert.NilError(t, err)

tns, err := simple.TNS()
assert.NilError(t, err)

stats, err := tns.Stats().Database()
assert.NilError(t, err)

assert.Equal(t, stats.Type(), kvdb.TypeCRDT)

assert.Equal(t, len(stats.Heads()), 0)

}
2 changes: 2 additions & 0 deletions clients/p2p/tns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Client struct {
cache *cache
}

type Stats Client

type subscription struct {
ctx context.Context
ctxC context.CancelFunc
Expand Down
Loading

0 comments on commit 8bf90b9

Please sign in to comment.