add NOTICE and OK envelope encoders with marshal/unmarshal support and comprehensive tests

This commit is contained in:
2025-08-30 14:02:33 +01:00
parent caf9546d1c
commit 431f37763d
5 changed files with 408 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
// Package messages is a collection of example/common messages and
// machine-readable prefixes to use with OK and CLOSED envelopes.
package messages
import (
"lukechampine.com/frand"
)
const (
// Duplicate is a machine readable prefix for OK envelopes indicating that the
// submitted event is already in the relay,s event store.
Duplicate = "duplicate"
// Pow is a machine readable prefix for OK envelopes indicating that the
// eventid.T lacks sufficient zeros at the front.
Pow = "pow"
// Blocked is a machine readable prefix for OK envelopes indicating the event
// submission or REQ has been rejected.
Blocked = "blocked"
// RateLimited is a machine readable prefix for CLOSED and OK envelopes
// indicating the relay is now slowing down processing of requests from the
// client.
RateLimited = "rate-limited"
// Invalid is a machine readable prefix for OK envelopes indicating
// that the submitted event or other request is not correctly formatted, and may
// mean a signature does not verify.
Invalid = "invalid"
// Error is a machine readable prefix for CLOSED and OK envelopes indicating
// there was some kind of error in processing the request.
Error = "error"
)
// Examples are some examples of the use of the prefixes above with appropriate
// human-readable suffixes.
var Examples = [][]byte{
[]byte("pow: difficulty 25>=24"),
[]byte("duplicate: already have this event"),
[]byte("blocked: you are banned from posting here"),
[]byte("blocked: please register your pubkey at " +
"https://my-expensive-relay.example.com"),
[]byte("rate-limited: slow down there chief"),
[]byte("invalid: event creation date is too far off from the current time"),
[]byte("pow: difficulty 26 is less than 30"),
[]byte("error: could not connect to the database"),
}
// RandomMessage generates a random message out of the above list of Examples.
func RandomMessage() []byte {
return Examples[frand.Intn(len(Examples)-1)]
}