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: state sync #969

Merged
merged 23 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 21 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: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion apps/gen-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ version = "0.1.0"
crate-type = ["cdylib"]

[dependencies]
calimero-sdk = {path = "../../crates/sdk"}
calimero-sdk = { path = "../../crates/sdk" }
calimero-sdk-near = { path = "../../crates/sdk/libs/near" }
calimero-storage = { path = "../../crates/storage" }
85 changes: 0 additions & 85 deletions apps/kv-store/src/__private.rs

This file was deleted.

20 changes: 8 additions & 12 deletions apps/kv-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

use std::collections::BTreeMap;

use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk::types::Error;
use calimero_sdk::{app, env};
use calimero_storage::collections::UnorderedMap;
use calimero_storage::entities::Element;
use calimero_storage::AtomicUnit;

mod __private;

#[app::state(emits = for<'a> Event<'a>)]
#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)]
#[root]
#[type_id(1)]
#[derive(Debug, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)]
#[borsh(crate = "calimero_sdk::borsh")]
pub struct KvStore {
items: UnorderedMap<String, String>,
#[storage]
storage: Element,
}

#[app::event]
Expand All @@ -33,8 +27,7 @@ impl KvStore {
#[app::init]
pub fn init() -> KvStore {
KvStore {
items: UnorderedMap::new().unwrap(),
storage: Element::root(),
items: UnorderedMap::new(),
}
}

Expand Down Expand Up @@ -93,7 +86,10 @@ impl KvStore {

app::emit!(Event::Removed { key });

self.items.remove(key).map_err(Into::into)
self.items
.remove(key)
.map(|v| v.is_some())
.map_err(Into::into)
}

pub fn clear(&mut self) -> Result<(), Error> {
Expand Down
1 change: 1 addition & 0 deletions apps/only-peers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ crate-type = ["cdylib"]

[dependencies]
calimero-sdk = { path = "../../crates/sdk" }
calimero-storage = { path = "../../crates/storage" }
18 changes: 5 additions & 13 deletions apps/visited/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,28 @@
use std::result::Result;

use calimero_sdk::app;
use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk::types::Error;
use calimero_storage::collections::{UnorderedMap, UnorderedSet};
use calimero_storage::entities::Element;
use calimero_storage::AtomicUnit;

#[app::state]
#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)]
#[root]
#[type_id(1)]
#[derive(Debug, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize)]
#[borsh(crate = "calimero_sdk::borsh")]
pub struct VisitedCities {
visited: UnorderedMap<String, UnorderedSet<String>>,
#[storage]
storage: Element,
}

#[app::logic]
impl VisitedCities {
#[app::init]
pub fn init() -> VisitedCities {
VisitedCities {
visited: UnorderedMap::new().unwrap(),
storage: Element::root(),
visited: UnorderedMap::new(),
}
}

pub fn add_person(&mut self, person: String) -> Result<bool, Error> {
Ok(self
.visited
.insert(person, UnorderedSet::new().unwrap())?
.is_some())
Ok(self.visited.insert(person, UnorderedSet::new())?.is_some())
}

pub fn add_visited_city(&mut self, person: String, city: String) -> Result<bool, Error> {
Expand Down
23 changes: 10 additions & 13 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,12 @@ impl Node {

let shared_key = SharedKey::from_sk(&sender_key);

let artifact = &shared_key
let artifact = shared_key
.decrypt(artifact, [0; 12])
.ok_or_eyre("failed to decrypt message")?;

let Some(outcome) = self
.execute(
&mut context,
"apply_state_delta",
to_vec(&artifact)?,
author_id,
)
.execute(&mut context, "__calimero_sync_next", artifact, author_id)
.await?
else {
bail!("application not installed");
Expand Down Expand Up @@ -500,11 +495,13 @@ impl Node {
})?;
}

if let Err(err) = self
.send_state_delta(&context, &outcome, executor_public_key)
.await
{
error!(%err, "Failed to send state delta.");
if !outcome.artifact.is_empty() {
if let Err(err) = self
.send_state_delta(&context, &outcome, executor_public_key)
.await
{
error!(%err, "Failed to send state delta.");
}
}

Ok(outcome)
Expand Down Expand Up @@ -539,7 +536,7 @@ impl Node {

if outcome.returns.is_ok() {
if let Some(root_hash) = outcome.root_hash {
if outcome.artifact.is_empty() {
if outcome.artifact.is_empty() && method != "__calimero_sync_next" {
eyre::bail!("context state changed, but no actions were generated, discarding execution outcome to mitigate potential state inconsistency");
}

Expand Down
18 changes: 18 additions & 0 deletions crates/node/src/sync/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ impl Node {
};

if root_hash == context.root_hash {
debug!(
context_id=%context.id,
our_identity=%our_identity,
their_identity=%their_identity,
"Root hashes match, up to date",
);

return Ok(());
}

Expand Down Expand Up @@ -206,6 +213,13 @@ impl Node {
.await?;

if their_root_hash == context.root_hash {
debug!(
context_id=%context.id,
our_identity=%our_identity,
their_identity=%their_identity,
"Root hashes match, up to date",
);

return Ok(());
}

Expand Down Expand Up @@ -294,6 +308,10 @@ impl Node {
Some(shared_key),
)
.await?;

if outcome.artifact.is_empty() {
break;
}
}

debug!(
Expand Down
Loading
Loading