Skip to content

Commit

Permalink
[framework] refactor gas relative module
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed May 14, 2024
1 parent a7dd12b commit c4d6f72
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 127 deletions.
4 changes: 4 additions & 0 deletions crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl FrameworksGasParameters {
)
}

pub fn load_from_gas_config(gas_config: &GasScheduleConfig) -> Result<Self> {
Self::load_from_gas_entries(gas_config.max_gas_amount, gas_config.entries.clone())
}

pub fn load_from_gas_entries(max_gas_amount: u64, entries: Vec<GasEntry>) -> Result<Self> {
let entries = entries
.into_iter()
Expand Down
4 changes: 3 additions & 1 deletion frameworks/bitcoin-move/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ use rooch_framework::natives::gas_parameter::gas_member::{
FromOnChainGasSchedule, ToOnChainGasSchedule,
};
use rooch_types::addresses::BITCOIN_MOVE_ADDRESS;
use serde::Deserialize;
use serde::Serialize;
use std::collections::BTreeMap;

mod gas_parameter;
pub mod ord;

#[derive(Debug, Clone)]
#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
pub struct GasParameters {
ord: ord::GasParameters,
}
Expand Down
5 changes: 3 additions & 2 deletions frameworks/bitcoin-move/src/natives/ord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ use move_vm_types::{
use moveos_stdlib::natives::helpers::{make_module_natives, make_native};
use moveos_types::state::{MoveState, MoveType};
use rooch_types::bitcoin::types::Witness;
use serde::{Deserialize, Serialize};
use smallvec::smallvec;
use std::collections::VecDeque;
use tracing::error;
use {envelope::ParsedEnvelope, envelope::RawEnvelope, inscription::Inscription};

#[derive(Debug, Clone)]
#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
pub struct FromWitnessGasParameters {
pub base: InternalGas,
pub per_byte: InternalGasPerByte,
Expand Down Expand Up @@ -91,7 +92,7 @@ pub(crate) fn native_from_witness(
Ok(NativeResult::ok(cost, smallvec![val]))
}

#[derive(Debug, Clone)]
#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
pub struct GasParameters {
pub from_witness: FromWitnessGasParameters,
}
Expand Down
12 changes: 12 additions & 0 deletions frameworks/rooch-framework/doc/chain_id.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Function `chain_id`](#0x3_chain_id_chain_id)
- [Function `is_local`](#0x3_chain_id_is_local)
- [Function `is_dev`](#0x3_chain_id_is_dev)
- [Function `is_local_or_dev`](#0x3_chain_id_is_local_or_dev)
- [Function `is_test`](#0x3_chain_id_is_test)
- [Function `is_main`](#0x3_chain_id_is_main)

Expand Down Expand Up @@ -141,6 +142,17 @@ The ChainID in the global storage



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

## Function `is_local_or_dev`



<pre><code><b>public</b> <b>fun</b> <a href="chain_id.md#0x3_chain_id_is_local_or_dev">is_local_or_dev</a>(): bool
</code></pre>



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

## Function `is_test`
Expand Down
45 changes: 3 additions & 42 deletions frameworks/rooch-framework/doc/gas_coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ This module defines Rooch Gas Coin.


- [Resource `GasCoin`](#0x3_gas_coin_GasCoin)
- [Resource `FaucetRecord`](#0x3_gas_coin_FaucetRecord)
- [Constants](#@Constants_0)
- [Function `balance`](#0x3_gas_coin_balance)
- [Function `burn`](#0x3_gas_coin_burn)
- [Function `deduct_gas`](#0x3_gas_coin_deduct_gas)
Expand All @@ -18,12 +16,11 @@ This module defines Rooch Gas Coin.


<pre><code><b>use</b> <a href="">0x1::string</a>;
<b>use</b> <a href="">0x2::account</a>;
<b>use</b> <a href="">0x2::object</a>;
<b>use</b> <a href="">0x2::signer</a>;
<b>use</b> <a href="account_coin_store.md#0x3_account_coin_store">0x3::account_coin_store</a>;
<b>use</b> <a href="coin.md#0x3_coin">0x3::coin</a>;
<b>use</b> <a href="timestamp.md#0x3_timestamp">0x3::timestamp</a>;
<b>use</b> <a href="onchain_config.md#0x3_onchain_config">0x3::onchain_config</a>;
</code></pre>


Expand All @@ -39,43 +36,6 @@ This module defines Rooch Gas Coin.



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

## Resource `FaucetRecord`

Record the last time when faucet is called for each address.


<pre><code><b>struct</b> <a href="gas_coin.md#0x3_gas_coin_FaucetRecord">FaucetRecord</a> <b>has</b> store, key
</code></pre>



<a name="@Constants_0"></a>

## Constants


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

Faucet too frequently


<pre><code><b>const</b> <a href="gas_coin.md#0x3_gas_coin_ErrorFaucetTooFrequently">ErrorFaucetTooFrequently</a>: u64 = 1;
</code></pre>



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

Faucet interval in seconds


<pre><code><b>const</b> <a href="gas_coin.md#0x3_gas_coin_FAUCET_INTERVAL">FAUCET_INTERVAL</a>: u64 = 86400;
</code></pre>



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

## Function `balance`
Expand Down Expand Up @@ -126,9 +86,10 @@ Mint gas coin to the given account.

## Function `faucet_entry`

Entry point for the faucet, Only sequencer account can call this function.


<pre><code><b>public</b> entry <b>fun</b> <a href="gas_coin.md#0x3_gas_coin_faucet_entry">faucet_entry</a>(<a href="">account</a>: &<a href="">signer</a>)
<pre><code><b>public</b> entry <b>fun</b> <a href="gas_coin.md#0x3_gas_coin_faucet_entry">faucet_entry</a>(<a href="">account</a>: &<a href="">signer</a>, amount: u256)
</code></pre>


Expand Down
12 changes: 12 additions & 0 deletions frameworks/rooch-framework/doc/onchain_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Function `update_framework_version`](#0x3_onchain_config_update_framework_version)
- [Function `framework_version`](#0x3_onchain_config_framework_version)
- [Function `onchain_config`](#0x3_onchain_config_onchain_config)
- [Function `ensure_sequencer`](#0x3_onchain_config_ensure_sequencer)
- [Function `add_to_publishing_allowlist`](#0x3_onchain_config_add_to_publishing_allowlist)
- [Function `remove_from_publishing_allowlist`](#0x3_onchain_config_remove_from_publishing_allowlist)
- [Function `change_feature_flags`](#0x3_onchain_config_change_feature_flags)
Expand Down Expand Up @@ -107,6 +108,17 @@ OnchainConfig is framework configurations stored on chain.



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

## Function `ensure_sequencer`



<pre><code><b>public</b> <b>fun</b> <a href="onchain_config.md#0x3_onchain_config_ensure_sequencer">ensure_sequencer</a>(<a href="">account</a>: &<a href="">signer</a>)
</code></pre>



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

## Function `add_to_publishing_allowlist`
Expand Down
6 changes: 3 additions & 3 deletions frameworks/rooch-framework/doc/transaction_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<pre><code><b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x2::account</a>;
<b>use</b> <a href="">0x2::gas_schedule</a>;
<b>use</b> <a href="">0x2::signer</a>;
<b>use</b> <a href="">0x2::tx_context</a>;
<b>use</b> <a href="">0x2::tx_result</a>;
Expand Down Expand Up @@ -57,12 +58,11 @@ Just using to get module signer



<a name="0x3_transaction_validator_ErrorOutOfGas"></a>
<a name="0x3_transaction_validator_ErrorMaxGasAmountExceeded"></a>

Transaction exceeded its allocated max gas


<pre><code><b>const</b> <a href="transaction_validator.md#0x3_transaction_validator_ErrorOutOfGas">ErrorOutOfGas</a>: u64 = 1;
<pre><code><b>const</b> <a href="transaction_validator.md#0x3_transaction_validator_ErrorMaxGasAmountExceeded">ErrorMaxGasAmountExceeded</a>: u64 = 1008;
</code></pre>


Expand Down
5 changes: 5 additions & 0 deletions frameworks/rooch-framework/sources/chain_id.move
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ module rooch_framework::chain_id {
chain_id() == CHAIN_ID_DEV
}

public fun is_local_or_dev() : bool {
let id = chain_id();
id == CHAIN_ID_LOCAL || id == CHAIN_ID_DEV
}

public fun is_test() : bool {
chain_id() == CHAIN_ID_TEST
}
Expand Down
35 changes: 5 additions & 30 deletions frameworks/rooch-framework/sources/gas_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,17 @@
module rooch_framework::gas_coin {
use std::string;
use moveos_std::signer;

use moveos_std::object::{Self, Object};
use moveos_std::account;
use rooch_framework::timestamp;
use rooch_framework::coin::{Self, Coin, CoinInfo};
use rooch_framework::account_coin_store;
use rooch_framework::onchain_config;

friend rooch_framework::genesis;
friend rooch_framework::transaction_validator;

//TODO should we allow user to transfer gas coin?
//If not, we can remove `store` ability from GasCoin.
struct GasCoin has key, store {}

/// Record the last time when faucet is called for each address.
struct FaucetRecord has key, store {
last_time: u64
}

/// Faucet interval in seconds
const FAUCET_INTERVAL: u64 = 24 * 60 * 60; // 1 day

/// Faucet too frequently
const ErrorFaucetTooFrequently: u64 = 1;

public fun balance(addr: address): u256 {
account_coin_store::balance<GasCoin>(addr)
}
Expand Down Expand Up @@ -71,21 +57,10 @@ module rooch_framework::gas_coin {
faucet(addr, amount);
}

public entry fun faucet_entry(account: &signer) {
//100 RGC
let amount = 100_000_000_000_000_000_000u256;
let addr = signer::address_of(account);

if (account::exists_resource<FaucetRecord>(addr)) {
let record = account::borrow_mut_resource<FaucetRecord>(addr);
assert!(timestamp::now_seconds() - record.last_time >= FAUCET_INTERVAL, ErrorFaucetTooFrequently);
record.last_time = timestamp::now_seconds();
} else {
account::move_resource_to(account, FaucetRecord {
last_time: timestamp::now_seconds()
});
};

/// Entry point for the faucet, Only sequencer account can call this function.
public entry fun faucet_entry(account: &signer, amount: u256) {
onchain_config::ensure_sequencer(account);
let addr = signer::address_of(account);
faucet(addr, amount);
}

Expand Down
14 changes: 8 additions & 6 deletions frameworks/rooch-framework/sources/onchain_config.move
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,26 @@ module rooch_framework::onchain_config {
object::borrow(obj)
}

public fun ensure_sequencer(account: &signer) {
let sender = signer::address_of(account);
assert!(sender == sequencer(), ErrorNotSequencer);
}

/****** API for update module publishing allowlist. ******/

/// When module_publishing_allowlist_feature is enabled, only address in allowlist
/// can publish modules.
/// Add `publisher` to publishing allowlist.
public entry fun add_to_publishing_allowlist(account: &signer, publisher: address) {
let sender = signer::address_of(account);
assert!(sender == sequencer(), ErrorNotSequencer);
ensure_sequencer(account);
let system_account = signer::module_signer<OnchainConfig>();
let allowlist = move_module::borrow_mut_allowlist();
move_module::add_to_allowlist(allowlist, &system_account, publisher);
}

/// Remove `publisher` from publishing allowlist.
public entry fun remove_from_publishing_allowlist(account: &signer, publisher: address) {
let sender = signer::address_of(account);
assert!(sender == sequencer(), ErrorNotSequencer);
ensure_sequencer(account);
let system_account = signer::module_signer<OnchainConfig>();
let allowlist = move_module::borrow_mut_allowlist();
move_module::remove_from_allowlist(allowlist, &system_account, publisher);
Expand All @@ -77,8 +80,7 @@ module rooch_framework::onchain_config {

/// Enable or disable features. You can find all feature flags in moveos_std::features.
public entry fun change_feature_flags(account: &signer, enable: vector<u64>, disable: vector<u64>) {
let sender = signer::address_of(account);
assert!(sender == sequencer(), ErrorNotSequencer);
ensure_sequencer(account);
let system_account = signer::module_signer<OnchainConfig>();
features::change_feature_flags(&system_account, enable, disable);
}
Expand Down
23 changes: 0 additions & 23 deletions frameworks/rooch-framework/sources/tests/gas_coin_test.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
/// This test module is used to test the gas coin
module rooch_framework::gas_coin_test{

use moveos_std::signer;
use rooch_framework::account as account_entry;
use rooch_framework::coin;
use rooch_framework::timestamp;
use rooch_framework::gas_coin::{Self, GasCoin};

#[test]
Expand Down Expand Up @@ -36,25 +34,4 @@ module rooch_framework::gas_coin_test{
assert!(gas_coin::balance(user) == init_gas, 1000);

}

#[test(sender=@0x42)]
#[expected_failure(abort_code = 1, location = rooch_framework::gas_coin)]
public fun test_faucet_interval_fail(sender: &signer) {
rooch_framework::genesis::init_for_test();
let user = signer::address_of(sender);
account_entry::create_account_for_testing(user);
gas_coin::faucet_entry(sender);
gas_coin::faucet_entry(sender);
}

#[test(sender=@0x42)]
public fun test_faucet_interval(sender: &signer) {
rooch_framework::genesis::init_for_test();
let user = signer::address_of(sender);
account_entry::create_account_for_testing(user);
gas_coin::faucet_entry(sender);
let interval = 24 * 60 * 60; // 1 day
timestamp::update_global_time_for_test_secs(timestamp::now_seconds() + interval);
gas_coin::faucet_entry(sender);
}
}
Loading

0 comments on commit c4d6f72

Please sign in to comment.