@@ -90,26 +90,25 @@ where
90
90
. build ( ) ) ) ;
91
91
} ;
92
92
93
- // Note that this is not an overflow safe operation. Given realistic
94
- // assumptions, the asset scale will be <=18 and >=1.
95
- // It is doubtful that the exchange rate between two assets,
96
- // multiplied by 18 would exceed std::f64::MAX
97
- let scaled_rate = rate. normalize_scale ( ConvertDetails {
93
+ // Can we overflow here?
94
+ let outgoing_amount = ( request. prepare . amount ( ) as f64 ) * rate;
95
+ let outgoing_amount = outgoing_amount. normalize_scale ( ConvertDetails {
98
96
from : request. from . asset_scale ( ) ,
99
97
to : request. to . asset_scale ( ) ,
100
98
} ) ;
101
99
102
- match scaled_rate {
103
- Ok ( scaled_rate) => {
104
- let outgoing_amount = ( request. prepare . amount ( ) as f64 ) * scaled_rate;
100
+ match outgoing_amount {
101
+ Ok ( outgoing_amount) => {
102
+ // NOTE: If the f64 amount could not fit in a u64, then the
103
+ // typecast makes the outgoing amount 0.
105
104
request. prepare . set_amount ( outgoing_amount as u64 ) ;
106
105
trace ! ( "Converted incoming amount of: {} {} (scale {}) from account {} to outgoing amount of: {} {} (scale {}) for account {}" ,
107
106
request. original_amount, request. from. asset_code( ) , request. from. asset_scale( ) , request. from. id( ) ,
108
107
outgoing_amount, request. to. asset_code( ) , request. to. asset_scale( ) , request. to. id( ) ) ;
109
108
}
110
109
Err ( _) => {
111
110
return Box :: new ( err ( RejectBuilder {
112
- code : ErrorCode :: F02_UNREACHABLE ,
111
+ code : ErrorCode :: F08_AMOUNT_TOO_LARGE ,
113
112
message : format ! (
114
113
"Could not convert exchange rate from {}:{} to: {}:{}" ,
115
114
request. from. asset_code( ) ,
0 commit comments