Make nonce values strings, and fix problem reading empty tags [""].

This commit is contained in:
Robert C. Martin 2022-10-28 10:15:12 -05:00
parent e2ef67fcaf
commit 7a0ec902fd
2 changed files with 14 additions and 13 deletions

View File

@ -17,7 +17,11 @@
(it "handles tags with many arguments"
(should= [:e 1 2 3 4] (process-tag ["e" 1 2 3 4]))
(should= [:e 1 2] (process-tag ["e" 1 2]))
))
)
(it "rejects tags without a valid tag name"
(should-be-nil (process-tag ["" "" ""]))
(should-be-nil (process-tag [nil])))
)
(describe "translate-event"
(it "translates from strings to clojure values"
@ -598,44 +602,41 @@
(with body {:pubkey 1 :created_at 1 :kind 1 :tags [] :content "hi"})
(it "makes id with no POW"
(let [[_id new-body] (make-id-with-pow 0 @body)]
(should= [[:nonce 0 0]] (:tags new-body))))
(should= [[:nonce "0" "0"]] (:tags new-body))))
(it "makes id with small POW"
(let [[id new-body] (make-id-with-pow 4 @body)
high-bits (subs (bytes->hex-string id) 0 1)
[nonce-tag nonce pow-promise] (first (:tags new-body))]
[nonce-tag _nonce pow-promise] (first (:tags new-body))]
(should= "0" high-bits)
(should= 1 (:pubkey new-body))
(should= 1 (:created_at new-body))
(should= 1 (:kind new-body))
(should= :nonce nonce-tag)
(should (number? nonce))
(should= 4 pow-promise)
(should= "4" pow-promise)
(should= "hi" (:content new-body))))
(it "makes id with larger POW"
(let [[id new-body] (make-id-with-pow 8 @body)
high-bits (subs (bytes->hex-string id) 0 2)
[nonce-tag nonce pow-promise] (first (:tags new-body))]
[nonce-tag _nonce pow-promise] (first (:tags new-body))]
(should= "00" high-bits)
(should= 1 (:pubkey new-body))
(should= 1 (:created_at new-body))
(should= 1 (:kind new-body))
(should= :nonce nonce-tag)
(should (number? nonce))
(should= 8 pow-promise)
(should= "8" pow-promise)
(should= "hi" (:content new-body))))
(it "makes id with even larger POW"
(let [[id new-body] (make-id-with-pow 16 @body)
high-bits (subs (bytes->hex-string id) 0 4)
[nonce-tag nonce pow-promise] (first (:tags new-body))]
[nonce-tag _nonce pow-promise] (first (:tags new-body))]
(should= "0000" high-bits)
(should= 1 (:pubkey new-body))
(should= 1 (:created_at new-body))
(should= 1 (:kind new-body))
(should= :nonce nonce-tag)
(should (number? nonce))
(should= 16 pow-promise)
(should= "16" pow-promise)
(should= "hi" (:content new-body))))
)

View File

@ -241,7 +241,7 @@
event-state))
(defn process-tag [tag]
(when (seq tag)
(when (and (seq tag) (seq (first tag)))
(let [tag-type (first tag)
tag-args (rest tag)
tag-type (.replace tag-type \: \-)]
@ -320,7 +320,7 @@
[pow body]
(let [limit (pow2 (- 256 pow))]
(loop [nonce 0]
(let [body (update-in body [:tags] concat [[:nonce nonce pow]])
(let [body (update-in body [:tags] concat [[:nonce (str nonce) (str pow)]])
id (make-id body)
id-num (bytes->num id)]
(if (< id-num limit)