Skip to content

Commit 64786e6

Browse files
Remove hack for prev_block_hash in Celestia genesis (#553)
1 parent 5dbe355 commit 64786e6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

adapters/celestia/src/celestia.rs

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
1212
use sov_rollup_interface::da::{BlockHeaderTrait as BlockHeader, CountedBufReader};
1313
use sov_rollup_interface::AddressTrait;
1414
pub use tendermint::block::Header as TendermintHeader;
15+
use tendermint::block::Height;
1516
use tendermint::crypto::default::Sha256;
1617
use tendermint::merkle::simple_hash_from_byte_vectors;
1718
use tendermint::Hash;
@@ -21,6 +22,8 @@ use tracing::debug;
2122

2223
const NAMESPACED_HASH_LEN: usize = 48;
2324

25+
pub const GENESIS_PLACEHOLDER_HASH: &[u8; 32] = &[255; 32];
26+
2427
use crate::pfb::{BlobTx, MsgPayForBlobs, Tx};
2528
use crate::shares::{read_varint, BlobIterator, BlobRefIterator, NamespaceGroup};
2629
use crate::utils::BoxError;
@@ -270,6 +273,20 @@ impl BlockHeader for CelestiaHeader {
270273
return hash.clone();
271274
}
272275

276+
// We special case the block following genesis, since genesis has a `None` hash, which
277+
// we don't want to deal with. In this case, we return a specail placeholder for the
278+
// block "hash"
279+
if Height::decode_vec(&self.header.height)
280+
.expect("header must be validly encoded")
281+
.value()
282+
== 1
283+
{
284+
let prev_hash = TmHash(tendermint::Hash::Sha256(*GENESIS_PLACEHOLDER_HASH));
285+
*cached_hash = Some(prev_hash.clone());
286+
return prev_hash;
287+
}
288+
289+
// In all other cases, we simply return the previous block hash parsed from the header
273290
let hash =
274291
<tendermint::block::Id as Protobuf<celestia_tm_version::types::BlockId>>::decode(
275292
self.header.last_block_id.as_ref(),
@@ -434,5 +451,12 @@ mod tests {
434451
let compact_header: CompactHeader = original_header.header.into();
435452

436453
assert_eq!(tm_header.hash(), compact_header.hash());
454+
assert_eq!(
455+
hex::decode("32381A0B7262F15F081ACEF769EE59E6BB4C42C1013A3EEE23967FBF32B86AE6")
456+
.unwrap(),
457+
compact_header.hash().as_bytes()
458+
);
459+
460+
assert_eq!(tm_header.hash(), compact_header.hash(),);
437461
}
438462
}

adapters/celestia/src/verifier/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl TmHash {
6868
tendermint::Hash::Sha256(ref h) => h,
6969
// Hack: when the hash is None, we return a hash of all 255s as a placeholder.
7070
// TODO: add special casing for the genesis block at a higher level
71-
tendermint::Hash::None => &[255u8; 32],
71+
tendermint::Hash::None => unreachable!("Only the genesis block has a None hash, and we use a placholder in that corner case")
7272
}
7373
}
7474
}

0 commit comments

Comments
 (0)