11use std:: fmt;
22
3- use at2_node:: { proto, ThinTransaction } ;
3+ use at2_node:: {
4+ proto:: { self , * } ,
5+ ThinTransaction , TransactionState ,
6+ } ;
47use contagion:: { Contagion , ContagionConfig , ContagionMessage } ;
58use drop:: {
69 crypto:: key:: exchange:: { self , Exchanger } ,
@@ -168,22 +171,31 @@ impl Service {
168171
169172 let sender = Box :: new ( msg. sender ( ) . to_owned ( ) ) ;
170173
171- self . accounts
174+ let processed = self
175+ . accounts
172176 . transfer (
173177 sender. clone ( ) ,
174178 msg. sequence ( ) ,
175179 Box :: new ( msg. payload ( ) . recipient ) ,
176180 msg. payload ( ) . amount ,
177181 )
178182 . await
179- . context ( ProcessTxForAccounts ) ? ;
183+ . context ( ProcessTxForAccounts ) ;
180184
181185 self . recent_transactions
182- . put ( sender, msg. payload ( ) . to_owned ( ) )
186+ . update (
187+ sender,
188+ msg. sequence ( ) ,
189+ if processed. is_ok ( ) {
190+ TransactionState :: Success
191+ } else {
192+ TransactionState :: Failure
193+ } ,
194+ )
183195 . await
184196 . context ( ProcessTxForRecent ) ?;
185197
186- Ok ( ( ) )
198+ processed
187199 }
188200}
189201
@@ -204,49 +216,57 @@ impl From<recent_transactions::Error> for tonic::Status {
204216}
205217
206218#[ tonic:: async_trait]
207- impl proto :: at2_server:: At2 for Service {
219+ impl at2_server:: At2 for Service {
208220 async fn send_asset (
209221 & self ,
210- request : tonic:: Request < proto :: SendAssetRequest > ,
211- ) -> Result < tonic:: Response < proto :: SendAssetReply > , tonic:: Status > {
222+ request : tonic:: Request < SendAssetRequest > ,
223+ ) -> Result < tonic:: Response < SendAssetReply > , tonic:: Status > {
212224 let message = request. into_inner ( ) ;
213225
226+ let thin = at2_node:: ThinTransaction {
227+ recipient : bincode:: deserialize ( & message. recipient ) . context ( Deserialize ) ?,
228+ amount : message. amount ,
229+ } ;
230+
231+ let sender = bincode:: deserialize ( & message. sender ) . context ( Deserialize ) ?;
232+
233+ self . recent_transactions
234+ . put ( Box :: new ( sender) , message. sequence , thin. clone ( ) )
235+ . await ?;
236+
214237 self . handle
215238 . clone ( )
216239 . broadcast ( & sieve:: Payload :: new (
217- bincode :: deserialize ( & message . sender ) . context ( Deserialize ) ? ,
240+ sender,
218241 message. sequence ,
219- at2_node:: ThinTransaction {
220- recipient : bincode:: deserialize ( & message. recipient ) . context ( Deserialize ) ?,
221- amount : message. amount ,
222- } ,
242+ thin,
223243 bincode:: deserialize ( & message. signature ) . context ( Deserialize ) ?,
224244 ) )
225245 . await
226- . expect ( "broadcasting failed" ) ;
246+ . map_err ( |err| tonic :: Status :: invalid_argument ( err . to_string ( ) ) ) ? ;
227247
228- Ok ( Response :: new ( proto :: SendAssetReply { } ) )
248+ Ok ( Response :: new ( SendAssetReply { } ) )
229249 }
230250
231251 async fn get_last_sequence (
232252 & self ,
233- request : tonic:: Request < proto :: GetLastSequenceRequest > ,
234- ) -> Result < tonic:: Response < proto :: GetLastSequenceReply > , tonic:: Status > {
253+ request : tonic:: Request < GetLastSequenceRequest > ,
254+ ) -> Result < tonic:: Response < GetLastSequenceReply > , tonic:: Status > {
235255 let sequence = self
236256 . accounts
237257 . get_last_sequence (
238258 bincode:: deserialize ( & request. get_ref ( ) . sender ) . context ( Deserialize ) ?,
239259 )
240260 . await ?;
241261
242- Ok ( Response :: new ( proto :: GetLastSequenceReply { sequence } ) )
262+ Ok ( Response :: new ( GetLastSequenceReply { sequence } ) )
243263 }
244264
245265 async fn get_balance (
246266 & self ,
247- request : tonic:: Request < proto :: GetBalanceRequest > ,
248- ) -> Result < tonic:: Response < proto :: GetBalanceReply > , tonic:: Status > {
249- Ok ( Response :: new ( proto :: GetBalanceReply {
267+ request : tonic:: Request < GetBalanceRequest > ,
268+ ) -> Result < tonic:: Response < GetBalanceReply > , tonic:: Status > {
269+ Ok ( Response :: new ( GetBalanceReply {
250270 amount : self
251271 . accounts
252272 . get_balance ( bincode:: deserialize ( & request. get_ref ( ) . sender ) . context ( Deserialize ) ?)
@@ -256,20 +276,28 @@ impl proto::at2_server::At2 for Service {
256276
257277 async fn get_latest_transactions (
258278 & self ,
259- _: tonic:: Request < proto:: GetLatestTransactionsRequest > ,
260- ) -> Result < tonic:: Response < proto:: GetLatestTransactionsReply > , tonic:: Status > {
261- Ok ( Response :: new ( proto:: GetLatestTransactionsReply {
279+ _: tonic:: Request < GetLatestTransactionsRequest > ,
280+ ) -> Result < tonic:: Response < GetLatestTransactionsReply > , tonic:: Status > {
281+ use full_transaction:: State ;
282+
283+ Ok ( Response :: new ( GetLatestTransactionsReply {
262284 transactions : self
263285 . recent_transactions
264286 . get_all ( )
265287 . await ?
266288 . iter ( )
267289 . map ( |tx| {
268- Ok ( proto:: ProcessedTransaction {
290+ Ok ( proto:: FullTransaction {
269291 timestamp : tx. timestamp . to_rfc3339 ( ) ,
270292 sender : bincode:: serialize ( & tx. sender ) . context ( Serialize ) ?,
293+ sender_sequence : tx. sender_sequence ,
271294 recipient : bincode:: serialize ( & tx. recipient ) . context ( Serialize ) ?,
272295 amount : tx. amount ,
296+ state : match tx. state {
297+ TransactionState :: Pending => State :: Pending as i32 ,
298+ TransactionState :: Success => State :: Success as i32 ,
299+ TransactionState :: Failure => State :: Failure as i32 ,
300+ } ,
273301 } )
274302 } )
275303 . collect :: < Result < _ , ProtoError > > ( ) ?,
0 commit comments