Skip to content

Commit 2514e1a

Browse files
cleanup client interface
1 parent 8772410 commit 2514e1a

3 files changed

Lines changed: 38 additions & 42 deletions

File tree

chain/src/actors/syncer/actor.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::{
1515
};
1616
use alto_types::{Block, Finalized, Notarized};
1717
use bytes::Bytes;
18-
use commonware_codec::{Decode, DecodeExt};
19-
use commonware_consensus::threshold_simplex::types::Finalization;
18+
use commonware_codec::{Decode, DecodeExt, Encode};
19+
use commonware_consensus::threshold_simplex::types::{Finalization, Seedable, Viewable};
2020
use commonware_cryptography::{bls12381, ed25519::PublicKey, sha256::Digest, Digestible};
2121
use commonware_macros::select;
2222
use commonware_p2p::{utils::requester, Receiver, Recipients, Sender};
@@ -341,9 +341,9 @@ impl<R: Rng + Spawner + Metrics + Clock + GClock + Storage, I: Indexer> Actor<R,
341341
.await
342342
.expect("Failed to get finalization");
343343
if let Some(finalization) = finalization {
344-
let finalization = Finalization::deserialize(None, &finalization)
344+
let finalization = Finalization::decode(finalization.as_ref())
345345
.expect("Failed to deserialize finalization");
346-
*last_view_processed.lock().await = finalization.view;
346+
*last_view_processed.lock().await = finalization.view();
347347
}
348348
continue;
349349
}
@@ -358,7 +358,7 @@ impl<R: Rng + Spawner + Metrics + Clock + GClock + Storage, I: Indexer> Actor<R,
358358
.await
359359
.expect("Failed to get finalized block")
360360
.expect("Gapped block missing");
361-
let gapped_block = Block::deserialize(&gapped_block)
361+
let gapped_block = Block::decode(gapped_block.as_ref())
362362
.expect("Failed to deserialize block");
363363

