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