• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari / 17834551818

18 Sep 2025 04:04PM UTC coverage: 60.651% (-0.3%) from 60.975%
17834551818

push

github

SWvheerden
chore: new release v5.1.0-pre.1

74207 of 122350 relevant lines covered (60.65%)

293003.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/base_layer/node_components/src/blocks/new_block_template.rs
1
// Copyright 2019. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22

23
use std::fmt::{Display, Formatter};
24

25
use borsh::{BorshDeserialize, BorshSerialize};
26
use serde::{Deserialize, Serialize};
27
use tari_transaction_components::{
28
    aggregated_body::AggregateBody,
29
    tari_proof_of_work::Difficulty,
30
    transaction_components::TransactionError,
31
    MicroMinotari,
32
};
33

34
use crate::blocks::{new_blockheader_template::NewBlockHeaderTemplate, Block};
35
/// The new block template is used constructing a new partial block, allowing a miner to added the coinbase utxo and as
36
/// a final step the Base node to add the MMR roots to the header.
37
#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
×
38
pub struct NewBlockTemplate {
39
    /// The NewBlockHeaderTemplate is used for the construction of a new mineable block. It contains all the metadata
40
    /// for the block that the Base Node is able to complete on behalf of a Miner.
41
    pub header: NewBlockHeaderTemplate,
42
    /// This flag indicates if the inputs, outputs and kernels have been sorted internally, that is, the sort() method
43
    /// has been called. This may be false even if all components are sorted.
44
    pub body: AggregateBody,
45
    /// The difficulty is defined as the maximum target divided by the block hash.
46
    pub target_difficulty: Difficulty,
47
    /// The reward is the sum of the coinbase utxo and the total fees.
48
    pub reward: MicroMinotari,
49
    /// The total fees is the sum of all the fees in the block.
50
    pub total_fees: MicroMinotari,
51
    /// Sometimes the mempool has not synced to the latest tip, this flag indicates if the mempool is out of sync.
52
    /// In most cases the next call to get_new_block_template will return a block with the mempool in sync.
53
    pub is_mempool_in_sync: bool,
54
}
55

56
impl NewBlockTemplate {
57
    pub fn from_block(
×
58
        block: Block,
×
59
        target_difficulty: Difficulty,
×
60
        reward: MicroMinotari,
×
61
        is_mempool_in_sync: bool,
×
62
    ) -> Result<Self, TransactionError> {
×
63
        let Block { header, body } = block;
×
64
        let total_fees = body.get_total_fee()?;
×
65
        Ok(Self {
×
66
            header: NewBlockHeaderTemplate::from_header(header),
×
67
            body,
×
68
            target_difficulty,
×
69
            reward,
×
70
            total_fees,
×
71
            is_mempool_in_sync,
×
72
        })
×
73
    }
×
74

75
    pub fn empty() -> Self {
×
76
        Self {
×
77
            header: NewBlockHeaderTemplate::empty(),
×
78
            body: AggregateBody::empty(),
×
79
            target_difficulty: Difficulty::default(),
×
80
            reward: MicroMinotari::default(),
×
81
            total_fees: MicroMinotari::default(),
×
82
            is_mempool_in_sync: false,
×
83
        }
×
84
    }
×
85
}
86

87
impl Display for NewBlockTemplate {
88
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
×
89
        writeln!(f, "----------------- Block template-----------------")?;
×
90
        writeln!(f, "--- Header ---")?;
×
91
        writeln!(f, "{}", self.header)?;
×
92
        writeln!(f, "---  Body ---")?;
×
93
        writeln!(f, "{}", self.body)?;
×
94
        writeln!(
×
95
            f,
×
96
            "Target difficulty: {}\nReward: {}\nTotal fees: {}",
×
97
            self.target_difficulty, self.reward, self.total_fees
×
98
        )?;
×
99
        Ok(())
×
100
    }
×
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc