Skip to content

Commit

Permalink
Fix framework (#4208)
Browse files Browse the repository at this point in the history
* fix starcoin-framework

1. add DerivedAuid schema to AuthenticationKey
2. add error::invalid_state

* comments out some MoveStructLayout

* add DeriveObjectAddressFromObject schema to AuthenticationKey

* fix starcoin-framework

1. add DISALLOW_USER_NATIVES to FeatureFlag
2. add SafeNativeError::LoadModule

* remove consensus_config from natives

* add FunctionGraph to SafeNativeError

* enable starcoin-sdk-builder

* udpate starcoin-crypto and move deps

* comments out ContractEvent::V2

* fix some clap3 errors

* fix conflicted definitions with same names

* silence warnings
  • Loading branch information
simonjiao authored Sep 25, 2024
1 parent 8a01c82 commit a3ff95c
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 169 deletions.
87 changes: 44 additions & 43 deletions Cargo.lock

Large diffs are not rendered by default.

62 changes: 31 additions & 31 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -386,35 +386,35 @@ sha2_0_10_6 = { package = "sha2", version = "0.10.6" }
siphasher = "0.3.10"
merkletree = { version = "0.22.1" }
mirai-annotations = "1.10.1"
move-binary-format = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-bytecode-source-map = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-bytecode-verifier = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-cli = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-command-line-common = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-compiler = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-compiler-v2 = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-core-types = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-coverage = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-disassembler = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-docgen = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-errmapgen = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-ir-compiler = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-ir-types = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-model = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-package = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-prover = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-prover-boogie-backend = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-prover-bytecode-pipeline = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-stackless-bytecode = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-prover-test-utils = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-stdlib = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-transactional-test-runner = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2", features = ["table-extension"] }
move-vm-runtime = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-vm-types = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-table-extension = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2" }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2", features = ["table-extension"] }
move-binary-format = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-bytecode-source-map = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-bytecode-verifier = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-cli = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-command-line-common = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-compiler = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-compiler-v2 = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-core-types = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-coverage = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-disassembler = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-docgen = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-errmapgen = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-ir-compiler = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-ir-types = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-model = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-package = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-prover = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-prover-boogie-backend = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-prover-bytecode-pipeline = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-stackless-bytecode = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-prover-test-utils = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-resource-viewer = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-stdlib = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-transactional-test-runner = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-unit-test = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee", features = ["table-extension"] }
move-vm-runtime = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-vm-types = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-table-extension = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee" }
move-vm-test-utils = { git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee", features = ["table-extension"] }

names = { version = "0.14.0", default-features = false }
network-api = { path = "network/api", package = "network-api" }
Expand Down Expand Up @@ -498,7 +498,7 @@ starcoin-chain-service = { path = "chain/service" }
starcoin-cmd = { path = "cmd/starcoin" }
starcoin-config = { path = "config" }
starcoin-consensus = { path = "consensus" }
starcoin-crypto = { git = "https://github.com/starcoinorg/starcoin-crypto", rev = "d3c51001cc34fcabf5f693d97c936fb7fe50e6a7" }
starcoin-crypto = { git = "https://github.com/starcoinorg/starcoin-crypto", rev = "2e8b5525258838236b87e9345002ef5b2c1f8796" }

starcoin-decrypt = { path = "commons/decrypt" }
starcoin-dev = { path = "vm/dev" }
Expand Down Expand Up @@ -594,7 +594,7 @@ unsigned-varint = { version = "0.6.0", features = [
"futures",
"asynchronous_codec",
] }
vm = { package = "move-binary-format", git = "https://github.com/starcoinorg/move", rev = "6e3ac2d90bebf567540f2d1ac3ace51dc3ebeff2", features = ["fuzzing"] }
vm = { package = "move-binary-format", git = "https://github.com/starcoinorg/move", rev = "3ca902f6d22f47eb86474801c9f702d628bc9aee", features = ["fuzzing"] }
vm-status-translator = { path = "vm/vm-status-translator" }
void = "1.0.2"
walkdir = "2.3.1"
Expand Down
34 changes: 34 additions & 0 deletions types/src/error/block_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use starcoin_crypto::HashValue;
use starcoin_vm_types::vm_status::DiscardedVMStatus;
use std::error::Error;
use thiserror::Error;

pub type ExecutorResult<T> = anyhow::Result<T, BlockExecutorError>;

#[derive(Error, Debug)]
pub enum BlockExecutorError {
#[error("block transaction execute discard, status:{0:?}, transaction_id: {1}")]
BlockTransactionDiscard(DiscardedVMStatus, HashValue),
#[error("block transaction accumulator append error")]
BlockAccumulatorAppendErr,
#[error("block accumulator get proof error")]
BlockAccumulatorGetProofErr,
#[error("block accumulator proof verify error")]
BlockAccumulatorVerifyErr(HashValue, u64),
#[error("block chain state read or write error:{0:?}")]
BlockChainStateErr(anyhow::Error),
#[error("block accumulator flush error")]
BlockAccumulatorFlushErr,
#[error("block transaction execute error, {0:?}")]
BlockTransactionExecuteErr(anyhow::Error),
// service error
#[error("account error, {0:?}")]
AccountError(anyhow::Error),
#[error("other error: {0:?}")]
OtherError(Box<dyn Error + Send + Sync + 'static>),
#[error("block transaction execute Retry error")]
BlockExecuteRetryErr,
}
40 changes: 12 additions & 28 deletions types/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use starcoin_crypto::HashValue;
use starcoin_vm_types::vm_status::DiscardedVMStatus;
use std::error::Error;
use thiserror::Error;
mod block_executor;

pub type ExecutorResult<T> = anyhow::Result<T, BlockExecutorError>;
pub use block_executor::*;

#[derive(Error, Debug)]
pub enum BlockExecutorError {
#[error("block transaction execute discard, status:{0:?}, transaction_id: {1}")]
BlockTransactionDiscard(DiscardedVMStatus, HashValue),
#[error("block transaction accumulator append error")]
BlockAccumulatorAppendErr,
#[error("block accumulator get proof error")]
BlockAccumulatorGetProofErr,
#[error("block accumulator proof verify error")]
BlockAccumulatorVerifyErr(HashValue, u64),
#[error("block chain state read or write error:{0:?}")]
BlockChainStateErr(anyhow::Error),
#[error("block accumulator flush error")]
BlockAccumulatorFlushErr,
#[error("block transaction execute error, {0:?}")]
BlockTransactionExecuteErr(anyhow::Error),
// service error
#[error("account error, {0:?}")]
AccountError(anyhow::Error),
#[error("other error: {0:?}")]
OtherError(Box<dyn Error + Send + Sync + 'static>),
#[error("block transaction execute Retry error")]
BlockExecuteRetryErr,
/// The system is not in a state where the operation can be performed (http: 400)
pub const INVALID_STATE: u64 = 0x3;

/// Construct a canonical error code from a category and a reason.
pub fn canonical(category: u64, reason: u64) -> u64 {
(category << 16) + reason
}

pub fn invalid_state(r: u64) -> u64 {
canonical(INVALID_STATE, r)
}
2 changes: 1 addition & 1 deletion vm/framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ starcoin-gas-algebra = { workspace = true }
starcoin-gas-schedule = { workspace = true }
starcoin-move-stdlib = { workspace = true }
starcoin-native-interface = { workspace = true }
# starcoin-sdk-builder = { workspace = true }
starcoin-sdk-builder = { workspace = true }
starcoin-types = { workspace = true }
starcoin-vm-types = { workspace = true }
starcoin-vm-runtime-types = { workspace = true }
Expand Down
13 changes: 11 additions & 2 deletions vm/framework/src/built_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
zip_metadata, zip_metadata_str, RuntimeModuleMetadataV1, APTOS_METADATA_KEY,
APTOS_METADATA_KEY_V1, METADATA_V1_MIN_FILE_FORMAT_VERSION,
};
use anyhow::bail;
use anyhow::{anyhow, bail};
use clap::Parser;
use codespan_reporting::{
diagnostic::Severity,
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct BuildOptions {
#[clap(skip)] // TODO: have a parser for this; there is one in the CLI buts its downstream
pub named_addresses: BTreeMap<String, AccountAddress>,
/// Whether to override the standard library with the given version.
#[clap(long, value_parser)]
#[clap(long, value_parser(parse_stdlib_version))]
pub override_std: Option<StdVersion>,
#[clap(skip)]
pub docgen_options: Option<DocgenOptions>,
Expand All @@ -97,6 +97,15 @@ pub struct BuildOptions {
pub known_attributes: BTreeSet<String>,
}

fn parse_stdlib_version(s: &str) -> anyhow::Result<StdVersion> {
match s.to_lowercase().as_str() {
"mainnet" => Ok(StdVersion::Mainnet),
"testnet" => Ok(StdVersion::Testnet),
"devnet" => Ok(StdVersion::Devnet),
_ => Err(anyhow!("unknown stdlib version")),
}
}

// Because named_addresses has no parser, we can't use clap's default impl. This must be aligned
// with defaults above.
impl Default for BuildOptions {
Expand Down
10 changes: 9 additions & 1 deletion vm/framework/src/docgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ pub struct DocgenOptions {
pub references_file: Option<String>,

/// Choose the output format
#[clap(long)]
#[clap(long, value_parser(parse_output_format))]
pub output_format: Option<OutputFormat>,
}

fn parse_output_format(s: &str) -> anyhow::Result<OutputFormat> {
match s.to_uppercase().as_str() {
"MD" => Ok(OutputFormat::MD),
"MDX" => Ok(OutputFormat::MDX),
_ => Err(anyhow!("unknown output format")),
}
}

impl DocgenOptions {
pub fn run(
&self,
Expand Down
32 changes: 0 additions & 32 deletions vm/framework/src/natives/consensus_config.rs

This file was deleted.

32 changes: 18 additions & 14 deletions vm/framework/src/natives/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ impl NativeEventContext {
}

#[cfg(feature = "testing")]
fn emitted_v1_events(&self, event_key: &EventKey, ty_tag: &TypeTag) -> Vec<&[u8]> {
fn emitted_events(&self, event_key: &EventKey, ty_tag: &TypeTag) -> Vec<&[u8]> {
let mut events = vec![];
for event in self.events.iter() {
if let (ContractEvent::V1(e), _) = event {
if let (ContractEvent::V0(e), _) = event {
if e.key() == event_key && e.type_tag() == ty_tag {
events.push(e.event_data());
}
Expand Down Expand Up @@ -103,7 +103,7 @@ fn native_write_to_event_store(

let ctx = context.extensions_mut().get_mut::<NativeEventContext>();
ctx.events.push((
ContractEvent::new_v1(key, seq_num, ty_tag, blob),
ContractEvent::new(key, seq_num, ty_tag, blob),
has_aggregator_lifting.then_some(layout),
));
Ok(smallvec![])
Expand Down Expand Up @@ -144,12 +144,12 @@ fn native_emitted_events_by_handle(
))
})?
.value_as::<AccountAddress>()?;
let key = EventKey::new(creation_num, addr);
let key = EventKey::new_from_address(&addr, creation_num);
let ty_tag = context.type_to_type_tag(&ty)?;
let ty_layout = context.type_to_type_layout(&ty)?;
let ctx = context.extensions_mut().get_mut::<NativeEventContext>();
let events = ctx
.emitted_v1_events(&key, &ty_tag)
.emitted_events(&key, &ty_tag)
.into_iter()
.map(|blob| {
Value::simple_deserialize(blob, &ty_layout).ok_or_else(|| {
Expand All @@ -162,6 +162,7 @@ fn native_emitted_events_by_handle(
Ok(smallvec![Value::vector_for_testing_only(events)])
}

/*
#[cfg(feature = "testing")]
fn native_emitted_events(
context: &mut SafeNativeContext,
Expand Down Expand Up @@ -189,6 +190,7 @@ fn native_emitted_events(
.collect::<SafeNativeResult<Vec<Value>>>()?;
Ok(smallvec![Value::vector_for_testing_only(events)])
}
*/

#[inline]
fn native_write_module_event_to_store(
Expand Down Expand Up @@ -232,19 +234,20 @@ fn native_write_module_event_to_store(
)));
}
}
let (layout, has_identifier_mappings) =
let (layout, _has_identifier_mappings) =
context.type_to_type_layout_with_identifier_mappings(&ty)?;
let blob = serialize_and_allow_delayed_values(&msg, &layout)?.ok_or_else(|| {
let _blob = serialize_and_allow_delayed_values(&msg, &layout)?.ok_or_else(|| {
SafeNativeError::InvariantViolation(
PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
.with_message("Event serialization failure".to_string()),
)
})?;
let ctx = context.extensions_mut().get_mut::<NativeEventContext>();
ctx.events.push((
ContractEvent::new_v2(type_tag, blob),
has_identifier_mappings.then_some(layout),
));
// TODO: currently we don't support ContractEvent::V2
//let ctx = context.extensions_mut().get_mut::<NativeEventContext>();
//ctx.events.push((
// ContractEvent::new_v2(type_tag, blob),
// has_identifier_mappings.then_some(layout),
//));

Ok(smallvec![])
}
Expand All @@ -264,8 +267,9 @@ pub fn make_all(
native_emitted_events_by_handle as RawSafeNative,
)]);

#[cfg(feature = "testing")]
natives.extend([("emitted_events", native_emitted_events as RawSafeNative)]);
// TODO: Currently we don't support ContractEvent::V2
//#[cfg(feature = "testing")]
//natives.extend([("emitted_events", native_emitted_events as RawSafeNative)]);

natives.extend([(
"write_to_event_store",
Expand Down
2 changes: 0 additions & 2 deletions vm/framework/src/natives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
pub mod account;
pub mod aggregator_natives;
pub mod code;
pub mod consensus_config;
pub mod create_signer;
pub mod cryptography;
pub mod debug;
Expand Down Expand Up @@ -85,7 +84,6 @@ pub fn all_natives(
add_natives_from_module!("object", object::make_all(builder));
add_natives_from_module!("debug", debug::make_all(builder));
add_natives_from_module!("string_utils", string_utils::make_all(builder));
add_natives_from_module!("consensus_config", consensus_config::make_all(builder));
add_natives_from_module!("function_info", function_info::make_all(builder));
add_natives_from_module!(
"dispatchable_fungible_asset",
Expand Down
3 changes: 2 additions & 1 deletion vm/framework/src/natives/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ fn native_create_user_derived_object_address_impl(
.borrow_mut()
.entry((derive_from, source))
.or_insert_with(|| {
AuthenticationKey::object_address_from_object(&source, &derive_from).account_address()
// Fixme: currently we only use the last 16-bytes of AuthenticationKey as the AccountAddress.
AuthenticationKey::object_address_from_object(&source, &derive_from).derived_address()
});

Ok(smallvec![Value::address(derived_address)])
Expand Down
4 changes: 3 additions & 1 deletion vm/framework/src/natives/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ fn native_format_impl(
)?;
out.push('}');
}
// TODO: update move-core-types to support these layouts
/*
MoveTypeLayout::Struct(MoveStructLayout::RuntimeVariants(variants)) => {
let struct_value = val.value_as::<Struct>()?;
let (tag, elems) = struct_value.unpack_with_tag()?;
Expand Down Expand Up @@ -347,7 +349,7 @@ fn native_format_impl(
)?;
out.push('}');
}

*/
// This is unreachable because we check layout at the start. Still, return
// an error to be safe.
MoveTypeLayout::Native(..) => {
Expand Down
Loading

0 comments on commit a3ff95c

Please sign in to comment.