Skip to content

Commit

Permalink
move natives to starcoin-frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Aug 29, 2024
1 parent 022c584 commit 6234cfd
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 107 deletions.
85 changes: 2 additions & 83 deletions vm/frameworks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,3 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

pub mod account;
pub mod hash;
pub mod signature;
pub mod token;
pub mod u256;
// for support evm compat and cross chain.
pub mod ecrecover;
pub mod from_bcs;
pub mod secp256k1;

mod helpers;
pub mod util;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GasParameters {
pub account: account::GasParameters,
pub hash: hash::GasParameters,
pub signature: signature::GasParameters,
pub token: token::GasParameters,
pub u256: u256::GasParameters,
pub secp256k1: secp256k1::GasParameters,
pub from_bcs: from_bcs::GasParameters,
}

impl GasParameters {
pub fn zeros() -> Self {
Self {
account: account::GasParameters {
create_signer: account::CreateSignerGasParameters { base: 0.into() },
destroy_signer: account::DestroySignerGasParameters { base: 0.into() },
},
hash: hash::GasParameters {
keccak256: hash::Keccak256HashGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ripemd160: hash::Ripemd160HashGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
signature: signature::GasParameters {
ed25519_validate_key: signature::Ed25519ValidateKeyGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ed25519_verify: signature::Ed25519VerifyGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ec_recover: ecrecover::EcrecoverGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
token: token::GasParameters {
name_of: token::NameOfGasParameters { base: 0.into() },
},
u256: u256::GasParameters {
add: u256::U256AddGasParameters { base: 0.into() },
sub: u256::U256SubGasParameters { base: 0.into() },
mul: u256::U256MulGasParameters { base: 0.into() },
div: u256::U256DivGasParameters { base: 0.into() },
rem: u256::U256RemGasParameters { base: 0.into() },
pow: u256::U256PowGasParameters { base: 0.into() },
from_bytes: u256::U256FromBytesGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
secp256k1: secp256k1::GasParameters {
base: 0.into(),
ecdsa_recover: 0.into(),
},
from_bcs: from_bcs::GasParameters {
base: 0.into(),
per_byte: 0.into(),
},
}
}
}
// ref aptos-core/aptos-move/framework/src/lib.rs
pub mod natives;
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
),
];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::util::make_native_from_func;
use crate::natives::util::make_native_from_func;
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
use move_core_types::vm_status::StatusCode;
Expand Down Expand Up @@ -73,5 +73,5 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
"from_bytes",
make_native_from_func(gas_params, native_from_bytes),
)];
crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::util::make_native_from_func;
use crate::natives::util::make_native_from_func;
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn native_keccak_256(

let cost = gas_params.base + gas_params.per_byte * NumBytes::new(input_arg.len() as u64);

let output = crate::ecrecover::keccak(input_arg.as_slice());
let output = crate::natives::ecrecover::keccak(input_arg.as_slice());

Ok(NativeResult::ok(cost, smallvec![Value::vector_u8(output)]))
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
),
];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}