364364
// Attempt to repair one block from other sources
@@ -368,10 +368,10 @@ impl<R: Rng + Spawner + Metrics + Clock + GClock + Storage, I: Indexer> Actor<R,
368368
.await
369369
.expect("Failed to get verified block");
370370
if let Some(verified) = verified {
371-
let verified = Block::deserialize(&verified)
371+
let verified = Block::decode(verified.as_ref())
372372
.expect("Failed to deserialize block");
373373
blocks
374-
.put(verified.height, target_block, verified.serialize().into())
374+
.put(verified.height, target_block, verified.encode().into())
375375
.await
376376
.expect("Failed to insert finalized block");
377377
debug!(height = verified.height, "repaired block from verified");
@@ -382,13 +382,13 @@ impl<R: Rng + Spawner + Metrics + Clock + GClock + Storage, I: Indexer> Actor<R,
382382
.await
383383
.expect("Failed to get notarized block");
384384
if let Some(notarization) = notarization {
385-
let notarization = Notarized::deserialize(None, &notarization)
385+
let notarization = Notarized::decode(notarization.as_ref())
386386
.expect("Failed to deserialize block");
387387
blocks
388388
.put(
389389
notarization.block.height,
390390
target_block,
391-
notarization.block.serialize().into(),
391+
notarization.block.encode().into(),
392392
)
393393
.await
394394
.expect("Failed to insert finalized block");
@@ -466,24 +466,23 @@ impl<R: Rng + Spawner + Metrics + Clock + GClock + Storage, I: Indexer> Actor<R,
466466
Message::Broadcast { payload } => {
467467
broadcast_network
468468
.0
469-
.send(Recipients::All, payload.serialize().into(), true)
469+
.send(Recipients::All, payload.encode().into(), true)
470470
.await
471471
.expect("Failed to broadcast");
472472
}
473473
Message::Verified { view, payload } => {
474474
verified
475-
.put(view, payload.digest(), payload.serialize().into())
475+
.put(view, payload.digest(), payload.encode().into())
476476
.await
477477
.expect("Failed to insert verified block");
478478
}
479-
Message::Notarized { proof, seed } => {
479+
Message::Notarization { notarization } => {
480480
// Upload seed to indexer (if available)
481481
if let Some(indexer) = self.indexer.as_ref() {
482482
self.context.with_label("indexer").spawn({
483483
let indexer = indexer.clone();
484-
let view = proof.view;
484+
let seed = notarization.seed();
485485
move |_| async move {
486-
let seed = seed.serialize().into();
487486
let result = indexer.seed_upload(seed).await;
488487
if let Err(e) = result {
489488
warn!(?e, "failed to upload seed");

chain/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::future::Future;
2-
3-
use bytes::Bytes;
1+
use alto_types::{Finalized, Notarized};
2+
use commonware_consensus::threshold_simplex::types::Seed;
43
use commonware_cryptography::bls12381;
54
use serde::{Deserialize, Serialize};
5+
use std::future::Future;
66

77
pub mod actors;
88
pub mod engine;
@@ -15,46 +15,44 @@ pub trait Indexer: Clone + Send + Sync + 'static {
1515
fn new(uri: &str, public: bls12381::PublicKey) -> Self;
1616

1717
/// Upload a seed to the indexer.
18-
fn seed_upload(&self, seed: Bytes) -> impl Future<Output = Result<(), Self::Error>> + Send;
18+
fn seed_upload(&self, seed: Seed) -> impl Future<Output = Result<(), Self::Error>> + Send;
1919

2020
/// Upload a notarization to the indexer.
21-
fn notarization_upload(
21+
fn notarized_upload(
2222
&self,
23-
notarized: Bytes,
23+
notarized: Notarized,
2424
) -> impl Future<Output = Result<(), Self::Error>> + Send;
2525

2626
/// Upload a finalization to the indexer.
27-
fn finalization_upload(
27+
fn finalized_upload(
2828
&self,
29-
finalized: Bytes,
29+
finalized: Finalized,
3030
) -> impl Future<Output = Result<(), Self::Error>> + Send;
3131
}
3232

3333
impl Indexer for alto_client::Client {
3434
type Error = alto_client::Error;
35+
3536
fn new(uri: &str, public: bls12381::PublicKey) -> Self {
3637
Self::new(uri, public)
3738
}
3839

39-
fn seed_upload(
40-
&self,
41-
seed: bytes::Bytes,
42-
) -> impl Future<Output = Result<(), Self::Error>> + Send {
40+
fn seed_upload(&self, seed: Seed) -> impl Future<Output = Result<(), Self::Error>> + Send {
4341
self.seed_upload(seed)
4442
}
4543

46-
fn notarization_upload(
44+
fn notarized_upload(
4745
&self,
48-
notarization: bytes::Bytes,
46+
notarized: Notarized,
4947
) -> impl Future<Output = Result<(), Self::Error>> + Send {
50-
self.notarization_upload(notarization)
48+
self.notarized_upload(notarized)
5149
}
5250

53-
fn finalization_upload(
51+
fn finalized_upload(
5452
&self,
55-
finalization: bytes::Bytes,
53+
finalized: Finalized,
5654
) -> impl Future<Output = Result<(), Self::Error>> + Send {
57-
self.finalization_upload(finalization)
55+
self.finalized_upload(finalized)
5856
}
5957
}
6058

client/src/consensus.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{Client, Error, IndexQuery, Query};
22
use alto_types::{Block, Finalized, Kind, Notarized, NAMESPACE};
3-
use bytes::Bytes;
4-
use commonware_codec::DecodeExt;
3+
use commonware_codec::{DecodeExt, Encode};
54
use commonware_consensus::threshold_simplex::types::{Seed, Viewable};
65
use commonware_cryptography::Digestible;
76
use futures::{channel::mpsc::unbounded, Stream, StreamExt};
@@ -53,11 +52,11 @@ pub enum Message {
5352
}
5453

5554
impl Client {
56-
pub async fn seed_upload(&self, seed: Bytes) -> Result<(), Error> {
55+
pub async fn seed_upload(&self, seed: Seed) -> Result<(), Error> {
5756
let result = self
5857
.client
5958
.post(seed_upload_path(self.uri.clone()))
60-
.body(seed)
59+
.body(seed.encode().to_vec())
6160
.send()
6261
.await
6362
.map_err(Error::Reqwest)?;
@@ -96,11 +95,11 @@ impl Client {
9695
Ok(seed)
9796
}
9897

99-
pub async fn notarization_upload(&self, notarized: Bytes) -> Result<(), Error> {
98+
pub async fn notarized_upload(&self, notarized: Notarized) -> Result<(), Error> {
10099
let result = self
101100
.client
102101
.post(notarization_upload_path(self.uri.clone()))
103-
.body(notarized)
102+
.body(notarized.encode().to_vec())
104103
.send()
105104
.await
106105
.map_err(Error::Reqwest)?;
@@ -110,7 +109,7 @@ impl Client {
110109
Ok(())
111110
}
112111

113-
pub async fn notarization_get(&self, query: IndexQuery) -> Result<Notarized, Error> {
112+
pub async fn notarized_get(&self, query: IndexQuery) -> Result<Notarized, Error> {
114113
// Get the notarization
115114
let result = self
116115
.client
@@ -139,11 +138,11 @@ impl Client {
139138
Ok(notarized)
140139
}
141140

142-
pub async fn finalization_upload(&self, finalized: Bytes) -> Result<(), Error> {
141+
pub async fn finalized_upload(&self, finalized: Finalized) -> Result<(), Error> {
143142
let result = self
144143
.client
145144
.post(finalization_upload_path(self.uri.clone()))
146-
.body(finalized)
145+
.body(finalized.encode().to_vec())
147146
.send()
148147
.await
149148
.map_err(Error::Reqwest)?;
@@ -153,7 +152,7 @@ impl Client {
153152
Ok(())
154153
}
155154

156-
pub async fn finalization_get(&self, query: IndexQuery) -> Result<Finalized, Error> {
155+
pub async fn finalized_get(&self, query: IndexQuery) -> Result<Finalized, Error> {
157156
// Get the finalization
158157
let result = self
159158
.client

0 commit comments

Comments
 (0)