Skip to content

Commit

Permalink
[cleanup][Sui Framework] Remove unused TxContexts (MystenLabs#2399)
Browse files Browse the repository at this point in the history
* [cleanup][Sui Framework] Remove unused TxContexts

- Remove unused transaction contexts, now that it is optional
  • Loading branch information
tnowacki authored Jun 3, 2022
1 parent 245a690 commit d4428d9
Show file tree
Hide file tree
Showing 31 changed files with 78 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module T2::O2 {
Transfer::transfer(new(child, ctx), TxContext::sender(ctx))
}

public(script) fun use_o2_o3(_o2: &mut O2, _o3: &mut O3, _ctx: &mut TxContext) {}
public(script) fun use_o2_o3(_o2: &mut O2, _o3: &mut O3) {}

fun new(child: O3, ctx: &mut TxContext): O2 {
let id = TxContext::new_id(ctx);
Expand Down Expand Up @@ -70,7 +70,7 @@ module T1::O1 {
}

// This function will be invalid if _o2 is a shared object and owns _o3.
public(script) fun use_o2_o3(_o2: &mut O2, _o3: &mut O3, _ctx: &mut TxContext) {}
public(script) fun use_o2_o3(_o2: &mut O2, _o3: &mut O3) {}

fun new(child: O2, ctx: &mut TxContext): O1 {
let id = TxContext::new_id(ctx);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
processed 4 tasks

task 1 'publish'. lines 6-14:
task 1 'publish'. lines 6-13:
created: object(103)
written: object(102)

task 2 'run'. lines 15-17:
Error: Function signature is invalid: "Expected 3 arguments calling function 'create', but found 2".
task 2 'run'. lines 14-16:
Error: Function signature is invalid: "Expected 2 arguments calling function 'create', but found 1".

task 3 'run'. lines 18-18:
task 3 'run'. lines 17-17:
Error: Execution aborted: "VMError with status FAILED_TO_DESERIALIZE_ARGUMENT at location UNDEFINED".
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
//# publish

module Test::M {
use Sui::TxContext::TxContext;
public(script) fun create(_value: u64, _recipient: address, _ctx: &mut TxContext) {}
public(script) fun create(_value: u64, _recipient: address) {}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Examples::TrustedCoin {
Coin::transfer(coin, TxContext::sender(ctx));
}

public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address) {
Coin::transfer_cap<EXAMPLE>(treasury_cap, recipient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ module ObjectOwner::ObjectOwner {
Transfer::transfer(parent, TxContext::sender(ctx));
}

public(script) fun add_child(parent: &mut Parent, child: Child, _ctx: &mut TxContext) {
public(script) fun add_child(parent: &mut Parent, child: Child) {
let child_ref = Transfer::transfer_to_object(child, parent);
Option::fill(&mut parent.child, child_ref);
}

// Call to mutate_child will fail if its owned by a parent,
// since all owners must be in the arguments for authentication.
public(script) fun mutate_child(_child: &mut Child, _ctx: &mut TxContext) {}
public(script) fun mutate_child(_child: &mut Child) {}

// This should always succeeds, even when child is not owned by parent.
public(script) fun mutate_child_with_parent(_child: &mut Child, _parent: &mut Parent, _ctx: &mut TxContext) {}
public(script) fun mutate_child_with_parent(_child: &mut Child, _parent: &mut Parent) {}

public(script) fun transfer_child(parent: &mut Parent, child: Child, new_parent: &mut Parent, _ctx: &mut TxContext) {
public(script) fun transfer_child(parent: &mut Parent, child: Child, new_parent: &mut Parent) {
let child_ref = Option::extract(&mut parent.child);
let new_child_ref = Transfer::transfer_child_to_object(child, child_ref, new_parent);
Option::fill(&mut new_parent.child, new_child_ref);
Expand All @@ -71,12 +71,12 @@ module ObjectOwner::ObjectOwner {
}

// Call to delete_child can fail if it's still owned by a parent.
public(script) fun delete_child(child: Child, _parent: &mut Parent, _ctx: &mut TxContext) {
public(script) fun delete_child(child: Child, _parent: &mut Parent) {
let Child { id } = child;
ID::delete(id);
}

public(script) fun delete_parent_and_child(parent: Parent, child: Child, _ctx: &mut TxContext) {
public(script) fun delete_parent_and_child(parent: Parent, child: Child) {
let Parent { id: parent_id, child: child_ref_opt } = parent;
let child_ref = Option::extract(&mut child_ref_opt);
Option::destroy_none(child_ref_opt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module ObjectWrapping::ObjectWrapping {
)
}

public(script) fun set_child(parent: &mut Parent, child: Child, _ctx: &mut TxContext) {
public(script) fun set_child(parent: &mut Parent, child: Child) {
Option::fill(&mut parent.child, child)
}

Expand All @@ -47,7 +47,7 @@ module ObjectWrapping::ObjectWrapping {
)
}

public(script) fun delete_parent(parent: Parent, _ctx: &mut TxContext) {
public(script) fun delete_parent(parent: Parent) {
let Parent { id: parent_id, child: child_opt } = parent;
ID::delete(parent_id);
if (Option::is_some(&child_opt)) {
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/sources/Coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ module Sui::Coin {

/// Consume the coin `c` and add its value to `self`.
/// Aborts if `c.value + self.value > U64_MAX`
public(script) fun join_<T>(self: &mut Coin<T>, c: Coin<T>, _ctx: &mut TxContext) {
public(script) fun join_<T>(self: &mut Coin<T>, c: Coin<T>) {
join(self, c)
}

/// Join everything in `coins` with `self`
public(script) fun join_vec_<T>(self: &mut Coin<T>, coins: vector<Coin<T>>, _ctx: &mut TxContext) {
public(script) fun join_vec_<T>(self: &mut Coin<T>, coins: vector<Coin<T>>) {
join_vec(self, coins)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/sui-framework/sources/Governance/Delegation.move
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module Sui::Delegation {

/// Destroy the delegation object. This can be done only when the delegation
/// is inactive and all reward have been claimed.
public(script) fun burn(self: Delegation, _ctx: &mut TxContext) {
public(script) fun burn(self: Delegation) {
assert!(!is_active(&self), 0);

let Delegation {
Expand All @@ -104,7 +104,7 @@ module Sui::Delegation {
assert!(next_reward_unclaimed_epoch == ending_epoch, 0);
}

public(script) fun transfer(self: Delegation, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(self: Delegation, recipient: address) {
Transfer::transfer(self, recipient)
}

Expand Down
10 changes: 5 additions & 5 deletions crates/sui-framework/sources/ObjectBasics.move
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ module Sui::ObjectBasics {
)
}

public(script) fun transfer(o: Object, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(o: Object, recipient: address) {
Transfer::transfer(o, recipient)
}

public(script) fun freeze_object(o: Object, _ctx: &mut TxContext) {
public(script) fun freeze_object(o: Object) {
Transfer::freeze_object(o)
}

public(script) fun set_value(o: &mut Object, value: u64, _ctx: &mut TxContext) {
public(script) fun set_value(o: &mut Object, value: u64) {
o.value = value;
}

// test that reading o2 and updating o1 works
public(script) fun update(o1: &mut Object, o2: &Object, _ctx: &mut TxContext) {
public(script) fun update(o1: &mut Object, o2: &Object) {
o1.value = o2.value;
// emit an event so the world can see the new value
Event::emit(NewValueEvent { new_value: o2.value })
}

public(script) fun delete(o: Object, _ctx: &mut TxContext) {
public(script) fun delete(o: Object) {
let Object { id, value: _ } = o;
ID::delete(id);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-framework/sources/SUI.move
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Sui::SUI {
}

/// Transfer to a recipient
public(script) fun transfer(c: Coin::Coin<SUI>, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(c: Coin::Coin<SUI>, recipient: address) {
Coin::transfer(c, recipient)
}

Expand Down
1 change: 1 addition & 0 deletions crates/sui-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ serde_json = "1.0.79"
schemars = "0.8.8"

sui-types = { path = "../sui-types" }
sui-verifier = { path = "../sui-verifier" }

move-binary-format = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b" }
move-core-types = { git = "https://github.com/move-language/move", rev = "1b2d3b4274345f5b4b6a1a1bde5aee452003ab5b", features = ["address20"] }
Expand Down
9 changes: 8 additions & 1 deletion crates/sui-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use anyhow::{anyhow, bail};
use move_binary_format::{
access::ModuleAccess,
binary_views::BinaryIndexedView,
file_format::{SignatureToken, Visibility},
};
use move_core_types::account_address::AccountAddress;
Expand All @@ -18,6 +19,7 @@ use std::collections::VecDeque;
use std::fmt::{Debug, Formatter};
use sui_types::base_types::{decode_bytes_hex, ObjectID, SuiAddress};
use sui_types::move_package::MovePackage;
use sui_verifier::entry_points_verifier::is_tx_context;

const HEX_PREFIX: &str = "0x";

Expand Down Expand Up @@ -375,7 +377,12 @@ pub fn resolve_move_function_args(
}

// Lengths have to match, less one, due to TxContext
let expected_len = parameters.len() - 1;
let expected_len = match parameters.last() {
Some(param) if is_tx_context(&BinaryIndexedView::Module(&module), param) => {
parameters.len() - 1
}
_ => parameters.len(),
};
if combined_args_json.len() != expected_len {
return Err(anyhow!(
"Expected {} args, found {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module Examples::TicTacToe {
}
}

public(script) fun delete_game(game: TicTacToe, _ctx: &mut TxContext) {
public(script) fun delete_game(game: TicTacToe) {
let TicTacToe { id, gameboard, cur_turn: _, game_status: _, x_address: _, o_address: _ } = game;
while (Vector::length(&gameboard) > 0) {
let row = Vector::pop_back(&mut gameboard);
Expand All @@ -156,12 +156,12 @@ module Examples::TicTacToe {
ID::delete(id);
}

public(script) fun delete_trophy(trophy: Trophy, _ctx: &mut TxContext) {
public(script) fun delete_trophy(trophy: Trophy) {
let Trophy { id } = trophy;
ID::delete(id);
}

public(script) fun delete_cap(cap: MarkMintCap, _ctx: &mut TxContext) {
public(script) fun delete_cap(cap: MarkMintCap) {
let MarkMintCap { id, game_id: _, remaining_supply: _ } = cap;
ID::delete(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Examples::TrustedCoin {
Coin::transfer(coin, TxContext::sender(ctx));
}

public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address) {
Coin::transfer_cap<EXAMPLE>(treasury_cap, recipient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Examples::TrustedCoin {
Coin::transfer(coin, TxContext::sender(ctx));
}

public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer(treasury_cap: TreasuryCap<EXAMPLE>, recipient: address) {
Coin::transfer_cap<EXAMPLE>(treasury_cap, recipient);
}

Expand Down
12 changes: 6 additions & 6 deletions sui_programmability/examples/basics/sources/Counter.move
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Basics::Counter {
}

/// Increment a counter by 1.
public(script) fun increment(counter: &mut Counter, _ctx: &mut TxContext) {
public(script) fun increment(counter: &mut Counter) {
counter.value = counter.value + 1;
}

Expand All @@ -47,7 +47,7 @@ module Basics::Counter {
}

/// Assert a value for the counter.
public(script) fun assert_value(counter: &Counter, value: u64, _ctx: &mut TxContext) {
public(script) fun assert_value(counter: &Counter, value: u64) {
assert!(counter.value == value, 0)
}
}
Expand Down Expand Up @@ -77,9 +77,9 @@ module Basics::CounterTest {
assert!(Counter::owner(counter) == owner, 0);
assert!(Counter::value(counter) == 0, 1);

Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter);
Counter::increment(counter);
Counter::increment(counter);
TestScenario::return_shared(scenario, counter_wrapper);
};

Expand All @@ -104,7 +104,7 @@ module Basics::CounterTest {
assert!(Counter::owner(counter) == owner, 0);
assert!(Counter::value(counter) == 100, 1);

Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter);

assert!(Counter::value(counter) == 101, 2);

Expand Down
1 change: 0 additions & 1 deletion sui_programmability/examples/basics/sources/Lock.move
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ module Basics::Lock {
obj: T,
lock: &mut Lock<T>,
key: &Key<T>,
_ctx: &mut TxContext,
) {
assert!(Option::is_none(&lock.locked), ELockIsFull);
assert!(&key.for == ID::id(lock), EKeyMismatch);
Expand Down
2 changes: 0 additions & 2 deletions sui_programmability/examples/defi/sources/Escrow.move
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ module DeFi::Escrow {
public(script) fun swap<T1: key + store, T2: key + store>(
obj1: EscrowedObj<T1, T2>,
obj2: EscrowedObj<T2, T1>,
_ctx: &mut TxContext
) {
let EscrowedObj {
id: id1,
Expand Down Expand Up @@ -86,7 +85,6 @@ module DeFi::Escrow {
/// Trusted third party can always return an escrowed object to its original owner
public(script) fun return_to_sender<T: key + store, ExchangeForT: key + store>(
obj: EscrowedObj<T, ExchangeForT>,
_ctx: &mut TxContext
) {
let EscrowedObj {
id, sender, recipient: _, exchange_for: _, escrowed
Expand Down
4 changes: 2 additions & 2 deletions sui_programmability/examples/defi/sources/FlashLender.move
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module DeFi::FlashLender {

/// Allow admin to add more funds to `self`
public(script) fun deposit<T>(
self: &mut FlashLender<T>, admin_cap: &AdminCap, coin: Coin<T>, _ctx: &mut TxContext
self: &mut FlashLender<T>, admin_cap: &AdminCap, coin: Coin<T>
) {
// only the holder of the `AdminCap` for `self` can deposit funds
check_admin(self, admin_cap);
Expand All @@ -139,7 +139,7 @@ module DeFi::FlashLender {

/// Allow admin to update the fee for `self`
public(script) fun update_fee<T>(
self: &mut FlashLender<T>, admin_cap: &AdminCap, new_fee: u64, _ctx: &mut TxContext
self: &mut FlashLender<T>, admin_cap: &AdminCap, new_fee: u64
) {
// only the holder of the `AdminCap` for `self` can update the fee
check_admin(self, admin_cap);
Expand Down
9 changes: 3 additions & 6 deletions sui_programmability/examples/defi/tests/EscrowTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ module DeFi::EscrowTests {
TestScenario::next_tx(scenario, &THIRD_PARTY_ADDRESS);
{
let item_a = TestScenario::take_owned<EscrowedObj<ItemA, ItemB>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::return_to_sender<ItemA, ItemB>(item_a, ctx);
Escrow::return_to_sender<ItemA, ItemB>(item_a);

let item_b = TestScenario::take_owned<EscrowedObj<ItemB, ItemA>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::return_to_sender<ItemB, ItemA>(item_b, ctx);
Escrow::return_to_sender<ItemB, ItemA>(item_b);
};

// Alice now owns item A, and Bob now owns item B
Expand Down Expand Up @@ -84,8 +82,7 @@ module DeFi::EscrowTests {
{
let item_a = TestScenario::take_owned<EscrowedObj<ItemA, ItemB>>(scenario);
let item_b = TestScenario::take_owned<EscrowedObj<ItemB, ItemA>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::swap(item_a, item_b, ctx);
Escrow::swap(item_a, item_b);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ module FungibleTokens::MANAGED {
}

/// Manager can burn coins
public(script) fun burn(treasury_cap: &mut TreasuryCap<MANAGED>, coin: Coin<MANAGED>, _ctx: &mut TxContext) {
public(script) fun burn(treasury_cap: &mut TreasuryCap<MANAGED>, coin: Coin<MANAGED>) {
Coin::burn(coin, treasury_cap)
}

/// Manager can transfer the treasury capability to a new manager
public(script) fun transfer_cap(treasury_cap: TreasuryCap<MANAGED>, recipient: address, _ctx: &mut TxContext) {
public(script) fun transfer_cap(treasury_cap: TreasuryCap<MANAGED>, recipient: address) {
Coin::transfer_cap<MANAGED>(treasury_cap, recipient);
}

Expand Down
Loading

0 comments on commit d4428d9

Please sign in to comment.