Skip to content

Commit ea1b17b

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat/alloy-rs
2 parents 524fe22 + 03ad531 commit ea1b17b

File tree

10 files changed

+66
-51
lines changed

10 files changed

+66
-51
lines changed

Cargo.lock

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

committer/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub struct Fuel {
3333
/// URL to a fuel-core graphql endpoint.
3434
#[serde(deserialize_with = "parse_url")]
3535
pub graphql_endpoint: Url,
36-
/// Block producer public key
37-
pub block_producer_public_key: ports::fuel::FuelPublicKey,
36+
/// Block producer address
37+
pub block_producer_address: ports::fuel::FuelBytes32,
3838
}
3939

4040
#[derive(Debug, Clone, Deserialize)]

committer/src/setup.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn block_committer(
5656
registry: &Registry,
5757
cancel_token: CancellationToken,
5858
) -> tokio::task::JoinHandle<()> {
59-
let validator = BlockValidator::new(config.fuel.block_producer_public_key);
59+
let validator = BlockValidator::new(*config.fuel.block_producer_address);
6060

6161
let block_committer = BlockCommitter::new(l1, storage, fuel, validator, commit_interval);
6262

@@ -94,7 +94,7 @@ pub fn state_importer(
9494
cancel_token: CancellationToken,
9595
config: &config::Config,
9696
) -> tokio::task::JoinHandle<()> {
97-
let validator = BlockValidator::new(config.fuel.block_producer_public_key);
97+
let validator = BlockValidator::new(*config.fuel.block_producer_address);
9898
let state_importer = services::StateImporter::new(storage, fuel, validator);
9999

100100
schedule_polling(

e2e/src/committer.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct Committer {
1212
state_contract_address: Option<String>,
1313
eth_rpc: Option<Url>,
1414
fuel_rpc: Option<Url>,
15-
fuel_block_producer_public_key: Option<String>,
15+
fuel_block_producer_addr: Option<String>,
1616
db_port: Option<u16>,
1717
db_name: Option<String>,
1818
kms_url: Option<String>,
@@ -49,8 +49,8 @@ impl Committer {
4949
get_field!(fuel_rpc).as_str(),
5050
)
5151
.env(
52-
"COMMITTER__FUEL__BLOCK_PRODUCER_PUBLIC_KEY",
53-
get_field!(fuel_block_producer_public_key),
52+
"COMMITTER__FUEL__BLOCK_PRODUCER_ADDRESS",
53+
get_field!(fuel_block_producer_addr),
5454
)
5555
.env("COMMITTER__APP__DB__PORT", get_field!(db_port).to_string())
5656
.env("COMMITTER__APP__DB__DATABASE", get_field!(db_name))
@@ -108,11 +108,8 @@ impl Committer {
108108
self
109109
}
110110

111-
pub fn with_fuel_block_producer_public_key(
112-
mut self,
113-
fuel_block_producer_public_key: FuelPublicKey,
114-
) -> Self {
115-
self.fuel_block_producer_public_key = Some(fuel_block_producer_public_key.to_string());
111+
pub fn with_fuel_block_producer_addr(mut self, fuel_block_producer_addr: [u8; 32]) -> Self {
112+
self.fuel_block_producer_addr = Some(hex::encode(fuel_block_producer_addr));
116113
self
117114
}
118115

e2e/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ mod tests {
4646

4747
let latest_block = stack.fuel_node.client().latest_block().await?;
4848

49-
let validated_block =
50-
BlockValidator::new(stack.fuel_node.consensus_pub_key()).validate(&latest_block)?;
49+
let validated_block = BlockValidator::new(*stack.fuel_node.consensus_pub_key().hash())
50+
.validate(&latest_block)?;
5151

5252
assert!(stack.deployed_contract.finalized(validated_block).await?);
5353

e2e/src/whole_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async fn start_committer(
126126
.with_db_port(random_db.port())
127127
.with_db_name(random_db.db_name())
128128
.with_state_contract_address(deployed_contract.address())
129-
.with_fuel_block_producer_public_key(fuel_node.consensus_pub_key())
129+
.with_fuel_block_producer_addr(*fuel_node.consensus_pub_key().hash())
130130
.with_main_key_id(main_key.id.clone())
131131
.with_kms_url(main_key.url.clone());
132132

packages/services/src/block_committer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ mod tests {
255255
async fn will_fetch_and_submit_missed_block() {
256256
// given
257257
let secret_key = given_secret_key();
258-
let block_validator = BlockValidator::new(secret_key.public_key());
258+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
259259
let missed_block = given_a_block(4, &secret_key);
260260
let latest_block = given_a_block(5, &secret_key);
261261
let fuel_adapter = given_fetcher(vec![latest_block, missed_block.clone()]);
@@ -278,7 +278,7 @@ mod tests {
278278
async fn will_not_reattempt_submitting_missed_block() {
279279
// given
280280
let secret_key = given_secret_key();
281-
let block_validator = BlockValidator::new(secret_key.public_key());
281+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
282282
let missed_block = given_a_block(4, &secret_key);
283283
let latest_block = given_a_block(5, &secret_key);
284284
let fuel_adapter = given_fetcher(vec![latest_block, missed_block]);
@@ -303,7 +303,7 @@ mod tests {
303303
async fn will_not_reattempt_committing_latest_block() {
304304
// given
305305
let secret_key = given_secret_key();
306-
let block_validator = BlockValidator::new(secret_key.public_key());
306+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
307307
let latest_block = given_a_block(6, &secret_key);
308308
let fuel_adapter = given_fetcher(vec![latest_block]);
309309

@@ -327,7 +327,7 @@ mod tests {
327327
async fn propagates_block_if_epoch_reached() {
328328
// given
329329
let secret_key = given_secret_key();
330-
let block_validator = BlockValidator::new(secret_key.public_key());
330+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
331331
let block = given_a_block(4, &secret_key);
332332
let fuel_adapter = given_fetcher(vec![block.clone()]);
333333

@@ -348,7 +348,7 @@ mod tests {
348348
async fn updates_block_metric_regardless_if_block_is_published() {
349349
// given
350350
let secret_key = given_secret_key();
351-
let block_validator = BlockValidator::new(secret_key.public_key());
351+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
352352
let block = given_a_block(5, &secret_key);
353353
let fuel_adapter = given_fetcher(vec![block]);
354354

packages/services/src/state_importer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ mod tests {
198198
let secret_key = given_secret_key();
199199
let block = given_a_block(1, &secret_key);
200200
let fuel_mock = given_fetcher(block);
201-
let block_validator = BlockValidator::new(secret_key.public_key());
201+
let block_validator = BlockValidator::new(*secret_key.public_key().hash());
202202

203203
let process = PostgresProcess::shared().await.unwrap();
204204
let db = process.create_random_db().await?;

packages/validator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mockall = { workspace = true, optional = true }
1616
rand = { workspace = true, optional = true }
1717
serde = { workspace = true, features = ["derive"] }
1818
thiserror = { workspace = true }
19+
hex = { workspace = true }
1920

2021
[dev-dependencies]
2122
fuel-crypto = { workspace = true, features = ["random"] }

packages/validator/src/validator.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ use fuel_core_client::client::types::{
33
Block as FuelBlock, Consensus as FuelConsensus, Header as FuelHeader,
44
PoAConsensus as FuelPoAConsensus,
55
},
6-
primitives::{BlockId as FuelBlockId, Bytes32 as FuelBytes32, PublicKey as FuelPublicKey},
6+
primitives::{BlockId as FuelBlockId, Bytes32 as FuelBytes32},
77
};
88
use fuel_crypto::{Hasher, Message};
99

1010
use crate::{block::ValidatedFuelBlock, Error, Result, Validator};
1111

1212
#[derive(Debug)]
1313
pub struct BlockValidator {
14-
producer_pub_key: FuelPublicKey,
14+
producer_addr: [u8; 32],
1515
}
1616

1717
impl Validator for BlockValidator {
@@ -21,12 +21,12 @@ impl Validator for BlockValidator {
2121
}
2222

2323
impl BlockValidator {
24-
pub fn new(producer_pub_key: FuelPublicKey) -> Self {
25-
Self { producer_pub_key }
24+
pub fn new(producer_addr: [u8; 32]) -> Self {
25+
Self { producer_addr }
2626
}
2727

2828
fn _validate(&self, fuel_block: &FuelBlock) -> Result<ValidatedFuelBlock> {
29-
self.validate_public_key(fuel_block)?;
29+
self.validate_producer_addr(fuel_block)?;
3030
Self::validate_block_id(fuel_block)?;
3131
self.validate_block_signature(fuel_block)?;
3232

@@ -36,18 +36,18 @@ impl BlockValidator {
3636
})
3737
}
3838

39-
fn validate_public_key(&self, fuel_block: &FuelBlock) -> Result<()> {
40-
let Some(producer_pub_key) = fuel_block.block_producer() else {
39+
fn validate_producer_addr(&self, fuel_block: &FuelBlock) -> Result<()> {
40+
let Some(producer_addr) = fuel_block.block_producer().map(|key| key.hash()) else {
4141
return Err(Error::BlockValidation(
4242
"producer public key not found in fuel block".to_string(),
4343
));
4444
};
4545

46-
if *producer_pub_key != self.producer_pub_key {
46+
if *producer_addr != self.producer_addr {
4747
return Err(Error::BlockValidation(format!(
48-
"producer public key `{producer_pub_key:x}` does not match \
49-
expected public key `{:x}`.",
50-
self.producer_pub_key
48+
"producer addr '{}' does not match expected addr '{}'.",
49+
hex::encode(producer_addr),
50+
hex::encode(self.producer_addr)
5151
)));
5252
}
5353

@@ -75,16 +75,23 @@ impl BlockValidator {
7575
));
7676
};
7777

78-
let block_id_message = Message::from_bytes(*fuel_block.id);
79-
80-
signature
81-
.verify(&self.producer_pub_key, &block_id_message)
82-
.map_err(|_| {
78+
let recovered_producer_addr = *signature
79+
.recover(&Message::from_bytes(*fuel_block.id))
80+
.map_err(|e| {
8381
Error::BlockValidation(format!(
84-
"signature validation failed for fuel block with id: `{:x}` and pub key: `{:x}`",
85-
fuel_block.id, &self.producer_pub_key
82+
"failed to recover public key from PoAConsensus signature: {e:?}",
8683
))
87-
})?;
84+
})?
85+
.hash();
86+
87+
if recovered_producer_addr != self.producer_addr {
88+
return Err(Error::BlockValidation(format!(
89+
"recovered producer addr `{}` does not match \
90+
expected addr`{}`.",
91+
hex::encode(recovered_producer_addr),
92+
hex::encode(self.producer_addr)
93+
)));
94+
}
8895

8996
Ok(())
9097
}
@@ -146,17 +153,17 @@ mod tests {
146153
#[should_panic(expected = "producer public key not found in fuel block")]
147154
fn validate_public_key_missing() {
148155
let fuel_block = given_a_block(None);
149-
let validator = BlockValidator::new(FuelPublicKey::default());
156+
let validator = BlockValidator::new([0; 32]);
150157

151158
validator.validate(&fuel_block).unwrap();
152159
}
153160

154161
#[test]
155-
#[should_panic(expected = "does not match expected public key")]
162+
#[should_panic(expected = "does not match expected addr")]
156163
fn validate_public_key_mistmach() {
157164
let secret_key = given_secret_key();
158165
let fuel_block = given_a_block(Some(secret_key));
159-
let validator = BlockValidator::new(FuelPublicKey::default());
166+
let validator = BlockValidator::new([0; 32]);
160167

161168
validator.validate(&fuel_block).unwrap();
162169
}
@@ -167,7 +174,7 @@ mod tests {
167174
let secret_key = given_secret_key();
168175
let mut fuel_block = given_a_block(Some(secret_key));
169176
fuel_block.header.height = 42; // Change a value to get a different block id
170-
let validator = BlockValidator::new(secret_key.public_key());
177+
let validator = BlockValidator::new(*secret_key.public_key().hash());
171178

172179
validator.validate(&fuel_block).unwrap();
173180
}
@@ -178,20 +185,29 @@ mod tests {
178185
let secret_key = given_secret_key();
179186
let mut fuel_block = given_a_block(Some(secret_key));
180187
fuel_block.consensus = FuelConsensus::Unknown;
181-
let validator = BlockValidator::new(secret_key.public_key());
188+
let validator = BlockValidator::new(*secret_key.public_key().hash());
182189

183190
validator.validate(&fuel_block).unwrap();
184191
}
185192

186193
#[test]
187-
#[should_panic(expected = "signature validation failed for fuel block with id:")]
194+
#[should_panic(
195+
expected = "recovered producer addr `286b769a36b01cebc43cd9820ba709b438b14566e16a287c36881194eacc45c6` does not match expected addr`f95112e76de29dca6ed315c5a5be7855e62dee55478077cf209554d5bfb7cd85`."
196+
)]
188197
fn validate_block_consensus_invalid_signature() {
189-
let secret_key = given_secret_key();
190-
let mut fuel_block = given_a_block(Some(secret_key));
198+
let correct_secret_key = given_secret_key();
199+
200+
let mut fuel_block = given_a_block(Some(correct_secret_key));
201+
let invalid_signature = {
202+
let different_secret_key = SecretKey::random(&mut StdRng::seed_from_u64(43));
203+
let id_message = Message::from_bytes(*fuel_block.id);
204+
Signature::sign(&different_secret_key, &id_message)
205+
};
206+
191207
fuel_block.consensus = FuelConsensus::PoAConsensus(FuelPoAConsensus {
192-
signature: Signature::default(),
208+
signature: invalid_signature,
193209
});
194-
let validator = BlockValidator::new(secret_key.public_key());
210+
let validator = BlockValidator::new(*correct_secret_key.public_key().hash());
195211

196212
validator.validate(&fuel_block).unwrap();
197213
}
@@ -200,7 +216,7 @@ mod tests {
200216
fn validate_fuel_block() {
201217
let secret_key = given_secret_key();
202218
let fuel_block = given_a_block(Some(secret_key));
203-
let validator = BlockValidator::new(secret_key.public_key());
219+
let validator = BlockValidator::new(*secret_key.public_key().hash());
204220

205221
validator.validate(&fuel_block).unwrap();
206222
}

0 commit comments

Comments
 (0)