Skip to content

Commit

Permalink
Historical evm events (#173)
Browse files Browse the repository at this point in the history
* Failing test for historical events

* Tidy up use statements

* EVM Contracts listen for both live and historical events

* Format

* Format

* Cache processed event_ids

* Formatting

* Formatting

* Store last_block

* Report last block to parent

* Hook up last_block to reader

* Tidy up test code

* Refactor to push persistence to EvmEventReader

* Tidy up dead code

* Resume after shutdown test

* Formatting

* Neaten up test

* Formatting

* Hook start_block up to config

* Formatting

* Remove unnecessary Actor

* Fix not loading snapshot(must have been using deduplication)

* Add passing test for config

* Formatting

* Rename parent -> processor

* Improve tracing transparency and fix config bug

* Centralize logging tag/id

* Tidy up instrument code and fix error

* Formatting

* Update packages/ciphernode/core/src/tag.rs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Formatting

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
ryardley and coderabbitai[bot] authored Nov 13, 2024
1 parent 90d33f9 commit dc7d047
Show file tree
Hide file tree
Showing 50 changed files with 1,310 additions and 452 deletions.
10 changes: 8 additions & 2 deletions packages/ciphernode/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 packages/ciphernode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fhe-util = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-bet
futures = "0.3.30"
futures-util = "0.3"
hex = "0.4.3"
lazy_static = "1.5.0"
num = "0.4.3"
rand_chacha = "0.3.1"
rand = "0.8.5"
Expand Down
33 changes: 33 additions & 0 deletions packages/ciphernode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,36 @@ sequenceDiagram
PTA--)PTA: Stop
KS--)-KS: Stop
```

# Debugging

You can debug using the `RUST_LOG` environment var to alter what output is produced by the node


```
RUST_LOG=info enclave start
```

if you supply a tag as an argument you can filter for that tag

```
RUST_LOG="[sortition{id=cn1}]" enclave start --tag cn1
```

This helps filter noise during tests where you might have multiple instances running and you need to see the output of a specific one.

In order to add tracing to a method or function it is recommended to use the `instrument` macro.

```rust
impl Sorition {
// ...
#[instrument(name="sortition", skip_all, fields(id = get_tag()))]
pub async fn attach(
bus: &Addr<EventBus>,
store: Repository<SortitionModule>,
) -> Result<Addr<Sortition>> {
// ...
}
}
```

4 changes: 2 additions & 2 deletions packages/ciphernode/aggregator/src/plaintext_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl FromSnapshotWithParams for PlaintextAggregator {
}

impl Checkpoint for PlaintextAggregator {
fn repository(&self) -> Repository<PlaintextAggregatorState> {
self.store.clone()
fn repository(&self) -> &Repository<PlaintextAggregatorState> {
&self.store
}
}
4 changes: 2 additions & 2 deletions packages/ciphernode/aggregator/src/publickey_aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl FromSnapshotWithParams for PublicKeyAggregator {
}

impl Checkpoint for PublicKeyAggregator {
fn repository(&self) -> Repository<PublicKeyAggregatorState> {
self.store.clone()
fn repository(&self) -> &Repository<PublicKeyAggregatorState> {
&self.store
}
}
50 changes: 44 additions & 6 deletions packages/ciphernode/config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,39 @@ use std::{
path::{Path, PathBuf},
};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum Contract {
Full {
address: String,
deploy_block: Option<u64>,
},
AddressOnly(String),
}

impl Contract {
pub fn address(&self) -> &String {
use Contract::*;
match self {
Full { address, .. } => address,
AddressOnly(v) => v,
}
}

pub fn deploy_block(&self) -> Option<u64> {
use Contract::*;
match self {
Full { deploy_block, .. } => deploy_block.clone(),
AddressOnly(_) => None,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ContractAddresses {
pub enclave: String,
pub ciphernode_registry: String,
pub filter_registry: String,
pub enclave: Contract,
pub ciphernode_registry: Contract,
pub filter_registry: Contract,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -297,7 +325,9 @@ chains:
rpc_url: "ws://localhost:8545"
contracts:
enclave: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
ciphernode_registry: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
ciphernode_registry:
address: "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
deploy_block: 1764352873645
filter_registry: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
"#,
)?;
Expand All @@ -308,10 +338,18 @@ chains:
assert_eq!(chain.name, "hardhat");
assert_eq!(chain.rpc_url, "ws://localhost:8545");
assert_eq!(
chain.contracts.enclave,
chain.contracts.enclave.address(),
"0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
);

assert_eq!(
chain.contracts.ciphernode_registry.address(),
"0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9"
);
assert_eq!(chain.contracts.enclave.deploy_block(), None);
assert_eq!(
chain.contracts.ciphernode_registry.deploy_block(),
Some(1764352873645)
);
Ok(())
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/ciphernode/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ alloy = { workspace = true }
alloy-primitives = { workspace = true }
alloy-sol-types = { workspace = true }
anyhow = { workspace = true }
lazy_static = { workspace = true }
29 changes: 15 additions & 14 deletions packages/ciphernode/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl TryFrom<E3id> for U256 {
pub struct EventId(pub [u8; 32]);

impl EventId {
fn from<T: Hash>(value: T) -> Self {
pub fn hash<T: Hash>(value: T) -> Self {
let mut hasher = Sha256::new();
let mut std_hasher = DefaultHasher::new();
value.hash(&mut std_hasher);
Expand Down Expand Up @@ -224,7 +224,7 @@ pub trait FromError {
impl From<KeyshareCreated> for EnclaveEvent {
fn from(data: KeyshareCreated) -> Self {
EnclaveEvent::KeyshareCreated {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -233,7 +233,7 @@ impl From<KeyshareCreated> for EnclaveEvent {
impl From<E3Requested> for EnclaveEvent {
fn from(data: E3Requested) -> Self {
EnclaveEvent::E3Requested {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -242,7 +242,7 @@ impl From<E3Requested> for EnclaveEvent {
impl From<PublicKeyAggregated> for EnclaveEvent {
fn from(data: PublicKeyAggregated) -> Self {
EnclaveEvent::PublicKeyAggregated {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -251,7 +251,7 @@ impl From<PublicKeyAggregated> for EnclaveEvent {
impl From<CiphertextOutputPublished> for EnclaveEvent {
fn from(data: CiphertextOutputPublished) -> Self {
EnclaveEvent::CiphertextOutputPublished {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -260,7 +260,7 @@ impl From<CiphertextOutputPublished> for EnclaveEvent {
impl From<DecryptionshareCreated> for EnclaveEvent {
fn from(data: DecryptionshareCreated) -> Self {
EnclaveEvent::DecryptionshareCreated {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -269,7 +269,7 @@ impl From<DecryptionshareCreated> for EnclaveEvent {
impl From<PlaintextAggregated> for EnclaveEvent {
fn from(data: PlaintextAggregated) -> Self {
EnclaveEvent::PlaintextAggregated {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -278,7 +278,7 @@ impl From<PlaintextAggregated> for EnclaveEvent {
impl From<E3RequestComplete> for EnclaveEvent {
fn from(data: E3RequestComplete) -> Self {
EnclaveEvent::E3RequestComplete {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -287,7 +287,7 @@ impl From<E3RequestComplete> for EnclaveEvent {
impl From<CiphernodeSelected> for EnclaveEvent {
fn from(data: CiphernodeSelected) -> Self {
EnclaveEvent::CiphernodeSelected {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -296,7 +296,7 @@ impl From<CiphernodeSelected> for EnclaveEvent {
impl From<CiphernodeAdded> for EnclaveEvent {
fn from(data: CiphernodeAdded) -> Self {
EnclaveEvent::CiphernodeAdded {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -305,7 +305,7 @@ impl From<CiphernodeAdded> for EnclaveEvent {
impl From<CiphernodeRemoved> for EnclaveEvent {
fn from(data: CiphernodeRemoved) -> Self {
EnclaveEvent::CiphernodeRemoved {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -314,7 +314,7 @@ impl From<CiphernodeRemoved> for EnclaveEvent {
impl From<EnclaveError> for EnclaveEvent {
fn from(data: EnclaveError) -> Self {
EnclaveEvent::EnclaveError {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -323,7 +323,7 @@ impl From<EnclaveError> for EnclaveEvent {
impl From<Shutdown> for EnclaveEvent {
fn from(data: Shutdown) -> Self {
EnclaveEvent::Shutdown {
id: EventId::from(data.clone()),
id: EventId::hash(data.clone()),
data: data.clone(),
}
}
Expand All @@ -332,7 +332,7 @@ impl From<Shutdown> for EnclaveEvent {
impl From<TestEvent> for EnclaveEvent {
fn from(value: TestEvent) -> Self {
EnclaveEvent::TestEvent {
id: EventId::from(value.clone()),
id: EventId::hash(value.clone()),
data: value.clone(),
}
}
Expand Down Expand Up @@ -552,6 +552,7 @@ impl Display for Shutdown {
#[rtype(result = "()")]
pub struct TestEvent {
pub msg: String,
pub entropy: u64,
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
mod eventbus;
mod events;
mod ordered_set;
mod tag;

pub use eventbus::*;
pub use events::*;
pub use ordered_set::*;
pub use tag::*;
21 changes: 21 additions & 0 deletions packages/ciphernode/core/src/tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Tag management for EVM event processing.
//!
//! This module provides thread-safe access to a global string tag that's used to
//! differentiate between different EVM contract instances during event processing.
//! The tag helps track and manage historical and live events for specific contracts.
use std::sync::OnceLock;

/// Global tag for contract event tracking with a default value of "_".
/// This tag is initialized once and remains constant throughout the lifecycle
/// of event processing to ensure consistent event tracking across restarts.
static TAG: OnceLock<String> = OnceLock::new();

pub fn get_tag() -> String {
TAG.get().cloned().unwrap_or_else(|| String::from("_"))
}

pub fn set_tag(new_tag: impl Into<String>) -> Result<(), &'static str> {
TAG.set(new_tag.into())
.map_err(|_| "Tag has already been initialized")
}
2 changes: 1 addition & 1 deletion packages/ciphernode/data/src/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Get {
}

/// Generate proxy for the DB
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct DataStore {
scope: Vec<u8>,
get: Recipient<Get>,
Expand Down
1 change: 1 addition & 0 deletions packages/ciphernode/data/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;

use crate::DataStore;

#[derive(Debug)]
pub struct Repository<S> {
store: DataStore,
_p: PhantomData<S>,
Expand Down
Loading

0 comments on commit dc7d047

Please sign in to comment.