Skip to content

Commit 36aa1c7

Browse files
committed
Make withdraw_funds_from_object public
1 parent e670384 commit 36aa1c7

File tree

19 files changed

+132
-27
lines changed

19 files changed

+132
-27
lines changed

crates/sui-core/src/authority/shared_object_version_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ mod tests {
10141014
// Create a shared object for testing
10151015
let shared_objects = vec![Object::shared_for_testing()];
10161016
let mut config = ProtocolConfig::get_for_max_version_UNSAFE();
1017-
config.enable_accumulators_for_testing();
1017+
config.set_enable_accumulators_for_testing(true);
10181018
let authority = TestAuthorityBuilder::new()
10191019
.with_starting_objects(&shared_objects)
10201020
.with_protocol_config(config)

crates/sui-core/src/execution_scheduler/balance_withdraw_scheduler/e2e_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async fn create_test_env(init_balances: BTreeMap<TypeTag, u64>) -> TestEnv {
6262
let account_objects = starting_objects.iter().map(|o| o.id()).collect();
6363
starting_objects.push(gas_object.clone());
6464
let mut protocol_config = ProtocolConfig::get_for_max_version_UNSAFE();
65-
protocol_config.enable_accumulators_for_testing();
65+
protocol_config.set_enable_accumulators_for_testing(true);
6666
let state = TestAuthorityBuilder::new()
6767
.with_protocol_config(protocol_config)
6868
.with_starting_objects(&starting_objects)

crates/sui-core/src/unit_tests/coin_deny_list_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ async fn new_authority_and_publish(path: &str) -> TestEnv {
418418

419419
let mut protocol_config =
420420
ProtocolConfig::get_for_version(ProtocolVersion::max(), Chain::Unknown);
421-
protocol_config.enable_accumulators_for_testing();
421+
protocol_config.set_enable_accumulators_for_testing(true);
422422
protocol_config
423423
.set_per_object_congestion_control_mode_for_testing(PerObjectCongestionControlMode::None);
424424

crates/sui-e2e-tests/tests/address_balance_tests.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async fn test_accumulators_root_created() {
105105
cfg.set_buffer_stake_for_protocol_upgrade_bps_for_testing(0);
106106
}
107107
if version == ProtocolVersion::MAX_ALLOWED {
108-
cfg.enable_accumulators_for_testing();
108+
cfg.set_enable_accumulators_for_testing(true);
109109
}
110110
cfg
111111
});
@@ -176,7 +176,7 @@ async fn test_accumulators_disabled() {
176176
cfg.set_buffer_stake_for_protocol_upgrade_bps_for_testing(0);
177177
}
178178
if version == ProtocolVersion::MAX_ALLOWED {
179-
cfg.enable_accumulators_for_testing();
179+
cfg.set_enable_accumulators_for_testing(true);
180180
}
181181
cfg
182182
});
@@ -293,7 +293,7 @@ async fn test_accumulators_disabled() {
293293
async fn test_deposits() {
294294
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
295295
cfg.create_root_accumulator_object_for_testing();
296-
cfg.enable_accumulators_for_testing();
296+
cfg.set_enable_accumulators_for_testing(true);
297297
cfg
298298
});
299299

@@ -347,7 +347,7 @@ async fn test_deposits() {
347347
#[sim_test]
348348
async fn test_multiple_settlement_txns() {
349349
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
350-
cfg.enable_accumulators_for_testing();
350+
cfg.set_enable_accumulators_for_testing(true);
351351
cfg.set_max_updates_per_settlement_txn_for_testing(3);
352352
cfg
353353
});
@@ -463,7 +463,7 @@ fn verify_accumulator_exists(
463463
async fn test_deposit_and_withdraw() {
464464
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
465465
cfg.create_root_accumulator_object_for_testing();
466-
cfg.enable_accumulators_for_testing();
466+
cfg.set_enable_accumulators_for_testing(true);
467467
cfg
468468
});
469469

@@ -510,7 +510,7 @@ async fn test_deposit_and_withdraw() {
510510
async fn test_deposit_and_withdraw_with_larger_reservation() {
511511
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
512512
cfg.create_root_accumulator_object_for_testing();
513-
cfg.enable_accumulators_for_testing();
513+
cfg.set_enable_accumulators_for_testing(true);
514514
cfg
515515
});
516516

@@ -543,7 +543,7 @@ async fn test_deposit_and_withdraw_with_larger_reservation() {
543543
async fn test_withdraw_non_existent_balance() {
544544
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
545545
cfg.create_root_accumulator_object_for_testing();
546-
cfg.enable_accumulators_for_testing();
546+
cfg.set_enable_accumulators_for_testing(true);
547547
cfg
548548
});
549549

