|
24 | 24 | #include "process_launcher.h" |
25 | 25 | #include "rpc_connections.h" |
26 | 26 | #include "sig_term.h" |
27 | | -#include "snapshots.h" |
| 27 | +#include "snapshots/fetch.h" |
| 28 | +#include "snapshots/snapshot_manager.h" |
28 | 29 | #include "ticker.h" |
29 | 30 | #include "time_updater.h" |
30 | 31 |
|
@@ -376,7 +377,7 @@ int main(int argc, char** argv) |
376 | 377 | config.ledger.read_only_directories); |
377 | 378 | ledger.register_message_handlers(bp.get_dispatcher()); |
378 | 379 |
|
379 | | - asynchost::SnapshotManager snapshots( |
| 380 | + snapshots::SnapshotManager snapshots( |
380 | 381 | config.snapshots.directory, |
381 | 382 | writer_factory, |
382 | 383 | config.snapshots.read_only_directory); |
@@ -507,8 +508,6 @@ int main(int argc, char** argv) |
507 | 508 |
|
508 | 509 | ccf::StartupConfig startup_config(config); |
509 | 510 |
|
510 | | - startup_config.snapshot_tx_interval = config.snapshots.tx_count; |
511 | | - |
512 | 511 | if (startup_config.attestation.snp_security_policy_file.has_value()) |
513 | 512 | { |
514 | 513 | auto security_policy_file = |
@@ -690,22 +689,62 @@ int main(int argc, char** argv) |
690 | 689 | config.command.type == StartType::Join || |
691 | 690 | config.command.type == StartType::Recover) |
692 | 691 | { |
693 | | - auto latest_committed_snapshot = |
694 | | - snapshots.find_latest_committed_snapshot(); |
695 | | - if (latest_committed_snapshot.has_value()) |
696 | | - { |
697 | | - auto& [snapshot_dir, snapshot_file] = latest_committed_snapshot.value(); |
698 | | - startup_snapshot = files::slurp(snapshot_dir / snapshot_file); |
| 692 | + auto latest_local_snapshot = snapshots.find_latest_committed_snapshot(); |
699 | 693 |
|
700 | | - LOG_INFO_FMT( |
701 | | - "Found latest snapshot file: {} (size: {})", |
702 | | - snapshot_dir / snapshot_file, |
703 | | - startup_snapshot.size()); |
| 694 | + if ( |
| 695 | + config.command.type == StartType::Join && |
| 696 | + config.command.join.fetch_recent_snapshot) |
| 697 | + { |
| 698 | + // Try to fetch a recent snapshot from peer |
| 699 | + const size_t latest_local_idx = latest_local_snapshot.has_value() ? |
| 700 | + snapshots::get_snapshot_idx_from_file_name( |
| 701 | + latest_local_snapshot->second) : |
| 702 | + 0; |
| 703 | + auto latest_peer_snapshot = snapshots::fetch_from_peer( |
| 704 | + config.command.join.target_rpc_address, |
| 705 | + config.command.service_certificate_file, |
| 706 | + latest_local_idx); |
| 707 | + |
| 708 | + if (latest_peer_snapshot.has_value()) |
| 709 | + { |
| 710 | + LOG_INFO_FMT( |
| 711 | + "Received snapshot {} from peer (size: {}) - writing this to disk " |
| 712 | + "and using for join startup", |
| 713 | + latest_peer_snapshot->snapshot_name, |
| 714 | + latest_peer_snapshot->snapshot_data.size()); |
| 715 | + |
| 716 | + const auto dst_path = fs::path(config.snapshots.directory) / |
| 717 | + fs::path(latest_peer_snapshot->snapshot_name); |
| 718 | + if (files::exists(dst_path)) |
| 719 | + { |
| 720 | + LOG_FATAL_FMT( |
| 721 | + "Unable to write peer snapshot - already have a file at {}. " |
| 722 | + "Exiting.", |
| 723 | + dst_path); |
| 724 | + return static_cast<int>(CLI::ExitCodes::FileError); |
| 725 | + } |
| 726 | + files::dump(latest_peer_snapshot->snapshot_data, dst_path); |
| 727 | + startup_snapshot = latest_peer_snapshot->snapshot_data; |
| 728 | + } |
704 | 729 | } |
705 | | - else |
| 730 | + |
| 731 | + if (startup_snapshot.empty()) |
706 | 732 | { |
707 | | - LOG_INFO_FMT( |
708 | | - "No snapshot found: Node will replay all historical transactions"); |
| 733 | + if (latest_local_snapshot.has_value()) |
| 734 | + { |
| 735 | + auto& [snapshot_dir, snapshot_file] = latest_local_snapshot.value(); |
| 736 | + startup_snapshot = files::slurp(snapshot_dir / snapshot_file); |
| 737 | + |
| 738 | + LOG_INFO_FMT( |
| 739 | + "Found latest local snapshot file: {} (size: {})", |
| 740 | + snapshot_dir / snapshot_file, |
| 741 | + startup_snapshot.size()); |
| 742 | + } |
| 743 | + else |
| 744 | + { |
| 745 | + LOG_INFO_FMT( |
| 746 | + "No snapshot found: Node will replay all historical transactions"); |
| 747 | + } |
709 | 748 | } |
710 | 749 | } |
711 | 750 |
|
|
0 commit comments