fn validate_append_block(
        &self,
        block: &Block,
        block_contents: &BlockContents,
    ) -> Result<(), Error> {
        // Check that version is correct
        if block.version != BLOCK_VERSION {
            return Err(Error::InvalidBlock);
        }

        // A block must have outputs.
        if block_contents.outputs.is_empty() {
            // TODO: better error type.
            return Err(Error::InvalidBlock);
        }

        // TODO: enable this.
        // // Non-origin blocks must have key images.
        // if block.index == 0 && block_contents.key_images.is_empty() {
        //     return Err(Error::InvalidBlock);
        // }

        // Check if block is being appended at the correct place.
        let num_blocks = self.num_blocks()?;
        if num_blocks == 0 {
            // This must be an origin block.
            if block.index != 0 || block.parent_id != BlockID::default() {
                return Err(Error::InvalidBlock);
            }
        } else {
            // The block must have the correct index and parent.
            let last_block = self.get_block(num_blocks - 1)?;
            if block.index != num_blocks || block.parent_id != last_block.id {
                return Err(Error::InvalidBlock);
            }
        }

        // Check that the block contents match the hash.
        if block.contents_hash != block_contents.hash() {
            return Err(Error::InvalidBlockContents);
        }

        // Check that none of the key images were previously spent.
        for key_image in &block_contents.key_images {
            if self.contains_key_image(key_image)? {
                return Err(Error::KeyImageAlreadySpent);
            }
        }

        // Validate block id.
        if !block.is_block_id_valid() {
            return Err(Error::InvalidBlockID);
        }

        // All good
        Ok(())
    }
/// Checks if a block can be appended to the db.
Checks if a block can be appended to the db.
Checks if a block can be appended to the db .
fn validate_append_block(
        &self,
        block: &Block,
        block_contents: &BlockContents,
    ) -> Result<(), Error> {
        // Check that version is correct
        if block.version != BLOCK_VERSION {
            return Err(Error::InvalidBlock);
        }
        // A block must have outputs.
        if block_contents.outputs.is_empty() {
            // TODO: better error type.
            return Err(Error::InvalidBlock);
        }
        // TODO: enable this.
        // // Non-origin blocks must have key images.
        // if block.index == 0 && block_contents.key_images.is_empty() {
        //     return Err(Error::InvalidBlock);
        // }
        // Check if block is being appended at the correct place.
        let num_blocks = self.num_blocks()?;
        if num_blocks == 0 {
            // This must be an origin block.
            if block.index != 0 || block.parent_id != BlockID::default() {
                return Err(Error::InvalidBlock);
            }
        } else {
            // The block must have the correct index and parent.
            let last_block = self.get_block(num_blocks - 1)?;
            if block.index != num_blocks || block.parent_id != last_block.id {
                return Err(Error::InvalidBlock);
            }
        }
        // Check that the block contents match the hash.
        if block.contents_hash != block_contents.hash() {
            return Err(Error::InvalidBlockContents);
        }
        // Check that none of the key images were previously spent.
        for key_image in &block_contents.key_images {
            if self.contains_key_image(key_image)? {
                return Err(Error::KeyImageAlreadySpent);
            }
        }
        // Validate block id.
        if !block.is_block_id_valid() {
            return Err(Error::InvalidBlockID);
        }
        // All good
        Ok(())
    }
fn validate_append_block ( & self , block : & Block , block_contents : & BlockContents , ) -> Result < ( ) , Error > { // Check that version is correct if block . version != BLOCK_VERSION { return Err ( Error :: InvalidBlock ) ; } // A block must have outputs. if block_contents . outputs . is_empty ( ) { // TODO: better error type. return Err ( Error :: InvalidBlock ) ; } // TODO: enable this. // // Non-origin blocks must have key images. // if block.index == 0 && block_contents.key_images.is_empty() { //     return Err(Error::InvalidBlock); // } // Check if block is being appended at the correct place. let num_blocks = self . num_blocks ( ) ? ; if num_blocks == 0 { // This must be an origin block. if block . index != 0 || block . parent_id != BlockID :: default ( ) { return Err ( Error :: InvalidBlock ) ; } } else { // The block must have the correct index and parent. let last_block = self . get_block ( num_blocks - 1 ) ? ; if block . index != num_blocks || block . parent_id != last_block . id { return Err ( Error :: InvalidBlock ) ; } } // Check that the block contents match the hash. if block . contents_hash != block_contents . hash ( ) { return Err ( Error :: InvalidBlockContents ) ; } // Check that none of the key images were previously spent. for key_image in & block_contents . key_images { if self . contains_key_image ( key_image ) ? { return Err ( Error :: KeyImageAlreadySpent ) ; } } // Validate block id. if ! block . is_block_id_valid ( ) { return Err ( Error :: InvalidBlockID ) ; } // All good Ok ( ( ) ) }
Checks if a block can be appended to the db.
Checks if a block can be appended to the db .
&self
block
&Block
block_contents
&BlockContents
&self
block
&Block
block_contents
&BlockContents