/
view_function.rs
223 lines (185 loc) · 7.36 KB
/
view_function.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
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use super::{new_test_context, new_test_context_with_config};
use aptos_api_test_context::{current_function_name, TestContext};
use aptos_cached_packages::aptos_stdlib;
use aptos_config::config::{NodeConfig, ViewFilter, ViewFunctionId};
use aptos_types::account_address::AccountAddress;
use serde_json::{json, Value};
use std::{path::PathBuf, str::FromStr};
fn build_coin_balance_request(address: &AccountAddress) -> Value {
json!({
"function":"0x1::coin::balance",
"arguments": vec![address.to_string()],
"type_arguments": vec!["0x1::aptos_coin::AptosCoin"],
})
}
fn build_coin_decimals_request() -> Value {
let arguments: Vec<String> = Vec::new();
json!({
"function":"0x1::coin::decimals",
"arguments": arguments,
"type_arguments": vec!["0x1::aptos_coin::AptosCoin"],
})
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_simple_view() {
let mut context = new_test_context(current_function_name!());
let creator = &mut context.gen_account();
let owner = &mut context.gen_account();
let txn1 = context.mint_user_account(creator).await;
let txn2 = context.account_transfer(creator, owner, 100_000);
context.commit_block(&vec![txn1, txn2]).await;
let resp = context
.post("/view", build_coin_balance_request(&owner.address()))
.await;
context.check_golden_output_no_prune(resp);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_view_allowlist() {
let mut node_config = NodeConfig::default();
// Allowlist only the balance function.
node_config.api.view_filter = ViewFilter::Allowlist(vec![ViewFunctionId {
address: AccountAddress::from_str("0x1").unwrap(),
module: "coin".to_string(),
function_name: "balance".to_string(),
}]);
let mut context = new_test_context_with_config(current_function_name!(), node_config);
let creator = &mut context.gen_account();
let owner = &mut context.gen_account();
let txn1 = context.mint_user_account(creator).await;
let txn2 = context.account_transfer(creator, owner, 100_000);
context.commit_block(&vec![txn1, txn2]).await;
// See that an allowed function works.
let resp1 = context
.expect_status_code(200)
.post("/view", build_coin_balance_request(&owner.address()))
.await;
// See that a non-allowed function is rejected.
let resp2 = context
.expect_status_code(403)
.post("/view", build_coin_decimals_request())
.await;
context.check_golden_output_no_prune(json!(vec![resp1, resp2]));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_view_blocklist() {
let mut node_config = NodeConfig::default();
// Blocklist the balance function.
node_config.api.view_filter = ViewFilter::Blocklist(vec![ViewFunctionId {
address: AccountAddress::from_str("0x1").unwrap(),
module: "coin".to_string(),
function_name: "balance".to_string(),
}]);
let mut context = new_test_context_with_config(current_function_name!(), node_config);
let creator = &mut context.gen_account();
let owner = &mut context.gen_account();
let txn1 = context.mint_user_account(creator).await;
let txn2 = context.account_transfer(creator, owner, 100_000);
context.commit_block(&vec![txn1, txn2]).await;
// See that a blocked function is rejected.
let resp1 = context
.expect_status_code(403)
.post("/view", build_coin_balance_request(&owner.address()))
.await;
// See that a non-blocked function is allowed.
let resp2 = context
.expect_status_code(200)
.post("/view", build_coin_decimals_request())
.await;
context.check_golden_output_no_prune(json!(vec![resp1, resp2]));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_simple_view_invalid() {
let mut context = new_test_context(current_function_name!());
let creator = &mut context.gen_account();
let owner = &mut context.gen_account();
let txn1 = context.mint_user_account(creator).await;
let txn2 = context.account_transfer(creator, owner, 100_000);
context.commit_block(&vec![txn1, txn2]).await;
let resp = context
.expect_status_code(400)
.post(
"/view",
json!({
"function":"0x1::aptos_account::assert_account_exists",
"arguments": vec![owner.address().to_string()],
"type_arguments": [],
}),
)
.await;
context.check_golden_output_no_prune(resp);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_versioned_simple_view() {
let mut context = new_test_context(current_function_name!());
let creator = &mut context.gen_account();
let owner = &mut context.gen_account();
let txn1 = context.mint_user_account(creator).await;
let txn2 = context.account_transfer(creator, owner, 100_000);
let txn3 = context.account_transfer(creator, owner, 100_000);
context.commit_block(&vec![txn1, txn2, txn3]).await;
let resp = context
.post(
"/view?ledger_version=3",
json!({
"function":"0x1::coin::balance",
"arguments": vec![owner.address().to_string()],
"type_arguments": vec!["0x1::aptos_coin::AptosCoin"],
}),
)
.await;
context.check_golden_output_no_prune(resp);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_view_tuple() {
let mut context = new_test_context(current_function_name!());
let payload = aptos_stdlib::publish_module_source(
"test_module",
r#"
module 0xa550c18::test_module {
#[view]
public fun return_tuple(): (u64, u64) {
(1, 2)
}
}
"#,
);
let root_account = context.root_account().await;
let module_txn =
root_account.sign_with_transaction_builder(context.transaction_factory().payload(payload));
context.commit_block(&vec![module_txn]).await;
let resp = context
.post(
"/view",
json!({
"function": "0xa550c18::test_module::return_tuple",
"arguments": [],
"type_arguments": [],
}),
)
.await;
context.check_golden_output_no_prune(resp);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_view_aggregator() {
let mut context = new_test_context(current_function_name!());
let account = context.root_account().await;
let named_addresses = vec![("addr".to_string(), account.address())];
let path = PathBuf::from(std::env!("CARGO_MANIFEST_DIR")).join("src/tests/move/pack_counter");
let payload = TestContext::build_package(path, named_addresses);
let txn = account.sign_with_transaction_builder(context.transaction_factory().payload(payload));
context.commit_block(&vec![txn]).await;
let function = format!("{}::counter::add_and_get_counter_value", account.address());
let resp = context
.post(
"/view",
json!({
"function": function,
"arguments": [],
"type_arguments": [],
}),
)
.await;
context.check_golden_output_no_prune(resp);
}