@@ -5,6 +5,12 @@ use std::ops::Range;
5
5
/// binary searching.
6
6
pub ( crate ) struct SearchRange ( Range < u64 > ) ;
7
7
8
+ impl core:: fmt:: Display for SearchRange {
9
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
10
+ write ! ( f, "{}..{}" , self . 0 . start, self . 0 . end)
11
+ }
12
+ }
13
+
8
14
impl From < Range < u64 > > for SearchRange {
9
15
fn from ( value : Range < u64 > ) -> Self {
10
16
Self ( value)
@@ -25,7 +31,7 @@ impl SearchRange {
25
31
26
32
/// Calculate the midpoint of the search range.
27
33
pub ( crate ) const fn midpoint ( & self ) -> u64 {
28
- ( self . 0 . end - self . 0 . start ) / 2
34
+ ( self . max ( ) + self . min ( ) ) / 2
29
35
}
30
36
31
37
/// Get the start of the search range.
@@ -38,6 +44,7 @@ impl SearchRange {
38
44
self . 0 . start = min;
39
45
}
40
46
47
+ /// Raise the minimum of the search range, if the candidate is higher.
41
48
pub ( crate ) const fn maybe_raise_min ( & mut self , candidate : u64 ) {
42
49
if candidate > self . min ( ) {
43
50
self . set_min ( candidate) ;
@@ -110,11 +117,33 @@ pub enum EstimationResult {
110
117
} ,
111
118
}
112
119
113
- impl From < & ExecutionResult > for EstimationResult {
114
- fn from ( value : & ExecutionResult ) -> Self {
120
+ impl core:: fmt:: Display for EstimationResult {
121
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
122
+ match self {
123
+ Self :: Success { estimation, refund, gas_used, .. } => {
124
+ write ! (
125
+ f,
126
+ "Success {{ estimation: {}, refund: {}, gas_used: {}, .. }}" ,
127
+ estimation, refund, gas_used
128
+ )
129
+ }
130
+ Self :: Revert { gas_used, .. } => {
131
+ write ! ( f, "Revert {{ gas_used: {}, .. }}" , gas_used)
132
+ }
133
+ Self :: Halt { reason, gas_used } => {
134
+ write ! ( f, "Halt {{ reason: {:?}, gas_used: {} }}" , reason, gas_used)
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ impl EstimationResult {
141
+ /// Initialize the estimation result from an execution result and the gas
142
+ /// limit of the transaction that produced the estimation.
143
+ pub fn from_limit_and_execution_result ( limit : u64 , value : & ExecutionResult ) -> Self {
115
144
match value {
116
145
ExecutionResult :: Success { gas_used, output, gas_refunded, .. } => Self :: Success {
117
- estimation : * gas_used ,
146
+ estimation : limit ,
118
147
refund : * gas_refunded,
119
148
gas_used : * gas_used,
120
149
output : output. clone ( ) ,
@@ -127,9 +156,7 @@ impl From<&ExecutionResult> for EstimationResult {
127
156
}
128
157
}
129
158
}
130
- }
131
159
132
- impl EstimationResult {
133
160
/// Create a successful estimation result with a gas estimation of 21000.
134
161
pub const fn basic_transfer_success ( estimation : u64 ) -> Self {
135
162
Self :: Success {
0 commit comments