performance improvements
This commit is contained in:
35
event.go
35
event.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
@@ -26,7 +27,17 @@ var (
|
|||||||
Hex128Pattern = regexp.MustCompile("^[a-f0-9]{128}$")
|
Hex128Pattern = regexp.MustCompile("^[a-f0-9]{128}$")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e Event) Serialize() ([]byte, error) {
|
var (
|
||||||
|
ErrMalformedPubKey = errors.New("pubkey must be 64 lowercase hex characters")
|
||||||
|
ErrMalformedID = errors.New("id must be 64 hex characters")
|
||||||
|
ErrMalformedSig = errors.New("signature must be 128 hex characters")
|
||||||
|
ErrMalformedTag = errors.New("tags must contain at least two elements")
|
||||||
|
ErrFailedIDComp = errors.New("failed to compute event id")
|
||||||
|
ErrNoEventID = errors.New("event id is empty")
|
||||||
|
ErrInvalidSig = errors.New("event signature is invalid")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (e *Event) Serialize() ([]byte, error) {
|
||||||
serialized := []interface{}{
|
serialized := []interface{}{
|
||||||
0,
|
0,
|
||||||
e.PubKey,
|
e.PubKey,
|
||||||
@@ -43,7 +54,7 @@ func (e Event) Serialize() ([]byte, error) {
|
|||||||
return bytes, nil
|
return bytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Event) GetID() (string, error) {
|
func (e *Event) GetID() (string, error) {
|
||||||
bytes, err := e.Serialize()
|
bytes, err := e.Serialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -73,7 +84,7 @@ func SignEvent(eventID, privateKeyHex string) (string, error) {
|
|||||||
return hex.EncodeToString(sig.Serialize()), nil
|
return hex.EncodeToString(sig.Serialize()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Event) Validate() error {
|
func (e *Event) Validate() error {
|
||||||
if err := e.ValidateStructure(); err != nil {
|
if err := e.ValidateStructure(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -85,35 +96,35 @@ func (e Event) Validate() error {
|
|||||||
return ValidateSignature(e.ID, e.Sig, e.PubKey)
|
return ValidateSignature(e.ID, e.Sig, e.PubKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Event) ValidateStructure() error {
|
func (e *Event) ValidateStructure() error {
|
||||||
if !Hex64Pattern.MatchString(e.PubKey) {
|
if !Hex64Pattern.MatchString(e.PubKey) {
|
||||||
return fmt.Errorf("pubkey must be 64 lowercase hex characters")
|
return ErrMalformedPubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
if !Hex64Pattern.MatchString(e.ID) {
|
if !Hex64Pattern.MatchString(e.ID) {
|
||||||
return fmt.Errorf("id must be 64 hex characters")
|
return ErrMalformedID
|
||||||
}
|
}
|
||||||
|
|
||||||
if !Hex128Pattern.MatchString(e.Sig) {
|
if !Hex128Pattern.MatchString(e.Sig) {
|
||||||
return fmt.Errorf("signature must be 128 hex characters")
|
return ErrMalformedSig
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tag := range e.Tags {
|
for _, tag := range e.Tags {
|
||||||
if len(tag) < 2 {
|
if len(tag) < 2 {
|
||||||
return fmt.Errorf("tags must contain at least two elements")
|
return ErrMalformedTag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Event) ValidateID() error {
|
func (e *Event) ValidateID() error {
|
||||||
computedID, err := e.GetID()
|
computedID, err := e.GetID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to compute event id")
|
return ErrFailedIDComp
|
||||||
}
|
}
|
||||||
if e.ID == "" {
|
if e.ID == "" {
|
||||||
return fmt.Errorf("event id is empty")
|
return ErrNoEventID
|
||||||
}
|
}
|
||||||
if computedID != e.ID {
|
if computedID != e.ID {
|
||||||
return fmt.Errorf("event id %q does not match computed id %q", e.ID, computedID)
|
return fmt.Errorf("event id %q does not match computed id %q", e.ID, computedID)
|
||||||
@@ -150,6 +161,6 @@ func ValidateSignature(eventID, eventSig, publicKeyHex string) error {
|
|||||||
if signature.Verify(idBytes, publicKey) {
|
if signature.Verify(idBytes, publicKey) {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("event signature is invalid")
|
return ErrInvalidSig
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
50
filter.go
50
filter.go
@@ -17,7 +17,7 @@ type Filter struct {
|
|||||||
Extensions map[string]json.RawMessage
|
Extensions map[string]json.RawMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) MarshalJSON() ([]byte, error) {
|
func (f *Filter) MarshalJSON() ([]byte, error) {
|
||||||
outputMap := make(map[string]interface{})
|
outputMap := make(map[string]interface{})
|
||||||
|
|
||||||
// Add standard fields
|
// Add standard fields
|
||||||
@@ -48,16 +48,12 @@ func (f Filter) MarshalJSON() ([]byte, error) {
|
|||||||
// Merge extensions
|
// Merge extensions
|
||||||
for key, raw := range f.Extensions {
|
for key, raw := range f.Extensions {
|
||||||
// Disallow standard keys in extensions
|
// Disallow standard keys in extensions
|
||||||
standardKeys := []string{"ids", "authors", "kinds", "since", "until", "limit"}
|
if key == "ids" ||
|
||||||
found := false
|
key == "authors" ||
|
||||||
for _, stdKey := range standardKeys {
|
key == "kinds" ||
|
||||||
// Skip standard key
|
key == "since" ||
|
||||||
if key == stdKey {
|
key == "until" ||
|
||||||
found = true
|
key == "limit" {
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if found {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +102,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := raw["since"]; ok {
|
if v, ok := raw["since"]; ok {
|
||||||
if string(raw["since"]) == "null" {
|
if len(v) == 4 && string(v) == "null" {
|
||||||
f.Since = nil
|
f.Since = nil
|
||||||
} else {
|
} else {
|
||||||
var val int
|
var val int
|
||||||
@@ -119,7 +115,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := raw["until"]; ok {
|
if v, ok := raw["until"]; ok {
|
||||||
if string(raw["until"]) == "null" {
|
if len(v) == 4 && string(v) == "null" {
|
||||||
f.Until = nil
|
f.Until = nil
|
||||||
} else {
|
} else {
|
||||||
var val int
|
var val int
|
||||||
@@ -132,7 +128,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := raw["limit"]; ok {
|
if v, ok := raw["limit"]; ok {
|
||||||
if string(raw["limit"]) == "null" {
|
if len(v) == 4 && string(v) == "null" {
|
||||||
f.Limit = nil
|
f.Limit = nil
|
||||||
} else {
|
} else {
|
||||||
var val int
|
var val int
|
||||||
@@ -151,7 +147,7 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
|
|||||||
if f.Tags == nil {
|
if f.Tags == nil {
|
||||||
f.Tags = make(map[string][]string)
|
f.Tags = make(map[string][]string)
|
||||||
}
|
}
|
||||||
tagKey := strings.TrimPrefix(key, "#")
|
tagKey := key[1:]
|
||||||
var tagValues []string
|
var tagValues []string
|
||||||
if err := json.Unmarshal(raw[key], &tagValues); err != nil {
|
if err := json.Unmarshal(raw[key], &tagValues); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -169,24 +165,24 @@ func (f *Filter) UnmarshalJSON(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f Filter) Matches(event Event) bool {
|
func (f *Filter) Matches(event *Event) bool {
|
||||||
// Check ID
|
// Check ID
|
||||||
if len(f.IDs) > 0 {
|
if len(f.IDs) > 0 {
|
||||||
if !matchesPrefix(event.ID, f.IDs) {
|
if !matchesPrefix(event.ID, &f.IDs) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Author
|
// Check Author
|
||||||
if len(f.Authors) > 0 {
|
if len(f.Authors) > 0 {
|
||||||
if !matchesPrefix(event.PubKey, f.Authors) {
|
if !matchesPrefix(event.PubKey, &f.Authors) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Kind
|
// Check Kind
|
||||||
if len(f.Kinds) > 0 {
|
if len(f.Kinds) > 0 {
|
||||||
if !matchesKinds(event.Kind, f.Kinds) {
|
if !matchesKinds(event.Kind, &f.Kinds) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,7 +194,7 @@ func (f Filter) Matches(event Event) bool {
|
|||||||
|
|
||||||
// Check Tags
|
// Check Tags
|
||||||
if len(f.Tags) > 0 {
|
if len(f.Tags) > 0 {
|
||||||
if !matchesTags(event.Tags, f.Tags) {
|
if !matchesTags(&event.Tags, &f.Tags) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,8 +202,8 @@ func (f Filter) Matches(event Event) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchesPrefix(candidate string, prefixes []string) bool {
|
func matchesPrefix(candidate string, prefixes *[]string) bool {
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range *prefixes {
|
||||||
if strings.HasPrefix(candidate, prefix) {
|
if strings.HasPrefix(candidate, prefix) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -215,8 +211,8 @@ func matchesPrefix(candidate string, prefixes []string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchesKinds(candidate int, kinds []int) bool {
|
func matchesKinds(candidate int, kinds *[]int) bool {
|
||||||
for _, kind := range kinds {
|
for _, kind := range *kinds {
|
||||||
if candidate == kind {
|
if candidate == kind {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -234,14 +230,14 @@ func matchesTimeRange(timestamp int, since *int, until *int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchesTags(eventTags [][]string, filterTags map[string][]string) bool {
|
func matchesTags(eventTags *[][]string, filterTags *map[string][]string) bool {
|
||||||
for tagName, filterValues := range filterTags {
|
for tagName, filterValues := range *filterTags {
|
||||||
if len(filterValues) == 0 {
|
if len(filterValues) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, eventTag := range eventTags {
|
for _, eventTag := range *eventTags {
|
||||||
if len(eventTag) < 2 {
|
if len(eventTag) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -388,7 +388,7 @@ func TestEventFilterMatching(t *testing.T) {
|
|||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
matchedIDs := []string{}
|
matchedIDs := []string{}
|
||||||
for _, event := range testEvents {
|
for _, event := range testEvents {
|
||||||
if tc.filter.Matches(event) {
|
if tc.filter.Matches(&event) {
|
||||||
matchedIDs = append(matchedIDs, event.ID[:8])
|
matchedIDs = append(matchedIDs, event.ID[:8])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user