Skip to content

Commit

Permalink
Check the Gas Free function during the compilation of the Move module. (
Browse files Browse the repository at this point in the history
#886)

* Check the Gas Free function during the compilation of the Move module.

* Check the gas_free function during module publishing.

* fix the ordering of verifier function

* execute the gas_validate and gas_charge_post function when use tx has been executed

* Add gas_free function to deduct module account fees and update test code.

* Fix error handling and code reuse.


* Report an error indicating that the definition of the gas_free function is incomplete.

* Fix the checking logic.

* Resolve the issue with executing the gas_free callback function.

---------

Co-authored-by: stevenlaw123 <[email protected]>
  • Loading branch information
stevenlaw123 and stevenlaw123 authored Oct 25, 2023
1 parent 5418339 commit aa7658a
Show file tree
Hide file tree
Showing 9 changed files with 955 additions and 12 deletions.
7 changes: 4 additions & 3 deletions crates/rooch-framework/sources/transaction_validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module rooch_framework::transaction_validator {
fun post_execute(
ctx: &mut Context,
) {
let sender = context::sender(ctx);
// let sender = context::sender(ctx);

// Active the session key

Expand All @@ -161,11 +161,12 @@ module rooch_framework::transaction_validator {

// Increment sequence number
account::increment_sequence_number(ctx);

let tx_result = context::tx_result(ctx);
let gas_payment_account = tx_result::gas_payment_account(&tx_result);
let gas_used = tx_result::gas_used(&tx_result);
let gas = transaction_fee::calculate_gas(ctx, gas_used);
let gas_coin = gas_coin::deduct_gas(ctx, sender, gas);
let gas_coin = gas_coin::deduct_gas(ctx, gas_payment_account, gas);
transaction_fee::deposit_fee(ctx, gas_coin);
}
}
14 changes: 14 additions & 0 deletions examples/gas_free/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "gas_payer"
version = "0.0.1"

[dependencies]
MoveosStdlib = { local = "../../moveos/moveos-stdlib/moveos-stdlib" }

[addresses]
rooch_examples = "_"
moveos_std = "0x2"
rooch_framework = "0x3"

[dev-addresses]
rooch_examples = "0x42"
15 changes: 15 additions & 0 deletions examples/gas_free/sources/gas_free.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module rooch_examples::gas_payer {
use moveos_std::context::Context;

fun gas_validate_function(_ctx: &Context): bool{
true
}

fun gas_charge_post_function(_ctx: &mut Context, _gas_used: u128): bool {
true
}

#[gas_free(gas_validate=gas_validate_function, gas_charge_post=gas_charge_post_function)]
public entry fun play(_ctx: &mut Context, _sender: &signer){
}
}
31 changes: 31 additions & 0 deletions moveos/moveos-stdlib/moveos-stdlib/doc/tx_result.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [Struct `TxResult`](#0x2_tx_result_TxResult)
- [Function `is_executed`](#0x2_tx_result_is_executed)
- [Function `gas_used`](#0x2_tx_result_gas_used)
- [Function `gas_payment_account`](#0x2_tx_result_gas_payment_account)


<pre><code></code></pre>
Expand Down Expand Up @@ -45,6 +46,12 @@ We can get the result in the <code>post_execute</code> function.
<dd>
The gas used by the transaction.
</dd>
<dt>
<code>gas_payment_account: <b>address</b></code>
</dt>
<dd>
The account for the gas payment.
</dd>
</dl>


Expand Down Expand Up @@ -96,4 +103,28 @@ We can get the result in the <code>post_execute</code> function.



</details>

<a name="0x2_tx_result_gas_payment_account"></a>

## Function `gas_payment_account`



<pre><code><b>public</b> <b>fun</b> <a href="tx_result.md#0x2_tx_result_gas_payment_account">gas_payment_account</a>(self: &<a href="tx_result.md#0x2_tx_result_TxResult">tx_result::TxResult</a>): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="tx_result.md#0x2_tx_result_gas_payment_account">gas_payment_account</a>(self: &<a href="tx_result.md#0x2_tx_result_TxResult">TxResult</a>): <b>address</b> {
self.gas_payment_account
}
</code></pre>



</details>
6 changes: 6 additions & 0 deletions moveos/moveos-stdlib/moveos-stdlib/sources/tx_result.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module moveos_std::tx_result {
executed: bool,
/// The gas used by the transaction.
gas_used: u64,
/// The account for the gas payment.
gas_payment_account: address,
}

public fun is_executed(self: &TxResult) : bool {
Expand All @@ -21,4 +23,8 @@ module moveos_std::tx_result {
self.gas_used
}

public fun gas_payment_account(self: &TxResult): address {
self.gas_payment_account
}

}
5 changes: 4 additions & 1 deletion moveos/moveos-types/src/moveos_std/tx_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const MODULE_NAME: &IdentStr = ident_str!("tx_result");
pub struct TxResult {
pub executed: bool,
pub gas_used: u64,
pub gas_payment_account: AccountAddress,
}

impl MoveStructType for TxResult {
Expand All @@ -29,15 +30,17 @@ impl MoveStructState for TxResult {
move_core_types::value::MoveStructLayout::new(vec![
move_core_types::value::MoveTypeLayout::Bool,
move_core_types::value::MoveTypeLayout::U64,
move_core_types::value::MoveTypeLayout::Address,
])
}
}

impl TxResult {
pub fn new(status: &KeptVMStatus, gas_used: u64) -> Self {
pub fn new(status: &KeptVMStatus, gas_used: u64, gas_payment_account: AccountAddress) -> Self {
Self {
executed: matches!(status, KeptVMStatus::Executed),
gas_used,
gas_payment_account,
}
}
}
Loading

0 comments on commit aa7658a

Please sign in to comment.