Skip to main content

dusk_consensus/
operations.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use std::fmt;
8use std::time::Duration;
9
10use node_data::StepName;
11use node_data::bls::{PublicKey, PublicKeyBytes};
12use node_data::ledger::{Block, Fault, Header, Slash, SpentTransaction};
13
14use crate::errors::*;
15
16pub type StateRoot = [u8; 32];
17pub type EventBloom = [u8; 256];
18pub type Voter = (PublicKey, usize);
19
20#[derive(Default, Clone, Debug)]
21pub struct StateTransitionData {
22    pub round: u64,
23    pub generator: node_data::bls::PublicKey,
24    pub slashes: Vec<Slash>,
25    pub cert_voters: Vec<Voter>,
26    pub max_txs_bytes: usize,
27    pub prev_state_root: StateRoot,
28}
29
30#[derive(Debug, PartialEq)]
31pub struct StateTransitionResult {
32    pub state_root: StateRoot,
33    pub event_bloom: EventBloom,
34}
35
36impl Default for StateTransitionResult {
37    fn default() -> Self {
38        Self {
39            state_root: [0u8; 32],
40            event_bloom: [0u8; 256],
41        }
42    }
43}
44
45impl fmt::Display for StateTransitionResult {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(
48            f,
49            "State transition result: {{ state_root: {}, event_bloom: {} }}",
50            hex::encode(self.state_root),
51            hex::encode(self.event_bloom)
52        )
53    }
54}
55
56#[async_trait::async_trait]
57pub trait Operations: Send + Sync {
58    async fn validate_block_header(
59        &self,
60        candidate_header: &Header,
61        expected_generator: &PublicKeyBytes,
62    ) -> Result<Vec<Voter>, HeaderError>;
63
64    async fn validate_faults(
65        &self,
66        block_height: u64,
67        faults: &[Fault],
68    ) -> Result<(), OperationError>;
69
70    async fn validate_state_transition(
71        &self,
72        prev_state: StateRoot,
73        blk: &Block,
74        cert_voters: &[Voter],
75    ) -> Result<(), OperationError>;
76
77    async fn generate_state_transition(
78        &self,
79        transition_data: StateTransitionData,
80    ) -> Result<(Vec<SpentTransaction>, StateTransitionResult), OperationError>;
81
82    async fn add_step_elapsed_time(
83        &self,
84        round: u64,
85        step_name: StepName,
86        elapsed: Duration,
87    ) -> Result<(), OperationError>;
88
89    async fn get_block_gas_limit(&self) -> u64;
90}