Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[types] Add chain_id and max_gas_amount to Transaction #681

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl ExecutorActor {
let multi_chain_address_sender = tx.sender();

let resolved_sender = self.resolve_or_generate(multi_chain_address_sender.clone())?;
let authenticator = tx.authenticator_info();
let authenticator = tx.authenticator_info()?;

let mut moveos_tx = tx.construct_moveos_transaction(resolved_sender)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn test_validate() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Ecdsa)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

bitcoin_validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn test_validate() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::EcdsaRecoverable)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

ethereum_validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ fn test_validate() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Ed25519)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

native_validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ fn test_validate() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Schnorr)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

nostr_validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ fn test_validate_ed25519() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Ed25519)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

transaction_validator
Expand All @@ -55,11 +55,11 @@ fn test_validate_ecdsa() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Ecdsa)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

transaction_validator
Expand All @@ -80,11 +80,11 @@ fn test_validate_ecdsa_recoverable() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::EcdsaRecoverable)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

transaction_validator
Expand All @@ -105,11 +105,11 @@ fn test_validate_schnorr() {
let sender = keystore.addresses()[0];
let sequence_number = 0;
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Schnorr)
.unwrap();
let auth_info = tx.authenticator_info();
let auth_info = tx.authenticator_info().unwrap();
let move_tx = tx.construct_moveos_transaction(sender.into()).unwrap();

