From f981a9f71035e23bbdad093f86eda77bfb3878f2 Mon Sep 17 00:00:00 2001 From: welbon <2261238+welbon@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:24:44 +0800 Subject: [PATCH] [compiler-v2 framework] Add integration test for test block meta data deserialize code --- executor/tests/block_test.rs | 38 +++++++++++++++++++ .../src/lib.rs | 1 - .../src/on_chain_resource/block_metadata.rs | 17 +++++---- vm/types/src/on_chain_resource/global_time.rs | 20 +++++++--- vm/types/src/state_view.rs | 16 ++++++++ 5 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 executor/tests/block_test.rs diff --git a/executor/tests/block_test.rs b/executor/tests/block_test.rs new file mode 100644 index 0000000000..8b47ada581 --- /dev/null +++ b/executor/tests/block_test.rs @@ -0,0 +1,38 @@ +use starcoin_types::account_address::AccountAddress; +use starcoin_vm_types::{ + event::{EventHandle, EventKey}, + on_chain_resource::BlockMetadata, + state_view::StateReaderExt, +}; +use starcoin_vm_types::account_config::genesis_address; +use test_helper::executor::prepare_genesis; + +#[stest::test] +fn test_block_metadata_bcs_deserialize() -> anyhow::Result<()> { + let (chain_state, _net) = prepare_genesis(); + + let block_metadata = BlockMetadata { + number: 0, + parent_hash: Default::default(), + author: AccountAddress::ONE, + uncles: 0, + parents_hash: vec![], + new_block_events: EventHandle::new(EventKey::new(1, AccountAddress::ONE), 1), + }; + let bcs_block_metadata = bcs_ext::to_bytes(&block_metadata)?; + println!("block_metadata: {:?}, length: {}", bcs_block_metadata, bcs_block_metadata.len()); + + let onchain_data = chain_state.get_resource_type_bytes::(genesis_address())?; + println!("onchain block_metadata: {:?}, data len: {}", onchain_data.to_vec(), onchain_data.len()); + + assert_eq!(bcs_block_metadata.len(), onchain_data.len()); + + //let output = bcs_ext::from_bytes::(bcs.as_slice())?; + // assert_eq!(output.number, block_metadata.number); + + // let data = chain_state.get_resource_type::(genesis_address())?; + // assert_ne!(data.number, 0); + // assert!(!block_metadata.number > 0); + + Ok(()) +} \ No newline at end of file diff --git a/vm/starcoin-transactional-test-harness/src/lib.rs b/vm/starcoin-transactional-test-harness/src/lib.rs index f203c7c398..172a3766f7 100644 --- a/vm/starcoin-transactional-test-harness/src/lib.rs +++ b/vm/starcoin-transactional-test-harness/src/lib.rs @@ -4,7 +4,6 @@ use crate::context::ForkContext; use anyhow::{bail, format_err, Result}; use clap::{Args, CommandFactory, Parser}; -use log::info; use move_binary_format::{file_format::CompiledScript, CompiledModule}; use move_command_line_common::address::ParsedAddress; use move_command_line_common::files::verify_and_create_named_address_mapping; diff --git a/vm/types/src/on_chain_resource/block_metadata.rs b/vm/types/src/on_chain_resource/block_metadata.rs index e9d7191d42..bb5fd42f36 100644 --- a/vm/types/src/on_chain_resource/block_metadata.rs +++ b/vm/types/src/on_chain_resource/block_metadata.rs @@ -2,15 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 use crate::event::EventHandle; -use move_core_types::account_address::AccountAddress; -use move_core_types::ident_str; -use move_core_types::identifier::IdentStr; -use move_core_types::move_resource::{MoveResource, MoveStructType}; +use move_core_types::{ + account_address::AccountAddress, + ident_str, + identifier::IdentStr, + move_resource::{MoveResource, MoveStructType}, +}; use serde::{Deserialize, Serialize}; use starcoin_crypto::HashValue; /// On chain resource BlockMetadata mapping for FlexiDag block -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct BlockMetadata { // number of the current block pub number: u64, @@ -18,11 +20,12 @@ pub struct BlockMetadata { pub parent_hash: HashValue, // Author of the current block. pub author: AccountAddress, + // Uncle blocks number pub uncles: u64, + // Parents hash for DAG + pub parents_hash: Vec, // Handle where events with the time of new blocks are emitted pub new_block_events: EventHandle, - // An Array of the parents hash for a Dag block. - pub parents_hash: Vec, } impl BlockMetadata { diff --git a/vm/types/src/on_chain_resource/global_time.rs b/vm/types/src/on_chain_resource/global_time.rs index 18329c8ed3..381e0e312d 100644 --- a/vm/types/src/on_chain_resource/global_time.rs +++ b/vm/types/src/on_chain_resource/global_time.rs @@ -1,13 +1,17 @@ // Copyright (c) The Starcoin Core Contributors // SPDX-License-Identifier: Apache-2.0 -use move_core_types::ident_str; -use move_core_types::identifier::IdentStr; -use move_core_types::move_resource::{MoveResource, MoveStructType}; -use serde::{Deserialize, Serialize}; +use move_core_types::{ + ident_str, + identifier::IdentStr, + move_resource::{MoveResource, MoveStructType}, +}; +use serde::{ + Deserialize, + Serialize, +}; use schemars::JsonSchema; -const TIMESTAMP_MODULE_NAME: &str = "timestamp"; /// The CurrentTimeMilliseconds on chain. #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, JsonSchema)] @@ -25,10 +29,14 @@ impl GlobalTimeOnChain { pub fn seconds(&self) -> u64 { self.microseconds / 1000000 } + + pub fn milli_seconds(&self) -> u64 { + self.microseconds / 1000 + } } impl MoveStructType for GlobalTimeOnChain { - const MODULE_NAME: &'static IdentStr = ident_str!(TIMESTAMP_MODULE_NAME); + const MODULE_NAME: &'static IdentStr = ident_str!("timestamp"); const STRUCT_NAME: &'static IdentStr = ident_str!("CurrentTimeMicroseconds"); } diff --git a/vm/types/src/state_view.rs b/vm/types/src/state_view.rs index 56d14f91d7..86fd45d10f 100644 --- a/vm/types/src/state_view.rs +++ b/vm/types/src/state_view.rs @@ -53,6 +53,22 @@ pub trait StateReaderExt: StateView { Ok(rsrc_bytes) } + fn get_resource_type_bytes(&self, address: AccountAddress) -> Result + where + R: MoveResource, + { + Ok(self + .get_state_value_bytes(&StateKey::resource_typed::(&address)?)? + .ok_or_else(|| { + format_err!( + "Resource {:?} {:?} not exists at address:{}", + R::module_identifier(), + R::struct_identifier(), + address + ) + })?) + } + /// Get Resource by type R fn get_resource_type(&self, address: AccountAddress) -> Result where