File tree 4 files changed +25
-10
lines changed
4 files changed +25
-10
lines changed Original file line number Diff line number Diff line change @@ -44,13 +44,9 @@ impl RpcApi for Client {
44
44
) -> bitcoincore_rpc:: Result < bitcoin:: Txid > {
45
45
let tx: Transaction = encode:: deserialize_hex ( & tx. raw_hex ( ) ) . unwrap ( ) ;
46
46
47
- if let Err ( e) = self . ledger . check_transaction ( tx. clone ( ) ) {
48
- return Err ( bitcoincore_rpc:: Error :: Io ( std:: io:: Error :: other ( format ! (
49
- "{e}"
50
- ) ) ) ) ;
51
- }
47
+ self . ledger . check_transaction ( tx. clone ( ) ) ?;
52
48
53
- self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ;
49
+ self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ? ;
54
50
55
51
Ok ( tx. compute_txid ( ) )
56
52
}
@@ -186,7 +182,7 @@ impl RpcApi for Client {
186
182
output : vec ! [ txout] ,
187
183
} ;
188
184
189
- self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ;
185
+ self . ledger . add_transaction_unconditionally ( tx. clone ( ) ) ? ;
190
186
191
187
for output in tx. output {
192
188
self . ledger . add_utxo ( output) ;
Original file line number Diff line number Diff line change @@ -10,3 +10,9 @@ pub enum LedgerError {
10
10
#[ error( "Database returned an error: {0}" ) ]
11
11
Database ( anyhow:: Error ) ,
12
12
}
13
+
14
+ impl From < LedgerError > for bitcoincore_rpc:: Error {
15
+ fn from ( error : LedgerError ) -> Self {
16
+ bitcoincore_rpc:: Error :: ReturnedError ( error. to_string ( ) )
17
+ }
18
+ }
Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ pub struct Ledger {
33
33
34
34
impl Ledger {
35
35
/// Creates a new empty ledger.
36
+ ///
37
+ /// # Panics
38
+ ///
39
+ /// If database connection cannot be established in bitcoin-simulator, it
40
+ /// will panic.
36
41
pub fn new ( ) -> Self {
37
42
Self {
38
43
database : Arc :: new ( Mutex :: new ( Database :: connect_temporary_database ( ) . unwrap ( ) ) ) ,
Original file line number Diff line number Diff line change @@ -15,14 +15,22 @@ impl Ledger {
15
15
}
16
16
17
17
/// Adds transaction to current block, without checking anything.
18
- pub fn add_transaction_unconditionally ( & self , transaction : Transaction ) {
19
- self . database
18
+ pub fn add_transaction_unconditionally (
19
+ & self ,
20
+ transaction : Transaction ,
21
+ ) -> Result < ( ) , LedgerError > {
22
+ if let Err ( e) = self
23
+ . database
20
24
. lock ( )
21
25
. unwrap ( )
22
26
. insert_transaction_unconditionally ( & transaction)
23
- . unwrap ( ) ;
27
+ {
28
+ return Err ( LedgerError :: Database ( e) ) ;
29
+ } ;
24
30
25
31
add_item ! ( self . transactions, transaction) ;
32
+
33
+ Ok ( ( ) )
26
34
}
27
35
/// Returns user's list of transactions.
28
36
pub fn get_transaction ( & self , txid : Txid ) -> Transaction {
You can’t perform that action at this time.
0 commit comments