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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::{
data_access_layer::BalanceIdentifier, system::runtime_native::Config as NativeRuntimeConfig,
tracking_copy::TrackingCopyError,
};
use casper_types::{
execution::Effects, Digest, InitiatorAddr, Phase, ProtocolVersion, TransactionHash, Transfer,
U512,
};
use num_rational::Ratio;
/// Selects refund operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandleRefundMode {
/// This variant will cause the refund amount to be calculated and then burned.
Burn {
/// Refund limit.
limit: U512,
/// Refund cost.
cost: U512,
/// Refund consumed.
consumed: U512,
/// Refund gas price.
gas_price: u8,
/// Refund source.
source: Box<BalanceIdentifier>,
/// Refund ratio.
ratio: Ratio<u64>,
},
/// This variant will cause the refund amount to be calculated and the refund to be executed.
Refund {
/// Refund initiator.
initiator_addr: Box<InitiatorAddr>,
/// Refund limit.
limit: U512,
/// Refund cost.
cost: U512,
/// Refund consumed.
consumed: U512,
/// Refund gas price.
gas_price: u8,
/// Refund ratio.
ratio: Ratio<u64>,
/// Refund source.
source: Box<BalanceIdentifier>,
/// Target for refund.
target: Box<BalanceIdentifier>,
},
/// This variant handles the edge case of custom payment plus no fee plus no refund.
/// This ultimately turns into a hold on the initiator, but it takes extra steps to get there
/// because the payment has already been fully processed up front and must first be unwound.
RefundNoFeeCustomPayment {
/// Refund initiator.
initiator_addr: Box<InitiatorAddr>,
/// Refund limit.
limit: U512,
/// Refund cost.
cost: U512,
/// Refund gas price.
gas_price: u8,
},
/// This variant only calculates and returns the refund amount. It does not
/// execute a refund.
CalculateAmount {
/// Refund limit.
limit: U512,
/// Refund cost.
cost: U512,
/// Refund consumed.
consumed: U512,
/// Refund gas price.
gas_price: u8,
/// Refund ratio.
ratio: Ratio<u64>,
/// Refund source.
source: Box<BalanceIdentifier>,
},
/// This variant will cause the refund purse tracked by handle_payment to be set.
SetRefundPurse {
/// Target for refund, which will receive any refunded token while set.
target: Box<BalanceIdentifier>,
},
/// This variant will cause the refund purse tracked by handle_payment to be cleared.
ClearRefundPurse,
}
impl HandleRefundMode {
/// Returns the appropriate phase for the mode.
pub fn phase(&self) -> Phase {
match self {
HandleRefundMode::Burn { .. }
| HandleRefundMode::Refund { .. }
| HandleRefundMode::RefundNoFeeCustomPayment { .. }
| HandleRefundMode::CalculateAmount { .. } => Phase::FinalizePayment,
HandleRefundMode::ClearRefundPurse | HandleRefundMode::SetRefundPurse { .. } => {
Phase::Payment
}
}
}
}
/// Handle refund request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandleRefundRequest {
/// 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,
/// Refund handling.
pub(crate) refund_mode: HandleRefundMode,
}
impl HandleRefundRequest {
/// Creates a new instance.
pub fn new(
config: NativeRuntimeConfig,
state_hash: Digest,
protocol_version: ProtocolVersion,
transaction_hash: TransactionHash,
refund_mode: HandleRefundMode,
) -> Self {
HandleRefundRequest {
config,
state_hash,
protocol_version,
transaction_hash,
refund_mode,
}
}
/// Returns a reference to the config.
pub fn config(&self) -> &NativeRuntimeConfig {
&self.config
}
/// Returns the state hash.
pub fn state_hash(&self) -> Digest {
self.state_hash
}
/// Returns the protocol version.
pub fn protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
/// Returns the transaction hash.
pub fn transaction_hash(&self) -> TransactionHash {
self.transaction_hash
}
/// Returns the refund mode.
pub fn refund_mode(&self) -> &HandleRefundMode {
&self.refund_mode
}
}
/// Handle refund result.
#[derive(Debug)]
pub enum HandleRefundResult {
/// Invalid state root hash.
RootNotFound,
/// Handle refund request succeeded.
Success {
/// Transfers.
transfers: Vec<Transfer>,
/// The effects.
effects: Effects,
/// The amount, if any.
amount: Option<U512>,
},
/// Invalid phase selected (programmer error).
InvalidPhase,
/// Handle refund request failed.
Failure(TrackingCopyError),
}
impl HandleRefundResult {
/// The effects, if any.
pub fn effects(&self) -> Effects {
match self {
HandleRefundResult::RootNotFound
| HandleRefundResult::InvalidPhase
| HandleRefundResult::Failure(_) => Effects::new(),
HandleRefundResult::Success { effects, .. } => effects.clone(),
}
}
/// The refund amount.
pub fn refund_amount(&self) -> U512 {
match self {
HandleRefundResult::RootNotFound
| HandleRefundResult::InvalidPhase
| HandleRefundResult::Failure(_) => U512::zero(),
HandleRefundResult::Success {
amount: refund_amount,
..
} => refund_amount.unwrap_or(U512::zero()),
}
}
/// The error message, if any.
pub fn error_message(&self) -> Option<String> {
match self {
HandleRefundResult::RootNotFound => Some("root not found".to_string()),
HandleRefundResult::InvalidPhase => Some("invalid phase selected".to_string()),
HandleRefundResult::Failure(tce) => Some(format!("{}", tce)),
HandleRefundResult::Success { .. } => None,
}
}
}