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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::{
data_access_layer::BalanceIdentifier, system::runtime_native::Config as NativeRuntimeConfig,
tracking_copy::TrackingCopyError,
};
use casper_types::{
execution::Effects, Digest, EraId, InitiatorAddr, ProtocolVersion, PublicKey, TransactionHash,
Transfer, U512,
};
/// Handle fee mode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandleFeeMode {
/// Pay the fee.
Pay {
/// Initiator.
initiator_addr: Box<InitiatorAddr>,
/// Source.
source: Box<BalanceIdentifier>,
/// Target.
target: Box<BalanceIdentifier>,
/// Amount.
amount: U512,
},
/// Burn the fee.
Burn {
/// Source.
source: BalanceIdentifier,
/// Amount.
amount: Option<U512>,
},
/// Validator credit (used in no fee mode).
Credit {
/// Validator.
validator: Box<PublicKey>,
/// Amount.
amount: U512,
/// EraId.
era_id: EraId,
},
}
impl HandleFeeMode {
/// Ctor for Pay mode.
pub fn pay(
initiator_addr: Box<InitiatorAddr>,
source: BalanceIdentifier,
target: BalanceIdentifier,
amount: U512,
) -> Self {
HandleFeeMode::Pay {
initiator_addr,
source: Box::new(source),
target: Box::new(target),
amount,
}
}
/// What source should be used to burn from, and how much?
/// If amount is None or greater than the available balance, the full available balance
/// will be burned. If amount is less than available balance, only that much will be
/// burned leaving a remaining balance.
pub fn burn(source: BalanceIdentifier, amount: Option<U512>) -> Self {
HandleFeeMode::Burn { source, amount }
}
/// Applies a staking credit to the imputed proposer for the imputed amount at the end
/// of the current era when the auction process is executed.
pub fn credit(validator: Box<PublicKey>, amount: U512, era_id: EraId) -> Self {
HandleFeeMode::Credit {
validator,
amount,
era_id,
}
}
}
/// Handle fee request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandleFeeRequest {
/// The runtime config.
pub(crate) config: NativeRuntimeConfig,
/// State root hash.
pub(crate) state_hash: Digest,
/// The protocol version.
pub(crate) protocol_version: ProtocolVersion,
/// Transaction hash.
pub(crate) transaction_hash: TransactionHash,
/// Handle fee mode.
pub(crate) handle_fee_mode: HandleFeeMode,
}
impl HandleFeeRequest {
/// Creates new request instance with runtime args.
#[allow(clippy::too_many_arguments)]
pub fn new(
config: NativeRuntimeConfig,
state_hash: Digest,
protocol_version: ProtocolVersion,
transaction_hash: TransactionHash,
handle_fee_mode: HandleFeeMode,
) -> Self {
Self {
config,
state_hash,
protocol_version,
transaction_hash,
handle_fee_mode,
}
}
/// Returns config.
pub fn config(&self) -> &NativeRuntimeConfig {
&self.config
}
/// Returns state hash.
pub fn state_hash(&self) -> Digest {
self.state_hash
}
/// Returns handle protocol version.
pub fn protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
/// Returns handle transaction hash.
pub fn transaction_hash(&self) -> TransactionHash {
self.transaction_hash
}
/// Returns handle fee mode.
pub fn handle_fee_mode(&self) -> &HandleFeeMode {
&self.handle_fee_mode
}
}
/// Result enum that represents all possible outcomes of a handle request.
#[derive(Debug)]
pub enum HandleFeeResult {
/// Invalid state root hash.
RootNotFound,
/// Handle request succeeded.
Success {
/// Transfers.
transfers: Vec<Transfer>,
/// Handle fee effects.
effects: Effects,
},
/// Handle request failed.
Failure(TrackingCopyError),
}
impl HandleFeeResult {
/// The effects, if any.
pub fn effects(&self) -> Effects {
match self {
HandleFeeResult::RootNotFound | HandleFeeResult::Failure(_) => Effects::new(),
HandleFeeResult::Success { effects, .. } => effects.clone(),
}
}
}