-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathlarge_package_publishing.rs
421 lines (376 loc) · 14.2 KB
/
large_package_publishing.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
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use crate::{assert_move_abort, assert_success, assert_vm_status, tests::common, MoveHarness};
use aptos_framework::{
chunked_publish::{
chunk_package_and_create_payloads, PublishType, CHUNK_SIZE_IN_BYTES,
LARGE_PACKAGES_MODULE_ADDRESS,
},
natives::{
code::{PackageMetadata, PackageRegistry, UpgradePolicy},
object_code_deployment::ManagingRefs,
},
BuildOptions, BuiltPackage,
};
use aptos_language_e2e_tests::account::Account;
use aptos_types::{
object_address::create_object_code_deployment_address,
transaction::{AbortInfo, TransactionPayload, TransactionStatus},
};
use move_core_types::{
account_address::AccountAddress, parser::parse_struct_tag, vm_status::StatusCode,
};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, option::Option, path::Path, str::FromStr};
/// Number of transactions needed for staging code chunks before publishing to accounts or objects
/// This is used to derive object address for testing object code deployment feature
const NUMBER_OF_TRANSACTIONS_FOR_STAGING: u64 = 2;
/// Mimics `0xcafe::eight::State`
#[derive(Serialize, Deserialize)]
struct State {
value: u64,
}
struct LargePackageTestContext {
harness: MoveHarness,
account: Account, // used for testing account code deployment for large packages
object_address: AccountAddress, // used for testing object code deployment for large packages
}
impl LargePackageTestContext {
/// Create a new test context with initialized accounts and published `large_packages.move` module.
fn new() -> Self {
let mut harness = MoveHarness::new();
let admin_account = harness.new_account_at(
AccountAddress::from_hex_literal(LARGE_PACKAGES_MODULE_ADDRESS).unwrap(),
);
let account = harness.new_account_at(AccountAddress::from_hex_literal("0xcafe").unwrap());
let sequence_number = harness.sequence_number(account.address());
let object_address = create_object_code_deployment_address(
*account.address(),
sequence_number + NUMBER_OF_TRANSACTIONS_FOR_STAGING + 1,
);
// publish `large_packages.move` module
let build_option = Self::get_named_addresses_build_options(vec![(
String::from("large_packages"),
AccountAddress::from_hex_literal(LARGE_PACKAGES_MODULE_ADDRESS).unwrap(),
)]);
let txn = harness.create_publish_package(
&admin_account,
&common::test_dir_path("../../../move-examples/large_packages"),
Some(build_option),
|_| {},
);
assert_success!(harness.run(txn));
LargePackageTestContext {
harness,
account,
object_address,
}
}
fn get_named_addresses_build_options(
named_addresses: Vec<(String, AccountAddress)>,
) -> BuildOptions {
let mut build_options = BuildOptions::default();
let mut map = BTreeMap::new();
for (k, v) in named_addresses {
map.insert(k, v);
}
build_options.named_addresses = map;
build_options
}
/// Publish a large package by creating and running the necessary transactions.
fn publish_large_package(
&mut self,
account: &Account,
path: &Path,
patch_metadata: impl FnMut(&mut PackageMetadata),
publish_type: PublishType,
) -> Vec<TransactionStatus> {
let deploy_address = match publish_type {
PublishType::AccountDeploy => AccountAddress::from_hex_literal("0xcafe").unwrap(),
PublishType::ObjectDeploy | PublishType::ObjectUpgrade => self.object_address,
};
let build_options = Self::get_named_addresses_build_options(vec![(
String::from("large_package_example"),
deploy_address,
)]);
let payloads = self.create_publish_large_package_from_path(
path,
Some(build_options),
patch_metadata,
publish_type,
);
payloads
.into_iter()
.map(|payload| {
let signed_tx = self
.harness
.create_transaction_without_sign(account, payload)
.sign();
self.harness.run(signed_tx)
})
.collect()
}
/// Create transactions for publishing a large package.
fn create_publish_large_package_from_path(
&mut self,
path: &Path,
options: Option<BuildOptions>,
mut patch_metadata: impl FnMut(&mut PackageMetadata),
publish_type: PublishType,
) -> Vec<TransactionPayload> {
let package = BuiltPackage::build(path.to_owned(), options.unwrap())
.expect("package build must succeed");
let package_code = package.extract_code();
let mut metadata = package
.extract_metadata()
.expect("extracting package metadata must succeed");
patch_metadata(&mut metadata);
let metadata_serialized = bcs::to_bytes(&metadata).expect("Failed deserializing metadata");
chunk_package_and_create_payloads(
metadata_serialized,
package_code,
publish_type,
Some(self.object_address),
AccountAddress::from_str(LARGE_PACKAGES_MODULE_ADDRESS).unwrap(),
CHUNK_SIZE_IN_BYTES,
)
}
}
#[test]
fn large_package_publishing_basic() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Test transactions for publishing the large package are successful
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Validate metadata
let registry = context
.harness
.read_resource::<PackageRegistry>(
acc.address(),
parse_struct_tag("0x1::code::PackageRegistry").unwrap(),
)
.unwrap();
assert_eq!(registry.packages.len(), 1);
assert_eq!(registry.packages[0].name, "LargePackageExample");
assert_eq!(registry.packages[0].modules.len(), 9); // `LargePackageExample` package includes 9 modules
// Validate code loaded as expected.
assert_success!(context.harness.run_entry_function(
&acc,
str::parse("0xcafe::eight::hello").unwrap(),
vec![],
vec![bcs::to_bytes::<u64>(&42).unwrap()]
));
let state = context
.harness
.read_resource::<State>(
acc.address(),
parse_struct_tag("0xcafe::eight::State").unwrap(),
)
.unwrap();
assert_eq!(state.value, 42);
}
#[test]
fn large_package_upgrade_success_compat() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Initial version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Upgrade to compatible version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"), // upgrade with the same package
|_| {},
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
}
#[test]
fn large_package_upgrade_fail_compat() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Initial version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Upgrade to incompatible version should fail
// Staging metadata and code should pass, and the final publishing transaction should fail
let mut tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("large_package_publishing.data/large_pack_upgrade_incompat"),
|_| {},
PublishType::AccountDeploy,
);
let last_tx_status = tx_statuses.pop().unwrap(); // transaction for publishing
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
assert_vm_status!(
last_tx_status,
StatusCode::BACKWARD_INCOMPATIBLE_MODULE_UPDATE
);
}
#[test]
fn large_package_upgrade_fail_immutable() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Initial version (immutable package)
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|metadata| metadata.upgrade_policy = UpgradePolicy::immutable(),
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Upgrading immutable package should fail
// Staging metadata and code should pass, and the final publishing transaction should fail
let mut tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::AccountDeploy,
);
let last_tx_status = tx_statuses.pop().unwrap(); // transaction for publishing
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
let abort_info = Some(AbortInfo {
reason_name: "EUPGRADE_IMMUTABLE".to_string(),
description: "Cannot upgrade an immutable package".to_string(),
});
assert_move_abort!(last_tx_status, abort_info);
}
#[test]
fn large_package_upgrade_fail_overlapping_module() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Initial version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::AccountDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Publishing the same package with different name should fail
// Staging metadata and code should pass, and the final publishing transaction should fail
let mut tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|metadata| metadata.name = "other_large_pack".to_string(),
PublishType::AccountDeploy,
);
let last_tx_status = tx_statuses.pop().unwrap(); // transaction for publishing
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
let abort_info = Some(AbortInfo {
reason_name: "EMODULE_NAME_CLASH".to_string(),
description: "Package contains duplicate module names with existing modules publised in other packages on this address".to_string(),
});
assert_move_abort!(last_tx_status, abort_info);
}
#[test]
fn large_package_object_code_deployment_basic() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Test transactions for publishing the large package are successful
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::ObjectDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Validate metadata
let registry = context
.harness
.read_resource::<PackageRegistry>(
&context.object_address,
parse_struct_tag("0x1::code::PackageRegistry").unwrap(),
)
.unwrap();
assert_eq!(registry.packages.len(), 1);
assert_eq!(registry.packages[0].name, "LargePackageExample");
assert_eq!(registry.packages[0].modules.len(), 9);
let code_object: ManagingRefs = context
.harness
.read_resource_from_resource_group(
&context.object_address,
parse_struct_tag("0x1::object::ObjectGroup").unwrap(),
parse_struct_tag("0x1::object_code_deployment::ManagingRefs").unwrap(),
)
.unwrap();
// Verify the object created owns the `ManagingRefs`
assert_eq!(code_object, ManagingRefs::new(context.object_address));
let module_address = context.object_address.to_string();
// Validate code loaded as expected.
assert_success!(context.harness.run_entry_function(
&acc,
str::parse(&format!("{}::eight::hello", module_address)).unwrap(),
vec![],
vec![bcs::to_bytes::<u64>(&42).unwrap()]
));
let state = context
.harness
.read_resource::<State>(
acc.address(),
parse_struct_tag(&format!("{}::eight::State", module_address)).unwrap(),
)
.unwrap();
assert_eq!(state.value, 42);
}
#[test]
fn large_package_object_code_deployment_upgrade_success_compat() {
let mut context = LargePackageTestContext::new();
let acc = context.account.clone();
// Initial version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"),
|_| {},
PublishType::ObjectDeploy,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
// Upgrade to compatible version
let tx_statuses = context.publish_large_package(
&acc,
&common::test_dir_path("../../../move-examples/large_packages/large_package_example"), // upgrade with the same package
|_| {},
PublishType::ObjectUpgrade,
);
for tx_status in tx_statuses.into_iter() {
assert_success!(tx_status);
}
}