Skip to content

Commit

Permalink
Update wrt modified validate API
Browse files Browse the repository at this point in the history
  • Loading branch information
fare committed Oct 4, 2023
1 parent 472dbdb commit fc39d3b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
9 changes: 4 additions & 5 deletions content-addressing.ss
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,14 @@
.ap: (lambda (v) (dv T v))
.unap: value<-dv}
.validate:
(lambda (dv (ctx '()))
(def c [[validating: dv] . ctx])
(unless (DV? dv) (type-error c "not a DV"))
(lambda (dv)
(unless (DV? dv) (raise-type-error "not a DV" dv))
(match (std/lazy#&lazy-e (DV-value dv))
(['resolved . v]
(validate T v c)
(validate T v)
(match (std/lazy#&lazy-e (DV-digest dv))
(['resolved . d]
(unless (equal? d (digest<- T v .digesting)) (type-error c "digest does not match"))
(unless (equal? d (digest<- T v .digesting)) (raise-type-error "digest does not match" dv))
dv)
(_ dv)))
(_ dv)))
Expand Down
9 changes: 4 additions & 5 deletions merkle-trie.ss
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@
(match (.refocus ($Costep -1 key) (.zipper<- trie))
([sub . up] (cons sub (.call Path .map .digest<- up)))))
.validate-proof:
(lambda (trie-digest sub up ctx)
(def c [[validate-proof: trie-digest sub up] . ctx])
(lambda (trie-digest sub up)
(match (.unwrap sub)
((Leaf v)
(validate Value v c)
(validate Value v)
(let (digest (car ((.@ Digested Path .up) (.call Digested .leaf v) up)))
(unless (equal? trie-digest digest)
(let (D (Digesting-Digest .digesting))
(type-error c "Digest doesn't match: " D trie-digest D digest up)))))
(raise-type-error "Digest doesn't match: " D trie-digest D digest up)))))
;; TODO: support negative proofs
(_ (type-error c "No leaf")))))
(_ (raise-type-error "No leaf" sub up)))))
(def (MerkleTrie Key: (Key Nat) Height: (Height Nat)
Value: (Value Any) Digesting: (.digesting keccak-addressing))
{(:: @ [MerkleTrie.]) Key Height Value .digesting})
10 changes: 5 additions & 5 deletions persist.ss
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
;; they will call the read method, that will indirectly call make-activity with proper arguments.
;; : @ <- Key (Unit <- State TX) State TX
.restore)
restore: (validate (Fun @ <- Key (Fun Unit <- State TX) State TX) .restore [[restore: sexp]])
restore: (validate (Fun @ <- Key (Fun Unit <- State TX) State TX) .restore)

;; Internal table of objects that have already been loaded from database.
;;loaded:: (Table @ <- Key)
Expand Down Expand Up @@ -86,7 +86,7 @@
;; either synchronously commit-transaction if it owns the transaction, or asynchronously call
;; sync-transaction if it doesn't, before it may assume the state being committed.
;; : (Fun @ <- Key State TX)
resume: (validate (Fun @ <- Key State TX) .resume [[resume: sexp]])
resume: (validate (Fun @ <- Key State TX) .resume)
.resume:
(lambda (key state tx)
(def db-key (db-key<- key))
Expand Down Expand Up @@ -118,7 +118,7 @@
;; has to sync-transaction to wait for it being saved.
;; Also, proper mutual exclusion must be used to ensure only one piece of code
;; may attempt create to create an activity with the given key at any point in time.
make: (validate (Fun @ <- Key (Fun State <- (Fun Unit <- State TX) TX) TX) .make [[make: sexp]])
make: (validate (Fun @ <- Key (Fun State <- (Fun Unit <- State TX) TX) TX) .make)
.make:
(lambda (key init tx)
(def db-key (db-key<- key))
Expand All @@ -140,7 +140,7 @@
;; For those kinds of objects where it makes sense, this may create a default activity.
;; Clients of this code must use proper mutual exclusion so there are no concurrent calls to get.
;; Get may indirectly call resume if the object is in the database, and make-default-state if not.
get: (validate (Fun @ <- Key TX) .get [[get: sexp]])
get: (validate (Fun @ <- Key TX) .get)
.get:
(lambda (key tx)
(def db-key (db-key<- key))
Expand All @@ -155,7 +155,7 @@
Key loaded resume-from-db db-key<- sexp)
;; Get the activity by its key.
;; No transaction is provided: the activity will make its own if needed.
<-key: (validate (Fun @ <- Key) .<-key [[<-key: sexp]])
<-key: (validate (Fun @ <- Key) .<-key)
.<-key:
(lambda (key)
(def db-key (db-key<- key))
Expand Down
2 changes: 1 addition & 1 deletion t/merkle-trie-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(match (F .proof<- t k)
([sub . up]
(assert-equal! (F .unwrap sub) (Leaf v))
(F .validate-proof (F .digest<- t) sub up '()))
(F .validate-proof (F .digest<- t) sub up))
(_ (error "foo"))))

(def (merkle-tests T)
Expand Down

0 comments on commit fc39d3b

Please sign in to comment.