Skip to content

Commit

Permalink
Introduce Package to store modules. (#1721)
Browse files Browse the repository at this point in the history
* refactor module_store from move_module

* add packages in module store

* reset stdlib to version 1

* packages as child objects of ModuleStore

* package ids as ModuleStore fields

* infer package_obj_id on the fly

* update object runtime for publishing and loading modules

* fix get_module of ModuleResolver

* recover released stdlib

* framework release build do not interrupt

* [state] Fix AccessPath for module and add more log for debug

* release stdlib v3

* [testsuit] Refactor debug info of integration runner and fix testcase

* fix format

---------

Co-authored-by: jolestar <[email protected]>
  • Loading branch information
pause125 and jolestar authored May 25, 2024
1 parent d212ac4 commit 945eec1
Show file tree
Hide file tree
Showing 41 changed files with 960 additions and 660 deletions.
87 changes: 54 additions & 33 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use rooch_types::framework::transaction_validator::TransactionValidator;
use rooch_types::framework::{system_post_execute_functions, system_pre_execute_functions};
use rooch_types::multichain_id::RoochMultiChainID;
use rooch_types::transaction::{AuthenticatorInfo, L1Block, L1BlockWithBody, RoochTransaction};
use tracing::{debug, warn};

pub struct ExecutorActor {
root: RootObjectEntity,
Expand Down Expand Up @@ -158,48 +159,68 @@ impl ExecutorActor {
}
}

pub fn validate_l2_tx(&self, tx: RoochTransaction) -> Result<VerifiedMoveOSTransaction> {
pub fn validate_l2_tx(&self, mut tx: RoochTransaction) -> Result<VerifiedMoveOSTransaction> {
debug!("executor validate_l2_tx: {:?}", tx.tx_hash());
let sender = tx.sender();
let tx_hash = tx.tx_hash();

let authenticator = tx.authenticator_info()?;
let authenticator = tx.authenticator_info();

let mut moveos_tx: MoveOSTransaction = tx.into_moveos_transaction(self.root.clone());
let result = self.validate_authenticator(&moveos_tx.ctx, authenticator);
match result {
Ok(vm_result) => match vm_result {
Ok((
tx_validate_result,
multi_chain_address,
pre_execute_functions,
post_execute_functions,
)) => {
// Add the original multichain address to the context
moveos_tx
.ctx
.add(multi_chain_address.unwrap_or(sender.into()))
.expect("add sender to context failed");

let vm_result = self.validate_authenticator(&moveos_tx.ctx, authenticator)?;
// Add the tx_validate_result to the context
moveos_tx
.ctx
.add(tx_validate_result)
.expect("add tx_validate_result failed");

match vm_result {
Ok((
tx_validate_result,
multi_chain_address,
pre_execute_functions,
post_execute_functions,
)) => {
// Add the original multichain address to the context
moveos_tx
.ctx
.add(multi_chain_address.unwrap_or(sender.into()))
.expect("add sender to context failed");

// Add the tx_validate_result to the context
moveos_tx
.ctx
.add(tx_validate_result)
.expect("add tx_validate_result failed");

moveos_tx.append_pre_execute_functions(pre_execute_functions);
moveos_tx.append_post_execute_functions(post_execute_functions);
Ok(self.moveos().verify(moveos_tx)?)
}
moveos_tx.append_pre_execute_functions(pre_execute_functions);
moveos_tx.append_post_execute_functions(post_execute_functions);
let verify_result = self.moveos.verify(moveos_tx);
match verify_result {
Ok(verified_tx) => Ok(verified_tx),
Err(e) => {
log::warn!(
"transaction verify vm error, tx_hash: {}, error:{:?}",
tx_hash,
e
);
Err(e.into())
}
}
}
Err(e) => {
let resolver = RootObjectResolver::new(self.root.clone(), &self.moveos_store);
let status_view = explain_vm_status(&resolver, e.clone())?;
warn!(
"transaction validate vm error, tx_hash: {}, error:{:?}",
tx_hash, status_view,
);
//TODO how to return the vm status to rpc client.
Err(e.into())
}
},
Err(e) => {
let resolver = RootObjectResolver::new(self.root.clone(), &self.moveos_store);
let status_view = explain_vm_status(&resolver, e.clone())?;
log::warn!(
"transaction validate vm error, tx_hash: {}, error:{:?}",
moveos_tx.ctx.tx_hash(),
status_view,
"transaction validate error, tx_hash: {}, error:{:?}",
tx_hash,
e
);
//TODO how to return the vm status to rpc client.
Err(e.into())
Err(e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn test_validate() {
let action = MoveAction::new_function_call(Empty::empty_function_id(), vec![], vec![]);
let tx_data = RoochTransactionData::new_for_test(sender, sequence_number, action);
let tx = keystore.sign_transaction(&sender, tx_data, None).unwrap();
let auth_info = tx.authenticator_info().unwrap();
let auth_info = tx.authenticator_info();

let move_tx: MoveOSTransaction = tx.into_moveos_transaction(root);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module test::m {
//# run --signers test
script {
use std::string::{Self};
use moveos_std::move_module;
use moveos_std::module_store;

fun main() {
let module_store = borrow_module_store();
assert!(move_module::exists_module(module_store, @moveos_std, string::utf8(b"move_module")), 0);
assert!(move_module::exists_module(module_store, @test, string::utf8(b"m")), 1);
assert!(module_store::exists_module(module_store, @moveos_std, string::utf8(b"module_store")), 0);
assert!(module_store::exists_module(module_store, @test, string::utf8(b"m")), 1);
}
}
25 changes: 24 additions & 1 deletion crates/rooch-genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,13 @@ impl RoochGenesis {
#[cfg(test)]
mod tests {
use crate::FrameworksGasParameters;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::ModuleId;
use move_core_types::resolver::ModuleResolver;
use moveos_store::MoveOSStore;
use moveos_types::moveos_std::move_module::ModuleStore;
use moveos_types::moveos_std::module_store::{ModuleStore, Package};
use moveos_types::state_resolver::{RootObjectResolver, StateResolver};
use rooch_framework::ROOCH_FRAMEWORK_ADDRESS;
use rooch_store::RoochStore;
use rooch_types::bitcoin::network::BitcoinNetwork;
use rooch_types::rooch_network::RoochNetwork;
Expand Down Expand Up @@ -374,6 +378,25 @@ mod tests {
module_store_obj.size > 0,
"module store fields size should > 0"
);

let package_object_state = resolver
.get_object(&Package::package_id(&ROOCH_FRAMEWORK_ADDRESS))
.unwrap();
assert!(package_object_state.is_some());
let package_obj = package_object_state
.unwrap()
.into_object::<Package>()
.unwrap();
assert!(package_obj.size > 0, "package fields size should > 0");

let module = resolver
.get_module(&ModuleId::new(
ROOCH_FRAMEWORK_ADDRESS,
Identifier::new("genesis").unwrap(),
))
.unwrap();
assert!(module.is_some(), "genesis module should exist");

let chain_id_state = resolver
.get_object(&rooch_types::framework::chain_id::ChainID::chain_id_object_id())
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-integration-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'a> MoveOSTestAdapter<'a> for MoveOSTestRunner<'a> {
FunctionId::new(
ModuleId::new(
MOVEOS_STD_ADDRESS,
Identifier::new("move_module".to_owned()).unwrap(),
Identifier::new("module_store".to_owned()).unwrap(),
),
Identifier::new("publish_modules_entry".to_owned()).unwrap(),
),
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@
"type": {
"type": "string",
"enum": [
"nomarl"
"normal"
]
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/rooch-pipeline-processor/src/actor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rooch_sequencer::proxy::SequencerProxy;
use rooch_types::transaction::{
ExecuteTransactionResponse, L1BlockWithBody, LedgerTransaction, LedgerTxData, RoochTransaction,
};
use tracing::debug;

/// PipelineProcessor aggregates the executor, sequencer, proposer, and indexer to process transactions.
pub struct PipelineProcessorActor {
Expand Down Expand Up @@ -61,8 +62,9 @@ impl PipelineProcessorActor {

pub async fn execute_l2_tx(
&mut self,
tx: RoochTransaction,
mut tx: RoochTransaction,
) -> Result<ExecuteTransactionResponse> {
debug!("pipeline execute_l2_tx: {:?}", tx.tx_hash());
let moveos_tx = self.executor.validate_l2_tx(tx.clone()).await?;
let ledger_tx = self
.sequencer
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ pub enum FieldChangeView {
#[serde(flatten)]
change: ObjectChangeView,
},
Nomarl {
Normal {
key: KeyStateView,
#[serde(flatten)]
change: NormalFieldChangeView,
Expand All @@ -290,7 +290,7 @@ impl From<(KeyState, FieldChange)> for FieldChangeView {
key: key.into(),
change: object_change.into(),
},
FieldChange::Normal(normal_field_change) => Self::Nomarl {
FieldChange::Normal(normal_field_change) => Self::Normal {
key: key.into(),
change: normal_field_change.into(),
},
Expand Down
5 changes: 1 addition & 4 deletions crates/rooch-rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ impl ModuleResolver for &Client {
fn get_module(&self, id: &ModuleId) -> Result<Option<Vec<u8>>> {
tokio::task::block_in_place(|| {
Handle::current().block_on(async {
let mut states = self
.rooch
.get_states(AccessPath::module(*id.address(), id.name().to_owned()))
.await?;
let mut states = self.rooch.get_states(AccessPath::module(id)).await?;
states
.pop()
.flatten()
Expand Down
7 changes: 1 addition & 6 deletions crates/rooch-rpc-server/src/service/rpc_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ impl RpcService {
}

pub async fn exists_module(&self, module_id: ModuleId) -> Result<bool> {
let mut resp = self
.get_states(AccessPath::module(
*module_id.address(),
module_id.name().into(),
))
.await?;
let mut resp = self.get_states(AccessPath::module(&module_id)).await?;
Ok(resp.pop().flatten().is_some())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-types/src/framework/transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ impl<'a> TransactionValidator<'a> {
.unwrap(),
],
);
let auth_validator =
let function_result =
self.caller
.call_function(ctx, tx_validator_call)?
.decode(|mut values| {
let value = values.pop().expect("should have one return value");
let result = bcs::from_bytes::<TxValidateResult>(&value.value)?;
Ok(result)
})?;
Ok(auth_validator)
Ok(function_result)
}

pub fn pre_execute_function_id() -> FunctionId {
Expand Down
8 changes: 2 additions & 6 deletions crates/rooch-types/src/transaction/rooch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl RoochTransaction {
bcs::to_bytes(self).expect("encode transaction should success")
}

//TODO unify the hash function
pub fn tx_hash(&mut self) -> H256 {
if let Some(hash) = self.data_hash {
hash
Expand All @@ -147,11 +146,8 @@ impl RoochTransaction {
}
}

pub fn authenticator_info(&self) -> Result<AuthenticatorInfo> {
Ok(AuthenticatorInfo::new(
self.chain_id(),
self.authenticator.clone(),
))
pub fn authenticator_info(&self) -> AuthenticatorInfo {
AuthenticatorInfo::new(self.chain_id(), self.authenticator.clone())
}

pub fn authenticator(&self) -> &Authenticator {
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch/src/commands/move_cli/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct Publish {

/// Whether publish modules by `MoveAction::ModuleBundle`?
/// If not set, publish moduels through Move entry function
/// `moveos_std::move_module::publish_modules_entry`.
/// `moveos_std::module_store::publish_modules_entry`.
/// **Deprecated**! Publish modules by `MoveAction::ModuleBundle` is no longer used anymore.
/// So you should never add this option.
/// For now, the option is kept for test only.
Expand Down Expand Up @@ -128,7 +128,7 @@ impl CommandAction<ExecuteTransactionResponseView> for Publish {
FunctionId::new(
ModuleId::new(
MOVEOS_STD_ADDRESS,
Identifier::new("move_module".to_owned()).unwrap(),
Identifier::new("module_store".to_owned()).unwrap(),
),
Identifier::new("publish_modules_entry".to_owned()).unwrap(),
),
Expand Down
8 changes: 4 additions & 4 deletions crates/testsuite/features/cmd.feature
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Feature: Rooch CLI integration tests
Then assert: "{{$.rpc[-1].data[0].object_type}} == 0x3::coin::CoinInfo"
Then assert: "{{$.rpc[-1].has_next_page}} == false"

Then cmd: "rpc request --method rooch_queryFieldStates --params '[{"object_id":"0x3"}, null, "10", {"descending": true,"showDisplay":false}]'"
Then cmd: "rpc request --method rooch_queryFieldStates --params '[{"object_id":"{{$.address_mapping.default}}"}, null, "10", {"descending": true,"showDisplay":false}]'"
Then assert: "{{$.rpc[-1].has_next_page}} == false"

# Then cmd: "rpc request --method rooch_syncStates --params '[null, null, "2", false]'"
Expand All @@ -154,9 +154,9 @@ Feature: Rooch CLI integration tests
Then assert: "{{$.move[-1].vm_status}} == Executed"
Then assert: "{{$.move[-1].return_values[0].decoded_value}} == value1"
#the access-path argument do not support named address yet, so, we use `{{$.address_mapping.default}}` template var to repleace it.
Then cmd: "state --access-path /resource/{{$.address_mapping.default}}/{{$.address_mapping.default}}::kv_store::KVStore
Then cmd: "state --access-path /table/{{$.state[-1][0].decoded_value.value.table.value.handle}}/key1"
Then assert: "{{$.state[-1][0].decoded_value}} == "value1""
Then cmd: "state --access-path /resource/{{$.address_mapping.default}}/{{$.address_mapping.default}}::kv_store::KVStore"
Then cmd: "state --access-path /fields/{{$.state[-1][0].decoded_value.value.table.value.handle.value.id}}/key1"
Then assert: "{{$.state[-1][0].decoded_value}} == value1"


Then stop the server
Expand Down
25 changes: 17 additions & 8 deletions crates/testsuite/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use testcontainers::{
core::{Container, ExecCommand, WaitFor},
RunnableImage,
};
use tracing::{debug, error, info};
use tracing::{debug, error, info, trace};
use uuid::Uuid;

const RPC_USER: &str = "roochuser";
Expand Down Expand Up @@ -222,23 +222,32 @@ async fn run_cmd(world: &mut World, args: String) {
args.push(config_dir.to_str().unwrap().to_string());
let opts: RoochCli = RoochCli::parse_from(args);
let ret = rooch::run_cli(opts).await;
debug!("run_cli result: {:?}", ret);
match ret {
Ok(output) => {
let result_json = serde_json::from_str::<Value>(&output);

if result_json.is_ok() {
tpl_ctx
.entry(cmd_name)
.append::<Value>(result_json.unwrap());
match result_json {
Ok(result_json) => {
debug!(
"cmd: {} output json: {:#}",
cmd_name,
result_json.to_string()
);
tpl_ctx.entry(cmd_name).append::<Value>(result_json);
}
Err(_err) => {
debug!("cmd: {} output string: {}", cmd_name, output);
let output = Value::String(output);
tpl_ctx.entry(cmd_name).append::<Value>(output);
}
}
}
Err(err) => {
debug!("cmd: {} output err: {}", cmd_name, err.to_string());
let err_msg = Value::String(err.to_string());
tpl_ctx.entry(cmd_name).append::<Value>(err_msg);
}
}
debug!("current tpl_ctx: {:?}", tpl_ctx);
trace!("current tpl_ctx: {:#}", tpl_ctx.as_value());
}

#[then(regex = r#"cmd ord bash: "(.*)?""#)]
Expand Down
4 changes: 2 additions & 2 deletions examples/module_template/sources/coin_factory.move
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ module rooch_examples::coin_factory {
// let old_decimal = vector::singleton(TEMPLATE_COIN_DECIMALS);
// let modules = move_module::replace_constant_u8(modules, old_decimal, new_decimal);

// let module_store = move_module::borrow_mut_module_store();
// let module_store = module_store::borrow_mut_module_store();
// // publish modules
// move_module::publish_modules(module_store, account, modules);
// module_store::publish_modules(module_store, account, modules);
// }
}
Loading

0 comments on commit 945eec1

Please sign in to comment.