Skip to content

Commit 760ad44

Browse files
committed
txn args
1 parent 12ef626 commit 760ad44

File tree

23 files changed

+2833
-55
lines changed

23 files changed

+2833
-55
lines changed

api/src/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ mod modules;
1414
mod multisig_transactions_test;
1515
mod objects;
1616
mod option_test;
17+
mod public_struct_enum_negative_test;
18+
mod public_struct_enum_test;
1719
mod resource_groups;
1820
mod secp256k1_ecdsa;
1921
mod signed_int_test;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "public_struct_enum_negative_test"
3+
version = "0.0.0"
4+
5+
[addresses]
6+
account = "_"
7+
8+
[dependencies]
9+
AptosFramework = { local = "../../../../../aptos-move/framework/aptos-framework" }
10+
AptosStdlib = { local = "../../../../../aptos-move/framework/aptos-stdlib" }
11+
MoveStdlib = { local = "../../../../../aptos-move/framework/move-stdlib" }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Module with invalid struct/enum types for negative testing
2+
module account::negative_test {
3+
4+
/// Private struct (not public) - should be rejected as txn arg
5+
struct PrivatePoint has copy, drop {
6+
x: u64,
7+
y: u64,
8+
}
9+
10+
/// Public struct without copy ability - should be rejected as txn arg
11+
public struct NoCopyPoint has drop {
12+
x: u64,
13+
y: u64,
14+
}
15+
16+
/// Public enum without copy ability - should be rejected as txn arg
17+
public enum NoCopyColor has drop {
18+
Red,
19+
Green,
20+
Blue,
21+
}
22+
23+
/// Entry function that takes private struct - should fail at validation
24+
public entry fun test_private_struct(_sender: &signer, _p: PrivatePoint) {
25+
// This should never execute
26+
}
27+
28+
/// Entry function that takes non-copy struct - should fail at validation
29+
public entry fun test_no_copy_struct(_sender: &signer, _p: NoCopyPoint) {
30+
// This should never execute
31+
}
32+
33+
/// Entry function that takes non-copy enum - should fail at validation
34+
public entry fun test_no_copy_enum(_sender: &signer, _c: NoCopyColor) {
35+
// This should never execute
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "public_struct_enum_test"
3+
version = "0.0.0"
4+
5+
[addresses]
6+
account = "_"
7+
8+
[dependencies]
9+
AptosFramework = { local = "../../../../../aptos-move/framework/aptos-framework" }
10+
AptosStdlib = { local = "../../../../../aptos-move/framework/aptos-stdlib" }
11+
MoveStdlib = { local = "../../../../../aptos-move/framework/move-stdlib" }
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/// Module to test public structs and enums with copy ability as transaction arguments
2+
module account::public_struct_test {
3+
use std::string::String;
4+
5+
/// Result resource to store test outcomes
6+
struct TestResult has key {
7+
value: u64,
8+
message: String,
9+
}
10+
11+
/// A public struct with copy ability - should be allowed as txn arg
12+
public struct Point has copy, drop {
13+
x: u64,
14+
y: u64,
15+
}
16+
17+
/// A public struct with nested public copy struct
18+
public struct Rectangle has copy, drop {
19+
top_left: Point,
20+
bottom_right: Point,
21+
}
22+
23+
/// A public struct with vector of primitives
24+
public struct Data has copy, drop {
25+
values: vector<u64>,
26+
name: String,
27+
}
28+
29+
/// A public enum with copy ability - should be allowed as txn arg
30+
public enum Color has copy, drop {
31+
Red,
32+
Green,
33+
Blue,
34+
Custom { r: u8, g: u8, b: u8 },
35+
}
36+
37+
/// A public enum with struct fields
38+
public enum Shape has copy, drop {
39+
Circle { center: Point, radius: u64 },
40+
Rect { rect: Rectangle },
41+
}
42+
43+
/// Entry function that takes a Point as argument
44+
public entry fun test_point(sender: &signer, p: Point) acquires TestResult {
45+
if (!exists<TestResult>(std::signer::address_of(sender))) {
46+
move_to(sender, TestResult {
47+
value: 0,
48+
message: std::string::utf8(b"")
49+
});
50+
};
51+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
52+
result.value = p.x + p.y;
53+
result.message = std::string::utf8(b"point_received");
54+
}
55+
56+
/// Entry function that takes a Rectangle as argument
57+
public entry fun test_rectangle(sender: &signer, r: Rectangle) acquires TestResult {
58+
if (!exists<TestResult>(std::signer::address_of(sender))) {
59+
move_to(sender, TestResult {
60+
value: 0,
61+
message: std::string::utf8(b"")
62+
});
63+
};
64+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
65+
result.value = r.top_left.x + r.top_left.y + r.bottom_right.x + r.bottom_right.y;
66+
result.message = std::string::utf8(b"rectangle_received");
67+
}
68+
69+
/// Entry function that takes a Data struct as argument
70+
public entry fun test_data(sender: &signer, d: Data) acquires TestResult {
71+
if (!exists<TestResult>(std::signer::address_of(sender))) {
72+
move_to(sender, TestResult {
73+
value: 0,
74+
message: std::string::utf8(b"")
75+
});
76+
};
77+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
78+
let sum = 0u64;
79+
let i = 0;
80+
let len = std::vector::length(&d.values);
81+
while (i < len) {
82+
sum = sum + *std::vector::borrow(&d.values, i);
83+
i = i + 1;
84+
};
85+
result.value = sum;
86+
result.message = d.name;
87+
}
88+
89+
/// Entry function that takes a Color enum as argument
90+
public entry fun test_color(sender: &signer, c: Color) acquires TestResult {
91+
if (!exists<TestResult>(std::signer::address_of(sender))) {
92+
move_to(sender, TestResult {
93+
value: 0,
94+
message: std::string::utf8(b"")
95+
});
96+
};
97+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
98+
let (value, msg) = match (c) {
99+
Color::Red => (1, b"red"),
100+
Color::Green => (2, b"green"),
101+
Color::Blue => (3, b"blue"),
102+
Color::Custom { r, g, b } => ((r as u64) + (g as u64) + (b as u64), b"custom"),
103+
};
104+
result.value = value;
105+
result.message = std::string::utf8(msg);
106+
}
107+
108+
/// Entry function that takes a Shape enum as argument
109+
public entry fun test_shape(sender: &signer, s: Shape) acquires TestResult {
110+
if (!exists<TestResult>(std::signer::address_of(sender))) {
111+
move_to(sender, TestResult {
112+
value: 0,
113+
message: std::string::utf8(b"")
114+
});
115+
};
116+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
117+
let (value, msg) = match (s) {
118+
Shape::Circle { center, radius } => (center.x + center.y + radius, b"circle"),
119+
Shape::Rect { rect } => (
120+
rect.top_left.x + rect.top_left.y + rect.bottom_right.x + rect.bottom_right.y,
121+
b"rect"
122+
),
123+
};
124+
result.value = value;
125+
result.message = std::string::utf8(msg);
126+
}
127+
128+
/// Entry function that takes a vector of Points
129+
public entry fun test_point_vector(sender: &signer, points: vector<Point>) acquires TestResult {
130+
if (!exists<TestResult>(std::signer::address_of(sender))) {
131+
move_to(sender, TestResult {
132+
value: 0,
133+
message: std::string::utf8(b"")
134+
});
135+
};
136+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
137+
let sum = 0u64;
138+
let i = 0;
139+
let len = std::vector::length(&points);
140+
while (i < len) {
141+
let p = std::vector::borrow(&points, i);
142+
sum = sum + p.x + p.y;
143+
i = i + 1;
144+
};
145+
result.value = sum;
146+
result.message = std::string::utf8(b"point_vector_received");
147+
}
148+
149+
/// Entry function using whitelisted String type - should always work
150+
public entry fun test_string(sender: &signer, s: String) acquires TestResult {
151+
if (!exists<TestResult>(std::signer::address_of(sender))) {
152+
move_to(sender, TestResult {
153+
value: 0,
154+
message: std::string::utf8(b"")
155+
});
156+
};
157+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
158+
result.value = std::string::length(&s);
159+
result.message = s;
160+
}
161+
162+
/// Entry function that takes Option<Point>
163+
public entry fun test_option_point(sender: &signer, opt_point: std::option::Option<Point>) acquires TestResult {
164+
if (!exists<TestResult>(std::signer::address_of(sender))) {
165+
move_to(sender, TestResult {
166+
value: 0,
167+
message: std::string::utf8(b"")
168+
});
169+
};
170+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
171+
if (std::option::is_some(&opt_point)) {
172+
let p = std::option::destroy_some(opt_point);
173+
result.value = p.x + p.y;
174+
result.message = std::string::utf8(b"some_point");
175+
} else {
176+
std::option::destroy_none(opt_point);
177+
result.value = 0;
178+
result.message = std::string::utf8(b"none_point");
179+
}
180+
}
181+
182+
/// Entry function that takes Option<Color>
183+
public entry fun test_option_color(sender: &signer, opt_color: std::option::Option<Color>) acquires TestResult {
184+
if (!exists<TestResult>(std::signer::address_of(sender))) {
185+
move_to(sender, TestResult {
186+
value: 0,
187+
message: std::string::utf8(b"")
188+
});
189+
};
190+
let result = borrow_global_mut<TestResult>(std::signer::address_of(sender));
191+
if (std::option::is_some(&opt_color)) {
192+
let color = std::option::destroy_some(opt_color);
193+
let (value, msg) = match (color) {
194+
Color::Red => (1, b"some_red"),
195+
Color::Green => (2, b"some_green"),
196+
Color::Blue => (3, b"some_blue"),
197+
Color::Custom { r, g, b } => ((r as u64) + (g as u64) + (b as u64), b"some_custom"),
198+
};
199+
result.value = value;
200+
result.message = std::string::utf8(msg);
201+
} else {
202+
std::option::destroy_none(opt_color);
203+
result.value = 0;
204+
result.message = std::string::utf8(b"none_color");
205+
}
206+
}
207+
208+
#[view]
209+
public fun get_result(addr: address): (u64, String) acquires TestResult {
210+
let result = borrow_global<TestResult>(addr);
211+
(result.value, result.message)
212+
}
213+
}

0 commit comments

Comments
 (0)