-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.rs
104 lines (91 loc) · 3.22 KB
/
application.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use log::trace;
use thiserror::Error;
use crate::api::types::{ApiBlock, ApiEphemeraMessage};
#[derive(Debug, Clone, PartialEq)]
pub enum RemoveMessages {
/// Remove all messages from the mempool
All,
/// Remove only inclued messages from the mempool
Selected(Vec<ApiEphemeraMessage>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum CheckBlockResult {
/// Accept the block
Accept,
/// Reject the block with a reason.
Reject,
/// Reject the block and remove messages from the mempool
RejectAndRemoveMessages(RemoveMessages),
}
#[derive(Debug, Clone, PartialEq)]
pub struct CheckBlockResponse {
pub accept: bool,
pub reason: Option<String>,
}
#[derive(Error, Debug)]
pub enum Error {
//Just a placeholder for now
#[error("ApplicationError: {0}")]
Application(#[from] anyhow::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
/// Cosmos style ABCI application hook
///
/// These functions should be relatively fast, as they are called synchronously by Ephemera main loop.
pub trait Application {
/// It's called when receiving a new message from network before adding it to the mempool.
/// It's up to the application to decide whether the message is valid or not.
/// Basic check could be for example signature verification.
///
/// # Arguments
/// * `message` - message to be checked
///
/// # Returns
/// * `true` - if the message is valid
/// * `false` - if the message is invalid
///
/// # Errors
/// * `Error::General` - if there was an error during validation
fn check_tx(&self, message: ApiEphemeraMessage) -> Result<bool>;
/// Ephemera produces new blocks with configured interval.
/// Application can decide whether to accept the block or not.
/// For example, if the block doesn't contain any transactions, it can be rejected.
///
/// # Arguments
/// * `block` - block to be checked
///
/// # Returns
/// * `CheckBlockResult::Accept` - if the block is valid
/// * `CheckBlockResult::Reject` - if the block is invalid
/// * `CheckBlockResult::RejectAndRemoveMessages` - if the block is invalid and some messages should be removed from the mempool
///
/// # Errors
/// * `Error::General` - if there was an error during validation
fn check_block(&self, block: &ApiBlock) -> Result<CheckBlockResult>;
/// Deliver Block is called after block is confirmed by Ephemera and persisted to the storage.
///
/// # Arguments
/// * `block` - block to be delivered
///
/// # Errors
/// * `Error::General` - if there was an error during validation
fn deliver_block(&self, block: ApiBlock) -> Result<()>;
}
/// Dummy application which doesn't do any validation.
/// Might be useful for testing.
#[derive(Default)]
pub struct Dummy;
impl Application for Dummy {
fn check_tx(&self, tx: ApiEphemeraMessage) -> Result<bool> {
trace!("check_tx: {tx:?}");
Ok(true)
}
fn check_block(&self, block: &ApiBlock) -> Result<CheckBlockResult> {
trace!("accept_block: {block:?}");
Ok(CheckBlockResult::Accept)
}
fn deliver_block(&self, block: ApiBlock) -> Result<()> {
trace!("deliver_block: {block:?}");
Ok(())
}
}