Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into bm-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mooori committed Feb 11, 2025
2 parents 0d66ffe + fb95d7b commit 8fa037e
Show file tree
Hide file tree
Showing 35 changed files with 428 additions and 140 deletions.
8 changes: 5 additions & 3 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3420,15 +3420,15 @@ impl Chain {
let is_new_chunk = chunk_header.is_new_chunk(block_height);

if let Some(result) = self.apply_chunk_results_cache.peek(&cached_shard_update_key) {
info!(target: "chain", ?shard_id, ?cached_shard_update_key, "Using cached ShardUpdate result");
debug!(target: "chain", ?shard_id, ?cached_shard_update_key, "Using cached ShardUpdate result");
let result = result.clone();
return Ok(Some((
shard_id,
cached_shard_update_key,
Box::new(move |_| -> Result<ShardUpdateResult, Error> { Ok(result) }),
)));
}
info!(target: "chain", ?shard_id, ?cached_shard_update_key, "Creating ShardUpdate job");
debug!(target: "chain", ?shard_id, ?cached_shard_update_key, "Creating ShardUpdate job");

let shard_update_reason = if is_new_chunk {
// Validate new chunk and collect incoming receipts for it.
Expand Down Expand Up @@ -3616,7 +3616,9 @@ impl Chain {
self.epoch_manager.is_next_block_epoch_start(&head.last_block_hash)?;
let will_shard_layout_change =
self.epoch_manager.will_shard_layout_change(&head.last_block_hash)?;
let protocol_version = self.epoch_manager.get_epoch_protocol_version(&head.epoch_id)?;
let next_block_epoch =
self.epoch_manager.get_epoch_id_from_prev_block(&head.last_block_hash)?;
let protocol_version = self.epoch_manager.get_epoch_protocol_version(&next_block_epoch)?;

let tries = self.runtime_adapter.get_tries();
let snapshot_config = tries.state_snapshot_config();
Expand Down
6 changes: 4 additions & 2 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = include_str!("../README.md")]

use actix_cors::Cors;
use actix_web::http::header;
use actix_web::http::header::{self, ContentType};
use actix_web::HttpRequest;
use actix_web::{get, http, middleware, web, App, Error as HttpError, HttpResponse, HttpServer};
pub use api::{RpcFrom, RpcInto, RpcRequest};
Expand Down Expand Up @@ -1496,7 +1496,9 @@ pub async fn prometheus_handler() -> Result<HttpResponse, HttpError> {
encoder.encode(&prometheus::gather(), &mut buffer).unwrap();

match String::from_utf8(buffer) {
Ok(text) => Ok(HttpResponse::Ok().body(text)),
Ok(text) => Ok(HttpResponse::Ok()
.content_type(ContentType("text/plain".parse().unwrap()))
.body(text)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
}
Expand Down
25 changes: 25 additions & 0 deletions core/parameters/res/runtime_configs/129.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
fix_contract_loading_cost: { old: false, new: true }
action_deploy_global_contract: {
old: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
},
new: {
send_sir: 184_765_750_000,
send_not_sir: 184_765_750_000,
execution: 184_765_750_000,
}
}
action_deploy_global_contract_per_byte: {
old: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
},
new: {
send_sir: 6_812_999,
send_not_sir: 6_812_999,
execution: 70_000_000,
}
}
global_contract_storage_amount_per_byte: { old: 999_999_999_999_999_999_999_999_999, new: 100_000_000_000_000_000_000 }
9 changes: 9 additions & 0 deletions core/parameters/res/runtime_configs/parameters.snap
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,12 @@ max_shard_bandwidth 4_500_000
max_single_grant 4_194_304
max_allowance 4_500_000
max_base_bandwidth 100_000
action_deploy_global_contract
- send_sir: 999_999_999_999_999
- send_not_sir: 999_999_999_999_999
- execution: 999_999_999_999_999
action_deploy_global_contract_per_byte
- send_sir: 999_999_999_999_999
- send_not_sir: 999_999_999_999_999
- execution: 999_999_999_999_999
global_contract_storage_amount_per_byte 999999999999999999999999999
13 changes: 13 additions & 0 deletions core/parameters/res/runtime_configs/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,16 @@ max_shard_bandwidth: 999_999_999_999_999
max_single_grant: 999_999_999_999_999
max_allowance: 999_999_999_999_999
max_base_bandwidth: 999_999_999_999_999

# Global contracts
action_deploy_global_contract: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
}
action_deploy_global_contract_per_byte: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
}
global_contract_storage_amount_per_byte: 999_999_999_999_999_999_999_999_999
13 changes: 13 additions & 0 deletions core/parameters/res/runtime_configs/parameters_testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,16 @@ max_shard_bandwidth: 999_999_999_999_999
max_single_grant: 999_999_999_999_999
max_allowance: 999_999_999_999_999
max_base_bandwidth: 999_999_999_999_999

