Skip to content

Commit

Permalink
merge fg flexidag
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Dec 25, 2023
1 parent ebe3b03 commit 2199f79
Show file tree
Hide file tree
Showing 77 changed files with 7,208 additions and 212 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ members = [
"cmd/miner_client/api",
"cmd/db-exporter",
"cmd/genesis-nft-miner",
"flexidag",
"flexidag/dag",
]

default-members = [
"consensus/dag",
"benchmarks",
"commons/stest",
"commons/bcs_ext",
Expand Down Expand Up @@ -219,6 +220,8 @@ default-members = [
"stratum",
"cmd/miner_client/api",
"cmd/db-exporter",
"flexidag",
"flexidag/dag",
]

[profile.dev]
Expand Down Expand Up @@ -248,7 +251,7 @@ api-limiter = { path = "commons/api-limiter" }
arc-swap = "1.5.1"
arrayref = "0.3"
ascii = "1.0.0"
async-std = "1.12"
async-std = { version = "1.12", features = ["attributes", "tokio1"] }
async-trait = "0.1.53"
asynchronous-codec = "0.5"
atomic-counter = "1.0.1"
Expand All @@ -259,6 +262,9 @@ bcs-ext = { path = "commons/bcs_ext" }
bech32 = "0.9"
bencher = "0.1.5"
bitflags = "1.3.2"
faster-hex = "0.6"
indexmap = "1.9.1"
bincode = { version = "1", default-features = false }
bs58 = "0.3.1"
byteorder = "1.3.4"
bytes = "1"
Expand Down Expand Up @@ -500,7 +506,8 @@ starcoin-parallel-executor = { path = "vm/parallel-executor" }
starcoin-transaction-benchmarks = { path = "vm/transaction-benchmarks" }
starcoin-language-e2e-tests = { path = "vm/e2e-tests" }
starcoin-proptest-helpers = { path = "vm/proptest-helpers" }

starcoin-flexidag = { path = "flexidag" }
starcoin-dag = {path = "flexidag/dag"}
syn = { version = "1.0.107", features = [
"full",
"extra-traits",
Expand Down Expand Up @@ -535,7 +542,7 @@ walkdir = "2.3.1"
wasm-timer = "0.2"
which = "4.1.0"
zeroize = "1.3.0"
starcoin-dag = {path = "consensus/dag"}

[profile.release.package]
starcoin-service-registry.debug = 1
starcoin-chain.debug = 1
Expand Down
2 changes: 1 addition & 1 deletion account/src/account_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub fn test_wallet_account() -> Result<()> {
);
//println!("verify result is {:?}", sign.verify(&raw_txn, &public_key)?);
println!("public key is {:?}", public_key.to_bytes().as_ref());
println!("hash value is {:?}", &hash_value);
println!("hash value is {:?}", hash_value);
println!("key is {:?}", key.derived_address());
println!("address is {:?},result is {:?}", address, result);

Expand Down
1 change: 1 addition & 0 deletions benchmarks/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starcoin_chain::BlockChain;
use starcoin_chain::{ChainReader, ChainWriter};
use starcoin_config::{temp_dir, ChainNetwork, DataDirPath, RocksdbConfig};
use starcoin_consensus::Consensus;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_genesis::Genesis;
use starcoin_storage::cache_storage::CacheStorage;
use starcoin_storage::db_storage::DBStorage;
Expand Down
8 changes: 5 additions & 3 deletions block-relayer/src/block_relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ impl BlockRelayer {
ctx: &mut ServiceContext<BlockRelayer>,
) -> Result<()> {
let network = ctx.get_shared::<NetworkServiceRef>()?;
let block_connector_service = ctx.service_ref::<BlockConnectorService>()?.clone();
let block_connector_service = ctx
.service_ref::<BlockConnectorService<TxPoolService>>()?
.clone();
let txpool = self.txpool.clone();
let metrics = self.metrics.clone();
let fut = async move {
Expand Down Expand Up @@ -277,7 +279,7 @@ impl EventHandler<Self, NewHeadBlock> for BlockRelayer {
fn handle_event(&mut self, event: NewHeadBlock, ctx: &mut ServiceContext<BlockRelayer>) {
debug!(
"[block-relay] Handle new head block event, block_id: {:?}",
event.0.block().id()
event.executed_block.block().id()
);
let network = match ctx.get_shared::<NetworkServiceRef>() {
Ok(network) => network,
Expand All @@ -286,7 +288,7 @@ impl EventHandler<Self, NewHeadBlock> for BlockRelayer {
return;
}
};
self.broadcast_compact_block(network, event.0);
self.broadcast_compact_block(network, event.executed_block);
}
}

Expand Down
5 changes: 4 additions & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ starcoin-vm-types = { workspace = true }
starcoin-storage = { workspace = true }
thiserror = { workspace = true }
starcoin-network-rpc-api = { workspace = true }
starcoin-dag = {workspace = true}
async-std = { workspace = true }
starcoin-flexidag ={ workspace = true }
starcoin-dag ={ workspace = true }

[dev-dependencies]
proptest = { workspace = true }
proptest-derive = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion chain/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ thiserror = { workspace = true }
starcoin-network-rpc-api = { workspace = true }
starcoin-config = { workspace = true }


[dev-dependencies]

[features]
Expand Down
4 changes: 4 additions & 0 deletions chain/api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2

use anyhow::Result;
use starcoin_config::ChainNetworkID;
use starcoin_crypto::HashValue;
use starcoin_state_api::ChainStateReader;
use starcoin_statedb::ChainStateDB;
Expand Down Expand Up @@ -102,6 +103,7 @@ pub trait ChainReader {
) -> Result<Option<TransactionInfoWithProof>>;

fn current_tips_hash(&self) -> Result<Option<Vec<HashValue>>>;
fn has_dag_block(&self, hash: HashValue) -> Result<bool>;
}

pub trait ChainWriter {
Expand All @@ -113,6 +115,8 @@ pub trait ChainWriter {
fn apply(&mut self, block: Block) -> Result<ExecutedBlock>;

fn chain_state(&mut self) -> &ChainStateDB;

fn update_tips(&mut self, new_tips: Vec<HashValue>) -> Result<()>;
}

/// `Chain` is a trait that defines a single Chain.
Expand Down
3 changes: 3 additions & 0 deletions chain/api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum ChainRequest {
access_path: Option<AccessPath>,
},
GetBlockInfos(Vec<HashValue>),
GetDagBlockChildren {
block_ids: Vec<HashValue>,
}
}

impl ServiceRequest for ChainRequest {
Expand Down
13 changes: 13 additions & 0 deletions chain/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub trait ReadableChainService {
) -> Result<Option<TransactionInfoWithProof>>;

fn get_block_infos(&self, ids: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>>;
fn get_dag_block_children(&self, ids: Vec<HashValue>) -> Result<Vec<HashValue>>;
}

/// Writeable block chain service trait
Expand Down Expand Up @@ -139,6 +140,7 @@ pub trait ChainAsyncService:
) -> Result<Option<TransactionInfoWithProof>>;

async fn get_block_infos(&self, hashes: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>>;
async fn get_dag_block_children(&self, hashes: Vec<HashValue>) -> Result<Vec<HashValue>>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -436,4 +438,15 @@ where
bail!("get block_infos error")
}
}

async fn get_dag_block_children(&self, hashes: Vec<HashValue>) -> Result<Vec<HashValue>> {
let response = self.send(ChainRequest::GetDagBlockChildren {
block_ids: hashes,
}).await??;
if let ChainResponse::HashVec(children) = response {
Ok(children)
} else {
bail!("get dag block children error")
}
}
}
3 changes: 1 addition & 2 deletions chain/chain-notify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ impl EventHandler<Self, NewHeadBlock> for ChainNotifyHandlerService {
item: NewHeadBlock,
ctx: &mut ServiceContext<ChainNotifyHandlerService>,
) {
let NewHeadBlock(block_detail) = item;
let block = block_detail.block();
let block = item.executed_block.block();
// notify header.
self.notify_new_block(block, ctx);
// notify events
Expand Down
11 changes: 3 additions & 8 deletions chain/mock/src/mock_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ impl MockChain {
}

pub fn produce(&self) -> Result<Block> {
let (template, _) = self.head.create_block_template(
*self.miner.address(),
None,
vec![],
vec![],
None,
None,
)?;
let (template, _) =
self.head
.create_block_template(*self.miner.address(), None, vec![], vec![], None, None)?;
self.head
.consensus()
.create_block(template, self.net.time_service().as_ref())
Expand Down
3 changes: 2 additions & 1 deletion chain/service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[dependencies]
anyhow = { workspace = true }
async-std = { workspace = true }
async-trait = { workspace = true }
futures = { workspace = true }
rand = { workspace = true }
Expand Down Expand Up @@ -36,7 +37,7 @@ edition = { workspace = true }
license = { workspace = true }
name = "starcoin-chain-service"
publish = { workspace = true }
version = "1.13.7"
version = "1.13.8"
homepage = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
51 changes: 38 additions & 13 deletions chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use starcoin_chain_api::{
use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
use starcoin_dag::blockdag::BlockDAG;
use starcoin_flexidag::FlexidagService;
use starcoin_logger::prelude::*;

use starcoin_service_registry::{
ActorService, EventHandler, ServiceContext, ServiceFactory, ServiceHandler,
ActorService, EventHandler, ServiceContext, ServiceFactory, ServiceHandler, ServiceRef,
};
use starcoin_storage::{BlockStore, Storage, Store};
use starcoin_types::block::ExecutedBlock;
Expand Down Expand Up @@ -46,11 +46,11 @@ impl ChainReaderService {
) -> Result<Self> {
Ok(Self {
inner: ChainReaderServiceInner::new(
config.clone(),
config,
startup_info,
storage.clone(),
storage,
dag,
vm_metrics.clone(),
vm_metrics,
)?,
})
}
Expand All @@ -63,11 +63,15 @@ impl ServiceFactory<Self> for ChainReaderService {
let startup_info = storage
.get_startup_info()?
.ok_or_else(|| format_err!("StartupInfo should exist at service init."))?;
let dag = ctx.get_shared::<BlockDAG>()?.clone();
let vm_metrics = ctx.get_shared_opt::<VMMetrics>()?;
let dag = ctx
.get_shared_opt::<BlockDAG>()?
.expect("dag should be initialized at service init");
Self::new(config, startup_info, storage, dag, vm_metrics)
Self::new(
config,
startup_info,
storage,
dag,
vm_metrics,
)
}
}

Expand All @@ -85,9 +89,14 @@ impl ActorService for ChainReaderService {

impl EventHandler<Self, NewHeadBlock> for ChainReaderService {
fn handle_event(&mut self, event: NewHeadBlock, _ctx: &mut ServiceContext<ChainReaderService>) {
let new_head = event.0.block().header();
if let Err(e) = if self.inner.get_main().can_connect(event.0.as_ref()) {
self.inner.update_chain_head(event.0.as_ref().clone())
let new_head = event.executed_block.block().header().clone();
if let Err(e) = if self
.inner
.get_main()
.can_connect(event.executed_block.as_ref())
{
self.inner
.update_chain_head(event.executed_block.as_ref().clone())
} else {
self.inner.switch_main(new_head.id())
} {
Expand Down Expand Up @@ -244,6 +253,9 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
ChainRequest::GetBlockInfos(ids) => Ok(ChainResponse::BlockInfoVec(Box::new(
self.inner.get_block_infos(ids)?,
))),
ChainRequest::GetDagBlockChildren { block_ids } => Ok(ChainResponse::HashVec(
self.inner.get_dag_block_children(block_ids)?,
)),
}
}
}
Expand All @@ -253,8 +265,8 @@ pub struct ChainReaderServiceInner {
startup_info: StartupInfo,
main: BlockChain,
storage: Arc<dyn Store>,
vm_metrics: Option<VMMetrics>,
dag: BlockDAG,
vm_metrics: Option<VMMetrics>,
}

impl ChainReaderServiceInner {
Expand Down Expand Up @@ -383,6 +395,7 @@ impl ReadableChainService for ChainReaderServiceInner {
fn main_startup_info(&self) -> StartupInfo {
self.startup_info.clone()
}

fn main_blocks_by_number(
&self,
number: Option<BlockNumber>,
Expand Down Expand Up @@ -433,6 +446,18 @@ impl ReadableChainService for ChainReaderServiceInner {
fn get_block_infos(&self, ids: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>> {
self.storage.get_block_infos(ids)
}

fn get_dag_block_children(&self, ids: Vec<HashValue>) -> Result<Vec<HashValue>> {
ids.into_iter().fold(Ok(vec![]), |mut result, id| {
match self.dag.get_children(id) {
anyhow::Result::Ok(children) => {
result.as_mut().map(|r| r.extend(children));
Ok(result?)
}
Err(e) => Err(e),
}
})
}
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 2199f79

Please sign in to comment.