-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathhvm.rs
4848 lines (4480 loc) · 163 KB
/
hvm.rs
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Welcome to Kindelia's High-order Virtual Machine!
// =================================================
//
// This file is a modification of the project hosted on github.com/kindelia/hvm, and it makes a
// series of changes with the goal of serving the requirements of a peer-to-peer computer.
//
// Kindelia-HVM's memory model
// ---------------------------
//
// The runtime memory consists of just a vector of u128 pointers. That is:
//
// Mem ::= Vec<Ptr>
//
// A pointer has 3 parts:
//
// Ptr ::= TT AAAAAAAAAAAAAAAAAA BBBBBBBBBBBB
//
// Where:
//
// T : u8 is the pointer tag
// A : u72 is the 1st value
// B : u48 is the 2nd value
//
// There are 12 possible tags:
//
// Tag | Val | Meaning
// ----| --- | -------------------------------
// DP0 | 0 | a variable, bound to the 1st argument of a duplication
// DP1 | 1 | a variable, bound to the 2nd argument of a duplication
// VAR | 2 | a variable, bound to the one argument of a lambda
// ARG | 3 | an used argument of a lambda or duplication
// ERA | 4 | an erased argument of a lambda or duplication
// LAM | 5 | a lambda
// APP | 6 | an application
// SUP | 7 | a superposition
// CTR | 8 | a constructor
// FUN | 9 | a function
// OP2 | 10 | a numeric operation
// NUM | 11 | a 120-bit number
//
// The semantics of the 1st and 2nd values depend on the pointer tag.
//
// Tag | 1st ptr value | 2nd ptr value
// --- | ---------------------------- | ---------------------------------
// DP0 | the duplication label | points to the duplication node
// DP1 | the duplication label | points to the duplication node
// VAR | not used | points to the lambda node
// ARG | not used | points to the variable occurrence
// ERA | not used | not used
// LAM | not used | points to the lambda node
// APP | not used | points to the application node
// SUP | the duplication label | points to the superposition node
// CTR | the constructor name | points to the constructor node
// FUN | the function name | points to the function node
// OP2 | the operation name | points to the operation node
// NUM | the most significant 72 bits | the least significant 48 bits
//
// Notes:
//
// 1. The duplication label is an internal value used on the DUP-SUP rule.
// 2. The operation name only uses 4 of the 72 bits, as there are only 16 ops.
// 3. NUM pointers don't point anywhere, they just store the number directly.
//
// A node is a tuple of N pointers stored on sequential memory indices.
// The meaning of each index depends on the node. There are 7 types:
//
// Duplication Node:
// - [0] => either an ERA or an ARG pointing to the 1st variable location
// - [1] => either an ERA or an ARG pointing to the 2nd variable location.
// - [2] => pointer to the duplicated expression
//
// Lambda Node:
// - [0] => either and ERA or an ERA pointing to the variable location
// - [1] => pointer to the lambda's body
//
// Application Node:
// - [0] => pointer to the lambda
// - [1] => pointer to the argument
//
// Superposition Node:
// - [0] => pointer to the 1st superposed value
// - [1] => pointer to the 2sd superposed value
//
// Constructor Node:
// - [0] => pointer to the 1st field
// - [1] => pointer to the 2nd field
// - ... => ...
// - [N] => pointer to the Nth field
//
// Function Node:
// - [0] => pointer to the 1st argument
// - [1] => pointer to the 2nd argument
// - ... => ...
// - [N] => pointer to the Nth argument
//
// Operation Node:
// - [0] => pointer to the 1st operand
// - [1] => pointer to the 2nd operand
//
// Notes:
//
// 1. Duplication nodes DON'T have a body. They "float" on the global scope.
// 2. Lambdas and Duplications point to their variables, and vice-versa.
// 3. ARG pointers can only show up inside Lambdas and Duplications.
// 4. Nums and vars don't require a node type, because they're unboxed.
// 5. Function and Constructor arities depends on the user-provided definition.
//
// Example 0:
//
// Term:
//
// {T2 #7 #8}
//
// Memory:
//
// Root : Ptr(CTR, 0x0000000007b9d30a43, 0x000000000000)
// 0x00 | Ptr(NUM, 0x000000000000000000, 0x000000000007) // the tuple's 1st field
// 0x01 | Ptr(NUM, 0x000000000000000000, 0x000000000008) // the tuple's 2nd field
//
// Notes:
//
// 1. This is just a pair with two numbers.
// 2. The root pointer is not stored on memory.
// 3. The '0x0000000007b9d30a43' constant encodes the 'T2' name.
// 4. Since nums are unboxed, a 2-tuple uses 2 memory slots, or 32 bytes.
//
// Example 1:
//
// Term:
//
// λ~ λb b
//
// Memory:
//
// Root : Ptr(LAM, 0x000000000000000000, 0x000000000000)
// 0x00 | Ptr(ERA, 0x000000000000000000, 0x000000000000) // 1st lambda's argument
// 0x01 | Ptr(LAM, 0x000000000000000000, 0x000000000002) // 1st lambda's body
// 0x02 | Ptr(ARG, 0x000000000000000000, 0x000000000003) // 2nd lambda's argument
// 0x03 | Ptr(VAR, 0x000000000000000000, 0x000000000002) // 2nd lambda's body
//
// Notes:
//
// 1. This is a λ-term that discards the 1st argument and returns the 2nd.
// 2. The 1st lambda's argument not used, thus, an ERA pointer.
// 3. The 2nd lambda's argument points to its variable, and vice-versa.
// 4. Each lambda uses 2 memory slots. This term uses 64 bytes in total.
//
// Example 2:
//
// Term:
//
// λx dup x0 x1 = x; (* x0 x1)
//
// Memory:
//
// Root : Ptr(LAM, 0x000000000000000000, 0x000000000000)
// 0x00 | Ptr(ARG, 0x000000000000000000, 0x000000000004) // the lambda's argument
// 0x01 | Ptr(OP2, 0x000000000000000002, 0x000000000005) // the lambda's body
// 0x02 | Ptr(ARG, 0x000000000000000000, 0x000000000005) // the duplication's 1st argument
// 0x03 | Ptr(ARG, 0x000000000000000000, 0x000000000006) // the duplication's 2nd argument
// 0x04 | Ptr(VAR, 0x000000000000000000, 0x000000000000) // the duplicated expression
// 0x05 | Ptr(DP0, 0x7b93e8d2b9ba31fb21, 0x000000000002) // the operator's 1st operand
// 0x06 | Ptr(DP1, 0x7b93e8d2b9ba31fb21, 0x000000000002) // the operator's 2st operand
//
// Notes:
//
// 1. This is a lambda function that squares a number.
// 2. Notice how every ARGs point to a VAR/DP0/DP1, that points back its source node.
// 3. DP1 does not point to its ARG. It points to the duplication node, which is at 0x02.
// 4. The lambda's body does not point to the dup node, but to the operator. Dup nodes float.
// 5. 0x7b93e8d2b9ba31fb21 is a globally unique random label assigned to the duplication node.
// 6. That duplication label is stored on the DP0/DP1 that point to the node, not on the node.
// 7. A lambda uses 2 memory slots, a duplication uses 3, an operator uses 2. Total: 112 bytes.
// 8. In-memory size is different to, and larger than, serialization size.
//
// How is Kindelia's HVM different from the conventional HVM?
// ----------------------------------------------------------
//
// First, it is a 128-bit, rather than a 64-bit architecture. It can store 120-bit unboxed
// integers, up from 32-bit unboxed uints stored by the conventional HVM. It allows addressing up
// to 2^72 function names, up from 2^30 allowed by the conventional HVM, which isn't enough for
// Kindelia. This change comes with a cost of about ~30% reduced performance, which is acceptable.
//
// Second, it implements a reversible heap machinery, which allows saving periodic snapshots of
// past heap states, and jump back to them. This is necessary because of decentralized consensus.
// If we couldn't revert to past states, we'd have to recompute the entire history anytime there is
// a block reorg, which isn't practical. On Ethereum, this is achieved by storing the state as a
// Map<U256> using Merkle Trees, which, being an immutable structure, allows non-destructive
// insertions and rollbacks. We could do the same, but we decided to further leverage the HVM by
// saving its whole heap as the network state. In other words, applications are allowed to persist
// arbitrary HVM structures on disk by using the io_save operation. For example:
//
// (io_save {Cons #1 {Cons #2 {Cons #3 {Nil}}}} ...)
//
// The operation above would persist the [1,2,3] list as the app's state, with no need for
// serialization. As such, when the app stops running, that list will not be freed from memory.
// Instead, the heap will persist between blocks, so the app just needs to store a pointer to
// the list's head, allowing it to retrieve its state later on. This is only possible because the
// HVM is garbage-collection free, otherwise, leaks would overwhelm the memory.
//
// How are reversible heaps stored?
// --------------------------------
//
// Kindelia's heap is set to grow exactly 8 GB per year. In other words, 10 years after the genesis
// block, the heap size will be of exactly 80 GB. But that doesn't mean a full node will be able
// to operate with even that much ram, because Kindelia must also save snapshots. Right now, it
// stores at most 10 snapshots, trying to keep them distributed with exponentially decreasing ages.
// For example, if we're on block 1000, it might store a snapshot of blocks 998, 996, 992, 984,
// 968, 872, 744 and 488, which is compatible with the fact that longer-term rollbacks are
// increasingly unlikely. If there is a rollback to block 990, we just go back to the earliest
// snapshot, 984, and reprocess blocks 985-1000, which is much faster than recomputing the entire
// history.
//
// In order to keep a good set of snapshots, we must be able to create and discard these heaps.
// Obviously, if this operation required copying the entire heap buffer every block, it would
// completely destroy the network's performance. As such, instead, heaps only actually store
// data that changed. So, using the example above, if a list was allocated and persisted on block 980,
// it will actually be stored on the snapshot 984, which is the earliest snapshot after 980. If the
// runtime, now on block 1000, attempts to read the memory where the list is allocated, it will
// actually receive a signal that it is stored on a past heap, and look for it on 996 and 992,
// until it is found on block 984.
//
// To achieve that, hashmaps are used to store defined functions and persistent state pointers. If
// a key isn't present, Kindelia will look for it on past snapshots. As for the runtime's memory,
// where HVM constructors and lambdas are stored, it doesn't use a hashmap. Instead, it uses a
// Nodes type, which stores data in a big pre-allocated u128 buffer, and keeps track of used memory
// slots in a separate buffer. We then reserve a constant, U128_NONE, to signal that an index isn't
// present, and must be found ina past heap. This is different from 0, which means that this index
// is empty, and can be allocated. This allows for fast write, read, disposal and merging of heaps,
// but comes at the cost of wasting a lot of memory. Because of that, Kindelia's current
// implementation demands up to ~10x more available memory than the current heap size, but that
// could be reduced ten-fold by replacing vectors by a hashmap, or by just saving fewer past heaps.
//
// Other than a 128-bit architecture and reversible heaps, Kindelia's HVM is similar to the
// conventional HVM. This file will be extensively commented, with in-depth explanations of every
// little aspect, from the HVM's memory model to interaction net rewrite rules.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(clippy::style)]
#![allow(clippy::identity_op)]
use std::collections::{hash_map, HashMap, HashSet};
use std::fmt::{self, Write};
use std::hash::{BuildHasherDefault, Hash};
use std::path::PathBuf;
use std::fs::File;
use std::sync::Arc;
use std::time::Instant;
use serde::{Serialize, Deserialize};
use serde_with::{serde_as, DisplayFromStr};
use crate::bits::ProtoSerialize;
use crate::constants;
use crate::crypto;
use crate::util::{self, U128_SIZE, mask};
use crate::util::{LocMap, NameMap, U128Map, U120Map};
use crate::NoHashHasher::NoHashHasher;
use crate::common::{Name, U120};
use crate::persistence::DiskSer;
use crate::parser::{parse_statements, parse_code, ParseErr};
/// This is the HVM's term type. It is used to represent an expression. It is not used in rewrite
/// rules. Instead, it is stored on HVM's heap using its memory model, which will be elaborated
/// later on. Below is a description of each variant:
/// - Var: variable. It stores up to 12 6-bit letters.
/// - Dup: a lazy duplication of any other term. Written as: `dup a b = term; body`
/// - Lam: an affine lambda. Written as: `@var body`.
/// - App: a lambda application. Written as: `(!f x)`.
/// - Ctr: a constructor. Written as: `{Ctr val0 val1 ...}`
/// - Fun: a function call. Written as: `(Fun arg0 arg1 ...)`
/// - Num: an unsigned integer.
/// - Op2: a numeric operation.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Term {
Var { name: Name },
Dup { nam0: Name, nam1: Name, expr: Box<Term>, body: Box<Term> },
Lam { name: Name, body: Box<Term> },
App { func: Box<Term>, argm: Box<Term> },
Ctr { name: Name, args: Vec<Term> },
Fun { name: Name, args: Vec<Term> },
Num { numb: U120 },
Op2 { oper: Oper, val0: Box<Term>, val1: Box<Term> }, // FIXME: refactor `oper` u128 to enum
}
impl Drop for Term {
fn drop(&mut self) {
/// Verify if `term` has recursive childs (any of its
/// `Term`'s properties is not a var or a num)
fn term_is_recursive(term: &Term) -> bool {
fn term_is_num_or_var(term: &Term) -> bool {
match term {
Term::Num { .. } | Term::Var { .. } => true,
_ => false
}
}
match term {
Term::Var { name } => false,
Term::Dup { nam0, nam1, expr, body } => !(term_is_num_or_var(expr) && term_is_num_or_var(body)),
Term::Lam { name, body } => !term_is_num_or_var(body),
Term::App { func, argm } => !(term_is_num_or_var(func) && term_is_num_or_var(argm)),
Term::Ctr { name, args } => args.iter().any(|term| !term_is_num_or_var(&term)),
Term::Fun { name, args } => args.iter().any(|term| !term_is_num_or_var(&term)),
Term::Num { numb } => false,
Term::Op2 { oper, val0, val1 } => !(term_is_num_or_var(val0) && term_is_num_or_var(val1)),
}
}
// if term is not recursive it will not enter this if
// and will be dropped normally
if term_is_recursive(&self) {
// `Self::Num { numb: U120::ZERO }` is being used as a default `Term`.
// It is being repeated to avoid create a Term variable that would be dropped
// and would call this implementation (could generate a stack overflow,
// or unecessary calls, depending where putted)
let term = std::mem::replace(self, Self::Num { numb: U120::ZERO });
let mut stack = vec![term]; // this will store the recursive terms
while let Some(mut in_term) = stack.pop() {
// if `in_term` is not recursive nothing will be done and, therefore,
// it will be dropped. This will call this drop function from the start
// with `in_term` as `self` and it will not pass the first
// `if term_is_recursive`, dropping the term normally.
if term_is_recursive(&in_term) {
// if the `in_term` is recursive, its children will be erased, and added to stack.
// The `in_term` will be dropped after this, but it will not be recursive anymore,
// so the drop will occur normally. The while will repeat this for all `in_term` children
match &mut in_term {
Term::Var { name } => {},
Term::Num { numb } => {},
Term::Dup { nam0, nam1, expr, body } => {
let expr = std::mem::replace(expr.as_mut(), Self::Num { numb: U120::ZERO });
let body = std::mem::replace(body.as_mut(), Self::Num { numb: U120::ZERO });
stack.push(expr);
stack.push(body);
},
Term::Lam { name, body } => {
let body = std::mem::replace(body.as_mut(), Self::Num { numb: U120::ZERO });
stack.push(body);
},
Term::App { func, argm } => {
let func = std::mem::replace(func.as_mut(), Self::Num { numb: U120::ZERO });
let argm = std::mem::replace(argm.as_mut(), Self::Num { numb: U120::ZERO });
stack.push(func);
stack.push(argm);
},
Term::Ctr { name, args } => {
for arg in args {
let arg = std::mem::replace(arg, Self::Num { numb: U120::ZERO });
stack.push(arg);
}
},
Term::Fun { name, args } => {
for arg in args {
let arg = std::mem::replace(arg, Self::Num { numb: U120::ZERO });
stack.push(arg);
}
},
Term::Op2 { oper, val0, val1 } => {
let val0 = std::mem::replace(val0.as_mut(), Self::Num { numb: U120::ZERO });
let val1 = std::mem::replace(val1.as_mut(), Self::Num { numb: U120::ZERO });
stack.push(val0);
stack.push(val1);
},
}
}
}
}
}
}
/// A native HVM 120-bit machine integer operation.
/// - Add: addition
/// - Sub: subtraction
/// - Mul: multiplication
/// - Div: division
/// - Mod: modulo
/// - And: bitwise and
/// - Or : bitwise or
/// - Xor: bitwise xor
/// - Shl: shift left
/// - Shr: shift right
/// - Ltn: less than
/// - Lte: less than or equal
/// - Eql: equal
/// - Gte: greater than or equal
/// - Gtn: greater than
/// - Neq: not equal
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum Oper {
Add, Sub, Mul, Div,
Mod, And, Or, Xor,
Shl, Shr, Ltn, Lte,
Eql, Gte, Gtn, Neq,
}
/// A rewrite rule, or equation, in the shape of `left_hand_side = right_hand_side`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Rule {
pub lhs: Term,
pub rhs: Term,
}
/// A function, which is just a vector of rewrite rules.
#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct Func {
pub rules: Vec<Rule>,
}
// The types below are used by the runtime to evaluate rewrite rules. They store the same data as
// the type aboves, except in a semi-compiled, digested form, allowing faster computation.
// Compiled information about a left-hand side variable.
#[derive(Clone, Debug, PartialEq)]
pub struct Var {
pub name : Name, // this variable's name
pub param: u64, // in what parameter is this variable located?
pub field: Option<u64>, // in what field is this variable located? (if any)
pub erase: bool, // should this variable be collected (because it is unused)?
}
// Compiled information about a rewrite rule.
#[derive(Clone, Debug, PartialEq)]
pub struct CompRule {
pub cond: Vec<RawCell>, // left-hand side matching conditions
pub vars: Vec<Var>, // left-hand side variable locations
pub eras: Vec<(u64, u64)>, // must-clear locations (argument number and arity)
pub body: Term, // right-hand side body of rule
}
// Compiled information about a function.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct CompFunc {
pub func: Func, // the original function
pub arity: u64, // number of arguments
pub redux: Vec<u64>, // index of strict arguments
pub rules: Vec<CompRule>, // vector of rules
}
// TODO: refactor all these maps to use `Name` newtype
// A file, which is just a map of `FuncID -> CompFunc`
// It is used to find a function when it is called, in order to apply its rewrite rules.
#[derive(Clone, Debug, PartialEq)]
pub struct Funcs {
pub funcs: NameMap<Arc<CompFunc>>,
}
// TODO: arity with no u128
// A map of `FuncID -> Arity`
// It is used in many places to find the arity (argument count) of functions and constructors.
#[derive(Clone, Debug, PartialEq)]
pub struct Arits {
pub arits: NameMap<u64>,
}
// A map of `FuncID -> FuncID
// Stores the owner of the 'FuncID' a namespace.
#[derive(Clone, Debug, PartialEq)]
pub struct Ownrs {
pub ownrs: NameMap<U120>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Indxs {
pub indxs: NameMap<u128>
}
// A map of `FuncID -> RawCell`
// It links a function id to its state on the runtime memory.
#[derive(Clone, Debug, PartialEq)]
pub struct Store {
pub links: U120Map<RawCell>,
}
/// A global statement that alters the state of the blockchain
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Statement {
Fun { name: Name, args: Vec<Name>, func: Func, init: Option<Term>, sign: Option<crypto::Signature> },
Ctr { name: Name, args: Vec<Name>, sign: Option<crypto::Signature> },
Run { expr: Term, sign: Option<crypto::Signature> },
Reg { name: Name, ownr: U120, sign: Option<crypto::Signature> },
}
/// RawCell
/// =======
/// An HVM memory cell/word.
/// It can point to an HVM node, a variable ocurrence, or store an unboxed U120.
#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy)]
#[repr(transparent)]
pub struct RawCell(u128);
impl std::ops::Deref for RawCell {
type Target = u128;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl RawCell {
pub const fn new(value: u128) -> Option<Self> {
let tag = value >> (EXT_SIZE + VAL_SIZE);
if matches!(tag, CellTag) {
Some(RawCell(value))
} else {
None
}
}
/// For testing purposes only. TODO: remove.
pub const fn new_unchecked(value: u128) -> Self {
RawCell(value)
}
pub fn get_tag(&self) -> CellTag {
let tag = (**self / TAG_SHL) as u8;
match tag {
tag if tag == CellTag::DP0 as u8 => CellTag::DP0 ,
tag if tag == CellTag::DP1 as u8 => CellTag::DP1 ,
tag if tag == CellTag::VAR as u8 => CellTag::VAR ,
tag if tag == CellTag::ARG as u8 => CellTag::ARG ,
tag if tag == CellTag::ERA as u8 => CellTag::ERA ,
tag if tag == CellTag::LAM as u8 => CellTag::LAM ,
tag if tag == CellTag::APP as u8 => CellTag::APP ,
tag if tag == CellTag::SUP as u8 => CellTag::SUP ,
tag if tag == CellTag::CTR as u8 => CellTag::CTR ,
tag if tag == CellTag::FUN as u8 => CellTag::FUN ,
tag if tag == CellTag::OP2 as u8 => CellTag::OP2 ,
tag if tag == CellTag::NUM as u8 => CellTag::NUM ,
tag if tag == CellTag::NIL as u8 => CellTag::NIL ,
_ => panic!("Unkown rawcell tag")
}
}
pub fn get_ext(&self) -> u128 {
(**self / EXT_SHL) & 0xFF_FFFF_FFFF_FFFF_FFFF
}
pub fn get_val(&self) -> u64 {
(**self & 0xFFFF_FFFF_FFFF) as u64
}
pub fn get_num(&self) -> U120 {
U120::from_u128_unchecked(**self & NUM_MASK)
}
//pub fn get_ari(lnk: RawCell) -> u128 {
//(lnk / ARI) & 0xF
//}
pub fn get_loc(&self, arg: u64) -> Loc {
Loc(self.get_val() + arg)
}
pub fn get_name_from_ext(&self) -> Name {
Name::new_unsafe(self.get_ext())
}
}
// Loc
// ===
/// A HVM memory location, or "pointer".
#[derive(Debug, Eq, PartialEq, Clone, Hash, Copy)]
#[repr(transparent)]
pub struct Loc(u64);
impl crate::NoHashHasher::IsEnabled for crate::hvm::Loc {}
impl Loc {
pub const _MAX: u64 = (1 << VAL_SIZE) - 1;
pub const MAX: Loc = Loc(Loc::_MAX);
pub fn new(num: u64) -> Option<Self> {
if num >> VAL_SIZE == 0 {
Some(Loc(num))
} else {
None
}
}
}
impl std::ops::Deref for Loc {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::Add<u64> for Loc {
type Output = Self;
fn add(self, other: u64) -> Self::Output {
Loc(self.0 + other)
}
}
impl std::ops::Add for Loc {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Loc(self.0 + other.0)
}
}
// A mergeable vector of RawCells
#[derive(Debug, Clone, PartialEq)]
pub struct Nodes {
pub nodes: LocMap<RawCell>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Hashs {
pub stmt_hashes: U128Map<crypto::Hash>,
}
// HVM's memory state (nodes, functions, metadata, statistics)
#[derive(Debug, Clone, PartialEq)]
pub struct Heap {
pub uuid: u128, // unique identifier
pub memo: Nodes, // memory block holding HVM nodes
pub disk: Store, // points to stored function states
pub file: Funcs, // function codes
pub arit: Arits, // function arities
pub indx: Indxs, // function name to position in heap
pub hash: Hashs,
pub ownr: Ownrs, // namespace owners
pub tick: u64, // tick counter
pub time: u128, // block timestamp
pub meta: u128, // block metadata
pub hax0: u128, // block hash, part 0
pub hax1: u128, // block hash, part 1
pub funs: u64, // total function count
pub dups: u64, // total dups count
pub rwts: u64, // total graph rewrites
pub mana: u64, // total mana cost
pub size: u64, // total used memory (in 64-bit words)
pub mcap: u64, // memory capacity (in 64-bit words)
pub next: u64, // memory index that *may* be empty
// TODO: store run results (Num). (block_idx, stmt_idx) [as u128] -> U120
}
// A list of past heap states, for block-reorg rollback
// FIXME: this should be replaced by a much simpler index array
#[derive(Debug, Clone)]
pub enum Rollback {
Cons {
keep: u64,
life: u64,
head: u64,
tail: Arc<Rollback>,
},
Nil,
}
// The current and past states
pub struct Runtime {
heap: Vec<Heap>, // heap objects
draw: u64, // drawing heap index
curr: u64, // current heap index
nuls: Vec<u64>, // reuse heap indices
back: Arc<Rollback>, // past states
path: PathBuf, // where to save runtime state
}
#[derive(Debug, Clone)]
pub enum RuntimeError {
NotEnoughMana,
NotEnoughSpace,
DivisionByZero,
TermIsInvalidNumber { term: RawCell},
CtrOrFunNotDefined { name: Name },
StmtDoesntExist { stmt_index: u128},
ArityMismatch { name: Name, expected: usize, got: usize },
UnboundVar { name: Name },
NameTooBig { numb: u128 },
TermIsNotLinear { term: Term, var: Name },
TermExceedsMaxDepth,
EffectFailure(EffectFailure),
DefinitionError(DefinitionError)
}
#[derive(Debug, Clone)]
pub enum DefinitionError {
FunctionHasNoRules,
LHSIsNotAFunction, // TODO: check at compile time
LHSArityMismatch { rule_index: usize, expected: usize, got: usize }, // TODO: check at compile time
LHSNotConstructor { rule_index: usize }, // TODO: check at compile time
VarIsUsedTwiceInDefinition { name : Name, rule_index: usize },
VarIsNotLinearInBody { name : Name, rule_index: usize },
VarIsNotUsed { name : Name, rule_index: usize },
NestedMatch { rule_index: usize },
UnsupportedMatch { rule_index: usize },
}
#[derive(Debug, Clone)]
pub enum EffectFailure {
NoSuchState { state: U120 },
InvalidCallArg {caller: U120, callee: U120, arg: RawCell},
InvalidIOCtr { name: Name },
InvalidIONonCtr { ptr: RawCell },
IoFail { err: RawCell },
}
//pub fn heaps_invariant(rt: &Runtime) -> (bool, Vec<u8>, Vec<u64>) {
//let mut seen = vec![0u8; 10];
//let mut heaps = vec![0u64; 0];
//let mut push = |id: u64| {
//let idx = id as usize;
//seen[idx] += 1;
//heaps.push(id);
//};
//push(rt.draw);
//push(rt.curr);
//for nul in &rt.nuls {
//push(*nul);
//}
//{
//let mut back = &*rt.back;
//while let Rollback::Cons { keep, life, head, tail } = back {
//push(*head);
//back = &*tail;
//}
//}
//let failed = seen.iter().all(|c| *c == 1);
//(failed, seen, heaps)
//}
pub type StatementResult = Result<StatementInfo, StatementErr>;
// TODO: refactor (de)serialization out or simplify
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StatementInfo {
Ctr { name: Name, args: Vec<Name> },
Fun { name: Name, args: Vec<Name> },
Run {
done_term: Term,
#[serde_as(as = "DisplayFromStr")]
used_mana: u64,
#[serde_as(as = "DisplayFromStr")]
size_diff: i64,
#[serde_as(as = "DisplayFromStr")]
end_size: u64,
},
Reg { name: Name, ownr: U120 },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatementErr {
pub err: String,
}
// Constants
// ---------
const U128_PER_KB: u128 = (1024 / U128_SIZE) as u128;
const U128_PER_MB: u128 = U128_PER_KB << 10;
const U128_PER_GB: u128 = U128_PER_MB << 10;
// With the constants below, we pre-alloc 6 heaps, which is enough for
// 4 snapshots: 16 seconds old, 4 minutes old, 1 hour old and 1 day old, on
// average.
/// Number of heaps (2 are used for draw/curr, the rest for rollbacks)
const MAX_HEAPS: u64 = 6;
// Number of heaps for snapshots
const MAX_ROLLBACK: u64 = MAX_HEAPS - 2;
pub const MAX_TERM_DEPTH: u128 = 256; // maximum depth of a LHS or RHS term
// Size of each RawCell field in bits
pub const VAL_SIZE: usize = 48;
pub const EXT_SIZE: usize = 72;
pub const TAG_SIZE: usize = 8;
pub const NUM_SIZE: usize = EXT_SIZE + VAL_SIZE;
// Position of each RawCell field
pub const VAL_POS: usize = 0;
pub const EXT_POS: usize = VAL_POS + VAL_SIZE;
pub const TAG_POS: usize = EXT_POS + EXT_SIZE;
pub const NUM_POS: usize = 0;
// First bit of each field
pub const VAL_SHL: u128 = 1 << VAL_POS;
pub const EXT_SHL: u128 = 1 << EXT_POS;
pub const TAG_SHL: u128 = 1 << TAG_POS;
pub const NUM_SHL: u128 = 1 << NUM_POS;
// Bit mask for each field
pub const VAL_MASK: u128 = mask(VAL_SIZE, VAL_POS);
pub const EXT_MASK: u128 = mask(EXT_SIZE, EXT_POS);
pub const TAG_MASK: u128 = mask(TAG_SIZE, TAG_POS);
pub const NUM_MASK: u128 = mask(NUM_SIZE, NUM_POS);
// TODO: refactor to enums with u8 / u128 repr
#[derive(PartialEq)]
#[repr(u8)]
pub enum CellTag {
DP0 = 0x0,
DP1 = 0x1,
VAR = 0x2,
ARG = 0x3,
ERA = 0x4,
LAM = 0x5,
APP = 0x6,
SUP = 0x7,
CTR = 0x8,
FUN = 0x9,
OP2 = 0xA,
NUM = 0xB,
NIL = 0xF,
}
#[repr(u8)]
pub enum Op {
ADD = 0x00,
SUB = 0x01,
MUL = 0x02,
DIV = 0x03,
MOD = 0x04,
AND = 0x05,
OR = 0x06,
XOR = 0x07,
SHL = 0x08,
SHR = 0x09,
LTN = 0x0A,
LTE = 0x0B,
EQL = 0x0C,
GTE = 0x0D,
GTN = 0x0E,
NEQ = 0x0F,
}
pub const U128_NONE : u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
pub const I128_NONE : i128 = -0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
pub const U64_NONE: u64 = u64::MAX; //TODO: rewrite as FFF's?if think it is easier to read like this.
// TODO: r -> U120
// (IO r:Type) : Type
// (DONE expr) : (IO r)
// (TAKE then) : (IO r)
// (SAVE expr then) : (IO r)
// (CALL name argm then) : (IO r)
// (SUBJ then) : (IO r)
// (FROM then) : (IO r)
// (TICK then) : (IO r)
// (TIME then) : (IO r)
const IO_DONE : u128 = 0x39960f; // name_to_u128("DONE")
const IO_TAKE : u128 = 0x78b54f; // name_to_u128("TAKE")
const IO_SAVE : u128 = 0x74b80f; // name_to_u128("SAVE")
const IO_CALL : u128 = 0x34b596; // name_to_u128("CALL")
const IO_SUBJ : u128 = 0x75f314; // name_to_u128("SUBJ")
const IO_FROM : u128 = 0x41c657; // name_to_u128("FROM")
const IO_LOAD : u128 = 0x5992ce; // name_to_u128("LOAD")
const IO_TICK : u128 = 0x793355; // name_to_u128("TICK")
const IO_TIME : u128 = 0x7935cf; // name_to_u128("TIME")
const IO_META : u128 = 0x5cf78b; // name_to_u128("META")
const IO_HAX0 : u128 = 0x48b881; // name_to_u128("HAX0")
const IO_HAX1 : u128 = 0x48b882; // name_to_u128("HAX1")
const IO_GIDX : u128 = 0x4533a2; // name_to_u128("GIDX")
const IO_STH0 : u128 = 0x75e481; // name_to_u128("STH0")
const IO_STH1 : u128 = 0x75e482; // name_to_u128("STH1")
const IO_FAIL : u128 = 0x40b4d6; // name_to_u128("FAIL")
const IO_NORM : u128 = 0x619717; // name_to_u128("NORM")
// TODO: GRUN -> get run result
// Maximum mana that can be spent in a block
pub const BLOCK_MANA_LIMIT : u64 = 4_000_000;
// Maximum state growth per block, in bits
pub const BLOCK_BITS_LIMIT : u64 = 2048; // 1024 bits per sec = about 8 GB per year
// Mana Table
// ----------
// |-----------|---------------------------------|-------|
// | Opcode | Effect | Mana |
// |-----------|---------------------------------|-------|
// | APP-LAM | applies a lambda | 2 |
// | APP-SUP | applies a superposition | 4 |
// | OP2-NUM | operates on a number | 2 |
// | OP2-SUP | operates on a superposition | 4 |
// | FUN-CTR | pattern-matches a constructor | 2 + M |
// | FUN-SUP | pattern-matches a superposition | 2 + A |
// | DUP-LAM | clones a lambda | 4 |
// | DUP-NUM | clones a number | 2 |
// | DUP-CTR | clones a constructor | 2 + A |
// | DUP-SUP-D | clones a superposition | 4 |
// | DUP-SUP-E | undoes a superposition | 2 |
// | DUP-ERA | clones an erasure | 2 |
// |-----------------------------------------------------|
// | * A is the constructor or function arity |
// | * M is the alloc count of the right-hand side |
// |-----------------------------------------------------|
fn AppLamMana() -> u64 {
return 2;
}
fn AppSupMana() -> u64 {
return 4;
}
fn Op2NumMana() -> u64 {
return 2;
}
fn Op2SupMana() -> u64 {
return 4;
}
fn FunCtrMana(body: &Term) -> u64 {
return 2 + count_allocs(body);
}
fn FunSupMana(arity: u64) -> u64 {
return 2 + arity;
}
fn DupLamMana() -> u64 {
return 4;
}
fn DupNumMana() -> u64 {
return 2;
}
fn DupCtrMana(arity: u64) -> u64 {
return 2 + arity;
}
fn DupDupMana() -> u64 {
return 4;
}
fn DupSupMana() -> u64 {
return 2;
}
fn DupEraMana() -> u64 {
return 2;
}
fn count_allocs(body: &Term) -> u64 {
match body {
Term::Var { name } => {
0
}
Term::Dup { nam0, nam1, expr, body } => {
let expr = count_allocs(expr);
let body = count_allocs(body);
3 + expr + body
}
Term::Lam { name, body } => {
let body = count_allocs(body);
2 + body
}
Term::App { func, argm } => {
let func = count_allocs(func);
let argm = count_allocs(argm);
2 + func + argm
}
Term::Fun { name, args } => {
let size = args.len() as u64;
let mut count = 0;
for (i, arg) in args.iter().enumerate() {
count += count_allocs(arg);
}
size + count
}
Term::Ctr { name, args } => {
let size = args.len() as u64;
let mut count = 0;
for (i, arg) in args.iter().enumerate() {
count += count_allocs(arg);
}
size + count
}
Term::Num { numb } => {
0
}
Term::Op2 { oper, val0, val1 } => {
let val0 = count_allocs(val0);
let val1 = count_allocs(val1);
2 + val0 + val1
}
}
}
// Utils