hotmint_consensus/application.rs
1use ruc::*;
2
3use hotmint_types::Block;
4use hotmint_types::context::BlockContext;
5use hotmint_types::evidence::EquivocationProof;
6use hotmint_types::validator_update::EndBlockResponse;
7
8/// ABCI-like application interface for the consensus engine.
9///
10/// The lifecycle for each committed block:
11/// 1. `begin_block` — called at the start of block execution
12/// 2. `deliver_tx` — called for each transaction in the payload
13/// 3. `end_block` — called after all transactions; may return validator updates
14/// 4. `on_commit` — called when the block is finalized
15///
16/// For block validation (before voting, not yet committed):
17/// - `validate_block` — full block validation
18/// - `validate_tx` — individual transaction validation for mempool
19///
20/// For evidence:
21/// - `on_evidence` — called when equivocation (double-voting) is detected
22pub trait Application: Send + Sync {
23 /// Create a payload for a new block proposal.
24 /// Typically pulls transactions from the mempool.
25 fn create_payload(&self, _ctx: &BlockContext) -> Vec<u8> {
26 vec![]
27 }
28
29 /// Validate a proposed block before voting.
30 fn validate_block(&self, _block: &Block, _ctx: &BlockContext) -> bool {
31 true
32 }
33
34 /// Validate a single transaction for mempool admission.
35 fn validate_tx(&self, _tx: &[u8]) -> bool {
36 true
37 }
38
39 /// Called at the beginning of block execution (during commit).
40 fn begin_block(&self, _ctx: &BlockContext) -> Result<()> {
41 Ok(())
42 }
43
44 /// Called for each transaction in the block payload (during commit).
45 fn deliver_tx(&self, _tx: &[u8]) -> Result<()> {
46 Ok(())
47 }
48
49 /// Called after all transactions in the block are delivered (during commit).
50 /// Return `EndBlockResponse` with `validator_updates` to trigger an epoch transition.
51 fn end_block(&self, _ctx: &BlockContext) -> Result<EndBlockResponse> {
52 Ok(EndBlockResponse::default())
53 }
54
55 /// Called when a block is committed to the chain.
56 fn on_commit(&self, block: &Block, ctx: &BlockContext) -> Result<()>;
57
58 /// Called when equivocation (double-voting) is detected.
59 /// The application can use this to implement slashing.
60 fn on_evidence(&self, _proof: &EquivocationProof) -> Result<()> {
61 Ok(())
62 }
63
64 /// Query application state (returns opaque bytes).
65 fn query(&self, _path: &str, _data: &[u8]) -> Result<Vec<u8>> {
66 Ok(vec![])
67 }
68}
69
70/// No-op application stub for testing
71pub struct NoopApplication;
72
73impl Application for NoopApplication {
74 fn on_commit(&self, _block: &Block, _ctx: &BlockContext) -> Result<()> {
75 Ok(())
76 }
77}