hotmint_consensus/application.rs
1use ruc::*;
2
3use hotmint_types::{Block, Height, ViewNumber};
4
5/// ABCI-like application interface for the consensus engine.
6///
7/// The lifecycle for each block:
8/// 1. `begin_block` — called when a new block is being proposed
9/// 2. `deliver_tx` — called for each transaction in the payload
10/// 3. `end_block` — called after all transactions are delivered
11/// 4. `on_commit` — called when the block is finalized in the committed chain
12///
13/// For block validation:
14/// - `validate_block` — full block validation before voting
15/// - `validate_tx` — individual transaction validation for mempool
16pub trait Application: Send + Sync {
17 /// Create a payload for a new block proposal.
18 /// Typically pulls transactions from the mempool.
19 fn create_payload(&self) -> Vec<u8> {
20 vec![]
21 }
22
23 /// Validate a proposed block before voting.
24 fn validate_block(&self, _block: &Block) -> bool {
25 true
26 }
27
28 /// Validate a single transaction for mempool admission.
29 fn validate_tx(&self, _tx: &[u8]) -> bool {
30 true
31 }
32
33 /// Called at the beginning of block execution.
34 fn begin_block(&self, _height: Height, _view: ViewNumber) -> Result<()> {
35 Ok(())
36 }
37
38 /// Called for each transaction in the block payload.
39 fn deliver_tx(&self, _tx: &[u8]) -> Result<()> {
40 Ok(())
41 }
42
43 /// Called after all transactions in the block are delivered.
44 fn end_block(&self, _height: Height) -> Result<()> {
45 Ok(())
46 }
47
48 /// Called when a block is committed to the chain.
49 fn on_commit(&self, block: &Block) -> Result<()>;
50
51 /// Query application state (returns opaque bytes).
52 fn query(&self, _path: &str, _data: &[u8]) -> Result<Vec<u8>> {
53 Ok(vec![])
54 }
55}
56
57/// No-op application stub
58pub struct NoopApplication;
59
60impl Application for NoopApplication {
61 fn on_commit(&self, _block: &Block) -> Result<()> {
62 Ok(())
63 }
64}