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

fix: clear triples after reshare #638

Merged
merged 7 commits into from
Jun 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/multichain-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ jobs:
working-directory: ./integration-tests/chain-signatures
run: cargo test --jobs 1 -- --test-threads 1
env:
RUST_LOG: INFO
RUST_LOG: info,workspaces=warn
RUST_BACKTRACE: 1
43 changes: 26 additions & 17 deletions chain-signatures/node/src/gcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,40 @@ impl DatastoreService {
)
})?;

batch.entity_results.ok_or_else(|| {
DatastoreStorageError::FetchEntitiesError(
"Could not retrieve entity results while fetching entities".to_string(),
)
})
// NOTE: if entity_results is None, we return an empty Vec since the fetch query
// could not find any entities in the DB.
Ok(batch.entity_results.unwrap_or_default())
}

#[tracing::instrument(level = "debug", skip_all)]
pub async fn delete<T: Keyable>(&self, keyable: T) -> DatastoreResult<()> {
let mut key = keyable.key();
if let Some(path) = key.path.as_mut().and_then(|p| p.first_mut()) {
path.kind = Some(format!("{}-{}", T::kind(), self.env));
}
self.delete_many(&[keyable]).await
}

#[tracing::instrument(level = "debug", skip_all)]
pub async fn delete_many<T: Keyable>(&self, keyables: &[T]) -> DatastoreResult<()> {
let mutations = keyables
.iter()
.map(|keyable| {
let mut key = keyable.key();
if let Some(path) = key.path.as_mut().and_then(|p| p.first_mut()) {
path.kind = Some(format!("{}-{}", T::kind(), self.env));
}
Mutation {
insert: None,
delete: Some(key),
update: None,
base_version: None,
upsert: None,
update_time: None,
}
})
.collect::<Vec<_>>();

let request = CommitRequest {
database_id: Some("".to_string()),
mode: Some(String::from("NON_TRANSACTIONAL")),
mutations: Some(vec![Mutation {
insert: None,
delete: Some(key),
update: None,
base_version: None,
upsert: None,
update_time: None,
}]),
mutations: Some(mutations),
single_use_transaction: None,
transaction: None,
};
Expand Down
9 changes: 7 additions & 2 deletions chain-signatures/node/src/protocol/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ impl ConsensusProtocol for WaitingForConsensusState {
.find_participant(ctx.my_account_id())
.unwrap();

// Clear triples from storage before starting the new epoch. This is necessary if the node has accumulated
// triples from previous epochs. If it was not able to clear the previous triples, we'll leave them as-is
if let Err(err) = ctx.triple_storage().write().await.clear().await {
tracing::warn!(?err, "failed to clear triples from storage");
}

let triple_manager = TripleManager::new(
me,
self.threshold,
Expand Down Expand Up @@ -725,8 +731,7 @@ async fn load_triples<C: ConsensusCtx + Send + Sync>(
let mut retries = 3;
let mut error = None;
while retries > 0 {
let read_lock = triple_storage.read().await;
match read_lock.load().await {
match triple_storage.read().await.load().await {
Err(DatastoreStorageError::FetchEntitiesError(_)) => {
tracing::info!("There are no triples persisted.");
return Ok(vec![]);
Expand Down
14 changes: 8 additions & 6 deletions chain-signatures/node/src/protocol/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,14 @@ impl MessageHandler for RunningState {
leftover_messages.push(message)
}
Err(presignature::GenerationError::TripleIsMissing(_)) => {
// Store the message until triple is ready
leftover_messages.push(message)
// If a triple is missing, that means our system cannot process this presignature. We will have to bin
// this message and have the other node timeout on that generation.
tracing::warn!(
presignature_id = id,
triple0 = message.triple0,
triple1 = message.triple1,
"unable to process presignature: one or more triples are missing",
);
}
Err(presignature::GenerationError::CaitSithInitializationError(error)) => {
// ignore the message since the generation had bad parameters. Also have the other node who
Expand All @@ -311,10 +317,6 @@ impl MessageHandler for RunningState {
);
continue;
}
Err(presignature::GenerationError::DatastoreStorageError(_)) => {
// Store the message until we are ready to process it
leftover_messages.push(message)
}
}
}
if !leftover_messages.is_empty() {
Expand Down
3 changes: 0 additions & 3 deletions chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::message::PresignatureMessage;
use super::triple::{Triple, TripleConfig, TripleId, TripleManager};
use crate::gcp::error::DatastoreStorageError;
use crate::protocol::contract::primitives::Participants;
use crate::types::{PresignatureProtocol, SecretKeyShare};
use crate::util::AffinePointExt;
Expand Down Expand Up @@ -87,8 +86,6 @@ pub enum GenerationError {
TripleIsMissing(TripleId),
#[error("cait-sith initialization error: {0}")]
CaitSithInitializationError(#[from] InitializationError),
#[error("datastore storage error: {0}")]
DatastoreStorageError(#[from] DatastoreStorageError),
#[error("triple {0} is generating")]
TripleIsGenerating(TripleId),
}
Expand Down
27 changes: 27 additions & 0 deletions chain-signatures/node/src/storage/triple_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ impl KeyKind for TripleData {
}
}

impl Keyable for TripleData {
fn key(&self) -> Key {
Key {
path: Some(vec![PathElement {
kind: None,
name: Some(format!("{}/{}", self.account_id, self.triple.id)),
id: None,
}]),
partition_id: None,
}
}
}

impl IntoValue for TripleData {
fn into_value(self) -> Value {
let triple_key = TripleKey {
Expand Down Expand Up @@ -147,6 +160,7 @@ type TripleResult<T> = std::result::Result<T, error::DatastoreStorageError>;
pub trait TripleNodeStorage {
async fn insert(&mut self, triple: Triple, mine: bool) -> TripleResult<()>;
async fn delete(&mut self, id: TripleId) -> TripleResult<()>;
async fn clear(&mut self) -> TripleResult<Vec<TripleData>>;
async fn load(&self) -> TripleResult<Vec<TripleData>>;
fn account_id(&self) -> &AccountId;
}
Expand Down Expand Up @@ -174,6 +188,13 @@ impl TripleNodeStorage for MemoryTripleNodeStorage {
Ok(())
}

async fn clear(&mut self) -> TripleResult<Vec<TripleData>> {
let res = self.load().await?;
self.triples.clear();
self.mine.clear();
Ok(res)
}

async fn load(&self) -> TripleResult<Vec<TripleData>> {
let mut res: Vec<TripleData> = vec![];
for (triple_id, triple) in self.triples.clone() {
Expand Down Expand Up @@ -232,6 +253,12 @@ impl TripleNodeStorage for DataStoreTripleNodeStorage {
Ok(())
}

async fn clear(&mut self) -> TripleResult<Vec<TripleData>> {
let triples = self.load().await?;
self.datastore.delete_many(&triples).await?;
Ok(triples)
}

async fn load(&self) -> TripleResult<Vec<TripleData>> {
tracing::debug!("loading triples using datastore");
let filter = if self.datastore.is_emulator() {
Expand Down
Loading