Skip to content

Commit

Permalink
change eventid to use an array
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Dec 3, 2024
1 parent b16f767 commit 0048ac8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bech32encoding/nip19.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func Decode(bech32string by) (prefix by, value any, err er) {
if len(v) < 32 {
return prefix, nil, errorf.E("id is less than 32 bytes (%d)", len(v))
}
result.ID, err = eventid.NewFromBytes(v)
result.ID = eventid.NewWith(v)
case TLVRelay:
result.Relays = append(result.Relays, v)
case TLVAuthor:
Expand Down
6 changes: 2 additions & 4 deletions bech32encoding/nip19_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,9 @@ func TestEncodeDecodeNEventTestEncodeDecodeNEvent(t *testing.T) {
func MustDecode[V st | by](s V) (ei *eventid.T) {
var err er
var b by
if b, err = hex.Dec(string(s)); chk.E(err) {
panic(err)
}
if ei, err = eventid.NewFromBytes(b); chk.E(err) {
if b, err = hex.Dec(st(s)); chk.E(err) {
panic(err)
}
ei = eventid.NewWith(b)
return
}
11 changes: 9 additions & 2 deletions envelopes/okenvelope/okenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"realy.lol/codec"
"realy.lol/envelopes"
"realy.lol/eventid"
"realy.lol/sha256"
"realy.lol/text"
)

Expand All @@ -27,6 +28,10 @@ func NewFrom[V st | by](eid V, ok bo, msg ...by) *T {
if len(msg) > 0 {
m = msg[0]
}
if len(eid) != sha256.Size {
log.W.F("event ID unexpected length, expect %d got %d",
len(eid), sha256.Size)
}
return &T{EventID: eventid.NewWith(eid), OK: ok, Reason: m}
}
func (en *T) Label() string { return L }
Expand Down Expand Up @@ -67,9 +72,11 @@ func (en *T) UnmarshalJSON(b by) (r by, err er) {
if idHex, r, err = text.UnmarshalHex(r); chk.E(err) {
return
}
if en.EventID, err = eventid.NewFromBytes(idHex); chk.E(err) {
return
if len(idHex) != sha256.Size {
err = errorf.E("invalid size for ID, require %d got %d",
len(idHex), sha256.Size)
}
en.EventID = eventid.NewWith(idHex)
if r, err = text.Comma(r); chk.E(err) {
return
}
Expand Down
78 changes: 53 additions & 25 deletions eventid/eventid.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eventid

import (
"bytes"
"errors"

"lukechampine.com/frand"
Expand All @@ -12,20 +11,32 @@ import (

// T is the SHA256 hash in hexadecimal of the canonical form of an event as
// produced by the output of T.ToCanonical().Bytes().
type T struct {
b by
}
type T [sha256.Size]byte

// New creates a new eventid.T. This is actually more wordy than simply creating a &T{} via
// slice literal.
func New() (ei *T) { return &T{} }

func NewWith[V st | by](s V) (ei *T) { return &T{b: by(s)} }
// NewWith creates an eventid.T out of bytes or string but assumes it is binary
// and that it is the right length. The result is either truncated or padded automatically by
// the use of the "copy" operation.
func NewWith[V st | by](s V) (ei *T) {
id := T{}
copy(id[:], s)
return &id
}

// Set the value of an eventid.T with checking of the length before copying it.
func (ei *T) Set(b by) (err er) {
if ei == nil {
err = errorf.E("event id is nil")
return
}
if len(b) != sha256.Size {
err = errorf.E("ID bytes incorrect size, got %d require %d", len(b), sha256.Size)
return
}
ei.b = b
copy(ei[:], b)
return
}

Expand All @@ -38,54 +49,64 @@ func NewFromBytes(b by) (ei *T, err er) {
}

func (ei *T) String() st {
if ei.b == nil {
if ei == nil {
return ""
}
return hex.Enc(ei.b)
return hex.Enc(ei[:])
}

func (ei *T) ByteString(src by) (b by) { return hex.EncAppend(src, ei.b) }
func (ei *T) ByteString(src by) (b by) {
return hex.EncAppend(src, ei[:])
}

func (ei *T) Bytes() (b by) { return ei.b }
func (ei *T) Bytes() (b by) { return ei[:] }

func (ei *T) Len() no {
if ei == nil {
log.W.Ln("nil event id")
return 0
}
if ei.b == nil {
return 0
}
return len(ei.b)
return len(ei)
}

func (ei *T) Equal(ei2 *T) bo { return bytes.Compare(ei.b, ei2.b) == 0 }
func (ei *T) Equal(ei2 *T) (eq bo) {
if ei == nil || ei2 == nil {
log.W.Ln("can't compare to nil event id")
return
}
return *ei == *ei2
}

func (ei *T) MarshalJSON() (b by, err er) {
if ei.b == nil {
err = errors.New("eventid nil")
if ei == nil {
err = errors.New("event id is nil")
return
}
b = make(by, 0, 2*sha256.Size+2)
b = append(b, '"')
hex.EncAppend(b, ei.b)
hex.EncAppend(b, ei[:])
b = append(b, '"')
return
}

func (ei *T) UnmarshalJSON(b by) (err er) {
if len(ei.b) != sha256.Size {
ei.b = make(by, 0, sha256.Size)
if ei == nil {
err = errors.New("event id is nil")
return
}
// trim off the quotes.
b = b[1 : 2*sha256.Size+1]
if len(b) != 2*sha256.Size {
err = errorf.E("event ID hex incorrect size, got %d require %d",
len(b), 2*sha256.Size)
log.E.Ln(string(b))
return
}
ei.b = make(by, 0, sha256.Size)
ei.b, err = hex.DecAppend(ei.b, b)
var bb by
if bb, err = hex.Dec(st(b)); chk.E(err) {
return
}
copy(ei[:], bb)
return
}

Expand All @@ -96,10 +117,17 @@ func NewFromString(s st) (ei *T, err er) {
return nil, errorf.E("event ID hex wrong size, got %d require %d",
len(s), 2*sha256.Size)
}
ei = &T{b: make(by, 0, sha256.Size)}
ei.b, err = hex.DecAppend(ei.b, by(s))
ei = &T{}
b := make(by, 0, sha256.Size)
b, err = hex.DecAppend(b, by(s))
copy(ei[:], b)
return
}

// Gen creates a fake pseudorandom generated event ID for tests.
func Gen() (ei *T) { return &T{frand.Bytes(sha256.Size)} }
func Gen() (ei *T) {
b := frand.Bytes(sha256.Size)
ei = &T{}
copy(ei[:], b)
return
}
2 changes: 1 addition & 1 deletion ratel/deleteevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *T) DeleteEvent(c cx, eid *eventid.T) (err er) {
// get the serial
k = it.Item().Key()
// copy serial out
keys.Read(k, index.Empty(), id.New(eventid.New()), seri)
keys.Read(k, index.Empty(), id.New(&eventid.T{}), seri)
// save into foundSerial
foundSerial = seri.Val
}
Expand Down
2 changes: 1 addition & 1 deletion ratel/saveevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *T) SaveEvent(c cx, ev *event.T) (err er) {
// get the serial
k = it.Item().Key()
// copy serial out
keys.Read(k, index.Empty(), id.New(eventid.New()), seri)
keys.Read(k, index.Empty(), id.New(&eventid.T{}), seri)
// save into foundSerial
foundSerial = seri.Val
}
Expand Down
15 changes: 3 additions & 12 deletions ws/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"realy.lol/envelopes/eventenvelope"
"realy.lol/envelopes/okenvelope"
"realy.lol/event"
"realy.lol/eventid"
"realy.lol/kind"
"realy.lol/normalize"
"realy.lol/p256k"
Expand Down Expand Up @@ -69,12 +68,8 @@ func TestPublish(t *testing.T) {
t.Errorf("received event:\n%s\nwant:\n%s", env.T.Serialize(), textNote.Serialize())
}
// send back an ok nip-20 command result
var eid *eventid.T
if eid, err = eventid.NewFromBytes(textNote.ID); chk.E(err) {
t.Fatal(err)
}
var res by
if res, err = okenvelope.NewFrom(eid.Bytes(), true, nil).MarshalJSON(res); chk.E(err) {
if res, err = okenvelope.NewFrom(textNote.ID, true, nil).MarshalJSON(res); chk.E(err) {
t.Fatal(err)
}
if err := websocket.Message.Send(conn, res); chk.T(err) {
Expand Down Expand Up @@ -117,20 +112,16 @@ func TestPublishBlocked(t *testing.T) {
t.Errorf("websocket.JSON.Receive: %v", err)
}
// send back a not ok nip-20 command result
var eid *eventid.T
if eid, err = eventid.NewFromBytes(textNote.ID); chk.E(err) {
t.Fatal(err)
}
var res by
if res, err = okenvelope.NewFrom(eid.Bytes(), false,
if res, err = okenvelope.NewFrom(textNote.ID, false,
normalize.Msg(normalize.Blocked, "no reason")).MarshalJSON(res); chk.E(err) {
t.Fatal(err)
}
if err := websocket.Message.Send(conn, res); chk.T(err) {
t.Errorf("websocket.JSON.Send: %v", err)
}
// res := []any{"OK", textNote.ID, false, "blocked"}
websocket.JSON.Send(conn, res)
chk.E(websocket.JSON.Send(conn, res))
})
defer ws.Close()

Expand Down

0 comments on commit 0048ac8

Please sign in to comment.