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

feat(voyager): add plugin to periodically update clients for liveliness #3401

Merged
merged 1 commit into from
Dec 24, 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
24 changes: 24 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ members = [
"voyager/plugins/client-update/movement",
"voyager/plugins/client-update/tendermint",

"voyager/plugins/periodic-client-update",

"voyager/plugins/event-source/cosmos-sdk",
"voyager/plugins/event-source/ethereum",
"voyager/plugins/event-source/movement",
Expand Down
50 changes: 33 additions & 17 deletions lib/voyager-message/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use serde::de::DeserializeOwned;
use tracing::instrument;
use unionlabs::traits::Member;
use voyager_core::{ClientInfo, IbcSpecId};
use voyager_vm::{CallbackT, Op, QueueError};
use voyager_vm::{BoxDynError, CallbackT, Op, QueueError};

use crate::{
context::WithId,
core::ChainId,
data::{ClientUpdate, Data, OrderedClientUpdates, OrderedHeaders},
data::{Data, IbcDatagram, OrderedHeaders, WithChainId},
error_object_to_queue_error, json_rpc_error_to_queue_error,
module::{ClientModuleClient, PluginClient},
Context, PluginMessage, RawClientId, VoyagerMessage,
Expand Down Expand Up @@ -51,7 +51,7 @@ impl CallbackT<VoyagerMessage> for Callback {
AggregateMsgUpdateClientsFromOrderedHeaders {
ibc_spec_id,
chain_id,
counterparty_client_id,
client_id,
},
) => {
let OrderedHeaders { headers } = data
Expand Down Expand Up @@ -80,7 +80,7 @@ impl CallbackT<VoyagerMessage> for Callback {
} = ctx
.rpc_server
.with_id(Some(ctx.id()))
.client_info(&chain_id, &ibc_spec_id, counterparty_client_id.clone())
.client_info(&chain_id, &ibc_spec_id, client_id.clone())
.await
.map_err(error_object_to_queue_error)?;

Expand All @@ -91,23 +91,39 @@ impl CallbackT<VoyagerMessage> for Callback {
.client_module(&client_type, &ibc_interface, &ibc_spec_id)?
.with_id(Some(ctx.id()));

Ok(voyager_vm::data(OrderedClientUpdates {
let ibc_spec_handler = ctx
.rpc_server
.modules()
.map_err(error_object_to_queue_error)?
.ibc_spec_handlers
.get(&ibc_spec_id)
.map_err(error_object_to_queue_error)?;

Ok(voyager_vm::data(WithChainId {
chain_id,
// REVIEW: Use FuturesOrdered here?
updates: stream::iter(headers.into_iter())
.then(|(meta, header)| {
message: stream::iter(headers.into_iter())
.then(|(_, header)| {
client_module
.encode_header(header)
.map_ok(|encoded_header| {
(
meta,
ClientUpdate {
client_id: counterparty_client_id.clone(),
ibc_spec_id: ibc_spec_id.clone(),
client_message: encoded_header,
},
.map_err(json_rpc_error_to_queue_error)
.and_then(|encoded_header| {
futures::future::ready(
(ibc_spec_handler.msg_update_client)(
client_id.clone(),
encoded_header,
)
.map_err(|e| {
QueueError::Fatal(<BoxDynError>::from(format!("{e:#}")))
})
.map(|datagram| {
IbcDatagram {
ibc_spec_id: ibc_spec_id.clone(),
datagram,
}
}),
)
})
.map_err(json_rpc_error_to_queue_error)
})
.try_collect::<Vec<_>>()
.await?,
Expand All @@ -127,5 +143,5 @@ impl CallbackT<VoyagerMessage> for Callback {
pub struct AggregateMsgUpdateClientsFromOrderedHeaders {
pub ibc_spec_id: IbcSpecId,
pub chain_id: ChainId,
pub counterparty_client_id: RawClientId,
pub client_id: RawClientId,
}
30 changes: 28 additions & 2 deletions lib/voyager-message/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use futures::{
Future, FutureExt, StreamExt, TryStreamExt,
};
use jsonrpsee::{
core::client::ClientT,
core::{client::ClientT, RpcResult},
server::middleware::rpc::RpcServiceT,
types::{ErrorObject, ErrorObjectOwned},
};
Expand All @@ -28,7 +28,9 @@ use tracing::{
debug, debug_span, error, info, info_span, instrument, instrument::Instrumented, trace, warn,
Instrument,
};
use unionlabs::{ethereum::keccak256, hash::hash_v2::HexUnprefixed, traits::Member, ErrorReporter};
use unionlabs::{
bytes::Bytes, ethereum::keccak256, hash::hash_v2::HexUnprefixed, traits::Member, ErrorReporter,
};
use voyager_core::{ConsensusType, IbcSpecId};
use voyager_vm::{ItemId, QueueError};

Expand Down Expand Up @@ -82,15 +84,33 @@ pub struct IbcSpecHandlers {
}

impl IbcSpecHandlers {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
handlers: HashMap::default(),
}
}

pub fn register<S: IbcSpec>(&mut self) {
self.handlers.insert(S::ID, IbcSpecHandler::new::<S>());
}

pub fn get(&self, ibc_spec_id: &IbcSpecId) -> RpcResult<&IbcSpecHandler> {
self.handlers.get(ibc_spec_id).ok_or_else(|| {
ErrorObject::owned(
FATAL_JSONRPC_ERROR_CODE,
format!("unknown IBC spec `{ibc_spec_id}`"),
None::<()>,
)
})
}
}

/// A type-erased version of the methods on [`IbcSpec`] (essentially a vtable).
pub struct IbcSpecHandler {
pub client_state_path: fn(RawClientId) -> anyhow::Result<Value>,
pub consensus_state_path: fn(RawClientId, String) -> anyhow::Result<Value>,
pub msg_update_client: fn(RawClientId, Bytes) -> anyhow::Result<Value>,
}

impl IbcSpecHandler {
Expand All @@ -107,6 +127,12 @@ impl IbcSpecHandler {
height.parse()?,
)))
},
msg_update_client: |client_id, client_message| {
Ok(into_value(T::update_client_datagram(
serde_json::from_value(client_id.0)?,
client_message,
)))
},
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions lib/voyager-message/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use tracing::{
};
use unionlabs::{bytes::Bytes, ibc::core::client::height::Height, traits::Member, ErrorReporter};
use voyager_core::{
ChainId, ClientInfo, ClientStateMeta, ClientType, IbcInterface, IbcSpec, IbcStorePathKey,
QueryHeight,
ChainId, ClientInfo, ClientStateMeta, ClientType, IbcInterface, IbcSpec, IbcSpecId,
IbcStorePathKey, QueryHeight,
};
use voyager_vm::{ItemId, QueueError, QueueMessage};

Expand Down Expand Up @@ -635,6 +635,19 @@ impl VoyagerClient {
.await
.map_err(json_rpc_error_to_error_object)
}

pub async fn client_meta_raw(
&self,
chain_id: ChainId,
ibc_spec_id: IbcSpecId,
at: QueryHeight,
client_id: RawClientId,
) -> RpcResult<ClientStateMeta> {
self.0
.client_meta(chain_id, ibc_spec_id, at, client_id)
.await
.map_err(json_rpc_error_to_error_object)
}
}

pub trait ExtensionsExt {
Expand Down
46 changes: 23 additions & 23 deletions voyager/plugins/client-update/cometbls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ name = "voyager-client-update-plugin-cometbls"
version = "0.1.0"

[dependencies]
cometbft-rpc = { workspace = true }
cometbft-types.workspace = true
cometbls-light-client-types.workspace = true
dashmap = { workspace = true }
enumorph = { workspace = true }
futures = { workspace = true }
galois-rpc.workspace = true
itertools = "0.13.0"
jsonrpsee = { workspace = true, features = ["macros", "server", "tracing"] }
macros = { workspace = true }
num-bigint = { workspace = true }
prost = { workspace = true }
protos = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
subset-of = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unionlabs = { workspace = true }
voyager-message = { workspace = true }
voyager-vm = { workspace = true }
cometbft-rpc = { workspace = true }
cometbft-types = { workspace = true }
cometbls-light-client-types = { workspace = true }
dashmap = { workspace = true }
enumorph = { workspace = true }
futures = { workspace = true }
galois-rpc = { workspace = true }
itertools = "0.13.0"
jsonrpsee = { workspace = true, features = ["macros", "server", "tracing"] }
macros = { workspace = true }
num-bigint = { workspace = true }
prost = { workspace = true }
protos = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
subset-of = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unionlabs = { workspace = true }
voyager-message = { workspace = true }
voyager-vm = { workspace = true }
34 changes: 17 additions & 17 deletions voyager/plugins/packet-filter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ name = "voyager-plugin-packet-filter"
version = "0.1.0"

[dependencies]
enumorph = { workspace = true }
futures = { workspace = true }
ibc-classic-spec.workspace = true
ibc-union-spec.workspace = true
jsonrpsee = { workspace = true, features = ["macros", "server", "tracing"] }
macros = { workspace = true }
regex = "1.10.6"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_with = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unionlabs = { workspace = true }
voyager-message = { workspace = true }
voyager-vm = { workspace = true }
enumorph = { workspace = true }
futures = { workspace = true }
ibc-classic-spec = { workspace = true }
ibc-union-spec = { workspace = true }
jsonrpsee = { workspace = true, features = ["macros", "server", "tracing"] }
macros = { workspace = true }
regex = "1.10.6"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
serde_with = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unionlabs = { workspace = true }
voyager-message = { workspace = true }
voyager-vm = { workspace = true }
24 changes: 24 additions & 0 deletions voyager/plugins/periodic-client-update/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
edition = "2021"
name = "voyager-periodic-client-update-plugin"
version = "0.1.0"

[dependencies]
clap = { workspace = true, features = ["derive"] }
enumorph = { workspace = true }
futures = { workspace = true }
ibc-classic-spec = { workspace = true }
ibc-union-spec = { workspace = true }
itertools = "0.13.0"
jsonrpsee = { workspace = true, features = ["macros", "server", "tracing"] }
macros = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
subset-of = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
unionlabs = { workspace = true }
voyager-message = { workspace = true }
voyager-vm = { workspace = true }
23 changes: 23 additions & 0 deletions voyager/plugins/periodic-client-update/src/call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use macros::model;
use voyager_message::{
core::{ChainId, IbcSpecId},
RawClientId,
};
use voyager_vm::BoxDynError;

#[model]
pub enum ModuleCall {
CheckForClientAge(CheckForClientAge),
}

#[model]
#[derive(clap::Args)]
pub struct CheckForClientAge {
#[arg(value_parser(|s: &str| Ok::<_, BoxDynError>(ChainId::new(s.to_owned()))))]
pub chain_id: ChainId,
#[arg(value_parser(|s: &str| Ok::<_, BoxDynError>(IbcSpecId::new(s.to_owned()))))]
pub ibc_spec_id: IbcSpecId,
pub client_id: RawClientId,
/// The maximum amount of blocks this client can lag behind the latest finalized height of the chain it's tracking.
pub max_age: u64,
}
Loading
Loading