2025-01-02 21:42:04 +09:00
|
|
|
//go:build !js
|
|
|
|
|
2022-12-17 19:39:10 +01:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
"bytes"
|
2022-12-17 19:39:10 +01:00
|
|
|
"context"
|
2024-12-03 00:49:27 -03:00
|
|
|
stdjson "encoding/json"
|
2022-12-17 19:39:10 +01:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-09-09 13:50:56 +03:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-12-17 19:39:10 +01:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
func TestPublish(t *testing.T) {
|
|
|
|
// test note to be sent over websocket
|
|
|
|
priv, pub := makeKeyPair(t)
|
|
|
|
textNote := Event{
|
2023-09-06 21:00:05 -03:00
|
|
|
Kind: KindTextNote,
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
Content: "hello",
|
2023-04-16 16:12:42 -03:00
|
|
|
CreatedAt: Timestamp(1672068534), // random fixed timestamp
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
Tags: Tags{[]string{"foo", "bar"}},
|
|
|
|
PubKey: pub,
|
|
|
|
}
|
2024-09-09 13:50:56 +03:30
|
|
|
err := textNote.Sign(priv)
|
|
|
|
assert.NoError(t, err)
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
|
|
|
|
// fake relay server
|
|
|
|
var mu sync.Mutex // guards published to satisfy go test -race
|
|
|
|
var published bool
|
|
|
|
ws := newWebsocketServer(func(conn *websocket.Conn) {
|
|
|
|
mu.Lock()
|
|
|
|
published = true
|
|
|
|
mu.Unlock()
|
|
|
|
// verify the client sent exactly the textNote
|
2024-12-03 00:49:27 -03:00
|
|
|
var raw []stdjson.RawMessage
|
2024-09-09 13:50:56 +03:30
|
|
|
err := websocket.JSON.Receive(conn, &raw)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
event := parseEventMessage(t, raw)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.True(t, bytes.Equal(event.Serialize(), textNote.Serialize()))
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
// send back an ok nip-20 command result
|
|
|
|
res := []any{"OK", textNote.ID, true, ""}
|
2024-09-09 13:50:56 +03:30
|
|
|
err = websocket.JSON.Send(conn, res)
|
|
|
|
assert.NoError(t, err)
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
})
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// connect a client and send the text note
|
2024-09-09 13:50:56 +03:30
|
|
|
rl := mustRelayConnect(t, ws.URL)
|
|
|
|
err = rl.Publish(context.Background(), textNote)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, published, "fake relay server saw no event")
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishBlocked(t *testing.T) {
|
|
|
|
// test note to be sent over websocket
|
2023-09-06 21:00:05 -03:00
|
|
|
textNote := Event{Kind: KindTextNote, Content: "hello"}
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
textNote.ID = textNote.GetID()
|
|
|
|
|
|
|
|
// fake relay server
|
|
|
|
ws := newWebsocketServer(func(conn *websocket.Conn) {
|
|
|
|
// discard received message; not interested
|
2024-12-03 00:49:27 -03:00
|
|
|
var raw []stdjson.RawMessage
|
2024-09-09 13:50:56 +03:30
|
|
|
err := websocket.JSON.Receive(conn, &raw)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
// send back a not ok nip-20 command result
|
|
|
|
res := []any{"OK", textNote.ID, false, "blocked"}
|
|
|
|
websocket.JSON.Send(conn, res)
|
|
|
|
})
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// connect a client and send a text note
|
2024-09-09 13:50:56 +03:30
|
|
|
rl := mustRelayConnect(t, ws.URL)
|
2023-12-07 21:39:40 -03:00
|
|
|
err := rl.Publish(context.Background(), textNote)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.Error(t, err)
|
2023-04-21 00:10:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishWriteFailed(t *testing.T) {
|
|
|
|
// test note to be sent over websocket
|
2023-09-06 21:00:05 -03:00
|
|
|
textNote := Event{Kind: KindTextNote, Content: "hello"}
|
2023-04-21 00:10:11 -04:00
|
|
|
textNote.ID = textNote.GetID()
|
|
|
|
|
|
|
|
// fake relay server
|
|
|
|
ws := newWebsocketServer(func(conn *websocket.Conn) {
|
|
|
|
// reject receive - force send error
|
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// connect a client and send a text note
|
2024-09-09 13:50:56 +03:30
|
|
|
rl := mustRelayConnect(t, ws.URL)
|
2023-04-26 07:06:05 -04:00
|
|
|
// Force brief period of time so that publish always fails on closed socket.
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
2023-12-07 21:39:40 -03:00
|
|
|
err := rl.Publish(context.Background(), textNote)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.Error(t, err)
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
}
|
|
|
|
|
2022-12-17 19:39:10 +01:00
|
|
|
func TestConnectContext(t *testing.T) {
|
|
|
|
// fake relay server
|
|
|
|
var mu sync.Mutex // guards connected to satisfy go test -race
|
|
|
|
var connected bool
|
|
|
|
ws := newWebsocketServer(func(conn *websocket.Conn) {
|
|
|
|
mu.Lock()
|
|
|
|
connected = true
|
|
|
|
mu.Unlock()
|
|
|
|
io.ReadAll(conn) // discard all input
|
|
|
|
})
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// relay client
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
2023-01-01 20:22:40 -03:00
|
|
|
r, err := RelayConnect(ctx, ws.URL)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-17 19:39:10 +01:00
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.True(t, connected, "fake relay server saw no client connect")
|
2022-12-17 19:39:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnectContextCanceled(t *testing.T) {
|
|
|
|
// fake relay server
|
2023-02-21 13:12:23 +01:00
|
|
|
ws := newWebsocketServer(discardingHandler)
|
2022-12-17 19:39:10 +01:00
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// relay client
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // make ctx expired
|
2023-01-01 20:22:40 -03:00
|
|
|
_, err := RelayConnect(ctx, ws.URL)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.ErrorIs(t, err, context.Canceled)
|
2022-12-17 19:39:10 +01:00
|
|
|
}
|
|
|
|
|
2023-02-21 13:12:23 +01:00
|
|
|
func TestConnectWithOrigin(t *testing.T) {
|
|
|
|
// fake relay server
|
|
|
|
// default handler requires origin golang.org/x/net/websocket
|
|
|
|
ws := httptest.NewServer(websocket.Handler(discardingHandler))
|
|
|
|
defer ws.Close()
|
|
|
|
|
|
|
|
// relay client
|
2025-01-03 01:15:12 -03:00
|
|
|
r := NewRelay(context.Background(), NormalizeURL(ws.URL),
|
|
|
|
WithRequestHeader(http.Header{"origin": {"https://example.com"}}))
|
2023-02-21 13:12:23 +01:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
err := r.Connect(ctx)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
2023-02-21 13:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func discardingHandler(conn *websocket.Conn) {
|
|
|
|
io.ReadAll(conn) // discard all input
|
|
|
|
}
|
|
|
|
|
2022-12-17 19:39:10 +01:00
|
|
|
func newWebsocketServer(handler func(*websocket.Conn)) *httptest.Server {
|
|
|
|
return httptest.NewServer(&websocket.Server{
|
|
|
|
Handshake: anyOriginHandshake,
|
|
|
|
Handler: handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// anyOriginHandshake is an alternative to default in golang.org/x/net/websocket
|
|
|
|
// which checks for origin. nostr client sends no origin and it makes no difference
|
|
|
|
// for the tests here anyway.
|
|
|
|
var anyOriginHandshake = func(conf *websocket.Config, r *http.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
|
|
|
|
func makeKeyPair(t *testing.T) (priv, pub string) {
|
|
|
|
t.Helper()
|
2024-09-09 13:50:56 +03:30
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
privkey := GeneratePrivateKey()
|
|
|
|
pubkey, err := GetPublicKey(privkey)
|
2024-09-09 13:50:56 +03:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
return privkey, pubkey
|
|
|
|
}
|
|
|
|
|
2024-09-09 13:50:56 +03:30
|
|
|
func mustRelayConnect(t *testing.T, url string) *Relay {
|
|
|
|
t.Helper()
|
|
|
|
|
2023-04-06 16:22:18 -03:00
|
|
|
rl, err := RelayConnect(context.Background(), url)
|
2024-09-09 13:50:56 +03:30
|
|
|
require.NoError(t, err)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:49:27 -03:00
|
|
|
func parseEventMessage(t *testing.T, raw []stdjson.RawMessage) Event {
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
t.Helper()
|
2024-09-09 13:50:56 +03:30
|
|
|
|
|
|
|
assert.Condition(t, func() (success bool) {
|
|
|
|
return len(raw) >= 2
|
|
|
|
})
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
var typ string
|
2024-09-09 13:50:56 +03:30
|
|
|
err := json.Unmarshal(raw[0], &typ)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "EVENT", typ)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
var event Event
|
2024-09-09 13:50:56 +03:30
|
|
|
err = json.Unmarshal(raw[1], &event)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
publish: correctly report failed command statuses from nip-20 relays
the client never reported a failed status, for example when a relay
responds with a:
["OK", event-id, false, "blocked"]
this was due to Relay.statusChans map missing a channel for an event
when a nip-20 status command is reported by a relay. the reason this
happened is due to the method's receiver, which was copied instead of
referenced by a pointer:
func (r Relay) Publish(event Event) chan Status {
// uses a **copy** of statusChans here:
r.statusChans.Store(event.ID, statusChan)
...
}
the bugfix is a one character change:
func (r *Relay) Publish(event Event) chan Status
but while there, spotted another bug where an ok variable was shadowed
and the status chan would've reported incorrect value:
// ok, which is a command status from earlier, is shadowed here:
if statusChan, ok := r.statusChans.Load(eventId); ok {
statusChan <- ...
}
as a side effect, Relay.Publish now reports PublishStatusSucceeded
twice for relays which implement nip-20: once from an OK command status
and the other one from its adhoc subscription to observe whether the
event has been seen. added a todo to address it in the future.
2022-12-26 17:20:56 +01:00
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2024-12-03 00:49:27 -03:00
|
|
|
func parseSubscriptionMessage(t *testing.T, raw []stdjson.RawMessage) (subid string, filters []Filter) {
|
2022-12-26 19:54:37 +01:00
|
|
|
t.Helper()
|
2024-09-09 13:50:56 +03:30
|
|
|
|
|
|
|
assert.Greater(t, len(raw), 3)
|
|
|
|
|
2022-12-26 19:54:37 +01:00
|
|
|
var typ string
|
2024-09-09 13:50:56 +03:30
|
|
|
err := json.Unmarshal(raw[0], &typ)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "REQ", typ)
|
|
|
|
|
2022-12-26 19:54:37 +01:00
|
|
|
var id string
|
2024-09-09 13:50:56 +03:30
|
|
|
err = json.Unmarshal(raw[1], &id)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-26 19:54:37 +01:00
|
|
|
var ff []Filter
|
2024-09-09 13:50:56 +03:30
|
|
|
for _, b := range raw[2:] {
|
2022-12-26 19:54:37 +01:00
|
|
|
var f Filter
|
2024-09-09 13:50:56 +03:30
|
|
|
err := json.Unmarshal(b, &f)
|
|
|
|
assert.NoError(t, err)
|
2022-12-26 19:54:37 +01:00
|
|
|
ff = append(ff, f)
|
|
|
|
}
|
|
|
|
return id, ff
|
|
|
|
}
|