Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge smolstc #3927

Merged
merged 33 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a2bdebb
add storage of dag accumulator
jackzhhuang Jul 24, 2023
9cb07f3
add flexi dag
jackzhhuang Jul 24, 2023
f744009
Merge branch 'starcoinorg:dag' into dag
jackzhhuang Jul 24, 2023
13e3ba1
merge from starcoin dag
jackzhhuang Jul 24, 2023
e5444e1
add dag block
jackzhhuang Jul 25, 2023
2ecb3e0
add dag block
jackzhhuang Jul 25, 2023
76a6f75
merge smolstc
jackzhhuang Jul 26, 2023
3b8fe03
add get accumulator leaves
jackzhhuang Jul 26, 2023
e400a9a
add gene::server to get the dag accumulator leaves
jackzhhuang Jul 26, 2023
01ff8fe
move database to consensusdb
simonjiao Jul 26, 2023
ac87398
make consensus module buildable
simonjiao Jul 26, 2023
397a7e9
make whole project buildable
simonjiao Jul 26, 2023
4f24494
rename consensusdb module
simonjiao Jul 26, 2023
e57ff8e
downgrade ghostdag and reachability crates
simonjiao Jul 26, 2023
affce35
downgrade dag-consensus crate
simonjiao Jul 26, 2023
73f240a
fix consensus tests
simonjiao Jul 26, 2023
221d453
expose neccessary dag-consensus types
simonjiao Jul 26, 2023
857a07f
Merge pull request #1 from simonjiao/migrate-smol
jackzhhuang Jul 27, 2023
727fcd5
add get dag accumulator details
jackzhhuang Jul 27, 2023
9568ba7
add verified client for dag
jackzhhuang Jul 27, 2023
ae32425
add sync task for getting the leaves of an accumulator
jackzhhuang Jul 27, 2023
7a448cf
add sync dag accumulator task
jackzhhuang Jul 27, 2023
6088b60
add dag block sync task
jackzhhuang Jul 27, 2023
b740bef
add chain info v2 including original chain info
jackzhhuang Jul 27, 2023
747aafa
use chain state info instead of chain info v2
jackzhhuang Jul 28, 2023
4eec0db
add sync dag ancestor
jackzhhuang Jul 28, 2023
543d38b
add sync dag accumulator task
jackzhhuang Jul 28, 2023
7544588
add dbupgrade
jackzhhuang Jul 28, 2023
23547cd
merge dag form simon
jackzhhuang Jul 31, 2023
dff2020
Merge branch 'starcoinorg:dag' into dag
jackzhhuang Jul 31, 2023
3c64e6e
use chain state info
jackzhhuang Jul 31, 2023
3af937a
block info ext including block info
jackzhhuang Jul 31, 2023
ea020d7
add header and block info ext
jackzhhuang Jul 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ starcoin-types = { package = "starcoin-types", workspace = true }
starcoin-vm-types = { workspace = true }
starcoin-storage = { workspace = true }
thiserror = { workspace = true }
starcoin-network-rpc-api = { workspace = true }

[dev-dependencies]
proptest = { workspace = true }
Expand All @@ -39,6 +40,7 @@ stdlib = { workspace = true }
stest = { workspace = true }
test-helper = { workspace = true }
tokio = { features = ["full"], workspace = true }
starcoin-network-rpc-api = { workspace = true }

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions chain/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ starcoin-time-service = { workspace = true }
starcoin-types = { workspace = true }
starcoin-vm-types = { workspace = true }
thiserror = { workspace = true }
starcoin-network-rpc-api = { workspace = true }


