@@ -5,6 +5,12 @@ use std::ops::Range;
55/// binary searching.
66pub ( crate ) struct SearchRange ( Range < u64 > ) ;
77
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+
814impl From < Range < u64 > > for SearchRange {
915 fn from ( value : Range < u64 > ) -> Self {
1016 Self ( value)
@@ -25,7 +31,7 @@ impl SearchRange {
2531
2632 /// Calculate the midpoint of the search range.
2733 pub ( crate ) const fn midpoint ( & self ) -> u64 {
28- ( self . 0 . end - self . 0 . start ) / 2
34+ ( self . max ( ) + self . min ( ) ) / 2
2935 }
3036
3137 /// Get the start of the search range.
@@ -38,6 +44,7 @@ impl SearchRange {
3844 self . 0 . start = min;
3945 }
4046
47+ /// Raise the minimum of the search range, if the candidate is higher.
4148 pub ( crate ) const fn maybe_raise_min ( & mut self , candidate : u64 ) {
4249 if candidate > self . min ( ) {
4350 self . set_min ( candidate) ;
@@ -110,11 +117,33 @@ pub enum EstimationResult {
110117 } ,
111118}
112119
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 {
115144 match value {
116145 ExecutionResult :: Success { gas_used, output, gas_refunded, .. } => Self :: Success {
117- estimation : * gas_used ,
146+ estimation : limit ,
118147 refund : * gas_refunded,
119148 gas_used : * gas_used,
120149 output : output. clone ( ) ,
@@ -127,9 +156,7 @@ impl From<&ExecutionResult> for EstimationResult {
127156 }
128157 }
129158 }
130- }
131159
132- impl EstimationResult {
133160 /// Create a successful estimation result with a gas estimation of 21000.
134161 pub const fn basic_transfer_success ( estimation : u64 ) -> Self {
135162 Self :: Success {
0 commit comments