Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leftovers for private groups #67

Merged
merged 24 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {
sbot, err := sbot.New()
check(err)

publish, err := multilogs.OpenPublishLog(sbot.RootLog, sbot.UserFeeds, *sbot.KeyPair)
publish, err := multilogs.OpenPublishLog(sbot.ReceiveLog, sbot.UserFeeds, *sbot.KeyPair)
check(err)

alice, err := refs.ParseFeedRef("@alicesKeyInActualBase64Bytes.ed25519")
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (c Client) MessagesByType(opts message.MessagesByTypeArgs) (luigi.Source, e
}

func (c Client) Tangles(o message.TanglesArgs) (luigi.Source, error) {
src, err := c.Source(c.rootCtx, o.MarshalType, muxrpc.Method{"tangles"}, o)
src, err := c.Source(c.rootCtx, o.MarshalType, muxrpc.Method{"tangles", "replies"}, o)
return src, errors.Wrap(err, "ssbClient/tangles: failed to create stream")
}

Expand Down
44 changes: 25 additions & 19 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"go.cryptoscope.co/ssb/internal/testutils"
"go.cryptoscope.co/ssb/message"
"go.cryptoscope.co/ssb/network"
"go.cryptoscope.co/ssb/plugins2"
"go.cryptoscope.co/ssb/plugins2/tangles"
"go.cryptoscope.co/ssb/sbot"
refs "go.mindeco.de/ssb-refs"
)
Expand Down Expand Up @@ -58,7 +56,10 @@ func TestUnixSock(t *testing.T) {
var msgs []*refs.MessageRef
const msgCount = 15
for i := 0; i < msgCount; i++ {
ref, err := c.Publish(struct{ I int }{i})
ref, err := c.Publish(struct {
Type string `json:"type"`
Test int
}{"test", i})
r.NoError(err)
r.NotNil(ref)
msgs = append(msgs, ref)
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestUnixSock(t *testing.T) {
msg, ok := v.(refs.Message)
r.True(ok, "%d: wrong type: %T", i, v)

r.True(msg.Key().Equal(*msgs[i]), "wrong message %d", i)
r.True(msg.Key().Equal(msgs[i]), "wrong message %d", i)
i++
}
r.Equal(msgCount, i, "did not get all messages")
Expand Down Expand Up @@ -319,7 +320,10 @@ func LotsOfStatusCalls(newPair mkPair) func(t *testing.T) {

for i := 25; i > 0; i-- {
time.Sleep(500 * time.Millisecond)
ref, err := c.Publish(struct{ Test int }{i})
ref, err := c.Publish(struct {
Type string `json:"type"`
Test int
}{"test", i})
r.NoError(err, "publish %d errored", i)
r.NotNil(ref)

Expand All @@ -339,7 +343,7 @@ func LotsOfStatusCalls(newPair mkPair) func(t *testing.T) {

a.GreaterOrEqual(statusCalls, uint32(1000), "expected more status calls")

v, err := srv.RootLog.Seq().Value()
v, err := srv.ReceiveLog.Seq().Value()
r.NoError(err)
r.EqualValues(24, v)

Expand All @@ -348,6 +352,12 @@ func LotsOfStatusCalls(newPair mkPair) func(t *testing.T) {
}
}

type testMsg struct {
Type string `json:"type"`
Foo string
Bar int
}

func TestPublish(t *testing.T) {
// defer leakcheck.Check(t)
r, a := require.New(t), assert.New(t)
Expand Down Expand Up @@ -381,25 +391,21 @@ func TestPublish(t *testing.T) {
// end test boilerplate

// no messages yet
seqv, err := srv.RootLog.Seq().Value()
seqv, err := srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
r.Equal(margaret.SeqEmpty, seqv)

type testMsg struct {
Foo string
Bar int
}
msg := testMsg{"hello", 23}
msg := testMsg{"test", "hello", 23}
ref, err := c.Publish(msg)
r.NoError(err, "failed to call publish")
r.NotNil(ref)

// get stored message from the log
seqv, err = srv.RootLog.Seq().Value()
seqv, err = srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
wantSeq := margaret.BaseSeq(0)
a.Equal(wantSeq, seqv)
msgv, err := srv.RootLog.Get(wantSeq)
msgv, err := srv.ReceiveLog.Get(wantSeq)
r.NoError(err)
newMsg, ok := msgv.(refs.Message)
r.True(ok)
Expand Down Expand Up @@ -442,7 +448,6 @@ func TestTangles(t *testing.T) {
sbot.WithInfo(srvLog),
sbot.WithRepoPath(srvRepo),
sbot.WithListenAddr(":0"),
sbot.LateOption(sbot.MountPlugin(&tangles.Plugin{}, plugins2.AuthMaster)),
)
r.NoError(err, "sbot srv init failed")

Expand All @@ -464,26 +469,27 @@ func TestTangles(t *testing.T) {
// end test boilerplate

type testMsg struct {
Type string `json:"type"`
Foo string
Bar int
Root *refs.MessageRef `json:"root,omitempty"`
}
msg := testMsg{"hello", 23, nil}
msg := testMsg{"test", "hello", 23, nil}
rootRef, err := c.Publish(msg)
r.NoError(err, "failed to call publish")
r.NotNil(rootRef)

rep1 := testMsg{"reply", 1, rootRef}
rep1 := testMsg{"test", "reply", 1, rootRef}
rep1Ref, err := c.Publish(rep1)
r.NoError(err, "failed to call publish")
r.NotNil(rep1Ref)
rep2 := testMsg{"reply", 2, rootRef}
rep2 := testMsg{"test", "reply", 2, rootRef}
rep2Ref, err := c.Publish(rep2)
r.NoError(err, "failed to call publish")
r.NotNil(rep2Ref)

opts := message.TanglesArgs{}
opts.Root = *rootRef
opts.Root = rootRef
opts.Limit = 2
opts.Keys = true
opts.MarshalType = refs.KeyValueRaw{}
Expand Down
7 changes: 5 additions & 2 deletions client/dont_break_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ func TestAskForSomethingWeird(t *testing.T) {
var msgs []*refs.MessageRef
const msgCount = 15
for i := 0; i < msgCount; i++ {
ref, err := c.Publish(struct{ I int }{i})
ref, err := c.Publish(struct {
Type string `json:"type"`
Test int
}{"test", i})
r.NoError(err)
r.NotNil(ref)
msgs = append(msgs, ref)
Expand Down Expand Up @@ -117,7 +120,7 @@ func TestAskForSomethingWeird(t *testing.T) {
msg, ok := v.(refs.Message)
r.True(ok, "%d: wrong type: %T", i, v)

r.True(msg.Key().Equal(*msgs[i]), "wrong message %d", i)
r.True(msg.Key().Equal(msgs[i]), "wrong message %d", i)
i++
}
r.Equal(msgCount, i, "did not get all messages")
Expand Down
11 changes: 3 additions & 8 deletions client/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,21 @@ func TestEncodeHistStreamAsJSON(t *testing.T) {
// end test boilerplate

// no messages yet
seqv, err := srv.RootLog.Seq().Value()
seqv, err := srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
r.Equal(margaret.SeqEmpty, seqv)

type testMsg struct {
Foo string
Bar int
}
var wantRefs []string
for i := 0; i < 10; i++ {

msg := testMsg{"hello", 23}
msg := testMsg{"test", "hello", 23}
ref, err := c.Publish(msg)
r.NoError(err, "failed to call publish")
r.NotNil(ref)

wantRefs = append(wantRefs, ref.Ref())
}

seqv, err = srv.RootLog.Seq().Value()
seqv, err = srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
r.EqualValues(9, seqv)

Expand Down
92 changes: 92 additions & 0 deletions client/private_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package client_test

import (
"context"
"os"
"path/filepath"
"testing"

"go.cryptoscope.co/luigi"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.cryptoscope.co/ssb/client"
"go.cryptoscope.co/ssb/internal/testutils"
"go.cryptoscope.co/ssb/message"
"go.cryptoscope.co/ssb/sbot"
)

func TestAutomaticUnboxing(t *testing.T) {
r, a := require.New(t), assert.New(t)

srvRepo := filepath.Join("testrun", t.Name(), "serv")
os.RemoveAll(srvRepo)
srvLog := testutils.NewRelativeTimeLogger(nil)

srv, err := sbot.New(
sbot.WithInfo(srvLog),
sbot.WithRepoPath(srvRepo),
sbot.WithListenAddr(":0"),
sbot.LateOption(sbot.WithUNIXSocket()),
)
r.NoError(err, "sbot srv init failed")

srv.PublishLog.Publish(map[string]string{"type": "test", "hello": "world"})

c, err := client.NewUnix(filepath.Join(srvRepo, "socket"))
r.NoError(err, "failed to make client connection")
// end test boilerplate

ref, err := c.Whoami()
r.NoError(err, "failed to call whoami")
r.NotNil(ref)
a.Equal(srv.KeyPair.Id.Ref(), ref.Ref())

groupID, root, err := srv.Groups.Create("client test group")
r.NoError(err, "failed to create group")

hello1, err := srv.Groups.PublishPostTo(groupID, "hello 1!")
r.NoError(err, "failed to post hello1")

hello2, err := srv.Groups.PublishPostTo(groupID, "hello 2!")
r.NoError(err, "failed to post hello2")

args := message.MessagesByTypeArgs{Type: "post"}
args.Private = false
src, err := c.MessagesByType(args)
r.NoError(err, "failed to create source for public messages")
testElementsInSource(t, src, 0)

args.Private = true
src, err = c.MessagesByType(args)
r.NoError(err, "failed to create source for private messages")
testElementsInSource(t, src, 2)

// TODO: check messages hello1 and hello2 are included in src
_ = hello1
_ = hello2

targs := message.TanglesArgs{
Root: root,
Name: "group",
}
targs.Private = false
src, err = c.Tangles(targs)
r.NoError(err, "failed to create source for tangled messages (public)")
testElementsInSource(t, src, 0)

targs.Private = true
src, err = c.Tangles(targs)
r.NoError(err, "failed to create source for tangled messages (private)")
testElementsInSource(t, src, 3) // add-member + the two posts
}

func testElementsInSource(t *testing.T, src luigi.Source, cnt int) {
ctx := context.Background()
r, a := require.New(t), assert.New(t)
var elems []interface{}
var snk = luigi.NewSliceSink(&elems)
err := luigi.Pump(ctx, snk, src)
r.NoError(err, "failed to get all elements from source (public)")
a.Len(elems, cnt)
}
5 changes: 3 additions & 2 deletions client/replicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ func TestReplicateUpTo(t *testing.T) {
kp.Id.Algo = refs.RefAlgoFeedGabby
}

publish, err := message.OpenPublishLog(srv.RootLog, uf, kp)
publish, err := message.OpenPublishLog(srv.ReceiveLog, uf, kp)
r.NoError(err)

testKeyPairs[kp.Id.Ref()] = i
for n := i; n > 0; n-- {

ref, err := publish.Publish(struct {
Type string `json:"type"`
Test bool
N int
Hello string
}{true, n, kp.Id.Ref()})
}{"test", true, n, kp.Id.Ref()})
r.NoError(err)
t.Log(ref.Ref())
}
Expand Down
13 changes: 4 additions & 9 deletions client/streamtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,23 @@ func TestReadStreamAsInterfaceMessage(t *testing.T) {
// end test boilerplate

// no messages yet
seqv, err := srv.RootLog.Seq().Value()
seqv, err := srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
r.Equal(margaret.SeqEmpty, seqv)

type testMsg struct {
Foo string
Bar int
}
var wantRefs []string
for i := 0; i < 10; i++ {

msg := testMsg{"hello", 23}
msg := testMsg{"test", "hello", 23}
ref, err := c.Publish(msg)
r.NoError(err, "failed to call publish")
r.NotNil(ref)

// get stored message from the log
seqv, err = srv.RootLog.Seq().Value()
seqv, err = srv.ReceiveLog.Seq().Value()
r.NoError(err, "failed to get root log sequence")
wantSeq := margaret.BaseSeq(i)
a.Equal(wantSeq, seqv)
msgv, err := srv.RootLog.Get(wantSeq)
msgv, err := srv.ReceiveLog.Get(wantSeq)
r.NoError(err)
newMsg, ok := msgv.(refs.Message)
r.True(ok)
Expand Down
3 changes: 2 additions & 1 deletion cmd/go-sbot/crashrecovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ func TestRecoverFromCrash(t *testing.T) {
}

ref, err := c.Publish(struct {
Type string `json:"type"`
Test string
Try, I int
}{"working!", try, i})
}{"test", "working!", try, i})
r.NoError(err)
t.Logf("%d:connection established (i:%d) %s", try, i, ref.Ref())

Expand Down
8 changes: 5 additions & 3 deletions cmd/go-sbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"go.cryptoscope.co/ssb"
"go.cryptoscope.co/ssb/internal/ctxutils"
"go.cryptoscope.co/ssb/internal/storedrefs"
"go.cryptoscope.co/ssb/internal/testutils"
"go.cryptoscope.co/ssb/multilogs"
mksbot "go.cryptoscope.co/ssb/sbot"
Expand Down Expand Up @@ -346,7 +347,7 @@ func runSbot() error {
}
RepoStats.With("part", "feeds").Set(float64(len(feeds)))

rseq, err := sbot.RootLog.Seq().Value()
rseq, err := sbot.ReceiveLog.Seq().Value()
if err != nil {
return errors.Wrap(err, "could not get root log sequence number")
}
Expand Down Expand Up @@ -385,7 +386,7 @@ func runSbot() error {
}

for _, blocked := range lst {
isStored, err := multilog.Has(uf, blocked.StoredAddr())
isStored, err := multilog.Has(uf, storedrefs.Feed(blocked))
if err != nil {
return errors.Wrap(err, "blocked lookup in multilog")
}
Expand Down Expand Up @@ -414,7 +415,8 @@ func runSbot() error {
time.Sleep(1 * time.Second)
select {
case <-ctx.Done():
return nil
err := sbot.Close()
return err
default:
}
}
Expand Down
Loading