[dev-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions chain/api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use crate::TransactionInfoWithProof;
use anyhow::Result;
use starcoin_crypto::HashValue;
use starcoin_network_rpc_api::dag_protocol::{
TargetDagAccumulatorLeaf, TargetDagAccumulatorLeafDetail,
};
use starcoin_service_registry::ServiceRequest;
use starcoin_types::transaction::RichTransactionInfo;
use starcoin_types::{
Expand Down Expand Up @@ -60,6 +63,14 @@ pub enum ChainRequest {
access_path: Option<AccessPath>,
},
GetBlockInfos(Vec<HashValue>),
GetDagAccumulatorLeaves {
start_index: u64,
batch_size: u64,
},
GetTargetDagAccumulatorLeafDetail {
leaf_index: u64,
batch_size: u64,
},
}

impl ServiceRequest for ChainRequest {
Expand Down Expand Up @@ -88,4 +99,6 @@ pub enum ChainResponse {
HashVec(Vec<HashValue>),
TransactionProof(Box<Option<TransactionInfoWithProof>>),
BlockInfoVec(Box<Vec<Option<BlockInfo>>>),
TargetDagAccumulatorLeaf(Vec<TargetDagAccumulatorLeaf>),
TargetDagAccumulatorLeafDetail(Vec<TargetDagAccumulatorLeafDetail>),
}
43 changes: 43 additions & 0 deletions chain/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::message::{ChainRequest, ChainResponse};
use crate::TransactionInfoWithProof;
use anyhow::{bail, Result};
use starcoin_crypto::HashValue;
use starcoin_network_rpc_api::dag_protocol;
use starcoin_service_registry::{ActorService, ServiceHandler, ServiceRef};
use starcoin_types::contract_event::{ContractEvent, ContractEventInfo};
use starcoin_types::filter::Filter;
Expand Down Expand Up @@ -139,6 +140,14 @@ pub trait ChainAsyncService:
) -> Result<Option<TransactionInfoWithProof>>;

async fn get_block_infos(&self, hashes: Vec<HashValue>) -> Result<Vec<Option<BlockInfo>>>;
async fn get_dag_accumulator_leaves(
&self,
req: dag_protocol::GetDagAccumulatorLeaves,
) -> Result<Vec<dag_protocol::TargetDagAccumulatorLeaf>>;
async fn get_dag_accumulator_leaves_detail(
&self,
req: dag_protocol::GetTargetDagAccumulatorLeafDetail,
) -> Result<Option<Vec<dag_protocol::TargetDagAccumulatorLeafDetail>>>;
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -180,6 +189,40 @@ where
}
}

async fn get_dag_accumulator_leaves(
&self,
req: dag_protocol::GetDagAccumulatorLeaves,
) -> Result<Vec<dag_protocol::TargetDagAccumulatorLeaf>> {
if let ChainResponse::TargetDagAccumulatorLeaf(leaves) = self
.send(ChainRequest::GetDagAccumulatorLeaves {
start_index: req.accumulator_leaf_index,
batch_size: req.batch_size,
})
.await??
{
Ok(leaves)
} else {
bail!("get_dag_accumulator_leaves response type error.")
}
}

async fn get_dag_accumulator_leaves_detail(
&self,
req: dag_protocol::GetTargetDagAccumulatorLeafDetail,
) -> Result<Option<Vec<dag_protocol::TargetDagAccumulatorLeafDetail>>> {
if let ChainResponse::TargetDagAccumulatorLeafDetail(details) = self
.send(ChainRequest::GetTargetDagAccumulatorLeafDetail {
leaf_index: req.leaf_index,
batch_size: req.batch_size,
})
.await??
{
Ok(Some(details))
} else {
Ok(None)
}
}

