Add most NIP-11 extension structures to the RelayInformationDocument struct. (#80)

* Fix race condition on status in Relay.Publish method and failure to send

A race-condition exists between setting of the (unprotected) status and the callback which sets the status upon receiving an OK.
The message is sent which can receive an OK in separate goroutine (setting status) prior to the status being set to 'sent.'
The OK can be received prior to the status being set.

This fix also sets the status to PublishStatusFailed if the WriteJSON call fails.

* Add some NIP-11 extension structures to the RelayInformationDocument struct.

Added additional NIP-11 fields for relays that want to provide additional details based on NIP-11 extensions.
The retention structure has been left out as it doesn't have a clean schema for kinds (array of kinds, or pairs of kinds?)
Specified the fields w/ omitempty so marshaled will be same as original NIP-11 if nothing else is specified.
Nested structs defined as pointers so they are omitted if not specified.

* Fix TestPublishWriteFailed so that the socket is given a brief amount of time to close prior to publish being called.
The test relies on Publish always failing.
This commit is contained in:
Patrick Bennett 2023-04-26 07:06:05 -04:00 committed by GitHub
parent 3838ed7e91
commit ebae5d41e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -8,4 +8,43 @@ type RelayInformationDocument struct {
SupportedNIPs []int `json:"supported_nips"`
Software string `json:"software"`
Version string `json:"version"`
Limitation *RelayLimitationDocument `json:"limitation,omitempty"`
RelayCountries []string `json:"relay_countries,omitempty"`
LanguageTags []string `json:"language_tags,omitempty"`
Tags []string `json:"tags,omitempty"`
PostingPolicy string `json:"posting_policy,omitempty"`
PaymentsURL string `json:"payments_url,omitempty"`
Fees *RelayFeesDocument `json:"fees,omitempty"`
}
type RelayLimitationDocument struct {
MaxMessageLength int `json:"max_message_length,omitempty"`
MaxSubscriptions int `json:"max_subscriptions,omitempty"`
MaxFilters int `json:"max_filters,omitempty"`
MaxLimit int `json:"max_limit,omitempty"`
MaxSubidLength int `json:"max_subid_length,omitempty"`
MinPrefix int `json:"min_prefix,omitempty"`
MaxEventTags int `json:"max_event_tags,omitempty"`
MaxContentLength int `json:"max_content_length,omitempty"`
MinPowDifficulty int `json:"min_pow_difficulty,omitempty"`
AuthRequired bool `json:"auth_required"`
PaymentRequired bool `json:"payment_required"`
}
type RelayFeesDocument struct {
Admission []struct {
Amount int `json:"amount"`
Unit string `json:"unit"`
} `json:"admission,omitempty"`
Subscription []struct {
Amount int `json:"amount"`
Unit string `json:"unit"`
Period int `json:"period"`
} `json:"subscription,omitempty"`
Publication []struct {
Kinds []int `json:"kinds"`
Amount int `json:"amount"`
Unit string `json:"unit"`
} `json:"publication,omitempty"`
}

View File

@ -105,9 +105,11 @@ func TestPublishWriteFailed(t *testing.T) {
// connect a client and send a text note
rl := mustRelayConnect(ws.URL)
status, _ := rl.Publish(context.Background(), textNote)
// Force brief period of time so that publish always fails on closed socket.
time.Sleep(1 * time.Millisecond)
status, err := rl.Publish(context.Background(), textNote)
if status != PublishStatusFailed {
t.Errorf("published status is %d, not %d", status, PublishStatusFailed)
t.Errorf("published status is %d, not %d, err: %v", status, PublishStatusFailed, err)
}
}