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
use super::{check, compile, compile_with_error};
#[test]
fn check_order() {
// Ensure that the order in which A, B, and C are included in the final compiled contract is
// correct and matches what the user wrote, even though the order in which the predicates
// should be compiled is not the same (since A depends on B and C, and so, B and C should be
// compiled to asm before A).
check(
&format!(
"{}",
compile(
r#"
predicate A(x: int) {
constraint B@[](x);
constraint C@[](x);
}
predicate B(x: int) {
constraint x == 1;
}
predicate C(x: int) {
constraint x == 2;
}
"#,
)
),
expect_test::expect![[r#"
predicate ::A {
--- Nodes ---
node 0 (,leaf)
Stack(Push(1))
Stack(Push(0))
Stack(Push(0))
Stack(Push(1))
Access(PredicateData)
Access(ThisContractAddress)
Stack(Push(9015223055306444934))
Stack(Push(-6810686121413325599))
Stack(Push(337190843018331120))
Stack(Push(8859481080018139671))
Stack(Push(80))
Crypto(Sha256)
Access(PredicateExists)
node 1 (,leaf)
Stack(Push(1))
Stack(Push(0))
Stack(Push(0))
Stack(Push(1))
Access(PredicateData)
Access(ThisContractAddress)
Stack(Push(3333570304599530831))
Stack(Push(4566450419198458868))
Stack(Push(-6811855229356900574))
Stack(Push(7709853129417343888))
Stack(Push(80))
Crypto(Sha256)
Access(PredicateExists)
}
predicate ::B {
--- Nodes ---
node 0 (,leaf)
Stack(Push(0))
Stack(Push(0))
Stack(Push(1))
Access(PredicateData)
Stack(Push(1))
Pred(Eq)
}
predicate ::C {
--- Nodes ---
node 0 (,leaf)
Stack(Push(0))
Stack(Push(0))
Stack(Push(1))
Access(PredicateData)
Stack(Push(2))
Pred(Eq)
}
"#]],
);
}
#[test]
fn identical_empty_predicates() {
// Predicates with identical constraints should fail to compile
check(
&compile_with_error(
r#"
predicate Foo() {
}
predicate Bar() {
}
predicate Cab() {
}
"#,
),
expect_test::expect![[r#"
identical predicates found in the same contract
@13..26: original predicate declaration here
@58..71: predicate with identical bytecode here
two predicates in a contract cannot have the exact same (optimized) bytecode
identical predicates found in the same contract
@13..26: original predicate declaration here
@103..116: predicate with identical bytecode here
two predicates in a contract cannot have the exact same (optimized) bytecode
"#]],
);
}
#[test]
fn identical_predicates() {
// Predicates with identical constraints should fail to compile
check(
&compile_with_error(
r#"
predicate Beast(x: int) {
constraint x > 2;
}
predicate Animal(x: int) {
constraint x > 2;
}
"#,
),
expect_test::expect![[r#"
identical predicates found in the same contract
@11..26: original predicate declaration here
@92..108: predicate with identical bytecode here
two predicates in a contract cannot have the exact same (optimized) bytecode
"#]],
);
}