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

[Backport releases/v0.2] fix: remove racy endpoint PENDING_ACCEPTED_ITEMS_ENDPOINT for SESSION_STATUS_ENDPOINT #4180

Merged
merged 2 commits into from Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fedimint-core/src/endpoint_constants.rs
Expand Up @@ -11,7 +11,7 @@ pub const SERVER_CONFIG_CONSENSUS_HASH_ENDPOINT: &str = "server_config_consensus
pub const SESSION_COUNT_ENDPOINT: &str = "session_count";
pub const AWAIT_SESSION_OUTCOME_ENDPOINT: &str = "await_session_outcome";
pub const AWAIT_SIGNED_SESSION_OUTCOME_ENDPOINT: &str = "await_signed_session_outcome";
pub const PENDING_ACCEPTED_ITEMS_ENDPOINT: &str = "pending_accepted_items";
pub const SESSION_STATUS_ENDPOINT: &str = "session_status";
pub const CONFIG_GEN_PEERS_ENDPOINT: &str = "config_gen_peers";
pub const CONSENSUS_CONFIG_GEN_PARAMS_ENDPOINT: &str = "consensus_config_gen_params";
pub const DEFAULT_CONFIG_GEN_PARAMS_ENDPOINT: &str = "default_config_gen_params";
Expand Down
7 changes: 7 additions & 0 deletions fedimint-core/src/session_outcome.rs
Expand Up @@ -60,6 +60,13 @@ pub struct SignedSessionOutcome {
pub signatures: std::collections::BTreeMap<PeerId, SchnorrSignature>,
}

#[derive(Debug, Clone, Eq, PartialEq, Encodable, Decodable)]
pub enum SessionStatus {
Initial,
Pending(Vec<AcceptedItem>),
Complete(SessionOutcome),
}

// TODO: remove this as soon as we bump bitcoin_hashes in fedimint_core to
// 0.12.0
pub fn consensus_hash_sha256<E: Encodable>(encodable: &E) -> sha256::Hash {
Expand Down
2 changes: 1 addition & 1 deletion fedimint-server/src/config/mod.rs
Expand Up @@ -211,7 +211,7 @@ impl ServerConfig {
pub fn supported_api_versions() -> SupportedCoreApiVersions {
SupportedCoreApiVersions {
core_consensus: CORE_CONSENSUS_VERSION,
api: MultiApiVersion::try_from_iter([ApiVersion { major: 0, minor: 0 }])
api: MultiApiVersion::try_from_iter([ApiVersion { major: 0, minor: 1 }])
.expect("not version conflicts"),
}
}
Expand Down
44 changes: 27 additions & 17 deletions fedimint-server/src/net/api.rs
@@ -1,4 +1,5 @@
//! Implements the client API through which users interact with the federation
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap};
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
Expand All @@ -20,10 +21,9 @@ use fedimint_core::db::{
use fedimint_core::endpoint_constants::{
AUDIT_ENDPOINT, AUTH_ENDPOINT, AWAIT_OUTPUT_OUTCOME_ENDPOINT, AWAIT_SESSION_OUTCOME_ENDPOINT,
AWAIT_SIGNED_SESSION_OUTCOME_ENDPOINT, AWAIT_TRANSACTION_ENDPOINT, BACKUP_ENDPOINT,
CLIENT_CONFIG_ENDPOINT, INVITE_CODE_ENDPOINT, MODULES_CONFIG_JSON_ENDPOINT,
PENDING_ACCEPTED_ITEMS_ENDPOINT, RECOVER_ENDPOINT, SERVER_CONFIG_CONSENSUS_HASH_ENDPOINT,
SESSION_COUNT_ENDPOINT, STATUS_ENDPOINT, SUBMIT_TRANSACTION_ENDPOINT,
VERIFY_CONFIG_HASH_ENDPOINT, VERSION_ENDPOINT,
CLIENT_CONFIG_ENDPOINT, INVITE_CODE_ENDPOINT, MODULES_CONFIG_JSON_ENDPOINT, RECOVER_ENDPOINT,
SERVER_CONFIG_CONSENSUS_HASH_ENDPOINT, SESSION_COUNT_ENDPOINT, SESSION_STATUS_ENDPOINT,
STATUS_ENDPOINT, SUBMIT_TRANSACTION_ENDPOINT, VERIFY_CONFIG_HASH_ENDPOINT, VERSION_ENDPOINT,
};
use fedimint_core::epoch::ConsensusItem;
use fedimint_core::module::audit::{Audit, AuditSummary};
Expand All @@ -33,7 +33,7 @@ use fedimint_core::module::{
SupportedApiVersionsSummary,
};
use fedimint_core::server::DynServerModule;
use fedimint_core::session_outcome::{AcceptedItem, SessionOutcome, SignedSessionOutcome};
use fedimint_core::session_outcome::{SessionOutcome, SessionStatus, SignedSessionOutcome};
use fedimint_core::transaction::{SerdeTransaction, Transaction, TransactionError};
use fedimint_core::{OutPoint, PeerId, TransactionId};
use fedimint_logging::LOG_NET_API;
Expand Down Expand Up @@ -177,15 +177,25 @@ impl ConsensusApi {
.0
}

pub async fn pending_accepted_items(&self) -> Vec<AcceptedItem> {
self.db
.begin_transaction_nc()
.await
.find_by_prefix(&AcceptedItemPrefix)
.await
.map(|entry| entry.1)
.collect()
.await
pub async fn session_status(&self, session_index: u64) -> SessionStatus {
let mut dbtx = self.db.begin_transaction_nc().await;

match session_index.cmp(&get_finished_session_count_static(&mut dbtx).await) {
Ordering::Greater => SessionStatus::Initial,
Ordering::Equal => SessionStatus::Pending(
dbtx.find_by_prefix(&AcceptedItemPrefix)
.await
.map(|entry| entry.1)
.collect()
.await,
),
Ordering::Less => SessionStatus::Complete(
dbtx.get_value(&SignedSessionOutcomeKey(session_index))
.await
.expect("There are no gaps in session outcomes")
.session_outcome,
),
}
}

pub async fn get_federation_status(&self) -> ApiResult<FederationStatus> {
Expand Down Expand Up @@ -439,9 +449,9 @@ pub fn server_endpoints() -> Vec<ApiEndpoint<ConsensusApi>> {
}
},
api_endpoint! {
PENDING_ACCEPTED_ITEMS_ENDPOINT,
async |fedimint: &ConsensusApi, _context, _v: ()| -> SerdeModuleEncoding<Vec<AcceptedItem>> {
Ok((&fedimint.pending_accepted_items().await).into())
SESSION_STATUS_ENDPOINT,
async |fedimint: &ConsensusApi, _context, index: u64| -> SerdeModuleEncoding<SessionStatus> {
Ok((&fedimint.session_status(index).await).into())
}
},
api_endpoint! {
Expand Down