Skip to content
Merged
Show file tree
Hide file tree
Changes from 182 commits
Commits
Show all changes
190 commits
Select commit Hold shift + click to select a range
4667163
add enable-experimental-collator-protocol CLI flag and new subsystem …
alindima Apr 3, 2025
96b2cd1
fix clap arg
alindima Apr 3, 2025
1d283d5
rollback experimental cli flag
alindima Apr 3, 2025
73b61fd
WIP peer manager impl
alindima Apr 7, 2025
d943dbb
continue impl
alindima Apr 8, 2025
2bfdaf0
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Apr 9, 2025
fa731c6
make taplo happy
alindima Apr 9, 2025
1691ec8
minor fix
alindima Apr 9, 2025
75f00f4
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Apr 11, 2025
3b17c38
fix todo after merging previous PR
alindima Apr 11, 2025
41127ae
initial memory db impl
alindima Apr 11, 2025
2bf2b05
satisfy clippy
alindima Apr 14, 2025
6fa2d4e
WIP
alindima Apr 23, 2025
f5ae202
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Apr 28, 2025
32c30ea
review comments
alindima Apr 29, 2025
5271ad7
switch to processing rep bumps only for finalized block notifications
alindima Apr 29, 2025
86cd891
add unit tests for Score primitive
alindima Apr 29, 2025
7ffde5c
some unit tests
alindima Apr 29, 2025
b5e1b13
some fixes and almost all needed unit tests for ConnectedPeers
alindima Apr 29, 2025
81140bc
add test for update_reputation
alindima Apr 30, 2025
c42c33b
some fixes
alindima Apr 30, 2025
608fcb0
add a log
alindima Apr 30, 2025
e37e6d3
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Apr 30, 2025
b23c4e1
enforce limit is non-zero
alindima Apr 30, 2025
e71d4e8
Merge remote-tracking branch 'origin/alindima/collator-protocol-revam…
alindima Apr 30, 2025
24a9097
fix merge damage, most importantly handling finalized blocks instead …
alindima Apr 30, 2025
4205200
Merge remote-tracking branch 'origin/alindima/collator-protocol-revam…
alindima May 1, 2025
82078e6
WIP
alindima May 6, 2025
7a490a9
still WIP
alindima May 7, 2025
c82006a
more work
alindima May 8, 2025
d90e5b5
working version: some fixes and logs
alindima May 13, 2025
c45e8c3
add potential delay to fetching collations from zero-rep peers
alindima May 14, 2025
cfc1514
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 14, 2025
cdb15dd
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 14, 2025
c902177
Merge remote-tracking branch 'origin/alindima/collator-protocol-revam…
alindima May 14, 2025
805b0c5
fix existing tests
alindima May 15, 2025
f1cc5de
clippy
alindima May 15, 2025
e177b99
fix outrageously large log
alindima May 15, 2025
f007cc0
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 28, 2025
e7e6e62
review feedback
alindima May 28, 2025
b8051c9
Merge branch 'master' into alindima/collator-protocol-revamp-reputati…
alindima May 28, 2025
5ae5351
Merge remote-tracking branch 'origin/alindima/collator-protocol-revam…
alindima May 28, 2025
0e28f8b
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 28, 2025
264d2cc
rollback changes to backing implicit view
alindima May 29, 2025
fb29312
remove unused stuff and random bits of work
alindima May 29, 2025
53922b6
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 29, 2025
8be53f7
restructure some code
alindima May 29, 2025
6b34df7
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima May 30, 2025
9109849
only sleep if we know we have better peers
alindima May 30, 2025
926de29
fix todo
alindima Jun 2, 2025
b362c0c
some unit testing
alindima Jun 4, 2025
65b28dd
more testing
alindima Jun 6, 2025
3368bba
testing testing testing
alindima Jun 11, 2025
e0bef16
test no assignments
alindima Jun 11, 2025
a27d37a
only query the keystore once per session
alindima Jun 12, 2025
8ae174c
moore testing
alindima Jun 16, 2025
4074e66
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 16, 2025
85ee00b
start testing fetches
alindima Jun 16, 2025
babd6e7
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 17, 2025
38dcc20
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 19, 2025
c8b8c72
deduplicate descriptor version sanity check
alindima Jun 19, 2025
0b933a9
some more testing scenarios for failed fetches
alindima Jun 19, 2025
30025c2
test v1 compat
alindima Jun 19, 2025
505841c
stupid clippy
alindima Jun 20, 2025
9dc28e1
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 20, 2025
0c90ab4
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 23, 2025
50883a2
fix nasty bug in polling requests
alindima Jun 25, 2025
cc026ad
some logs
alindima Jun 25, 2025
d269a5d
more testing
alindima Jun 25, 2025
c5b6562
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 25, 2025
83f5b43
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jun 26, 2025
46000b0
more testing and bugfixing
alindima Jul 4, 2025
4b17bac
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jul 4, 2025
f570ba6
some refactoring and bugfixing
alindima Jul 9, 2025
4d1eb80
a v1 advertisement test
alindima Jul 9, 2025
4bce989
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jul 9, 2025
fe2b102
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jul 24, 2025
fdc8e01
fix fix
alindima Jul 25, 2025
aaf3a00
impl paraid pruning using the new runtime api
alindima Jul 28, 2025
98f7d19
Merge remote-tracking branch 'origin/master' into alindima/collator-p…
alindima Jul 29, 2025
8ac8f7b
Add missing doc comments in claim_queue_state (#10241)
tdimitrov Nov 7, 2025
d06d771
Fix commented out tests (#10245)
tdimitrov Nov 7, 2025
1b1b562
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Nov 11, 2025
ff56674
add trace logs which was removed during the merge
tdimitrov Nov 11, 2025
11a9474
Add `--experimental-collator-protocol` cli argument to enable the new…
tdimitrov Nov 11, 2025
27b0889
fix collator binaries
tdimitrov Nov 11, 2025
d15dae0
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Nov 12, 2025
7abb6f3
`ClaimQueueState` cosmetics (#10334)
serban300 Nov 19, 2025
b34a129
Fix ClaimQueue inconsistencies (#10368)
serban300 Nov 20, 2025
86b9972
Nits
serban300 Nov 18, 2025
0e46e52
ensure_matches_advertisement -> FetchedCollation::ensure_matches_adve…
serban300 Nov 18, 2025
e731075
taplo
serban300 Nov 20, 2025
1a17b4c
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
serban300 Nov 20, 2025
de348df
Parameters order inconsistencies + Nits
serban300 Nov 20, 2025
7e6d97a
Renamings
serban300 Nov 20, 2025
aa1b5b0
Merge remote-tracking branch 'upstream/master' into alindima/collator…
serban300 Nov 21, 2025
03e8915
[TODO: ROLLBACK] use ValidatorExperimental by default
serban300 Nov 21, 2025
05e1497
[TODO: ROLLBACK] Fix build
serban300 Nov 21, 2025
83600e8
Revert "[TODO: ROLLBACK] Fix build"
serban300 Nov 21, 2025
a75e984
Revert "[TODO: ROLLBACK] use ValidatorExperimental by default"
serban300 Nov 21, 2025
5b8749d
Disable `experimental-collator-protocol` by default (#10414)
serban300 Nov 25, 2025
0b2f18b
candidates -> candidates_per_rp
serban300 Nov 26, 2025
05b09f6
Update from github-actions[bot] running command 'prdoc --bump patch -…
github-actions[bot] Nov 26, 2025
d461b2b
Update prdoc
serban300 Nov 26, 2025
6abe2ad
Propagate experimental-collator-protocol up to the polkadot binary
serban300 Nov 26, 2025
329c05b
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Nov 27, 2025
aa3e283
CR comments
serban300 Nov 27, 2025
b72eab5
Fix prdoc
serban300 Nov 27, 2025
9651528
fix prdoc
serban300 Nov 27, 2025
6a07636
comment
tdimitrov Nov 17, 2025
b531d37
log which implementation of the collator protocol is used
tdimitrov Nov 28, 2025
60a6fc5
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Dec 1, 2025
8c5a23a
Additional logs
tdimitrov Nov 27, 2025
3505c9a
fix a compilation error
tdimitrov Dec 3, 2025
9a7c493
Remove experimental-collator-protocol feature flag
serban300 Dec 8, 2025
0fa8ecb
Continue processing other paras if `request_candidates_pending_availa…
tdimitrov Dec 10, 2025
1e78249
Collator protocol revamp: small fixes (#10456)
serban300 Dec 12, 2025
23c2d3c
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
serban300 Dec 12, 2025
1320dce
CR comments
serban300 Dec 15, 2025
6e24e46
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
serban300 Dec 16, 2025
fc50170
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
serban300 Dec 16, 2025
7fb078c
ClaimQueueState tests: use consts for relay parents
serban300 Dec 16, 2025
1cf32c3
ClaimQueueState tests: use more statics for deduplication
serban300 Dec 17, 2025
a3e0ca4
Fix CANDIDATE_B2 hash
serban300 Dec 17, 2025
e957396
ClaimQueueState tests: make more compact
serban300 Dec 17, 2025
0f17094
ClaimQueueState: Move fork() unit tests out of separate mod
serban300 Dec 18, 2025
3d86aa5
ClaimQueueState: improve fork() unit testing
serban300 Dec 18, 2025
d2d1476
ClaimQueueState: deduplication and nits
serban300 Dec 18, 2025
75682e5
Store the advertisement timestamp together with the advertisement
serban300 Dec 19, 2025
68bcf72
CR comments
serban300 Dec 22, 2025
f2a7877
`test_view_update_preserves_relay_parent_state`
tdimitrov Jan 6, 2026
3b8bf7d
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Jan 7, 2026
c1786f3
some extra logging
tdimitrov Dec 11, 2025
5a49048
Extract metrics from validator_side to the crate root so that it is s…
tdimitrov Dec 11, 2025
24f0142
Update the metrics in validator_side_experimental
tdimitrov Jan 6, 2026
62df7c6
leftovers
serban300 Jan 8, 2026
445b59e
comments
tdimitrov Jan 9, 2026
8144cf5
prdoc
tdimitrov Jan 9, 2026
44b04f9
Use sender instead of context where possible
serban300 Jan 12, 2026
5505d51
Update parameters in common.rs
tdimitrov Jan 14, 2026
dc4426e
Add comment
serban300 Jan 14, 2026
e48add5
small fix
serban300 Jan 14, 2026
efa3008
Update parameters in common.rs - missed msg
tdimitrov Jan 14, 2026
5f183c3
Failing fetching test
tdimitrov Jan 14, 2026
c42ef31
Simplify advertisement fetching logic
tdimitrov Jan 16, 2026
67373ea
fmt
tdimitrov Jan 16, 2026
e1c301d
Adjust MAX_SCORE, FAILED_FETCH_SLASH and INVALID_COLLATION_SLASH
tdimitrov Jan 23, 2026
c5d5210
clippy
tdimitrov Jan 23, 2026
89cd009
Update polkadot/node/network/collator-protocol/src/validator_side_exp…
tdimitrov Jan 23, 2026
8165457
spelling
tdimitrov Jan 23, 2026
d088c5d
prdoc
tdimitrov Jan 23, 2026
4ac381f
Move non-pub types in collation_manager after the pub types
tdimitrov Jan 23, 2026
c9a658c
fix a log line
tdimitrov Jan 23, 2026
bc237d5
Increase `INVALID_COLLATION_SLASH`
tdimitrov Jan 23, 2026
9f36299
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Jan 26, 2026
6574a59
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Jan 26, 2026
d52e2df
Add a new column to parachains_db for collators reputation
AlexandruCihodaru Jan 22, 2026
f8eaa63
Add persistent disk storage for collator reputation database
AlexandruCihodaru Jan 27, 2026
2991942
Update undying collator to send valid PeerId in ApprovedPeer UMP signal
AlexandruCihodaru Jan 27, 2026
d1ae2f5
Add zombienet tests for collator reputation persistence
AlexandruCihodaru Jan 27, 2026
562a6a1
Collator reputation persistent DB:
AlexandruCihodaru Feb 1, 2026
9d3bc40
Replace the `test-persistence` feature with a runtime CLI argument
AlexandruCihodaru Feb 1, 2026
406e948
Add dirty tracking optimization for collator reputation persistence
AlexandruCihodaru Feb 1, 2026
b32910a
Update zombienet_polkadot_tests to run collator reputation tests on CI
AlexandruCihodaru Feb 1, 2026
98c2185
Update from github-actions[bot] running command 'prdoc --audience nod…
github-actions[bot] Feb 2, 2026
8516537
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Feb 2, 2026
c26bfc5
Merge branch 'master' into alindima/collator-protocol-revamp-collatio…
tdimitrov Feb 2, 2026
53d7389
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Feb 2, 2026
8078923
Merge branch 'alindima/collator-protocol-revamp-collation-manager' in…
AlexandruCihodaru Feb 3, 2026
6de5478
Refactor collators reputation persistence implementation
AlexandruCihodaru Feb 5, 2026
4d404fd
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 5, 2026
88043ab
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 5, 2026
50e6dea
Move reputation persistence to async background writer task
AlexandruCihodaru Feb 6, 2026
3c452d5
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 6, 2026
afe9b8f
delete unused method
AlexandruCihodaru Feb 6, 2026
df99932
fix merge error
AlexandruCihodaru Feb 6, 2026
2db2397
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Feb 6, 2026
d04b52f
clippy
AlexandruCihodaru Feb 6, 2026
43430cf
address feedback and fix clippy
AlexandruCihodaru Feb 9, 2026
3da169b
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 9, 2026
b5d39ab
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Feb 10, 2026
5da5e17
feedback v2
AlexandruCihodaru Feb 10, 2026
814812e
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 10, 2026
0040780
Address feedback
AlexandruCihodaru Feb 10, 2026
3a9cb30
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 10, 2026
d055853
Update from github-actions[bot] running command 'fmt'
github-actions[bot] Feb 11, 2026
31234b1
fix parity db migration
AlexandruCihodaru Feb 11, 2026
ab6d936
make test more reliable
AlexandruCihodaru Feb 11, 2026
fb8f73e
Merge remote-tracking branch 'origin' into acihodaru/persistent_db_mem
AlexandruCihodaru Feb 13, 2026
8e8e9bb
increase level of peer disconnect log
AlexandruCihodaru Feb 13, 2026
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
5 changes: 5 additions & 0 deletions .github/zombienet-tests/zombienet_polkadot_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,8 @@
test-filter: "functional::shared_core_idle_parachain::shared_core_idle_parachain_test"
runner-type: "default"
use-zombienet-sdk: true

- job-name: "zombienet-polkadot-collators-basic-reputation-persistence"
test-filter: "functional::collators_reputation_persistence"
runner-type: "default"
use-zombienet-sdk: true
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.

1 change: 1 addition & 0 deletions cumulus/client/relay-chain-inprocess-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ fn build_polkadot_full_node(
invulnerable_ah_collators: HashSet::new(),
collator_protocol_hold_off: None,
experimental_collator_protocol: false,
collator_reputation_persist_interval: None,
};

let (relay_chain_full_node, paranode_req_receiver) = match config.network.network_backend {
Expand Down
7 changes: 7 additions & 0 deletions polkadot/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ pub struct RunCmd {
/// Enable experimental collator protocol. TESTING ONLY! Don't use on production
#[arg(long, hide = true, default_value = "false")]
pub experimental_collator_protocol: bool,

/// Collator reputation persistence interval in seconds.
/// If not specified, defaults to 600 seconds (10 minutes).
/// This should be used only with experimental_collator_protocol
/// and only on validators.
#[arg(long, requires = "experimental_collator_protocol", requires = "validator")]
pub collator_reputation_persist_interval: Option<u64>,
}

#[allow(missing_docs)]
Expand Down
4 changes: 4 additions & 0 deletions polkadot/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ where
invulnerable_ah_collators,
collator_protocol_hold_off,
experimental_collator_protocol,
collator_reputation_persist_interval: cli
.run
.collator_reputation_persist_interval
.map(std::time::Duration::from_secs),
},
)
.map(|full| full.task_manager)?;
Expand Down
3 changes: 3 additions & 0 deletions polkadot/node/network/collator-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ sp-core = { workspace = true, default-features = true }
sp-keystore = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }

codec.workspace = true
fatality = { workspace = true }
polkadot-node-network-protocol = { workspace = true, default-features = true }
polkadot-node-primitives = { workspace = true, default-features = true }
polkadot-node-subsystem = { workspace = true, default-features = true }
polkadot-node-subsystem-util = { workspace = true, default-features = true }
polkadot-primitives = { workspace = true, default-features = true }
thiserror = { workspace = true }
tokio.workspace = true
tokio-util = { workspace = true }

[dev-dependencies]
assert_matches = { workspace = true }
kvdb-memorydb = { workspace = true }
rstest = { workspace = true }
sc-network-types = { workspace = true, default-features = true }
sp-tracing = { workspace = true }
Expand Down
15 changes: 10 additions & 5 deletions polkadot/node/network/collator-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use std::{
collections::HashSet,
sync::Arc,
time::{Duration, Instant},
};

Expand All @@ -31,16 +32,16 @@ use futures::{
FutureExt, TryFutureExt,
};

use polkadot_node_subsystem_util::reputation::ReputationAggregator;
use polkadot_node_subsystem_util::{database::Database, reputation::ReputationAggregator};
use sp_keystore::KeystorePtr;

use polkadot_node_network_protocol::{
request_response::{v2 as protocol_v2, IncomingRequestReceiver},
PeerId, UnifiedReputationChange as Rep,
};
use polkadot_primitives::CollatorPair;

use polkadot_node_subsystem::{errors::SubsystemError, overseer, DummySubsystem, SpawnedSubsystem};
use polkadot_primitives::CollatorPair;
pub use validator_side_experimental::ReputationConfig;

mod collator_side;
mod validator_side;
Expand Down Expand Up @@ -91,6 +92,10 @@ pub enum ProtocolSide {
keystore: KeystorePtr,
/// Prometheus metrics for validators.
metrics: validator_side_experimental::Metrics,
/// Database used for reputation house keeping.
db: Arc<dyn Database>,
/// Reputation configuration (column number).
reputation_config: validator_side_experimental::ReputationConfig,
},
/// Collators operate on a parachain.
Collator {
Expand Down Expand Up @@ -148,8 +153,8 @@ impl<Context> CollatorProtocolSubsystem {
.map_err(|e| SubsystemError::with_origin("collator-protocol", e))
.boxed()
},
ProtocolSide::ValidatorExperimental { keystore, metrics } => {
validator_side_experimental::run(ctx, keystore, metrics)
ProtocolSide::ValidatorExperimental { keystore, metrics, db, reputation_config } => {
validator_side_experimental::run(ctx, keystore, metrics, db, reputation_config)
.map_err(|e| SubsystemError::with_origin("collator-protocol", e))
.boxed()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use codec::{Decode, Encode};
use polkadot_node_network_protocol::{
peer_set::CollationVersion,
request_response::{outgoing::RequestError, v2 as request_v2},
Expand Down Expand Up @@ -84,7 +85,7 @@ pub const MAX_FETCH_DELAY: Duration = Duration::from_millis(300);
pub const MIN_FETCH_TIMER_DELAY: Duration = Duration::from_millis(150);

/// Reputation score type.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Default)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Default, Encode, Decode)]
pub struct Score(u16);

impl Score {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use crate::LOG_TARGET;
use crate::{validator_side_experimental::peer_manager::PersistenceError, LOG_TARGET};
use fatality::Nested;
use polkadot_node_subsystem::{ChainApiError, SubsystemError};
use polkadot_node_subsystem_util::{backing_implicit_view, runtime};
Expand Down Expand Up @@ -44,6 +44,12 @@ pub enum Error {
#[fatal]
#[error("Receiving message from overseer failed: {0}")]
SubsystemReceive(#[source] SubsystemError),
#[fatal]
#[error("Failed to initialize reputation database: {0}")]
ReputationDbInit(PersistenceError),
#[fatal]
#[error("Failed to spawn background task: {0}")]
SpawnTask(String),
#[error("Unable to retrieve block number for {0:?} from implicit view")]
BlockNumberNotFoundInImplicitView(Hash),
#[fatal(forward)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ mod state;
#[cfg(test)]
mod tests;

use crate::{validator_side_experimental::common::MIN_FETCH_TIMER_DELAY, LOG_TARGET};
use crate::{
validator_side_experimental::{common::MIN_FETCH_TIMER_DELAY, peer_manager::PersistentDb},
LOG_TARGET,
};
use collation_manager::CollationManager;
use common::{ProspectiveCandidate, MAX_STORED_SCORES_PER_PARA};
use error::{log_error, FatalError, FatalResult, Result};
Expand All @@ -35,25 +38,50 @@ use polkadot_node_subsystem::{
messages::{CollatorProtocolMessage, NetworkBridgeEvent},
overseer, ActivatedLeaf, CollatorProtocolSenderTrait, FromOrchestra, OverseerSignal,
};
use polkadot_node_subsystem_util::database::Database;
use sp_keystore::KeystorePtr;
use std::{future, future::Future, pin::Pin, time::Duration};
use std::{future, future::Future, pin::Pin, sync::Arc, time::Duration};

use peer_manager::{Db, PeerManager};
#[cfg(test)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed?

use peer_manager::Db;
use peer_manager::PeerManager;

use state::State;

pub use crate::validator_side_metrics::Metrics;

/// Default interval for persisting the reputation database to disk (in seconds).
const DEFAULT_PERSIST_INTERVAL_SECS: u64 = 600;

/// Configuration for the reputation db.
#[derive(Debug, Clone, Copy)]
pub struct ReputationConfig {
/// The data column in the store to use for reputation data.
pub col_reputation_data: u32,
/// How often to persist the reputation database to disk.
/// If None, defaults to DEFAULT_PERSIST_INTERVAL_SECS seconds.
pub persist_interval: Option<Duration>,
}

/// The main run loop.
#[overseer::contextbounds(CollatorProtocol, prefix = self::overseer)]
pub(crate) async fn run<Context>(
mut ctx: Context,
keystore: KeystorePtr,
metrics: Metrics,
db: Arc<dyn Database>,
reputation_config: ReputationConfig,
) -> FatalResult<()> {
gum::info!(LOG_TARGET, "Running experimental collator protocol");
if let Some(state) = initialize(&mut ctx, keystore, metrics).await? {
run_inner(ctx, state).await?;
let persist_interval = reputation_config
.persist_interval
.unwrap_or(Duration::from_secs(DEFAULT_PERSIST_INTERVAL_SECS));
gum::info!(
LOG_TARGET,
persist_interval_secs = persist_interval.as_secs(),
"Running experimental collator protocol"
);
if let Some(state) = initialize(&mut ctx, keystore, metrics, db, reputation_config).await? {
run_inner(ctx, state, persist_interval).await?;
}

Ok(())
Expand All @@ -64,7 +92,9 @@ async fn initialize<Context>(
ctx: &mut Context,
keystore: KeystorePtr,
metrics: Metrics,
) -> FatalResult<Option<State<Db>>> {
db: Arc<dyn Database>,
reputation_config: ReputationConfig,
) -> FatalResult<Option<State<PersistentDb>>> {
loop {
let first_leaf = match wait_for_first_leaf(ctx).await? {
Some(activated_leaf) => {
Expand All @@ -84,7 +114,30 @@ async fn initialize<Context>(

let scheduled_paras = collation_manager.assignments();

let backend = Db::new(MAX_STORED_SCORES_PER_PARA).await;
// Create PersistentDb with disk persistence
let (backend, task) = match PersistentDb::new(
db.clone(),
reputation_config,
MAX_STORED_SCORES_PER_PARA,
)
.await
{
Ok(result) => result,
Err(e) => {
gum::error!(
target: LOG_TARGET,
error = ?e,
"Failed to initialize persistent reputation DB"
);
return Err(FatalError::ReputationDbInit(e));
},
};

// Background task for async writes
ctx.spawn_blocking("collator-reputation-persistence-task", task)
.map_err(|e| FatalError::SpawnTask(e.to_string()))?;

gum::debug!(target: LOG_TARGET, "Spawned background reputation persistence task");

match PeerManager::startup(backend, ctx.sender(), scheduled_paras.into_iter().collect())
.await
Expand Down Expand Up @@ -134,9 +187,21 @@ fn create_timer(maybe_delay: Option<Duration>) -> Fuse<Pin<Box<dyn Future<Output
timer.fuse()
}

/// Create the persistence timer that fires after the given interval.
fn create_persistence_timer(interval: Duration) -> Fuse<Pin<Box<dyn Future<Output = ()> + Send>>> {
let delay: Pin<Box<dyn Future<Output = ()> + Send>> = Box::pin(Delay::new(interval));
delay.fuse()
}

#[overseer::contextbounds(CollatorProtocol, prefix = self::overseer)]
async fn run_inner<Context>(mut ctx: Context, mut state: State<Db>) -> FatalResult<()> {
async fn run_inner<Context>(
mut ctx: Context,
mut state: State<PersistentDb>,
persist_interval: Duration,
) -> FatalResult<()> {
let mut timer = create_timer(None);
let mut persistence_timer = create_persistence_timer(persist_interval);

loop {
select! {
// Calling `fuse()` here is useless, because the termination state of the resulting
Expand All @@ -154,7 +219,11 @@ async fn run_inner<Context>(mut ctx: Context, mut state: State<Db>) -> FatalResu
msg,
).await;
}
Ok(FromOrchestra::Signal(OverseerSignal::Conclude)) | Err(_) => break,
Ok(FromOrchestra::Signal(OverseerSignal::Conclude)) | Err(_) => {
// Persist to disk before shutdown
state.persist_reputations().await;
break
},
Ok(FromOrchestra::Signal(OverseerSignal::BlockFinalized(hash, number))) => {
state.handle_finalized_block(ctx.sender(), hash, number).await?;
},
Expand All @@ -168,6 +237,12 @@ async fn run_inner<Context>(mut ctx: Context, mut state: State<Db>) -> FatalResu
// We don't need to do anything specific here.
// If the timer expires, we only need to trigger the advertisement fetching logic.
},
_ = &mut persistence_timer => {
// Periodic persistence - write reputation DB to disk
state.background_persist_reputations();
// Reset the timer for the next interval
persistence_timer = create_persistence_timer(persist_interval);
},
}

// Now try triggering advertisement fetching, if we have room in any of the active leaves
Expand All @@ -187,7 +262,7 @@ async fn run_inner<Context>(mut ctx: Context, mut state: State<Db>) -> FatalResu
/// The main message receiver switch.
async fn process_msg<Sender: CollatorProtocolSenderTrait>(
sender: &mut Sender,
state: &mut State<Db>,
state: &mut State<PersistentDb>,
msg: CollatorProtocolMessage,
) {
use CollatorProtocolMessage::*;
Expand Down Expand Up @@ -241,7 +316,7 @@ async fn process_msg<Sender: CollatorProtocolSenderTrait>(
/// Bridge event switch.
async fn handle_network_msg<Sender: CollatorProtocolSenderTrait>(
sender: &mut Sender,
state: &mut State<Db>,
state: &mut State<PersistentDb>,
bridge_message: NetworkBridgeEvent<net_protocol::CollatorProtocolMessage>,
) -> Result<()> {
use NetworkBridgeEvent::*;
Expand Down Expand Up @@ -289,7 +364,7 @@ async fn handle_network_msg<Sender: CollatorProtocolSenderTrait>(

async fn process_incoming_peer_message<Sender: CollatorProtocolSenderTrait>(
sender: &mut Sender,
state: &mut State<Db>,
state: &mut State<PersistentDb>,
origin: PeerId,
msg: CollationProtocols<
protocol_v1::CollatorProtocolMessage,
Expand Down
Loading
Loading