From 75e6befdd0f83d4d5be774fa50ed2cb116bdc993 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 21 Aug 2024 16:54:02 -0300 Subject: [PATCH] nip13: move Generate() to deprecated.go --- nip13/deprecated.go | 32 ++++++++++++++++++++++++++++++++ nip13/nip13.go | 25 ------------------------- 2 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 nip13/deprecated.go diff --git a/nip13/deprecated.go b/nip13/deprecated.go new file mode 100644 index 0000000..c83b5b1 --- /dev/null +++ b/nip13/deprecated.go @@ -0,0 +1,32 @@ +package nip13 + +import ( + "strconv" + "time" + + "github.com/nbd-wtf/go-nostr" +) + +// Deprecated: use DoWork() +func Generate(event *nostr.Event, targetDifficulty int, timeout time.Duration) (*nostr.Event, error) { + if event.PubKey == "" { + return nil, ErrMissingPubKey + } + + tag := nostr.Tag{"nonce", "", strconv.Itoa(targetDifficulty)} + event.Tags = append(event.Tags, tag) + var nonce uint64 + start := time.Now() + for { + nonce++ + tag[1] = strconv.FormatUint(nonce, 10) + if Difficulty(event.GetID()) >= targetDifficulty { + return event, nil + } + // benchmarks show one iteration is approx 3000ns on i7-8565U @ 1.8GHz. + // so, check every 30ms; arbitrary + if nonce%10000 == 0 && time.Since(start) > timeout { + return nil, ErrGenerateTimeout + } + } +} diff --git a/nip13/nip13.go b/nip13/nip13.go index d324efe..ded506d 100644 --- a/nip13/nip13.go +++ b/nip13/nip13.go @@ -7,7 +7,6 @@ import ( "math/bits" "runtime" "strconv" - "time" nostr "github.com/nbd-wtf/go-nostr" ) @@ -65,30 +64,6 @@ func Check(id string, minDifficulty int) error { return nil } -// Deprecated: use DoWork() -func Generate(event *nostr.Event, targetDifficulty int, timeout time.Duration) (*nostr.Event, error) { - if event.PubKey == "" { - return nil, ErrMissingPubKey - } - - tag := nostr.Tag{"nonce", "", strconv.Itoa(targetDifficulty)} - event.Tags = append(event.Tags, tag) - var nonce uint64 - start := time.Now() - for { - nonce++ - tag[1] = strconv.FormatUint(nonce, 10) - if Difficulty(event.GetID()) >= targetDifficulty { - return event, nil - } - // benchmarks show one iteration is approx 3000ns on i7-8565U @ 1.8GHz. - // so, check every 30ms; arbitrary - if nonce%10000 == 0 && time.Since(start) > timeout { - return nil, ErrGenerateTimeout - } - } -} - // DoWork() performs work in multiple threads (given by runtime.NumCPU()) and returns the first // nonce (as a nostr.Tag) that yields the required work. // Returns an error if the context expires before that.