# Global contracts
action_deploy_global_contract: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
}
action_deploy_global_contract_per_byte: {
send_sir: 999_999_999_999_999,
send_not_sir: 999_999_999_999_999,
execution: 999_999_999_999_999,
}
global_contract_storage_amount_per_byte: 999_999_999_999_999_999_999_999_999
22 changes: 21 additions & 1 deletion core/parameters/src/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ pub enum ActionCosts {
new_data_receipt_base = 13,
new_data_receipt_byte = 14,
delegate = 15,
deploy_global_contract_base = 16,
deploy_global_contract_byte = 17,
}

impl ExtCosts {
Expand Down Expand Up @@ -438,6 +440,8 @@ pub struct StorageUsageConfig {
pub num_bytes_account: u64,
/// Additional number of bytes for a k/v record
pub num_extra_bytes_record: u64,
/// Amount of yN burned per byte of deployed Global Contract code.
pub global_contract_storage_amount_per_byte: Balance,
}

impl RuntimeFeesConfig {
Expand Down Expand Up @@ -532,6 +536,16 @@ impl RuntimeFeesConfig {
send_not_sir: 200_000_000_000,
execution: 200_000_000_000,
},
ActionCosts::deploy_global_contract_base => Fee {
send_sir: 184_765_750_000,
send_not_sir: 184_765_750_000,
execution: 184_765_750_000,
},
ActionCosts::deploy_global_contract_byte => Fee {
send_sir: 6_812_999,
send_not_sir: 6_812_999,
execution: 70_000_000,
},
},
}
}
Expand Down Expand Up @@ -563,11 +577,17 @@ impl StorageUsageConfig {
num_bytes_account: 100,
num_extra_bytes_record: 40,
storage_amount_per_byte: 909 * 100_000_000_000_000_000,
global_contract_storage_amount_per_byte: 100_000_000_000_000_000_000,
}
}

pub(crate) fn free() -> StorageUsageConfig {
Self { num_bytes_account: 0, num_extra_bytes_record: 0, storage_amount_per_byte: 0 }
Self {
num_bytes_account: 0,
num_extra_bytes_record: 0,
storage_amount_per_byte: 0,
global_contract_storage_amount_per_byte: 0,
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions core/parameters/src/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ pub enum Parameter {
MaxSingleGrant,
MaxAllowance,
MaxBaseBandwidth,

// Global contracts
ActionDeployGlobalContract,
ActionDeployGlobalContractPerByte,
GlobalContractStorageAmountPerByte,
}

#[derive(
Expand Down Expand Up @@ -261,6 +266,8 @@ pub enum FeeParameter {
ActionAddFunctionCallKeyPerByte,
ActionDeleteKey,
ActionDelegate,
ActionDeployGlobalContract,
ActionDeployGlobalContractPerByte,
}

impl Parameter {
Expand Down Expand Up @@ -324,6 +331,8 @@ impl From<ActionCosts> for FeeParameter {
ActionCosts::new_action_receipt => Self::ActionReceiptCreation,
ActionCosts::new_data_receipt_base => Self::DataReceiptCreationBase,
ActionCosts::new_data_receipt_byte => Self::DataReceiptCreationPerByte,
ActionCosts::deploy_global_contract_base => Self::ActionDeployGlobalContract,
ActionCosts::deploy_global_contract_byte => Self::ActionDeployGlobalContractPerByte,
}
}
}
2 changes: 2 additions & 0 deletions core/parameters/src/parameter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ impl TryFrom<&ParameterTable> for RuntimeConfig {
storage_amount_per_byte: params.get(Parameter::StorageAmountPerByte)?,
num_bytes_account: params.get(Parameter::StorageNumBytesAccount)?,
num_extra_bytes_record: params.get(Parameter::StorageNumExtraBytesRecord)?,
global_contract_storage_amount_per_byte: params
.get(Parameter::GlobalContractStorageAmountPerByte)?,
},
}),
wasm_config: Arc::new(Config {
Expand Down
13 changes: 10 additions & 3 deletions core/primitives-core/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use borsh::{BorshDeserialize, BorshSerialize};
pub use near_account_id as id;
use near_account_id::AccountId;
use near_schema_checker_lib::ProtocolSchema;
use std::borrow::Cow;
use std::io;

#[derive(
Expand Down Expand Up @@ -109,6 +110,10 @@ impl AccountContract {
AccountContract::Local(code_hash)
}
}

pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
}

#[derive(
Expand Down Expand Up @@ -179,10 +184,12 @@ impl Account {
}

