|
| 1 | +import { TaskEither } from 'fp-ts/TaskEither'; |
| 2 | +import * as TE from 'fp-ts/TaskEither'; |
| 3 | +import { JsonProof } from 'o1js'; |
| 4 | +import { Option } from 'fp-ts/Option'; |
| 5 | +import * as IOE from 'fp-ts/IOEither'; |
| 6 | +import { pipe } from 'fp-ts/lib/function'; |
| 7 | +import { createHash } from 'crypto'; |
| 8 | +import * as O from 'fp-ts/Option'; |
| 9 | +import * as R from 'fp-ts/Record'; |
| 10 | +import { IORef, newIORef } from 'fp-ts/lib/IORef'; |
| 11 | +import * as IO from 'fp-ts/IO'; |
| 12 | +import { fromFailablePromise } from '@utils/fp/TaskEither'; |
| 13 | +import * as E from 'fp-ts/Either'; |
| 14 | + |
| 15 | +export interface CachedProof { |
| 16 | + publicInputArgs: unknown; |
| 17 | + proof: JsonProof; |
| 18 | +} |
| 19 | + |
| 20 | +export type ProofKey = string; |
| 21 | + |
| 22 | +export type FpCheckCachedProofs = ( |
| 23 | + check: (p: CachedProof) => TaskEither<string, boolean> |
| 24 | +) => TaskEither<string, void>; |
| 25 | + |
| 26 | +export type TsCheckCachedProofs = ( |
| 27 | + check: (p: CachedProof) => Promise<boolean> |
| 28 | +) => Promise<void>; |
| 29 | + |
| 30 | +export interface IProofCache { |
| 31 | + storeProof(p: CachedProof): TaskEither<string, ProofKey>; |
| 32 | + getProof(k: ProofKey): TaskEither<string, Option<CachedProof>>; |
| 33 | + invalidateProof(k: ProofKey): TaskEither<string, void>; |
| 34 | + |
| 35 | + checkEachProof: FpCheckCachedProofs; |
| 36 | +} |
| 37 | + |
| 38 | +export const toTsCheckCachedProof: ( |
| 39 | + _: FpCheckCachedProofs |
| 40 | +) => TsCheckCachedProofs = |
| 41 | + (f: FpCheckCachedProofs) => |
| 42 | + (check: (p: CachedProof) => Promise<boolean>): Promise<void> => |
| 43 | + f((p: CachedProof) => fromFailablePromise(() => check(p)))().then( |
| 44 | + E.match( |
| 45 | + (err) => Promise.reject(err), |
| 46 | + () => Promise.resolve() |
| 47 | + ) |
| 48 | + ); |
| 49 | + |
| 50 | +export interface IProofCacheProvider { |
| 51 | + getCacheOf(plugin: string): TaskEither<string, IProofCache>; |
| 52 | + |
| 53 | + initCacheFor(plugin: string): TaskEither<string, void>; |
| 54 | +} |
| 55 | + |
| 56 | +class InMemoryProofCache implements IProofCache { |
| 57 | + private cache: IORef<Record<ProofKey, CachedProof>>; |
| 58 | + |
| 59 | + constructor(cache: IORef<Record<ProofKey, CachedProof>>) { |
| 60 | + this.cache = cache; |
| 61 | + } |
| 62 | + |
| 63 | + storeProof(proof: CachedProof): TaskEither<string, ProofKey> { |
| 64 | + return TE.fromIOEither( |
| 65 | + pipe( |
| 66 | + IOE.tryCatch( |
| 67 | + () => |
| 68 | + createHash('sha256').update(JSON.stringify(proof)).digest('hex'), |
| 69 | + (reason) => `unable to hash the proof: ${reason}` |
| 70 | + ), |
| 71 | + IOE.tapIO((hash) => () => this.cache.modify(R.upsertAt(hash, proof))) |
| 72 | + ) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + getProof(k: ProofKey): TaskEither<string, Option<CachedProof>> { |
| 77 | + return TE.fromIO(pipe(this.cache.read, IO.map(R.lookup(k)))); |
| 78 | + } |
| 79 | + |
| 80 | + invalidateProof(k: ProofKey): TaskEither<string, void> { |
| 81 | + return TE.fromIO(this.cache.modify(R.deleteAt(k))); |
| 82 | + } |
| 83 | + |
| 84 | + checkEachProof( |
| 85 | + f: (p: CachedProof) => TaskEither<string, boolean> |
| 86 | + ): TaskEither<string, void> { |
| 87 | + return pipe( |
| 88 | + TE.fromIO(this.cache.read), |
| 89 | + R.traverse(TE.ApplicativePar)((proof: CachedProof) => |
| 90 | + pipe( |
| 91 | + f(proof), |
| 92 | + TE.map((keep) => (keep ? O.some(proof) : O.none)) |
| 93 | + ) |
| 94 | + ), |
| 95 | + TE.map(R.compact), |
| 96 | + TE.chain((r) => TE.fromIO(this.cache.write(r))) |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export class InMemoryProofCacheProvider implements IProofCacheProvider { |
| 102 | + private store: Record<string, Record<ProofKey, CachedProof>> = {}; |
| 103 | + |
| 104 | + getCacheOf(plugin: string): TaskEither<string, IProofCache> { |
| 105 | + return pipe( |
| 106 | + TE.fromOption(() => `cache for plugin ${plugin} not initialized`)( |
| 107 | + R.lookup(plugin)(this.store) |
| 108 | + ), |
| 109 | + TE.chain((r) => TE.fromIO(newIORef(r))), |
| 110 | + TE.chain((r) => TE.right(new InMemoryProofCache(r))) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + initCacheFor(plugin: string): TaskEither<string, void> { |
| 115 | + return pipe(R.has(plugin, this.store), (hasCache) => |
| 116 | + TE.fromIO(() => { |
| 117 | + if (!hasCache) this.store = R.upsertAt(plugin, {})(this.store); |
| 118 | + }) |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments