Skip to content

Commit 1f52306

Browse files
MitchTurnerxgreenxAurelienFT
authored
Integrate the new pre-confirmation signing task adapters (#2784)
## Linked Issues/PRs <!-- List of related issues/PRs --> Part of #2739 ## Description <!-- List of detailed changes --> ## Checklist - [ ] Breaking changes are clearly marked as such in the PR description and changelog - [ ] New behavior is reflected in tests - [ ] [The specification](https://github.com/FuelLabs/fuel-specs/) matches the implemented behavior (link update PR if changes are needed) ### Before requesting review - [ ] I have reviewed the code myself - [ ] I have created follow-up issues caused by this PR and linked them here ### After merging, notify other teams [Add or remove entries as needed] - [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/) - [ ] [Sway compiler](https://github.com/FuelLabs/sway/) - [ ] [Platform documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+) (for out-of-organization contributors, the person merging the PR will do this) - [ ] Someone else? --------- Co-authored-by: Green Baneling <[email protected]> Co-authored-by: AurelienFT <[email protected]>
1 parent c075313 commit 1f52306

File tree

24 files changed

+404
-230
lines changed

24 files changed

+404
-230
lines changed

.changes/added/2784.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Integrate the pre conf signature task into the main consensus task

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/fuel-core/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ axum = { workspace = true }
1919
clap = { workspace = true, features = ["derive"] }
2020
cosmrs = { version = "0.21", optional = true }
2121
derive_more = { version = "0.99" }
22+
ed25519 = { version = "2.2.3", features = ["serde"] }
23+
ed25519-dalek = { version = "2.1.1", features = ["serde"] }
2224
enum-iterator = { workspace = true }
2325
fuel-core-chain-config = { workspace = true, features = ["std"] }
2426
fuel-core-compression = { workspace = true }
@@ -49,7 +51,7 @@ mockall = { workspace = true, optional = true }
4951
num_cpus = { version = "1.16.0", optional = true }
5052
parking_lot = { workspace = true }
5153
paste = { workspace = true }
52-
postcard = { workspace = true, optional = true }
54+
postcard = { workspace = true }
5355
rand = { workspace = true }
5456
rocksdb = { version = "0.21", default-features = false, features = [
5557
"lz4",
@@ -102,7 +104,7 @@ smt = [
102104
p2p = ["dep:fuel-core-p2p", "dep:fuel-core-sync"]
103105
relayer = ["dep:fuel-core-relayer"]
104106
shared-sequencer = ["dep:fuel-core-shared-sequencer", "dep:cosmrs"]
105-
rocksdb = ["dep:rocksdb", "dep:tempfile", "dep:num_cpus", "dep:postcard"]
107+
rocksdb = ["dep:rocksdb", "dep:tempfile", "dep:num_cpus"]
106108
backup = ["rocksdb", "fuel-core-database/backup"]
107109
test-helpers = [
108110
"fuel-core-database/test-helpers",
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
use fuel_core_types::{
2-
fuel_tx::TxId,
3-
services::p2p::PreconfirmationStatus,
4-
};
5-
61
pub mod broadcast;
72
pub mod key_generator;
83
pub mod parent_signature;
9-
pub mod signing_key;
104
pub mod trigger;
115
pub mod tx_receiver;
12-
13-
pub type Preconfirmations = Vec<(TxId, PreconfirmationStatus)>;

crates/fuel-core/src/service/adapters/consensus_module/poa/pre_confirmation_signature/broadcast.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
use crate::service::adapters::consensus_module::poa::pre_confirmation_signature::{
2-
parent_signature::FuelParentSignature,
3-
signing_key::DummyKey,
4-
Preconfirmations,
2+
key_generator::Ed25519Key,
3+
parent_signature::FuelParentSigner,
54
};
65
use fuel_core_poa::pre_confirmation_signature_service::{
7-
broadcast::Broadcast,
6+
broadcast::{
7+
Broadcast,
8+
PublicKey,
9+
Signature,
10+
},
811
error::Result as PoAResult,
9-
Signed,
12+
parent_signature::ParentSignature,
13+
};
14+
use fuel_core_types::services::p2p::{
15+
DelegatePreConfirmationKey,
16+
Preconfirmation,
1017
};
1118

1219
/// TODO: Implement `Broadcast` properly: <https://github.com/FuelLabs/fuel-core/issues/2783>
1320
pub struct P2PBroadcast;
1421

1522
impl Broadcast for P2PBroadcast {
16-
type PreConfirmations = Preconfirmations;
17-
type ParentSignature = FuelParentSignature<DummyKey>;
18-
type DelegateKey = DummyKey;
23+
type DelegateKey = Ed25519Key;
24+
type ParentKey = FuelParentSigner;
25+
type Preconfirmations = Vec<Preconfirmation>;
1926

20-
async fn broadcast_txs(
27+
async fn broadcast_preconfirmations(
2128
&mut self,
22-
_txs: Signed<Self::DelegateKey, Self::PreConfirmations>,
29+
_: Self::Preconfirmations,
30+
_: Signature<Self>,
2331
) -> PoAResult<()> {
2432
todo!()
2533
}
2634

2735
async fn broadcast_delegate_key(
2836
&mut self,
29-
_delegate_key: Self::ParentSignature,
37+
_: DelegatePreConfirmationKey<PublicKey<Self>>,
38+
_: <Self::ParentKey as ParentSignature>::Signature,
3039
) -> PoAResult<()> {
3140
todo!()
3241
}
Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1-
use crate::service::adapters::consensus_module::poa::pre_confirmation_signature::{
2-
signing_key::DummyKey,
3-
};
1+
use ed25519::signature::Signer;
2+
use ed25519_dalek::SigningKey as DalekSigningKey;
43
use fuel_core_poa::pre_confirmation_signature_service::{
5-
error::Result as PoAResult,
4+
error::{
5+
Error as PoAError,
6+
Result as PoAResult,
7+
},
68
key_generator::KeyGenerator,
9+
signing_key::SigningKey,
10+
};
11+
use fuel_core_types::fuel_crypto::SecretKey;
12+
use rand::{
13+
prelude::StdRng,
14+
SeedableRng,
715
};
16+
use serde::Serialize;
17+
use std::ops::Deref;
818

9-
pub struct DelegateKeyGenerator;
19+
pub struct Ed25519KeyGenerator;
1020

11-
impl KeyGenerator for DelegateKeyGenerator {
12-
type Key = DummyKey;
21+
impl KeyGenerator for Ed25519KeyGenerator {
22+
type Key = Ed25519Key;
1323

1424
async fn generate(&mut self) -> PoAResult<Self::Key> {
15-
todo!()
25+
let mut rng = StdRng::from_entropy();
26+
let secret = SecretKey::random(&mut rng);
27+
28+
Ok(Ed25519Key {
29+
signer: DalekSigningKey::from_bytes(secret.deref()),
30+
})
31+
}
32+
}
33+
34+
#[derive(Clone)]
35+
pub struct Ed25519Key {
36+
signer: DalekSigningKey,
37+
}
38+
39+
impl SigningKey for Ed25519Key {
40+
type Signature = ed25519::Signature;
41+
type PublicKey = ed25519_dalek::VerifyingKey;
42+
43+
fn public_key(&self) -> Self::PublicKey {
44+
self.signer.verifying_key()
45+
}
46+
47+
fn sign<T>(&self, data: &T) -> PoAResult<Self::Signature>
48+
where
49+
T: Serialize,
50+
{
51+
let bytes = postcard::to_allocvec(data)
52+
.map_err(|e| PoAError::Signature(format!("{e:?}")))?;
53+
let signature = self.signer.sign(&bytes);
54+
Ok(signature)
1655
}
1756
}

crates/fuel-core/src/service/adapters/consensus_module/poa/pre_confirmation_signature/parent_signature.rs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,28 @@ use fuel_core_poa::pre_confirmation_signature_service::{
77
};
88
use fuel_core_types::{
99
fuel_crypto,
10+
fuel_vm::Signature,
1011
signer::SignMode,
1112
};
13+
use serde::Serialize;
1214

13-
pub struct FuelParentSigner<T> {
15+
pub struct FuelParentSigner {
1416
mode: SignMode,
15-
_phantom: std::marker::PhantomData<T>,
1617
}
1718

18-
pub struct FuelParentSignature<T> {
19-
signature: fuel_core_types::fuel_vm::Signature,
20-
_phantom: std::marker::PhantomData<T>,
21-
}
22-
23-
impl<T> FuelParentSignature<T> {
24-
pub fn signature(&self) -> fuel_core_types::fuel_vm::Signature {
25-
self.signature
26-
}
27-
}
28-
29-
impl<T> From<fuel_core_types::fuel_vm::Signature> for FuelParentSignature<T> {
30-
fn from(signature: fuel_core_types::fuel_vm::Signature) -> Self {
31-
Self {
32-
signature,
33-
_phantom: std::marker::PhantomData,
34-
}
35-
}
36-
}
37-
38-
impl<T> ParentSignature<T> for FuelParentSigner<T>
39-
where
40-
T: Send + Sync,
41-
T: Into<fuel_crypto::Message>,
42-
{
43-
type SignedData = FuelParentSignature<T>;
19+
impl ParentSignature for FuelParentSigner {
20+
type Signature = Signature;
4421

45-
async fn sign(&self, data: T) -> PoAResult<Self::SignedData> {
46-
let message = data.into();
22+
async fn sign<T>(&self, data: &T) -> PoAResult<Self::Signature>
23+
where
24+
T: Serialize + Send + Sync,
25+
{
26+
let bytes = postcard::to_allocvec(data)
27+
.map_err(|e| PoaError::ParentSignature(format!("{e:?}")))?;
28+
let message = fuel_crypto::Message::new(bytes);
4729
let signature = self.mode.sign_message(message).await.map_err(|e| {
4830
PoaError::ParentSignature(format!("Failed to sign message: {}", e))
4931
})?;
50-
Ok(signature.into())
32+
Ok(signature)
5133
}
5234
}

crates/fuel-core/src/service/adapters/consensus_module/poa/pre_confirmation_signature/signing_key.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

crates/fuel-core/src/service/adapters/consensus_module/poa/pre_confirmation_signature/tx_receiver.rs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,84 @@
1-
use crate::service::adapters::consensus_module::poa::pre_confirmation_signature::Preconfirmations;
21
use fuel_core_poa::pre_confirmation_signature_service::{
32
error::{
43
Error as PoaError,
54
Result as PoAResult,
65
},
76
tx_receiver::TxReceiver,
87
};
9-
use fuel_core_types::{
10-
fuel_tx::TxId,
11-
services::p2p::PreconfirmationStatus,
12-
};
8+
use fuel_core_types::services::p2p::Preconfirmation;
9+
use tokio::sync::mpsc;
10+
11+
pub struct PreconfirmationsReceiver {
12+
capacity: usize,
13+
receiver: mpsc::Receiver<Vec<Preconfirmation>>,
14+
}
15+
16+
impl Default for PreconfirmationsReceiver {
17+
fn default() -> Self {
18+
let (_, receiver) = mpsc::channel(1);
19+
Self::new(receiver)
20+
}
21+
}
1322

1423
// TODO(#2739): Remove when integrated
1524
// link: https://github.com/FuelLabs/fuel-core/issues/2739
1625
#[allow(dead_code)]
17-
pub struct MPSCTxReceiver<T> {
18-
receiver: tokio::sync::mpsc::Receiver<T>,
26+
impl PreconfirmationsReceiver {
27+
pub fn new(receiver: mpsc::Receiver<Vec<Preconfirmation>>) -> Self {
28+
let capacity = receiver.capacity();
29+
PreconfirmationsReceiver { capacity, receiver }
30+
}
1931
}
2032

21-
impl TxReceiver for MPSCTxReceiver<Vec<(TxId, PreconfirmationStatus)>> {
22-
type Txs = Preconfirmations;
33+
impl TxReceiver for PreconfirmationsReceiver {
34+
type Txs = Vec<Preconfirmation>;
2335

2436
async fn receive(&mut self) -> PoAResult<Self::Txs> {
25-
self.receiver.recv().await.ok_or(PoaError::TxReceiver(
26-
"Failed to receive transaction, channel closed".to_string(),
27-
))
37+
let mut buffer = Vec::new();
38+
let received = self.receiver.recv_many(&mut buffer, self.capacity).await;
39+
40+
if received == 0 {
41+
return Err(PoaError::TxReceiver(
42+
"Failed to receive transaction, channel closed".to_string(),
43+
));
44+
}
45+
46+
Ok(buffer.into_iter().flatten().collect::<Vec<_>>())
2847
}
2948
}
49+
3050
#[cfg(test)]
3151
mod tests {
3252
#![allow(non_snake_case)]
53+
3354
use super::*;
34-
use fuel_core_types::fuel_types::BlockHeight;
55+
use fuel_core_types::{
56+
fuel_tx::TxId,
57+
fuel_types::BlockHeight,
58+
services::p2p::PreconfirmationStatus,
59+
};
3560

3661
#[tokio::test]
3762
async fn receive__gets_what_is_sent_through_channel() {
3863
// given
39-
let (sender, receiver) = tokio::sync::mpsc::channel(1);
4064
let txs = vec![
41-
(
42-
TxId::default(),
43-
PreconfirmationStatus::SuccessByBlockProducer {
65+
Preconfirmation {
66+
tx_id: TxId::default(),
67+
status: PreconfirmationStatus::SuccessByBlockProducer {
4468
block_height: BlockHeight::from(123),
4569
},
46-
),
47-
(
48-
TxId::default(),
49-
PreconfirmationStatus::SqueezedOutByBlockProducer {
70+
},
71+
Preconfirmation {
72+
tx_id: TxId::default(),
73+
status: PreconfirmationStatus::SqueezedOutByBlockProducer {
5074
reason: "test".to_string(),
5175
},
52-
),
76+
},
5377
];
5478

55-
let mut receiver = MPSCTxReceiver { receiver };
79+
let (sender, receiver) = mpsc::channel(1);
80+
81+
let mut receiver = PreconfirmationsReceiver::new(receiver);
5682

5783
// when
5884
sender.send(txs.clone()).await.unwrap();

crates/fuel-core/src/service/adapters/relayer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use fuel_core_storage::transactional::{
1111
use fuel_core_types::blockchain::primitives::DaBlockHeight;
1212

1313
impl Transactional for Database<Relayer> {
14-
type Transaction<'a> = StorageTransaction<&'a mut Self> where Self: 'a;
14+
type Transaction<'a>
15+
= StorageTransaction<&'a mut Self>
16+
where
17+
Self: 'a;
1518

1619
fn transaction(&mut self) -> Self::Transaction<'_> {
1720
self.into_transaction()

0 commit comments

Comments
 (0)