#[inline]
pub fn contract(&self) -> AccountContract {
pub fn contract(&self) -> Cow<AccountContract> {
match self {
Self::V1(account) => AccountContract::from_local_code_hash(account.code_hash),
Self::V2(account) => account.contract.clone(),
Self::V1(account) => {
Cow::Owned(AccountContract::from_local_code_hash(account.code_hash))
}
Self::V2(account) => Cow::Borrowed(&account.contract),
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/primitives-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl ProtocolFeature {
// Nightly features:
#[cfg(feature = "protocol_feature_fix_contract_loading_cost")]
ProtocolFeature::FixContractLoadingCost => 129,
ProtocolFeature::GlobalContracts => 129,
// TODO(#11201): When stabilizing this feature in mainnet, also remove the temporary code
// that always enables this for mocknet (see config_mocknet function).
ProtocolFeature::ShuffleShardAssignments => 143,
Expand All @@ -275,7 +276,6 @@ impl ProtocolFeature {
149
}
// Place features that are not yet in Nightly below this line.
ProtocolFeature::GlobalContracts => 200,
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/primitives/src/profile_data_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ mod test {
let pretty_debug_str = format!("{profile_data:#?}");
expect_test::expect![[r#"
------------------------------
Action gas: 16120
Action gas: 18153
------ Host functions --------
contract_loading_base -> 1 [0% host]
contract_loading_bytes -> 2 [0% host]
Expand Down Expand Up @@ -348,6 +348,8 @@ mod test {
new_data_receipt_base -> 1013
new_data_receipt_byte -> 1014
delegate -> 1015
deploy_global_contract_base -> 1016
deploy_global_contract_byte -> 1017
------------------------------
"#]]
.assert_eq(&pretty_debug_str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ expression: view
"cost": "DEPLOY_CONTRACT_BYTE",
"gas_used": "1003"
},
{
"cost_category": "ACTION_COST",
"cost": "DEPLOY_GLOBAL_CONTRACT_BASE",
"gas_used": "1016"
},
{
"cost_category": "ACTION_COST",
"cost": "DEPLOY_GLOBAL_CONTRACT_BYTE",
"gas_used": "1017"
},
{
"cost_category": "ACTION_COST",
"cost": "FUNCTION_CALL_BASE",
Expand Down
1 change: 1 addition & 0 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::LazyLock;
pub struct Version {
pub version: String,
pub build: String,
pub commit: String,
#[serde(default)]
pub rustc_version: String,
}
Expand Down
11 changes: 6 additions & 5 deletions core/primitives/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ pub struct ContractCodeView {

impl From<&Account> for AccountView {
fn from(account: &Account) -> Self {
let (global_contract_hash, global_contract_account_id) = match account.contract() {
AccountContract::Global(contract) => (Some(contract), None),
AccountContract::GlobalByAccount(account_id) => (None, Some(account_id)),
AccountContract::Local(_) | AccountContract::None => (None, None),
};
let (global_contract_hash, global_contract_account_id) =
match account.contract().into_owned() {
AccountContract::Global(contract) => (Some(contract), None),
AccountContract::GlobalByAccount(account_id) => (None, Some(account_id)),
AccountContract::Local(_) | AccountContract::None => (None, None),
};
AccountView {
amount: account.amount(),
locked: account.locked(),
Expand Down
36 changes: 30 additions & 6 deletions core/store/src/trie/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,46 @@ impl TrieUpdate {
mode: KeyLookupMode,
) -> Result<Option<TrieUpdateValuePtr<'_>>, StorageError> {
let key = key.to_vec();
if let Some(key_value) = self.prospective.get(&key) {
return Ok(key_value.value.as_deref().map(TrieUpdateValuePtr::MemoryRef));
} else if let Some(changes_with_trie_key) = self.committed.get(&key) {
if let Some(RawStateChange { data, .. }) = changes_with_trie_key.changes.last() {
return Ok(data.as_deref().map(TrieUpdateValuePtr::MemoryRef));
}
if let Some(value_ref) = self.get_ref_from_updates(&key) {
return Ok(value_ref);
}

let result = self
.trie
.get_optimized_ref(&key, mode)?
.map(|optimized_value_ref| TrieUpdateValuePtr::Ref(&self.trie, optimized_value_ref));
Ok(result)
}

pub fn get_ref_no_side_effects(
&self,
key: &TrieKey,
mode: KeyLookupMode,
) -> Result<Option<TrieUpdateValuePtr<'_>>, StorageError> {
let key = key.to_vec();
if let Some(value_ref) = self.get_ref_from_updates(&key) {
return Ok(value_ref);
}

let result = self
.trie
.get_optimized_ref_no_side_effects(&key, mode)?
.map(|optimized_value_ref| TrieUpdateValuePtr::Ref(&self.trie, optimized_value_ref));

Ok(result)
}

fn get_ref_from_updates(&self, key: &[u8]) -> Option<Option<TrieUpdateValuePtr<'_>>> {
if let Some(key_value) = self.prospective.get(key) {
return Some(key_value.value.as_deref().map(TrieUpdateValuePtr::MemoryRef));
} else if let Some(changes_with_trie_key) = self.committed.get(key) {
if let Some(RawStateChange { data, .. }) = changes_with_trie_key.changes.last() {
return Some(data.as_deref().map(TrieUpdateValuePtr::MemoryRef));
}
}
None
}

pub fn contains_key(&self, key: &TrieKey) -> Result<bool, StorageError> {
let key = key.to_vec();
if self.prospective.contains_key(&key) {
Expand Down
Loading

0 comments on commit 8fa037e

Please sign in to comment.