to_id.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 impl<N: Network> BatchHeader<N> { 19 /// Returns the batch ID. 20 pub fn to_id(&self) -> Result<Field<N>> { 21 Self::compute_batch_id( 22 self.author, 23 self.round, 24 self.timestamp, 25 self.committee_id, 26 &self.transmission_ids, 27 &self.previous_certificate_ids, 28 ) 29 } 30 } 31 32 impl<N: Network> BatchHeader<N> { 33 /// Returns the batch ID. 34 pub fn compute_batch_id( 35 author: Address<N>, 36 round: u64, 37 timestamp: i64, 38 committee_id: Field<N>, 39 transmission_ids: &IndexSet<TransmissionID<N>>, 40 previous_certificate_ids: &IndexSet<Field<N>>, 41 ) -> Result<Field<N>> { 42 let mut preimage = Vec::new(); 43 // Insert the author. 44 author.write_le(&mut preimage)?; 45 // Insert the round number. 46 round.write_le(&mut preimage)?; 47 // Insert the timestamp. 48 timestamp.write_le(&mut preimage)?; 49 // Insert the committee ID. 50 committee_id.write_le(&mut preimage)?; 51 // Insert the number of transmissions. 52 u32::try_from(transmission_ids.len())?.write_le(&mut preimage)?; 53 // Insert the transmission IDs. 54 for transmission_id in transmission_ids { 55 transmission_id.write_le(&mut preimage)?; 56 } 57 // Insert the number of previous certificate IDs. 58 u32::try_from(previous_certificate_ids.len())?.write_le(&mut preimage)?; 59 // Insert the previous certificate IDs. 60 for certificate_id in previous_certificate_ids { 61 // Insert the certificate ID. 62 certificate_id.write_le(&mut preimage)?; 63 } 64 // Hash the preimage. 65 N::hash_bhp1024(&preimage.to_bits_le()) 66 } 67 }