Reduce amount of clone() going on when applying data to MMRs (#2046)
* less cloning when pushing to PMMR * pass a ref to data when applying to the MMR and appending to the backend * cleanup * rustfmt * fixup tests
This commit is contained in:
@@ -162,7 +162,7 @@ impl Default for BlockHeader {
|
||||
impl PMMRable for BlockHeader {
|
||||
type E = Hash;
|
||||
|
||||
fn as_elmt(self) -> Self::E {
|
||||
fn as_elmt(&self) -> Self::E {
|
||||
self.hash()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ pub trait Backend<T: PMMRable> {
|
||||
/// associated data element to flatfile storage (for leaf nodes only). The
|
||||
/// position of the first element of the Vec in the MMR is provided to
|
||||
/// help the implementation.
|
||||
fn append(&mut self, data: T, hashes: Vec<Hash>) -> Result<(), String>;
|
||||
fn append(&mut self, data: &T, hashes: Vec<Hash>) -> Result<(), String>;
|
||||
|
||||
/// Rewind the backend state to a previous position, as if all append
|
||||
/// operations after that had been canceled. Expects a position in the PMMR
|
||||
|
||||
@@ -171,7 +171,7 @@ where
|
||||
|
||||
/// Push a new element into the MMR. Computes new related peaks at
|
||||
/// the same time if applicable.
|
||||
pub fn push(&mut self, elmt: T) -> Result<u64, String> {
|
||||
pub fn push(&mut self, elmt: &T) -> Result<u64, String> {
|
||||
let elmt_pos = self.last_pos + 1;
|
||||
let mut current_hash = elmt.hash_with_index(elmt_pos - 1);
|
||||
|
||||
|
||||
@@ -192,8 +192,8 @@ impl Readable for TxKernel {
|
||||
impl PMMRable for TxKernel {
|
||||
type E = TxKernelEntry;
|
||||
|
||||
fn as_elmt(self) -> Self::E {
|
||||
self.into()
|
||||
fn as_elmt(&self) -> TxKernelEntry {
|
||||
TxKernelEntry::from_kernel(self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,6 +260,9 @@ impl TxKernel {
|
||||
}
|
||||
|
||||
/// Wrapper around a tx kernel used when maintaining them in the MMR.
|
||||
/// These will be useful once we implement relative lockheights via relative kernels
|
||||
/// as a kernel may have an optional rel_kernel but we will not want to store these
|
||||
/// directly in the kernel MMR.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct TxKernelEntry {
|
||||
/// The underlying tx kernel.
|
||||
@@ -305,6 +308,13 @@ impl TxKernelEntry {
|
||||
pub fn verify(&self) -> Result<(), Error> {
|
||||
self.kernel.verify()
|
||||
}
|
||||
|
||||
/// Build a new tx kernel entry from a kernel.
|
||||
pub fn from_kernel(kernel: &TxKernel) -> TxKernelEntry {
|
||||
TxKernelEntry {
|
||||
kernel: kernel.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TxKernel> for TxKernelEntry {
|
||||
@@ -1151,8 +1161,8 @@ impl Readable for Output {
|
||||
impl PMMRable for Output {
|
||||
type E = OutputIdentifier;
|
||||
|
||||
fn as_elmt(self) -> Self::E {
|
||||
self.into()
|
||||
fn as_elmt(&self) -> OutputIdentifier {
|
||||
OutputIdentifier::from_output(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -515,8 +515,8 @@ impl FixedLength for RangeProof {
|
||||
impl PMMRable for RangeProof {
|
||||
type E = Self;
|
||||
|
||||
fn as_elmt(self) -> Self::E {
|
||||
self
|
||||
fn as_elmt(&self) -> Self::E {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -695,7 +695,7 @@ pub trait PMMRable: Writeable + Clone + Debug {
|
||||
type E: FixedLength + Readable + Writeable;
|
||||
|
||||
/// Convert the pmmrable into the element to be stored in the MMR data file.
|
||||
fn as_elmt(self) -> Self::E;
|
||||
fn as_elmt(&self) -> Self::E;
|
||||
}
|
||||
|
||||
/// Generic trait to ensure PMMR elements can be hashed with an index
|
||||
|
||||
Reference in New Issue
Block a user