transaction_validator
Expand Down Expand Up @@ -143,7 +143,7 @@ fn test_session_key_ed25519() {
expiration_time,
max_inactive_interval,
);
let tx_data = RoochTransactionData::new(sender, sequence_number, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore
.sign_transaction(&sender, tx_data, BuiltinScheme::Ed25519)
.unwrap();
Expand All @@ -164,7 +164,7 @@ fn test_session_key_ed25519() {
// send transaction via session key

let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new(sender, sequence_number + 1, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number + 1, action);
let tx = keystore
.sign_transaction_via_session_key(&sender, tx_data, &session_auth_key)
.unwrap();
Expand All @@ -183,7 +183,7 @@ fn test_session_key_ed25519() {
.simple_serialize()
.unwrap()],
);
let tx_data = RoochTransactionData::new(sender, sequence_number + 2, action);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number + 2, action);
let tx = keystore
.sign_transaction_via_session_key(&sender, tx_data, &session_auth_key)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/rooch-framework/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This is the reference documentation of the Rooch Framework.
- [`0x3::ethereum_address`](ethereum_address.md#0x3_ethereum_address)
- [`0x3::ethereum_validator`](ethereum_validator.md#0x3_ethereum_validator)
- [`0x3::gas_coin`](gas_coin.md#0x3_gas_coin)
- [`0x3::gas_price`](gas_price.md#0x3_gas_price)
- [`0x3::genesis`](genesis.md#0x3_genesis)
- [`0x3::hash`](hash.md#0x3_hash)
- [`0x3::multi_ed25519_validator`](multi_ed25519_validator.md#0x3_multi_ed25519_validator)
Expand Down
39 changes: 39 additions & 0 deletions crates/rooch-framework/doc/gas_price.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

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

# Module `0x3::gas_price`



- [Function `get_gas_price_per_unit`](#0x3_gas_price_get_gas_price_per_unit)


<pre><code></code></pre>



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

## Function `get_gas_price_per_unit`

Returns the gas price per unit of gas.


<pre><code><b>public</b> <b>fun</b> <a href="gas_price.md#0x3_gas_price_get_gas_price_per_unit">get_gas_price_per_unit</a>(): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="gas_price.md#0x3_gas_price_get_gas_price_per_unit">get_gas_price_per_unit</a>(): u64 {
//TODO we should provide a algorithm <b>to</b> cordanate the gas price based on the network throughput
<b>return</b> 1
}
</code></pre>



</details>
16 changes: 13 additions & 3 deletions crates/rooch-framework/doc/transaction_validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
<pre><code><b>use</b> <a href="">0x1::error</a>;
<b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x2::storage_context</a>;
<b>use</b> <a href="">0x2::tx_result</a>;
<b>use</b> <a href="account.md#0x3_account">0x3::account</a>;
<b>use</b> <a href="account_authentication.md#0x3_account_authentication">0x3::account_authentication</a>;
<b>use</b> <a href="address_mapping.md#0x3_address_mapping">0x3::address_mapping</a>;
<b>use</b> <a href="auth_validator.md#0x3_auth_validator">0x3::auth_validator</a>;
<b>use</b> <a href="auth_validator_registry.md#0x3_auth_validator_registry">0x3::auth_validator_registry</a>;
<b>use</b> <a href="builtin_validators.md#0x3_builtin_validators">0x3::builtin_validators</a>;
<b>use</b> <a href="gas_price.md#0x3_gas_price">0x3::gas_price</a>;
<b>use</b> <a href="session_key.md#0x3_session_key">0x3::session_key</a>;
</code></pre>

Expand Down Expand Up @@ -131,7 +133,7 @@ This function is for Rooch to validate the transaction sender's authenticator.
If the authenticator is invaid, abort this function.


<pre><code><b>public</b> <b>fun</b> <a href="transaction_validator.md#0x3_transaction_validator_validate">validate</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, tx_sequence_number: u64, scheme: u64, authenticator_payload: <a href="">vector</a>&lt;u8&gt;): <a href="auth_validator.md#0x3_auth_validator_TxValidateResult">auth_validator::TxValidateResult</a>
<pre><code><b>public</b> <b>fun</b> <a href="transaction_validator.md#0x3_transaction_validator_validate">validate</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, _chain_id: u64, scheme: u64, authenticator_payload: <a href="">vector</a>&lt;u8&gt;): <a href="auth_validator.md#0x3_auth_validator_TxValidateResult">auth_validator::TxValidateResult</a>
</code></pre>


Expand All @@ -142,12 +144,16 @@ If the authenticator is invaid, abort this function.

<pre><code><b>public</b> <b>fun</b> <a href="transaction_validator.md#0x3_transaction_validator_validate">validate</a>(
ctx: &StorageContext,
tx_sequence_number: u64,
_chain_id: u64,
scheme: u64,
authenticator_payload: <a href="">vector</a>&lt;u8&gt;
): TxValidateResult {
// === validate the sequence number ===

// === validate the chain id ===
//TODO validate the chain id

// === validate the sequence number ===
<b>let</b> tx_sequence_number = <a href="_sequence_number">storage_context::sequence_number</a>(ctx);
<b>assert</b>!(
(tx_sequence_number <b>as</b> u128) &lt; <a href="transaction_validator.md#0x3_transaction_validator_MAX_U64">MAX_U64</a>,
<a href="_out_of_range">error::out_of_range</a>(<a href="transaction_validator.md#0x3_transaction_validator_EValidateSequenceNumberTooBig">EValidateSequenceNumberTooBig</a>)
Expand All @@ -166,6 +172,10 @@ If the authenticator is invaid, abort this function.
<a href="_invalid_argument">error::invalid_argument</a>(<a href="transaction_validator.md#0x3_transaction_validator_EValidateSequenceNumberTooNew">EValidateSequenceNumberTooNew</a>)
);

// === validate gas ===
<b>let</b> _max_gas_amount = <a href="_max_gas_amount">storage_context::max_gas_amount</a>(ctx);
//TODO check the <a href="account.md#0x3_account">account</a> can pay the gas fee

// === validate the authenticator ===

// <b>if</b> the authenticator authenticator_payload is session key, validate the session key
Expand Down
1 change: 1 addition & 0 deletions crates/rooch-framework/sources/gas_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ module rooch_framework::gas_coin {
let MintCapStore { mint_cap } = account_storage::global_move_from<MintCapStore>(ctx,@rooch_framework);
coin::destroy_mint_cap(mint_cap);
}

}
8 changes: 8 additions & 0 deletions crates/rooch-framework/sources/gas_price.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module rooch_framework::gas_price {

/// Returns the gas price per unit of gas.
public fun get_gas_price_per_unit(): u64 {
//TODO we should provide a algorithm to cordanate the gas price based on the network throughput
return 1
}
}
27 changes: 22 additions & 5 deletions crates/rooch-framework/sources/transaction_validator.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module rooch_framework::transaction_validator {
use std::error;
use std::option;
use moveos_std::storage_context::{Self, StorageContext};
use moveos_std::tx_result;
use rooch_framework::account;
use rooch_framework::address_mapping::{Self, MultiChainAddress};
use rooch_framework::account_authentication;
use rooch_framework::auth_validator::{Self, TxValidateResult};
use rooch_framework::auth_validator_registry;
use rooch_framework::session_key;
use rooch_framework::gas_price;

const MAX_U64: u128 = 18446744073709551615;

Expand Down Expand Up @@ -36,12 +38,16 @@ module rooch_framework::transaction_validator {
/// If the authenticator is invaid, abort this function.
public fun validate(
ctx: &StorageContext,
tx_sequence_number: u64,
_chain_id: u64,
scheme: u64,
authenticator_payload: vector<u8>
): TxValidateResult {
// === validate the sequence number ===

// === validate the chain id ===
//TODO validate the chain id

// === validate the sequence number ===
let tx_sequence_number = storage_context::sequence_number(ctx);
assert!(
(tx_sequence_number as u128) < MAX_U64,
error::out_of_range(EValidateSequenceNumberTooBig)
Expand All @@ -60,6 +66,10 @@ module rooch_framework::transaction_validator {
error::invalid_argument(EValidateSequenceNumberTooNew)
);

// === validate gas ===
let _max_gas_amount = storage_context::max_gas_amount(ctx);
//TODO check the account can pay the gas fee

// === validate the authenticator ===

// if the authenticator authenticator_payload is session key, validate the session key
Expand Down Expand Up @@ -101,7 +111,11 @@ module rooch_framework::transaction_validator {
if (!address_mapping::exists_mapping(ctx, multichain_address)) {
address_mapping::bind_no_check(ctx, sender, multichain_address);
};
}
};
let max_gas_amount = storage_context::max_gas_amount(ctx);
let gas_price = gas_price::get_gas_price_per_unit();
let _gas = max_gas_amount * gas_price;
//TODO prepare the gas coin
}

/// Transaction post_execute function.
Expand All @@ -110,8 +124,7 @@ module rooch_framework::transaction_validator {
fun post_execute(
ctx: &mut StorageContext,
) {
//TODO handle transaction gas fee


// Active the session key

let session_key_opt = auth_validator::get_session_key_from_tx_ctx_option(ctx);
Expand All @@ -122,5 +135,9 @@ module rooch_framework::transaction_validator {

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

let tx_result = storage_context::tx_result(ctx);
let _gas_used = tx_result::gas_used(&tx_result);
//TODO Charge gas fee and return remaining gas
}
}
Binary file modified crates/rooch-genesis/genesis/genesis
Binary file not shown.
9 changes: 8 additions & 1 deletion crates/rooch-rpc-client/src/client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rooch_config::rpc::server_config::ServerConfig;
use rooch_config::Config;
use rooch_key::keystore::{AccountKeystore, Keystore};
use rooch_types::address::RoochAddress;
use rooch_types::chain_id::ChainID;
use serde::Deserialize;
use serde::Serialize;
use serde_with::serde_as;
Expand Down Expand Up @@ -63,6 +64,7 @@ impl ClientConfig {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Env {
pub chain_id: u64,
pub alias: String,
pub rpc: String,
pub ws: Option<String>,
Expand Down Expand Up @@ -91,6 +93,7 @@ impl Env {
impl Default for Env {
fn default() -> Self {
Env {
chain_id: ChainID::Dev as u64,
alias: "default".to_string(),
rpc: ServerConfig::default().url(false),
ws: None,
Expand All @@ -101,7 +104,11 @@ impl Default for Env {
impl Display for Env {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut writer = String::new();
writeln!(writer, "Active environment : {}", self.alias)?;
writeln!(
writer,
"Active environment : {}, ChainID: {}",
self.alias, self.chain_id
)?;
write!(writer, "RPC URL: {}", self.rpc)?;
if let Some(ws) = &self.ws {
writeln!(writer)?;
Expand Down
Loading
Loading