Skip to content

Commit

Permalink
Integrate fuel-core-sync with importer and p2p (#924)
Browse files Browse the repository at this point in the history
Adds the p2p adapter for fuel-sync 

closes #885
closes #886 
closes #901

Co-authored-by: leviathanbeak88 <elvisdedic@outlook.com>
Co-authored-by: green <xgreenx9999@gmail.com>
Co-authored-by: Brandon Kite <brandonkite92@gmail.com>
Co-authored-by: ControlCplusControlV <44706811+ControlCplusControlV@users.noreply.github.com>
  • Loading branch information
5 people authored Jan 24, 2023
1 parent 84c2ebf commit f43f006
Show file tree
Hide file tree
Showing 39 changed files with 477 additions and 93 deletions.
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.

18 changes: 13 additions & 5 deletions bin/fuel-core/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use anyhow::{
};
use clap::Parser;
use fuel_core::{
chain_config::ChainConfig,
chain_config::{
default_consensus_dev_key,
ChainConfig,
},
producer::Config as ProducerConfig,
service::{
config::{
default_consensus_dev_key,
Trigger,
},
config::Trigger,
Config,
DbType,
ServiceTrait,
Expand Down Expand Up @@ -138,6 +138,10 @@ pub struct Command {
#[cfg(feature = "p2p")]
pub p2p_args: p2p::P2PArgs,

#[cfg_attr(feature = "p2p", clap(flatten))]
#[cfg(feature = "p2p")]
pub sync_args: p2p::SyncArgs,

#[arg(long = "metrics", env)]
pub metrics: bool,
}
Expand All @@ -162,6 +166,8 @@ impl Command {
relayer_args,
#[cfg(feature = "p2p")]
p2p_args,
#[cfg(feature = "p2p")]
sync_args,
metrics,
} = self;

Expand Down Expand Up @@ -235,6 +241,8 @@ impl Command {
relayer: relayer_args.into(),
#[cfg(feature = "p2p")]
p2p: p2p_cfg,
#[cfg(feature = "p2p")]
sync: sync_args.into(),
consensus_key,
})
}
Expand Down
19 changes: 19 additions & 0 deletions bin/fuel-core/src/cli/run/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ pub struct P2PArgs {
pub connection_keep_alive: u64,
}

#[derive(Debug, Clone, Args)]
pub struct SyncArgs {
/// The maximum number of get header requests to make in a single batch.
#[clap(long = "sync_max_get_header", default_value = "10", env)]
pub max_get_header_requests: usize,
/// The maximum number of get transaction requests to make in a single batch.
#[clap(long = "sync_max_get_txns", default_value = "10", env)]
pub max_get_txns_requests: usize,
}

#[derive(Clone, Debug)]
pub enum KeypairArg {
Path(PathBuf),
Expand All @@ -172,6 +182,15 @@ impl KeypairArg {
}
}

impl From<SyncArgs> for fuel_core::sync::Config {
fn from(value: SyncArgs) -> Self {
Self {
max_get_header_requests: value.max_get_header_requests,
max_get_txns_requests: value.max_get_txns_requests,
}
}
}

impl P2PArgs {
pub fn into_config(self, metrics: bool) -> anyhow::Result<Config<NotInitialized>> {
let local_keypair = {
Expand Down
2 changes: 2 additions & 0 deletions crates/chain-config/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod chain;
mod coin;
mod consensus;
mod contract;
mod message;
mod state;

pub use chain::*;
pub use coin::*;
pub use consensus::*;
pub use contract::*;
pub use message::*;
pub use state::*;
Expand Down
42 changes: 31 additions & 11 deletions crates/chain-config/src/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use bech32::{
use fuel_core_storage::MerkleRoot;
use fuel_core_types::{
fuel_crypto::Hasher,
fuel_tx::ConsensusParameters,
fuel_tx::{
ConsensusParameters,
Input,
UtxoId,
},
fuel_types::{
Address,
AssetId,
Expand All @@ -14,6 +18,7 @@ use fuel_core_types::{
fuel_vm::{
GasCosts,
GasCostsValues,
SecretKey,
},
};
use itertools::Itertools;
Expand Down Expand Up @@ -41,7 +46,9 @@ use crate::{
coin::CoinConfig,
state::StateConfig,
},
default_consensus_dev_key,
genesis::GenesisCommitment,
ConsensusConfig,
};

// Fuel Network human-readable part for bech32 encoding
Expand All @@ -63,6 +70,7 @@ pub struct ChainConfig {
#[serde(default)]
#[serde_as(as = "FromInto<GasCostsValues>")]
pub gas_costs: GasCosts,
pub consensus: ConsensusConfig,
}

impl Default for ChainConfig {
Expand All @@ -73,6 +81,9 @@ impl Default for ChainConfig {
transaction_parameters: ConsensusParameters::DEFAULT,
initial_state: None,
gas_costs: GasCosts::default(),
consensus: ConsensusConfig::PoA {
signing_key: Input::owner(&default_consensus_dev_key().public_key()),
},
}
}
}
Expand All @@ -91,23 +102,14 @@ impl ChainConfig {
let bech32_data = Bytes32::new(*address).to_base32();
let bech32_encoding =
bech32::encode(FUEL_BECH32_HRP, bech32_data, Bech32m).unwrap();

tracing::info!(
"PrivateKey({:#x}), Address({:#x} [bech32: {}]), Balance({})",
secret,
address,
bech32_encoding,
TESTNET_INITIAL_BALANCE
);
CoinConfig {
tx_id: None,
output_index: None,
block_created: None,
maturity: None,
owner: address,
amount: TESTNET_INITIAL_BALANCE,
asset_id: Default::default(),
}
Self::initial_coin(secret, TESTNET_INITIAL_BALANCE, None)
})
.collect_vec();

Expand All @@ -120,6 +122,24 @@ impl ChainConfig {
..Default::default()
}
}

pub fn initial_coin(
secret: SecretKey,
amount: u64,
utxo_id: Option<UtxoId>,
) -> CoinConfig {
let address = Address::from(*secret.public_key().hash());

CoinConfig {
tx_id: utxo_id.as_ref().map(|u| *u.tx_id()),
output_index: utxo_id.as_ref().map(|u| u.output_index() as u64),
block_created: None,
maturity: None,
owner: address,
amount,
asset_id: Default::default(),
}
}
}

impl FromStr for ChainConfig {
Expand Down
10 changes: 10 additions & 0 deletions crates/chain-config/src/config/consensus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use fuel_core_types::fuel_types::Address;
use serde::{
Deserialize,
Serialize,
};

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum ConsensusConfig {
PoA { signing_key: Address },
}
9 changes: 9 additions & 0 deletions crates/chain-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ mod genesis;
mod serialization;

pub use config::*;
use fuel_core_types::fuel_vm::SecretKey;
pub use genesis::GenesisCommitment;

/// A default secret key to use for testing purposes only
pub fn default_consensus_dev_key() -> SecretKey {
const DEV_KEY_PHRASE: &str =
"winner alley monkey elephant sun off boil hope toward boss bronze dish";
SecretKey::new_from_mnemonic_phrase_with_path(DEV_KEY_PHRASE, "m/44'/60'/0'/0/0")
.expect("valid key")
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,10 @@ expression: json
"base": 424,
"dep_per_unit": 6
}
},
"consensus": {
"PoA": {
"signing_key": "22ec92c3105c942a6640bdc4e4907286ec4728e8cfc0d8ac59aad4d8e1ccaefb"
}
}
}
3 changes: 2 additions & 1 deletion crates/fuel-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fuel-core-producer = { path = "../services/producer", version = "0.15.1" }
fuel-core-relayer = { path = "../services/relayer", version = "0.15.1", optional = true }
fuel-core-services = { path = "../services", version = "0.15.1" }
fuel-core-storage = { path = "../storage", version = "0.15.1" }
fuel-core-sync = { path = "../services/sync", version = "0.15.1", optional = true }
fuel-core-txpool = { path = "../services/txpool", version = "0.15.1" }
fuel-core-types = { path = "../types", version = "0.15.1", features = [
"serde",
Expand Down Expand Up @@ -71,7 +72,7 @@ test-case = "2.2"
debug = ["fuel-core-types/debug"]
default = ["debug", "metrics", "rocksdb"]
metrics = ["dep:fuel-core-metrics"]
p2p = ["dep:fuel-core-p2p"]
p2p = ["dep:fuel-core-p2p", "dep:fuel-core-sync"]
relayer = ["dep:fuel-core-relayer"]
rocksdb = ["dep:rocksdb"]
# features to enable in production, but increase build times
Expand Down
3 changes: 3 additions & 0 deletions crates/fuel-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use fuel_core_producer as producer;
#[cfg(feature = "relayer")]
#[doc(no_inline)]
pub use fuel_core_relayer as relayer;
#[cfg(feature = "p2p")]
#[doc(no_inline)]
pub use fuel_core_sync as sync;
#[doc(no_inline)]
pub use fuel_core_txpool as txpool;
#[doc(no_inline)]
Expand Down
4 changes: 4 additions & 0 deletions crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub use config::{
};
pub use fuel_core_services::Service as ServiceTrait;

use self::adapters::BlockImporterAdapter;

pub mod adapters;
pub mod config;
pub(crate) mod genesis;
Expand All @@ -40,6 +42,8 @@ pub struct SharedState {
pub relayer: Option<fuel_core_relayer::SharedState>,
/// The GraphQL shared state.
pub graph_ql: crate::fuel_core_graphql_api::service::SharedState,
/// Subscribe to new block production.
pub block_importer: BlockImporterAdapter,
}

pub struct FuelService {
Expand Down
4 changes: 4 additions & 0 deletions crates/fuel-core/src/service/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub mod p2p;
pub mod producer;
pub mod txpool;

#[cfg(feature = "p2p")]
pub mod sync;

#[derive(Clone)]
pub struct PoAAdapter {
shared_state: fuel_core_poa::service::SharedState,
Expand Down Expand Up @@ -57,6 +60,7 @@ pub struct BlockProducerAdapter {
pub struct BlockImporterAdapter {
pub block_importer:
Arc<fuel_core_importer::Importer<Database, ExecutorAdapter, VerifierAdapter>>,
execution_semaphore: Arc<tokio::sync::Semaphore>,
}

#[cfg(feature = "p2p")]
Expand Down
Loading

0 comments on commit f43f006

Please sign in to comment.