Skip to content

Commit

Permalink
[compiler-v2 framework] Add integration test for test block meta data…
Browse files Browse the repository at this point in the history
… deserialize code
  • Loading branch information
welbon committed Nov 18, 2024
1 parent 3e09bab commit f981a9f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
38 changes: 38 additions & 0 deletions executor/tests/block_test.rs
Original file line number Diff line number Diff line change
@@ -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::<BlockMetadata>(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::<BlockMetadata>(bcs.as_slice())?;
// assert_eq!(output.number, block_metadata.number);

// let data = chain_state.get_resource_type::<BlockMetadata>(genesis_address())?;
// assert_ne!(data.number, 0);
// assert!(!block_metadata.number > 0);

Ok(())
}
1 change: 0 additions & 1 deletion vm/starcoin-transactional-test-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions vm/types/src/on_chain_resource/block_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@
// 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,
// Hash of the parent block.
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<u8>,
// 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<u8>,
}

impl BlockMetadata {
Expand Down
20 changes: 14 additions & 6 deletions vm/types/src/on_chain_resource/global_time.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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");
}

Expand Down
16 changes: 16 additions & 0 deletions vm/types/src/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ pub trait StateReaderExt: StateView {
Ok(rsrc_bytes)
}

fn get_resource_type_bytes<R>(&self, address: AccountAddress) -> Result<Bytes>
where
R: MoveResource,
{
Ok(self
.get_state_value_bytes(&StateKey::resource_typed::<R>(&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<R>(&self, address: AccountAddress) -> Result<R>
where
Expand Down

0 comments on commit f981a9f

Please sign in to comment.