|
|
@@ -16,9 +16,9 @@
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-use darkfi::{system::sleep, util::encoding::base64};
|
|
|
+use darkfi::{system::sleep, util::encoding::base64, Result};
|
|
|
use darkfi_serial::serialize;
|
|
|
-use log::{debug, info, warn, error};
|
|
|
+use log::{debug, info, warn};
|
|
|
use tinyjson::JsonValue;
|
|
|
|
|
|
use crate::{
|
|
|
@@ -27,7 +27,7 @@ use crate::{
|
|
|
};
|
|
|
|
|
|
/// async task used for block syncing
|
|
|
-pub async fn sync_task(node: &Darkfid) {
|
|
|
+pub async fn sync_task(node: &Darkfid) -> Result<()> {
|
|
|
info!(target: "darkfid::task::sync_task", "Starting blockchain sync...");
|
|
|
// Block until at least node is connected to at least one peer
|
|
|
loop {
|
|
|
@@ -44,13 +44,7 @@ pub async fn sync_task(node: &Darkfid) {
|
|
|
// Communication setup
|
|
|
let msg_subsystem = channel.message_subsystem();
|
|
|
msg_subsystem.add_dispatch::<SyncResponse>().await;
|
|
|
- let block_response_sub = match channel.subscribe_msg::<SyncResponse>().await {
|
|
|
- Err(why) => {
|
|
|
- // if there is Error::NetworkOperationFailed returns at dispatcher subscription attempt
|
|
|
- panic!("darkiid2::task::sync_task channel subscribe_msg failed at dispatcher subscription: {:?}", why);
|
|
|
- },
|
|
|
- Ok(value) => value
|
|
|
- };
|
|
|
+ let block_response_sub = channel.subscribe_msg::<SyncResponse>().await?;
|
|
|
let notif_sub = node.subscribers.get("blocks").unwrap();
|
|
|
|
|
|
// TODO: make this parallel and use a head selection method,
|
|
|
@@ -61,36 +55,20 @@ pub async fn sync_task(node: &Darkfid) {
|
|
|
// Node sends the last known block hash of the canonical blockchain
|
|
|
// and loops until the response is the same block (used to utilize
|
|
|
// batch requests).
|
|
|
- let mut last = match node.validator.read().await.blockchain.last() {
|
|
|
- Err(why) => {
|
|
|
- panic!("darkfid::task::sync_task attempting to retrive last block at empty BlockOrderStore sledTree: {:?}", why)
|
|
|
- },
|
|
|
- Ok(value) => value
|
|
|
- };
|
|
|
+ let mut last = node.validator.read().await.blockchain.last()?;
|
|
|
info!(target: "darkfid::task::sync_task", "Last known block: {:?} - {:?}", last.0, last.1);
|
|
|
loop {
|
|
|
// Node creates a `SyncRequest` and sends it
|
|
|
let request = SyncRequest { slot: last.0, block: last.1 };
|
|
|
- if let Err(why) = channel.send(&request).await {
|
|
|
- error!(target: "darkfid::task::sync_task", "request send failure: {:}", why);
|
|
|
- sleep(10).await;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ channel.send(&request).await?;
|
|
|
|
|
|
// TODO: add a timeout here to retry
|
|
|
// Node waits for response
|
|
|
- let response = match block_response_sub.receive().await {
|
|
|
- Err(why) => {
|
|
|
- panic!("darkfid::task::sync_task block_response_sub receive error at recv_queue recv: {:?}", why)
|
|
|
- },
|
|
|
- Ok(value) => value
|
|
|
- };
|
|
|
+ let response = block_response_sub.receive().await?;
|
|
|
|
|
|
// Verify and store retrieved blocks
|
|
|
debug!(target: "darkfid::task::sync_task", "Processing received blocks");
|
|
|
- if let Err(why) = node.validator.write().await.add_blocks(&response.blocks).await {
|
|
|
- panic!("darkfid::task::sync_task block validation failed: {:?}", why)
|
|
|
- };
|
|
|
+ node.validator.write().await.add_blocks(&response.blocks).await?;
|
|
|
|
|
|
// Notify subscriber
|
|
|
for block in &response.blocks {
|
|
|
@@ -98,12 +76,7 @@ pub async fn sync_task(node: &Darkfid) {
|
|
|
notif_sub.notify(vec![encoded_block].into()).await;
|
|
|
}
|
|
|
|
|
|
- let last_received = match node.validator.read().await.blockchain.last() {
|
|
|
- Err(why) => {
|
|
|
- panic!("darkfid::task::sync_task attempting to retrive last block at empty BlockOrderStore sledTree: {:?}", why)
|
|
|
- },
|
|
|
- Ok(value) => value
|
|
|
- };
|
|
|
+ let last_received = node.validator.read().await.blockchain.last()?;
|
|
|
info!(target: "darkfid::task::sync_task", "Last received block: {:?} - {:?}", last_received.0, last_received.1);
|
|
|
|
|
|
if last == last_received {
|
|
|
@@ -115,4 +88,5 @@ pub async fn sync_task(node: &Darkfid) {
|
|
|
|
|
|
node.validator.write().await.synced = true;
|
|
|
info!(target: "darkfid::task::sync_task", "Blockchain synced!");
|
|
|
+ Ok(())
|
|
|
}
|