#[cfg(test)]
Expand All @@ -111,7 +111,7 @@ mod test {
#[test]
fn test_keccak() {
let input: Vec<u8> = FromHex::from_hex("616263").unwrap();
let output = crate::ecrecover::keccak(input.as_slice());
let output = crate::natives::ecrecover::keccak(input.as_slice());
let expect_output: Vec<u8> =
FromHex::from_hex("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
.unwrap();
Expand Down
File renamed without changes.
84 changes: 84 additions & 0 deletions vm/frameworks/src/natives/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

pub mod account;
pub mod hash;
pub mod signature;
pub mod token;
pub mod u256;
// for support evm compat and cross chain.
pub mod ecrecover;
pub mod from_bcs;
pub mod secp256k1;

mod helpers;
pub mod util;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GasParameters {
pub account: account::GasParameters,
pub hash: hash::GasParameters,
pub signature: signature::GasParameters,
pub token: token::GasParameters,
pub u256: u256::GasParameters,
pub secp256k1: secp256k1::GasParameters,
pub from_bcs: from_bcs::GasParameters,
}

impl GasParameters {
pub fn zeros() -> Self {
Self {
account: account::GasParameters {
create_signer: account::CreateSignerGasParameters { base: 0.into() },
destroy_signer: account::DestroySignerGasParameters { base: 0.into() },
},
hash: hash::GasParameters {
keccak256: hash::Keccak256HashGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ripemd160: hash::Ripemd160HashGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
signature: signature::GasParameters {
ed25519_validate_key: signature::Ed25519ValidateKeyGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ed25519_verify: signature::Ed25519VerifyGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
ec_recover: ecrecover::EcrecoverGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
token: token::GasParameters {
name_of: token::NameOfGasParameters { base: 0.into() },
},
u256: u256::GasParameters {
add: u256::U256AddGasParameters { base: 0.into() },
sub: u256::U256SubGasParameters { base: 0.into() },
mul: u256::U256MulGasParameters { base: 0.into() },
div: u256::U256DivGasParameters { base: 0.into() },
rem: u256::U256RemGasParameters { base: 0.into() },
pow: u256::U256PowGasParameters { base: 0.into() },
from_bytes: u256::U256FromBytesGasParameters {
base: 0.into(),
per_byte: 0.into(),
},
},
secp256k1: secp256k1::GasParameters {
base: 0.into(),
ecdsa_recover: 0.into(),
},
from_bcs: from_bcs::GasParameters {
base: 0.into(),
per_byte: 0.into(),
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
**************************************************************************************************/

use crate::util::make_native_from_func;
use crate::natives::util::make_native_from_func;
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::{InternalGas, InternalGasPerArg, NumArgs};
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
Expand Down Expand Up @@ -102,5 +102,5 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
make_native_from_func(gas_params, native_ecdsa_recover),
)];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::ecrecover::{make_native_ecrecover, EcrecoverGasParameters};
use crate::natives::ecrecover::{make_native_ecrecover, EcrecoverGasParameters};
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
Expand Down Expand Up @@ -131,5 +131,5 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
),
];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct GasParameters {
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [("name_of", make_native_token_name_of(gas_params.name_of))];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::util::make_native_from_func;
use crate::natives::util::make_native_from_func;
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
use move_core_types::vm_status::StatusCode;
Expand Down Expand Up @@ -215,5 +215,5 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
),
];

crate::helpers::make_module_natives(natives)
crate::natives::helpers::make_module_natives(natives)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion vm/gas-algebra-ext/src/starcoin_framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::gas_meter::EXECUTION_GAS_MULTIPLIER as MUL;
use starcoin_frameworks::GasParameters;
use starcoin_frameworks::natives::GasParameters;

// see starcoin/vm/types/src/on_chain_config/genesis_gas_schedule.rs
// same order as from https://github.com/starcoinorg/starcoin-framework/blob/main/sources/VMConfig.move#native_schedule
Expand Down
4 changes: 2 additions & 2 deletions vm/starcoin-gas-meter/src/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const MIN_EXISTS_DATA_SIZE: AbstractMemorySize = AbstractMemorySize::new(100);
pub struct NativeGasParameters {
pub move_stdlib: move_stdlib::natives::GasParameters,
pub nursery: move_stdlib::natives::NurseryGasParameters,
pub starcoin_natives: starcoin_frameworks::GasParameters,
pub starcoin_natives: starcoin_frameworks::natives::GasParameters,
pub table: move_table_extension::GasParameters,
}

Expand Down Expand Up @@ -70,7 +70,7 @@ impl NativeGasParameters {
Self {
move_stdlib: move_stdlib::natives::GasParameters::zeros(),
nursery: move_stdlib::natives::NurseryGasParameters::zeros(),
starcoin_natives: starcoin_frameworks::GasParameters::zeros(),
starcoin_natives: starcoin_frameworks::natives::GasParameters::zeros(),
table: move_table_extension::GasParameters::zeros(),
}
}
Expand Down
14 changes: 7 additions & 7 deletions vm/vm-runtime/src/natives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ pub fn starcoin_natives(gas_params: NativeGasParameters) -> NativeFunctionTable
);
add_natives_from_module!(
"Hash",
starcoin_frameworks::hash::make_all(gas_params.starcoin_natives.hash)
starcoin_frameworks::natives::hash::make_all(gas_params.starcoin_natives.hash)
);
add_natives_from_module!(
"BCS",
move_stdlib::natives::bcs::make_all(gas_params.move_stdlib.bcs)
);
add_natives_from_module!(
"FromBCS",
starcoin_frameworks::from_bcs::make_all(gas_params.starcoin_natives.from_bcs)
starcoin_frameworks::natives::from_bcs::make_all(gas_params.starcoin_natives.from_bcs)
);
add_natives_from_module!(
"Signature",
starcoin_frameworks::signature::make_all(gas_params.starcoin_natives.signature)
starcoin_frameworks::natives::signature::make_all(gas_params.starcoin_natives.signature)
);
add_natives_from_module!(
"Vector",
Expand All @@ -53,19 +53,19 @@ pub fn starcoin_natives(gas_params: NativeGasParameters) -> NativeFunctionTable
);
add_natives_from_module!(
"Account",
starcoin_frameworks::account::make_all(gas_params.starcoin_natives.account)
starcoin_frameworks::natives::account::make_all(gas_params.starcoin_natives.account)
);
add_natives_from_module!(
"Signer",
move_stdlib::natives::signer::make_all(gas_params.move_stdlib.signer)
);
add_natives_from_module!(
"Token",
starcoin_frameworks::token::make_all(gas_params.starcoin_natives.token)
starcoin_frameworks::natives::token::make_all(gas_params.starcoin_natives.token)
);
add_natives_from_module!(
"U256",
starcoin_frameworks::u256::make_all(gas_params.starcoin_natives.u256)
starcoin_frameworks::natives::u256::make_all(gas_params.starcoin_natives.u256)
);
#[cfg(feature = "testing")]
add_natives_from_module!(
Expand All @@ -82,7 +82,7 @@ pub fn starcoin_natives(gas_params: NativeGasParameters) -> NativeFunctionTable
);
add_natives_from_module!(
"Secp256k1",
starcoin_frameworks::secp256k1::make_all(gas_params.starcoin_natives.secp256k1)
starcoin_frameworks::natives::secp256k1::make_all(gas_params.starcoin_natives.secp256k1)
);

let natives = make_table_from_iter(CORE_CODE_ADDRESS, natives);
Expand Down

0 comments on commit 6234cfd

Please sign in to comment.