-
Notifications
You must be signed in to change notification settings - Fork 7
/
cell.clj
890 lines (505 loc) · 15.5 KB
/
cell.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
(ns convex.cell
"Constructors for CVM cells."
{:author "Adam Helinski"}
(:import (clojure.lang IDeref)
(convex.core ErrorCodes)
(convex.core.data AccountKey
Address
ABlob
ABlobMap
ACell
AList
AMap
ASet
AString
AVector
Blob
BlobMaps
Blobs
Format
Hash
Keyword
Keywords
Lists
MapEntry
Maps
Sets
Strings
Symbol
Syntax
Vectors)
(convex.core.data.prim CVMBigInteger
CVMBool
CVMChar
CVMDouble
CVMLong)
(convex.core.lang RT)
(convex.core.transactions Call
Invoke
Transfer)
(java.util Collection
List))
(:refer-clojure :exclude [*
bigint
boolean
byte
char
double
hash
key
keyword
list
long
map
set
symbol
vector])
(:require [clojure.core]
[convex.write :as $.write]))
(set! *warn-on-reflection*
true)
(declare keyword
map
vector)
;;;;;;;;;; Printing cells
(defmethod print-method ACell
[cell ^java.io.Writer w]
(.write w
(str "#cvx "
($.write/string cell))))
(defmethod print-method Syntax
[^Syntax syntax ^java.io.Writer w]
(.write w
(str "#cvx ^"
($.write/string (.getMeta syntax))
" "
($.write/string (.getValue syntax)))))
;;;
(prefer-method print-method
ACell
IDeref)
(prefer-method print-method
ACell
java.util.List)
(prefer-method print-method
ACell
java.util.Map)
(prefer-method print-method
ACell
java.util.Set)
;;;;;;;;;; Creating values
(defn address
"Creates an address from a long."
^Address
[long]
(Address/create (clojure.core/long long)))
(defn bigint
"Creates a big integer cell from the given Clojure or Java a big integer."
[n]
(CVMBigInteger/wrap (biginteger n)))
(defn blob
"Creates a blob from a byte array."
^Blob
[^bytes byte-array]
(Blob/create byte-array))
(defn blob<-hex
"Creates a blob from a hex string."
^Blob
[^String hex-string]
(Blobs/fromHex hex-string))
(defn blob-map
"Creates a blob map from a collection of `[blob value]`."
(^ABlobMap []
(BlobMaps/empty))
(^ABlobMap [kvs]
(reduce (fn [^ABlobMap bm [^ACell k ^ACell v]]
(let [bm-2 (.assoc bm
k
v)]
(or bm-2
(throw (IllegalArgumentException. "Key must be a blob")))))
(blob-map)
kvs)))
(defn boolean
"Creates a boolean cell given a falsy or truthy value."
^CVMBool
[x]
(CVMBool/create (clojure.core/boolean x)))
(defn call
"Creates a transaction for invoking a callable function."
(^Call [address sequence address-callable function-name arg+]
(Call/create address
sequence
address-callable
function-name
arg+))
(^Call [address sequence address-callable offer function-name arg+]
(Call/create address
sequence
address-callable
offer
function-name
arg+)))
(defmacro code-std*
"Given a Clojure keyword, returns the corresponding standard error code.
Those are errors codes used by the CVM itself:
- `:ARGUMENT`
- `:ARITY`
- `:ASSERT`
- `:BOUNDS`
- `:CAST`
- `:COMPILE`
- `:DEPTH`
- `:EXCEPTION`
- `:EXPAND`
- `:FATAL`
- `:FUNDS`
- `:HALT`
- `:JUICE`
- `:MEMORY`
- `:NOBODY`
- `:RECUR`
- `:REDUCED`
- `:RETURN`
- `:ROLLBACK`
- `:SEQUENCE`
- `:SIGNATURE`
- `:STATE`
- `:TAILCALL`
- `:TODO`
- `:TRUST`
- `:UNDECLARED`
- `:UNEXPECTED`
Throws if keyword does not match any of those.
Note that in user functions, codes can be anything, any type, using those codes is not at all mandatory."
[kw]
(case kw
:ARGUMENT 'convex.core.ErrorCodes/ARGUMENT
:ARITY 'convex.core.ErrorCodes/ARITY
:ASSERT 'convex.core.ErrorCodes/ASSERT
:BOUNDS 'convex.core.ErrorCodes/BOUNDS
:CAST 'convex.core.ErrorCodes/CAST
:COMPILE 'convex.core.ErrorCodes/COMPILE
:DEPTH 'convex.core.ErrorCodes/DEPTH
:EXCEPTION 'convex.core.ErrorCodes/EXCEPTION
:EXPAND 'convex.core.ErrorCodes/EXPAND
:FATAL 'convex.core.ErrorCodes/FATAL
:FUNDS 'convex.core.ErrorCodes/FUNDS
:HALT 'convex.core.ErrorCodes/HALT
:JUICE 'convex.core.ErrorCodes/JUICE
:MEMORY 'convex.core.ErrorCodes/MEMORY
:NOBODY 'convex.core.ErrorCodes/NOBODY
:RECUR 'convex.core.ErrorCodes/RECUR
:REDUCED 'convex.core.ErrorCodes/REDUCED
:RETURN 'convex.core.ErrorCodes/RETURN
:ROLLBACK 'convex.core.ErrorCodes/ROLLBACK
:SEQUENCE 'convex.core.ErrorCodes/SEQUENCE
:SIGNATURE 'convex.core.ErrorCodes/SIGNATURE
:STATE 'convex.core.ErrorCodes/STATE
:TAILCALL 'convex.core.ErrorCodes/TAILCALL
:TODO 'convex.core.ErrorCodes/TODO
:TRUST 'convex.core.ErrorCodes/TRUST
:UNDECLARED 'convex.core.ErrorCodes/UNDECLARED
:UNEXPECTED 'convex.core.ErrorCodes/UNEXPECTED
(throw (ex-info (str "There is no official exception code for: "
kw)
{:convex.cell/code kw}))))
(defn char
"Creates a character cell from a regular character."
^CVMChar
[ch]
(CVMChar/create (clojure.core/long ch)))
(defn double
"Creates a double cell."
^CVMDouble
[x]
(CVMDouble/create x))
(defn encoding
"Returns a blob representing the encoding of the given `cell`.
This encoding is meant for incremental updates."
^Blob
[cell]
(Format/encodedBlob cell))
(defn hash
"Returns the hash of the given `cell`.
A hash is a specialized 32-byte [[blob]]."
^Hash
[^ACell cell]
(Hash/compute cell))
(defn hash<-blob
"Converts a 32-byte blob to a hash.
See [[hash]]."
^Hash
[^ABlob blob]
(RT/ensureHash blob))
(defn hash<-hex
"Creates a hash from a hex string.
See [[hash]].
Returns nil if hex string is of wrong format."
^Hash
[^String hex-string]
(Hash/fromHex hex-string))
(defn invoke
"Creates a transaction for invoking code (a cell)."
^Invoke
[^Address address ^Long sequence-id ^ACell cell]
(Invoke/create address
sequence-id
cell))
(defn key
"Creates an account key from a 32-byte blob.
Returns nil if the given [[blob]] is of wrong size."
^AccountKey
[^ABlob blob]
(AccountKey/create blob))
(def key-fake
"Zeroed account key for testing purposes.
See [[key]]."
(key (blob (byte-array 32))))
(defn keyword
"Creates a keyword cell from a string."
^Keyword
[^String string]
(Keyword/create string))
(let [kw-message (keyword "message")]
(defn error
"An error value as Convex data.
`code` is often a keyword cell (`:ASSERT` by default), `message` could be any cell (albeit often a human-readable
string), and `trace` is an optional stacktrace (vector cell of string cells)."
(^AMap [message]
(map [[Keywords/CODE ErrorCodes/ASSERT]
[kw-message message]]))
(^AMap [code message]
(map [[Keywords/CODE code]
[kw-message message]]))
(^AMap [code message trace]
(map [[Keywords/CODE code]
[kw-message message]
[Keywords/TRACE trace]]))))
(defn list
"Creates a list cell from a collection of cells."
(^AList []
(list []))
(^AList [x]
(Lists/create x)))
(defn long
"Creates a long cell."
^CVMLong
[^long x]
(CVMLong/create x))
(defn map
"Creates a map cell from a collection of `[key value]`."
(^AMap []
(map []))
(^AMap [kvs]
(Maps/create ^List (clojure.core/map (fn [[k v]]
(MapEntry/create k
v))
kvs))))
(defn set
"Creates a set cell from a collection of items cell."
(^ASet []
(set []))
(^ASet [^Collection x]
(Sets/fromCollection x)))
(defn string
"Creates a string cell from a regular string."
^AString
[^String string]
(Strings/create string))
(defn symbol
"Creates a symbol cell from a string."
^Symbol
[^String string]
(Symbol/create string))
(defn syntax
"Creates a syntax cell.
It wraps the given `cell` and allow attaching a metadata [[map]]."
(^Syntax [^ACell cell]
(Syntax/create cell))
(^Syntax [^ACell cell ^AMap metadata]
(Syntax/create cell
metadata)))
(defn transfer
"Creates a transaction for transferring Convex Coins."
^Transfer
[address sequence address-receiver amount]
(Transfer/create address
sequence
address-receiver
amount))
(defn vector
"Creates a vector cell from a collection of items cell."
(^AVector []
(vector []))
(^AVector [^Collection x]
(Vectors/create x)))
;;;;;;;;;; Miscellaneous
(defn quoted
"Wraps `x` in `quote`."
[x]
(list [(symbol "quote")
x]))
;;;;;;;;;; Generic conversion Clojure -> Convex
(defprotocol ^:no-doc IEquivalent
"Translates Clojure types to equivalent Convex types. Other objects remain as they are.
However, the [[*]] macro is usually preferred for performance.
```clojure
(any {:a ['b]})
```"
(any [data]))
(extend-protocol IEquivalent
nil
(any [_]
nil)
Object
(any [x]
x)
clojure.lang.ASeq
(any [s]
(list (clojure.core/map any
s)))
clojure.lang.BigInt
(any [n]
(bigint n))
clojure.lang.Keyword
(any [k]
(keyword (name k)))
clojure.lang.IPersistentList
(any [l]
(list (clojure.core/map any
l)))
clojure.lang.IPersistentMap
(any [m]
(map (clojure.core/map (fn [[k v]]
[(any k)
(any v)])
m)))
clojure.lang.IPersistentSet
(any [s]
(set (clojure.core/map any
s)))
clojure.lang.IPersistentVector
(any [v]
(vector (clojure.core/map any
v)))
clojure.lang.Symbol
(any [s]
(symbol (name s)))
java.lang.Boolean
(any [b]
(boolean b))
java.lang.Character
(any [c]
(char c))
java.lang.Double
(any [d]
(double d))
java.lang.Long
(any [i]
(long i))
java.lang.String
(any [s]
(string s))
java.math.BigInteger
(any [n]
(bigint n)))
(declare ^:no-doc -*)
(defn ^:no-doc -splice
;; Offers unquote splicing support for [[-*]].
[x+]
(list* 'clojure.core/concat
(clojure.core/map (fn [x]
(if (and (seq? x)
(clojure.core/= (first x)
'clojure.core/unquote-splicing))
(second x)
[(-* x)]))
x+)))
(defn- ^:no-doc -*
;; Helper for [[*]].
[form]
(cond
(clojure.core/seq? form) (condp clojure.core/=
(first form)
'clojure.core/unquote (second form)
'clojure.core/unquote-splicing (throw (ex-info "Can only splice inside of a collection"
{::form form}))
`(list ~(-splice form)))
(clojure.core/map? form) `(map ~(clojure.core/mapv (fn [[k v]]
[(-* k)
(-* v)])
form))
(clojure.core/set? form) `(set ~(-splice form))
(clojure.core/vector? form) `(vector ~(-splice form))
(clojure.core/symbol? form) (let [nmspace (namespace form)
nm (name form)]
(if nmspace
`(list [(symbol "lookup")
(symbol ~nmspace)
(symbol ~nm)])
`(symbol ~nm)))
:else `(any ~form)))
(defmacro *
"Macro for translating Clojure types to Convex types.
Convex types can be inserted using `~`, especially useful for inserting values dynamically or inserting types
that have no equivalent in Clojure (eg. `address`).
Also understands `~@` (aka unquote splicing).
```clojure
;; Cell for `(transfer #42 500000)`
;;
(* (transfer ~(address 42)
500000))
```"
[x]
(-* x))
;;;;;;;;;; Fake cells
(def ^:private ^Keyword -deref-me
;; See [[fake]].
(keyword "DEREF-ME"))
(defn fake
"Returns a proxy class which looks like a cell, wrapping `x` which can be
any JVM value.
For expert users only!
Primarily useful for allowing actual cells to store any arbitrary JVM values.
`deref` will return `x`.
Prints as the keyword cell `DEREF-ME` but is not an actual symbol cell.
Similarly, writing this fake cell to Etch writes the symbol cell `DEREF-ME`.
Obviously, reading from Etch will return that actual symbol cell since only
real cells can be serialized and deserialized (see [[convex.db]])."
^ACell
[x]
(proxy
[ACell
IDeref]
[]
;; IDeref
(deref []
x)
;; AObject
(print [blob-builder limit]
(.print -deref-me
blob-builder
limit))
;; ACell
(encode [byte+ pos]
(.encode -deref-me
byte+
pos))
(estimatedEncodingSize []
(.estimatedEncodingSize -deref-me))
(getRefCount []
(.getRefCount -deref-me))
(getTag []
(.getTag -deref-me))
(isCanonical []
true)
(toCanonical []
this)))
(defn fake?
"Returns true if `cell` has been produced by [[fake]]."
[x]
(and (instance? ACell
x)
(instance? IDeref
x)))