Skip to content

Commit

Permalink
feat(starknet_state_sync): create state sync components
Browse files Browse the repository at this point in the history
  • Loading branch information
noamsp-starkware committed Dec 12, 2024
1 parent dc8ca7a commit 9a29359
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tmp_venv/*
.vscode/settings.json
/data
/logs
/sequencer_data
/target
/.vscode
# Git hooks
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 170 additions & 0 deletions config/sequencer/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,176 @@
"privacy": "Public",
"value": ""
},
"state_sync_config.network_config.advertised_multiaddr": {
"description": "The external address other peers see this node. If this is set, the node will not try to find out which addresses it has and will write this address as external instead",
"privacy": "Public",
"value": ""
},
"state_sync_config.network_config.advertised_multiaddr.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
},
"state_sync_config.network_config.bootstrap_peer_multiaddr": {
"description": "The multiaddress of the peer node. It should include the peer's id. For more info: https://docs.libp2p.io/concepts/fundamentals/peers/",
"privacy": "Public",
"value": ""
},
"state_sync_config.network_config.bootstrap_peer_multiaddr.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
},
"state_sync_config.network_config.chain_id": {
"description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
"pointer_target": "chain_id",
"privacy": "Public"
},
"state_sync_config.network_config.discovery_config.bootstrap_dial_retry_config.base_delay_millis": {
"description": "The base delay in milliseconds for the exponential backoff strategy.",
"privacy": "Public",
"value": 2
},
"state_sync_config.network_config.discovery_config.bootstrap_dial_retry_config.factor": {
"description": "The factor for the exponential backoff strategy.",
"privacy": "Public",
"value": 5
},
"state_sync_config.network_config.discovery_config.bootstrap_dial_retry_config.max_delay_seconds": {
"description": "The maximum delay in seconds for the exponential backoff strategy.",
"privacy": "Public",
"value": 5
},
"state_sync_config.network_config.discovery_config.heartbeat_interval": {
"description": "The interval between each discovery (Kademlia) query in milliseconds.",
"privacy": "Public",
"value": 100
},
"state_sync_config.network_config.idle_connection_timeout": {
"description": "Amount of time in seconds that a connection with no active sessions will stay alive.",
"privacy": "Public",
"value": 120
},
"state_sync_config.network_config.peer_manager_config.malicious_timeout_seconds": {
"description": "The duration in seconds a peer is blacklisted after being marked as malicious.",
"privacy": "Public",
"value": 31536000
},
"state_sync_config.network_config.peer_manager_config.unstable_timeout_millis": {
"description": "The duration in milliseconds a peer blacklisted after being reported as unstable.",
"privacy": "Public",
"value": 1000
},
"state_sync_config.network_config.quic_port": {
"description": "The port that the node listens on for incoming quic connections.",
"privacy": "Public",
"value": 10001
},
"state_sync_config.network_config.secret_key": {
"description": "The secret key used for building the peer id. If it's an empty string a random one will be used.",
"privacy": "Private",
"value": ""
},
"state_sync_config.network_config.session_timeout": {
"description": "Maximal time in seconds that each session can take before failing on timeout.",
"privacy": "Public",
"value": 120
},
"state_sync_config.network_config.tcp_port": {
"description": "The port that the node listens on for incoming tcp connections.",
"privacy": "Public",
"value": 10000
},
"state_sync_config.p2p_sync_client_config.buffer_size": {
"description": "Size of the buffer for read from the storage and for incoming responses.",
"privacy": "Public",
"value": 100000
},
"state_sync_config.p2p_sync_client_config.num_block_classes_per_query": {
"description": "The maximum amount of block's classes to ask from peers in each iteration.",
"privacy": "Public",
"value": 100
},
"state_sync_config.p2p_sync_client_config.num_block_state_diffs_per_query": {
"description": "The maximum amount of block's state diffs to ask from peers in each iteration.",
"privacy": "Public",
"value": 100
},
"state_sync_config.p2p_sync_client_config.num_block_transactions_per_query": {
"description": "The maximum amount of blocks to ask their transactions from peers in each iteration.",
"privacy": "Public",
"value": 100
},
"state_sync_config.p2p_sync_client_config.num_headers_per_query": {
"description": "The maximum amount of headers to ask from peers in each iteration.",
"privacy": "Public",
"value": 10000
},
"state_sync_config.p2p_sync_client_config.stop_sync_at_block_number": {
"description": "Stops the sync at given block number and closes the node cleanly. Used to run profiling on the node.",
"privacy": "Public",
"value": 1000
},
"state_sync_config.p2p_sync_client_config.stop_sync_at_block_number.#is_none": {
"description": "Flag for an optional field.",
"privacy": "TemporaryValue",
"value": true
},
"state_sync_config.p2p_sync_client_config.wait_period_for_new_data": {
"description": "Time in millisseconds to wait when a query returned with partial data before sending a new query",
"privacy": "Public",
"value": 50
},
"state_sync_config.storage_config.db_config.chain_id": {
"description": "The chain to follow. For more details see https://docs.starknet.io/documentation/architecture_and_concepts/Blocks/transactions/#chain-id.",
"pointer_target": "chain_id",
"privacy": "Public"
},
"state_sync_config.storage_config.db_config.enforce_file_exists": {
"description": "Whether to enforce that the path exists. If true, `open_env` fails when the mdbx.dat file does not exist.",
"privacy": "Public",
"value": false
},
"state_sync_config.storage_config.db_config.growth_step": {
"description": "The growth step in bytes, must be greater than zero to allow the database to grow.",
"privacy": "Public",
"value": 4294967296
},
"state_sync_config.storage_config.db_config.max_size": {
"description": "The maximum size of the node's storage in bytes.",
"privacy": "Public",
"value": 1099511627776
},
"state_sync_config.storage_config.db_config.min_size": {
"description": "The minimum size of the node's storage in bytes.",
"privacy": "Public",
"value": 1048576
},
"state_sync_config.storage_config.db_config.path_prefix": {
"description": "Prefix of the path of the node's storage directory, the storage file path will be <path_prefix>/<chain_id>. The path is not created automatically.",
"privacy": "Public",
"value": "./sequencer_data"
},
"state_sync_config.storage_config.mmap_file_config.growth_step": {
"description": "The growth step in bytes, must be greater than max_object_size.",
"privacy": "Public",
"value": 1073741824
},
"state_sync_config.storage_config.mmap_file_config.max_object_size": {
"description": "The maximum size of a single object in the file in bytes",
"privacy": "Public",
"value": 268435456
},
"state_sync_config.storage_config.mmap_file_config.max_size": {
"description": "The maximum size of a memory mapped file in bytes. Must be greater than growth_step.",
"privacy": "Public",
"value": 1099511627776
},
"state_sync_config.storage_config.scope": {
"description": "The categories of data saved in storage.",
"privacy": "Public",
"value": "FullArchive"
},
"strk_fee_token_address": {
"description": "A required param! Address of the STRK fee token.",
"param_type": "String",
Expand Down
3 changes: 1 addition & 2 deletions crates/papyrus_storage/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::sync::Arc;

use libmdbx::{DatabaseFlags, Geometry, PageSize, WriteMap};
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::validators::{validate_ascii, validate_path_exists};
use papyrus_config::validators::validate_ascii;
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_proc_macros::latency_histogram;
use serde::{Deserialize, Serialize};
Expand All @@ -57,7 +57,6 @@ type DbValueType<'env> = Cow<'env, [u8]>;
pub struct DbConfig {
/// The path prefix of the database files. The final path is the path prefix followed by the
/// chain id.
#[validate(custom = "validate_path_exists")]
pub path_prefix: PathBuf,
/// The [chain id](https://docs.rs/starknet_api/latest/starknet_api/core/struct.ChainId.html) of the Starknet network.
#[validate(custom = "validate_ascii")]
Expand Down
10 changes: 9 additions & 1 deletion crates/papyrus_storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub mod test_utils;

use std::collections::{BTreeMap, HashMap};
use std::fmt::Debug;
use std::fs;
use std::sync::Arc;

use body::events::EventIndex;
Expand All @@ -128,7 +129,7 @@ use starknet_api::deprecated_contract_class::ContractClass as DeprecatedContract
use starknet_api::state::{SierraContractClass, StateNumber, StorageKey, ThinStateDiff};
use starknet_api::transaction::{Transaction, TransactionHash, TransactionOutput};
use starknet_types_core::felt::Felt;
use tracing::{debug, warn};
use tracing::{debug, info, warn};
use validator::Validate;
use version::{StorageVersionError, Version};

Expand Down Expand Up @@ -162,6 +163,13 @@ pub const STORAGE_VERSION_BLOCKS: Version = Version { major: 4, minor: 0 };
pub fn open_storage(
storage_config: StorageConfig,
) -> StorageResult<(StorageReader, StorageWriter)> {
if !storage_config.db_config.path_prefix.exists()
&& !storage_config.db_config.enforce_file_exists
{
fs::create_dir_all(storage_config.db_config.path_prefix.clone())?;
info!("Created storage directory: {}", storage_config.db_config.path_prefix.display());
}

let (db_reader, mut db_writer) = open_env(&storage_config.db_config)?;
let tables = Arc::new(Tables {
block_hash_to_number: db_writer.create_simple_table("block_hash_to_number")?,
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ starknet_mempool_p2p.workspace = true
starknet_monitoring_endpoint = { workspace = true, features = ["testing"] }
starknet_sequencer_infra = { workspace = true, features = ["testing"] }
starknet_sequencer_node = { workspace = true, features = ["testing"] }
starknet_state_sync.workspace = true
starknet_task_executor.workspace = true
strum.workspace = true
tempfile.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_integration_tests/src/config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub(crate) fn dump_config_file_changes(
config.http_server_config.ip,
config.http_server_config.port,
config.consensus_manager_config.consensus_config.start_height,
config.state_sync_config.storage_config.db_config.path_prefix,
config.state_sync_config.network_config.tcp_port,
);
let node_config_path = dump_json_data(json_data, NODE_CONFIG_CHANGES_FILE_PATH, dir);
assert!(node_config_path.exists(), "File does not exist: {:?}", node_config_path);
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_integration_tests/src/flow_test_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct SequencerSetup {
// Handlers for the storage files, maintained so the files are not deleted.
pub batcher_storage_file_handle: TempDir,
pub rpc_storage_file_handle: TempDir,
pub state_sync_storage_file_handle: TempDir,

// Handle of the sequencer node.
pub sequencer_node_handle: JoinHandle<Result<(), anyhow::Error>>,
Expand Down Expand Up @@ -125,6 +126,7 @@ impl SequencerSetup {
chain_info,
rpc_server_addr,
storage_for_test.batcher_storage_config,
storage_for_test.state_sync_storage_config,
consensus_manager_config,
)
.await;
Expand All @@ -150,6 +152,7 @@ impl SequencerSetup {
add_tx_http_client,
batcher_storage_file_handle: storage_for_test.batcher_storage_handle,
rpc_storage_file_handle: storage_for_test.rpc_storage_handle,
state_sync_storage_file_handle: storage_for_test.state_sync_storage_handle,
sequencer_node_handle,
config,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct IntegrationTestSetup {
pub node_config_path: PathBuf,
// Storage reader for the batcher.
pub batcher_storage_config: StorageConfig,
// Storage reader for the state sync.
pub state_sync_storage_config: StorageConfig,
// Handlers for the storage and config files, maintained so the files are not deleted. Since
// these are only maintained to avoid dropping the handlers, private visibility suffices, and
// as such, the '#[allow(dead_code)]' attributes are used to suppress the warning.
Expand All @@ -38,6 +40,8 @@ pub struct IntegrationTestSetup {
rpc_storage_handle: TempDir,
#[allow(dead_code)]
node_config_dir_handle: TempDir,
#[allow(dead_code)]
state_sync_storage_handle: TempDir,
}

impl IntegrationTestSetup {
Expand All @@ -62,6 +66,7 @@ impl IntegrationTestSetup {
chain_info,
rpc_server_addr,
storage_for_test.batcher_storage_config,
storage_for_test.state_sync_storage_config,
consensus_manager_configs.pop().unwrap(),
)
.await;
Expand All @@ -88,6 +93,8 @@ impl IntegrationTestSetup {
rpc_storage_handle: storage_for_test.rpc_storage_handle,
node_config_dir_handle,
node_config_path,
state_sync_storage_handle: storage_for_test.state_sync_storage_handle,
state_sync_storage_config: config.state_sync_config.storage_config,
}
}
}
16 changes: 15 additions & 1 deletion crates/starknet_integration_tests/src/state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ type ContractClassesMap =
(Vec<(ClassHash, DeprecatedContractClass)>, Vec<(ClassHash, CasmContractClass)>);

pub struct StorageTestSetup {
// TODO(Shahak): Remove rpc storage reader and handle
pub rpc_storage_reader: StorageReader,
pub rpc_storage_handle: TempDir,
pub batcher_storage_config: StorageConfig,
pub batcher_storage_handle: TempDir,
pub state_sync_storage_config: StorageConfig,
pub state_sync_storage_handle: TempDir,
}

impl StorageTestSetup {
Expand All @@ -68,12 +71,23 @@ impl StorageTestSetup {
.scope(StorageScope::StateOnly)
.chain_id(chain_info.chain_id.clone())
.build();
create_test_state(&mut batcher_storage_writer, chain_info, test_defined_accounts);
create_test_state(&mut batcher_storage_writer, chain_info, test_defined_accounts.clone());
let (
(_, mut state_sync_storage_writer),
state_sync_storage_config,
state_sync_storage_handle,
) = TestStorageBuilder::default()
.scope(StorageScope::FullArchive)
.chain_id(chain_info.chain_id.clone())
.build();
create_test_state(&mut state_sync_storage_writer, chain_info, test_defined_accounts);
Self {
rpc_storage_reader,
rpc_storage_handle: rpc_storage_file_handle,
batcher_storage_config,
batcher_storage_handle: batcher_storage_file_handle,
state_sync_storage_config,
state_sync_storage_handle,
}
}
}
Expand Down
Loading

0 comments on commit 9a29359

Please sign in to comment.