@@ -572,7 +572,7 @@ async fn test_withdraw_non_existent_balance() {
572572
async fn test_withdraw_insufficient_balance() {
573573
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
574574
cfg.create_root_accumulator_object_for_testing();
575-
cfg.enable_accumulators_for_testing();
575+
cfg.set_enable_accumulators_for_testing(true);
576576
cfg
577577
});
578578

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
module move_test_code::object_balance;
5+
6+
use sui::balance::Balance;
7+
8+
public struct Vault has key {
9+
id: UID,
10+
}
11+
12+
public fun new(ctx: &mut TxContext) {
13+
let vault = Vault {
14+
id: object::new(ctx),
15+
};
16+
transfer::transfer(vault, ctx.sender());
17+
}
18+
19+
public fun withdraw_funds<T>(vault: &mut Vault, amount: u64): Balance<T> {
20+
sui::balance::withdraw_funds_from_object(&mut vault.id, amount).redeem()
21+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::path::PathBuf;
5+
6+
use sui_json_rpc_types::SuiTransactionBlockEffectsAPI;
7+
use sui_macros::sim_test;
8+
use sui_protocol_config::ProtocolConfig;
9+
use sui_types::{
10+
gas_coin::GAS,
11+
transaction::{CallArg, ObjectArg},
12+
};
13+
use test_cluster::TestClusterBuilder;
14+
15+
#[sim_test]
16+
async fn test_object_balance_withdraw_disabled() {
17+
telemetry_subscribers::init_for_testing();
18+
19+
let _guard = ProtocolConfig::apply_overrides_for_testing(|_, mut cfg| {
20+
cfg.create_root_accumulator_object_for_testing();
21+
cfg.set_enable_accumulators_for_testing(false);
22+
cfg
23+
});
24+
25+
let test_cluster = TestClusterBuilder::new()
26+
.with_num_validators(1)
27+
.build()
28+
.await;
29+
30+
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/move_test_code");
31+
let tx = test_cluster
32+
.test_transaction_builder()
33+
.await
34+
.publish(path)
35+
.build();
36+
let resp = test_cluster.sign_and_execute_transaction(&tx).await;
37+
let package_id = resp.get_new_package_obj().unwrap().0;
38+
39+
let tx = test_cluster
40+
.test_transaction_builder()
41+
.await
42+
.move_call(package_id, "object_balance", "new", vec![])
43+
.build();
44+
let resp = test_cluster.sign_and_execute_transaction(&tx).await;
45+
let vault_obj = resp
46+
.effects
47+
.unwrap()
48+
.created()
49+
.first()
50+
.unwrap()
51+
.reference
52+
.to_object_ref();
53+
54+
let tx = test_cluster
55+
.test_transaction_builder()
56+
.await
57+
.move_call(
58+
package_id,
59+
"object_balance",
60+
"withdraw_funds",
61+
vec![
62+
CallArg::Object(ObjectArg::ImmOrOwnedObject(vault_obj)),
63+
CallArg::from(100u64),
64+
],
65+
)
66+
.with_type_args(vec![GAS::type_tag()])
67+
.build();
68+
let tx = test_cluster.sign_transaction(&tx).await;
69+
let resp = test_cluster
70+
.wallet
71+
.execute_transaction_may_fail(tx)
72+
.await
73+
.unwrap();
74+
let effects = resp.effects.unwrap();
75+
assert!(effects.status().is_err());
76+
}

crates/sui-framework/docs/sui/balance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ accumulator.
489489
Create a <code>Withdrawal&lt;<a href="../sui/balance.md#sui_balance_Balance">Balance</a>&lt;T&gt;&gt;</code> from an object to withdraw funds from it.
490490

491491

492-
<pre><code><b>public</b>(<a href="../sui/package.md#sui_package">package</a>) <b>fun</b> <a href="../sui/balance.md#sui_balance_withdraw_funds_from_object">withdraw_funds_from_object</a>&lt;T&gt;(obj: &<b>mut</b> <a href="../sui/object.md#sui_object_UID">sui::object::UID</a>, <a href="../sui/balance.md#sui_balance_value">value</a>: u64): <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">sui::funds_accumulator::Withdrawal</a>&lt;<a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a>&lt;T&gt;&gt;
492+
<pre><code><b>public</b> <b>fun</b> <a href="../sui/balance.md#sui_balance_withdraw_funds_from_object">withdraw_funds_from_object</a>&lt;T&gt;(obj: &<b>mut</b> <a href="../sui/object.md#sui_object_UID">sui::object::UID</a>, <a href="../sui/balance.md#sui_balance_value">value</a>: u64): <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">sui::funds_accumulator::Withdrawal</a>&lt;<a href="../sui/balance.md#sui_balance_Balance">sui::balance::Balance</a>&lt;T&gt;&gt;
493493
</code></pre>
494494

495495

@@ -498,7 +498,7 @@ Create a <code>Withdrawal&lt;<a href="../sui/balance.md#sui_balance_Balance">Bal
498498
<summary>Implementation</summary>
499499

500500

501-
<pre><code><b>public</b>(<a href="../sui/package.md#sui_package">package</a>) <b>fun</b> <a href="../sui/balance.md#sui_balance_withdraw_funds_from_object">withdraw_funds_from_object</a>&lt;T&gt;(
501+
<pre><code><b>public</b> <b>fun</b> <a href="../sui/balance.md#sui_balance_withdraw_funds_from_object">withdraw_funds_from_object</a>&lt;T&gt;(
502502
obj: &<b>mut</b> UID,
503503
<a href="../sui/balance.md#sui_balance_value">value</a>: u64,
504504
): Withdrawal&lt;<a href="../sui/balance.md#sui_balance_Balance">Balance</a>&lt;T&gt;&gt; {

crates/sui-framework/docs/sui/funds_accumulator.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Aborts with <code><a href="../sui/funds_accumulator.md#sui_funds_accumulator_EOv
227227

228228

229229

230-
<pre><code><b>public</b>(<a href="../sui/package.md#sui_package">package</a>) <b>fun</b> <a href="../sui/funds_accumulator.md#sui_funds_accumulator_redeem">redeem</a>&lt;T: store&gt;(withdrawal: <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">sui::funds_accumulator::Withdrawal</a>&lt;T&gt;): T
230+
<pre><code><b>public</b> <b>fun</b> <a href="../sui/funds_accumulator.md#sui_funds_accumulator_redeem">redeem</a>&lt;T: store&gt;(withdrawal: <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">sui::funds_accumulator::Withdrawal</a>&lt;T&gt;): T
231231
</code></pre>
232232

233233

@@ -236,7 +236,7 @@ Aborts with <code><a href="../sui/funds_accumulator.md#sui_funds_accumulator_EOv
236236
<summary>Implementation</summary>
237237

238238

239-
<pre><code><b>public</b>(<a href="../sui/package.md#sui_package">package</a>) <b>fun</b> <a href="../sui/funds_accumulator.md#sui_funds_accumulator_redeem">redeem</a>&lt;/* internal */ T: store&gt;(withdrawal: <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">Withdrawal</a>&lt;T&gt;): T {
239+
<pre><code><b>public</b> <b>fun</b> <a href="../sui/funds_accumulator.md#sui_funds_accumulator_redeem">redeem</a>&lt;/* internal */ T: store&gt;(withdrawal: <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">Withdrawal</a>&lt;T&gt;): T {
240240
<b>let</b> <a href="../sui/funds_accumulator.md#sui_funds_accumulator_Withdrawal">Withdrawal</a> { owner, limit: value } = withdrawal;
241241
<a href="../sui/funds_accumulator.md#sui_funds_accumulator_withdraw_impl">withdraw_impl</a>(owner, value)
242242
}

crates/sui-framework/packages/sui-framework/sources/balance.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public fun redeem_funds<T>(withdrawal: sui::funds_accumulator::Withdrawal<Balanc
110110
}
111111

112112
/// Create a `Withdrawal<Balance<T>>` from an object to withdraw funds from it.
113-
public(package) fun withdraw_funds_from_object<T>(
113+
public fun withdraw_funds_from_object<T>(
114114
obj: &mut UID,
115115
value: u64,
116116
): Withdrawal<Balance<T>> {

crates/sui-framework/packages/sui-framework/sources/funds_accumulator.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public fun withdrawal_join<T: store>(withdrawal: &mut Withdrawal<T>, other: With
7171
// TODO When this becomes `public` we need
7272
// - custom verifier rules for `T`
7373
// - private generic rules for `T`
74-
public(package) fun redeem</* internal */ T: store>(withdrawal: Withdrawal<T>): T {
74+
public fun redeem</* internal */ T: store>(withdrawal: Withdrawal<T>): T {
7575
let Withdrawal { owner, limit: value } = withdrawal;
7676
withdraw_impl(owner, value)
7777
}

0 commit comments

Comments
 (0)