async fn get_headers(&self, ids: Vec<HashValue>) -> Result<Vec<Option<BlockHeader>>> {
if let ChainResponse::BlockHeaderVec(headers) =
self.send(ChainRequest::GetHeaders(ids)).await??
Expand Down
1 change: 1 addition & 0 deletions chain/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ starcoin-vm-runtime = { workspace = true }
starcoin-vm-types = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
starcoin-network-rpc-api = { workspace = true }

[dev-dependencies]
stest = { workspace = true }
Expand Down
35 changes: 34 additions & 1 deletion chain/service/src/chain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{format_err, Error, Result};
use starcoin_chain::dag_chain::DagBlockChain;
use starcoin_chain::BlockChain;
use starcoin_chain_api::message::{ChainRequest, ChainResponse};
use starcoin_chain_api::{
Expand All @@ -10,6 +11,9 @@ use starcoin_chain_api::{
use starcoin_config::NodeConfig;
use starcoin_crypto::HashValue;
use starcoin_logger::prelude::*;
use starcoin_network_rpc_api::dag_protocol::{
GetDagAccumulatorLeaves, GetTargetDagAccumulatorLeafDetail,
};
use starcoin_service_registry::{
ActorService, EventHandler, ServiceContext, ServiceFactory, ServiceHandler,
};
Expand All @@ -32,6 +36,8 @@ use std::sync::Arc;
/// A Chain reader service to provider Reader API.
pub struct ChainReaderService {
inner: ChainReaderServiceInner,

dag_chain: DagBlockChain,
}

impl ChainReaderService {
Expand All @@ -42,7 +48,13 @@ impl ChainReaderService {
vm_metrics: Option<VMMetrics>,
) -> Result<Self> {
Ok(Self {
inner: ChainReaderServiceInner::new(config, startup_info, storage, vm_metrics)?,
inner: ChainReaderServiceInner::new(
config.clone(),
startup_info,
storage.clone(),
vm_metrics.clone(),
)?,
dag_chain: DagBlockChain::new(config.clone(), storage.clone(), vm_metrics)?,
})
}
}
Expand Down Expand Up @@ -232,6 +244,27 @@ impl ServiceHandler<Self, ChainRequest> for ChainReaderService {
ChainRequest::GetBlockInfos(ids) => Ok(ChainResponse::BlockInfoVec(Box::new(
self.inner.get_block_infos(ids)?,
))),
ChainRequest::GetDagAccumulatorLeaves {
start_index,
batch_size,
} => Ok(ChainResponse::TargetDagAccumulatorLeaf(
self.dag_chain
.get_accumulator_leaves(GetDagAccumulatorLeaves {
accumulator_leaf_index: start_index,
batch_size,
})?,
)),
ChainRequest::GetTargetDagAccumulatorLeafDetail {
leaf_index,
batch_size,
} => Ok(ChainResponse::TargetDagAccumulatorLeafDetail(
self.dag_chain.get_target_dag_accumulator_leaf_detail(
GetTargetDagAccumulatorLeafDetail {
leaf_index,
batch_size,
},
)?,
)),
}
}
}
Expand Down
162 changes: 162 additions & 0 deletions chain/src/dag_chain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use std::sync::Arc;

use anyhow::bail;
use starcoin_accumulator::Accumulator;
use starcoin_accumulator::{node::AccumulatorStoreType, MerkleAccumulator};
use starcoin_config::NodeConfig;
use starcoin_consensus::{BlockDAG, FlexiDagStorage, FlexiDagStorageConfig};
use starcoin_crypto::HashValue;
use starcoin_executor::VMMetrics;
use starcoin_network_rpc_api::dag_protocol::{
GetDagAccumulatorLeaves, GetTargetDagAccumulatorLeafDetail, RelationshipPair,
TargetDagAccumulatorLeaf, TargetDagAccumulatorLeafDetail,
};
use starcoin_storage::storage::CodecKVStore;
use starcoin_storage::{flexi_dag::SyncFlexiDagSnapshotStorage, Store};
use starcoin_types::block::BlockHeader;
use starcoin_types::{blockhash::ORIGIN, header::Header};

pub struct DagBlockChain {
dag: Option<BlockDAG>,
dag_sync_accumulator: MerkleAccumulator,
dag_sync_accumulator_snapshot: Arc<SyncFlexiDagSnapshotStorage>,
}

impl DagBlockChain {
pub fn new(
config: Arc<NodeConfig>,
storage: Arc<dyn Store>,
_vm_metrics: Option<VMMetrics>,
) -> anyhow::Result<Self> {
// initialize the dag
let db_path = config.storage.dir();
let config = FlexiDagStorageConfig::create_with_params(1, 0, 1024);
let db = FlexiDagStorage::create_from_path(db_path, config)?;
let dag = BlockDAG::new(
Header::new(BlockHeader::random(), vec![HashValue::new(ORIGIN)]),
16,
db,
);

// initialize the block accumulator
let startup_info = match storage.get_flexi_dag_startup_info()? {
Some(startup_info) => startup_info,
None => {
return Ok(Self {
dag: Some(dag),
dag_sync_accumulator: MerkleAccumulator::new_empty(
storage.get_accumulator_store(AccumulatorStoreType::SyncDag),
),
dag_sync_accumulator_snapshot: storage.get_accumulator_snapshot_storage(),
})
}
};

// let accmulator_info = sync_flexi_dag_store.get_snapshot_storage().get(startup_info.main);
let accumulator_info = match storage.query_by_hash(startup_info.main) {
Ok(op_snapshot) => match op_snapshot {
Some(snapshot) => snapshot.dag_accumulator_info,
None => bail!("failed to get sync accumulator info since it is None"),
},
Err(error) => bail!("failed to get sync accumulator info: {}", error.to_string()),
};

Ok(Self {
dag: Some(dag),
dag_sync_accumulator: MerkleAccumulator::new_with_info(
accumulator_info,
storage.get_accumulator_store(AccumulatorStoreType::SyncDag),
),
dag_sync_accumulator_snapshot: storage.get_accumulator_snapshot_storage(),
})
}

pub fn get_accumulator_leaves(
&self,
req: GetDagAccumulatorLeaves,
) -> anyhow::Result<Vec<TargetDagAccumulatorLeaf>> {
if self.dag.is_none() {
bail!("the dag is None");
}
match self
.dag_sync_accumulator
.get_leaves(req.accumulator_leaf_index, true, req.batch_size)
{
Ok(leaves) => Ok(leaves
.into_iter()
.enumerate()
.map(
|(index, leaf)| match self.dag_sync_accumulator_snapshot.get(leaf) {
Ok(op_snapshot) => {
let snapshot = op_snapshot.expect("snapshot must exist");
TargetDagAccumulatorLeaf {
accumulator_root: snapshot.dag_accumulator_info.accumulator_root,
leaf_index: req.accumulator_leaf_index.saturating_sub(index as u64),
}
}
Err(error) => {
panic!(
"error occured when query the accumulator snapshot: {}",
error.to_string()
);
}
},
)
.collect()),
Err(error) => {
bail!(
"an error occured when getting the leaves of the accumulator, {}",
error.to_string()
);
}
}
}

pub fn get_target_dag_accumulator_leaf_detail(
&self,
req: GetTargetDagAccumulatorLeafDetail,
) -> anyhow::Result<Vec<TargetDagAccumulatorLeafDetail>> {
let dag = if self.dag.is_some() {
self.dag.as_ref().unwrap()
} else {
bail!("the dag is None");
};
let end_index = std::cmp::min(
req.leaf_index + req.batch_size - 1,
self.dag_sync_accumulator.get_info().num_leaves - 1,
);
let mut details = [].to_vec();
for index in req.leaf_index..=end_index {
let leaf_hash = self
.dag_sync_accumulator
.get_leaf(index)
.unwrap_or(None)
.expect("leaf hash should not be None");
let snapshot = self
.dag_sync_accumulator_snapshot
.get(leaf_hash)
.unwrap_or(None)
.expect("the snapshot should not be None");
let mut relationship_pair = [].to_vec();
relationship_pair.extend(
snapshot
.child_hashes
.into_iter()
.fold([].to_vec(), |mut pairs, child| {
let parents = dag.get_parents(child).expect("a child must have parents");
parents.into_iter().for_each(|parent| {
pairs.push(RelationshipPair { parent, child });
});
pairs
})
.into_iter(),
);

details.push(TargetDagAccumulatorLeafDetail {
accumulator_root: snapshot.dag_accumulator_info.accumulator_root,
relationship_pair,
});
}
Ok(details)
}
}
1 change: 1 addition & 0 deletions chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
#![deny(clippy::integer_arithmetic)]
mod chain;
pub mod dag_chain;
pub mod verifier;
pub use chain::BlockChain;
pub use starcoin_chain_api::{ChainReader, ChainWriter};
Loading
Loading