Skip to content

Commit 7a75586

Browse files
author
Ed Gamble
authored
Merge pull request #361 from breadwallet/bugfix/CORE-704
CORE-704: On System.systemExtract failure, cryptoWalletManagerGive() CWM
2 parents 4fb9856 + 660e30b commit 7a75586

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

Swift/BRCrypto/BRCryptoSystem.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,14 +1327,26 @@ extension System {
13271327
}
13281328
}
13291329

1330+
extension System {
1331+
private static func cleanup (_ message: String,
1332+
cwm: BRCryptoWalletManager? = nil,
1333+
wid: BRCryptoWallet? = nil,
1334+
tid: BRCryptoTransfer? = nil) -> Void {
1335+
print (message)
1336+
cwm.map { cryptoWalletManagerGive ($0) }
1337+
wid.map { cryptoWalletGive ($0) }
1338+
tid.map { cryptoTransferGive ($0) }
1339+
}
1340+
}
1341+
13301342
extension System {
13311343
internal var clientBTC: BRCryptoCWMClientBTC {
13321344
return BRCryptoCWMClientBTC (
13331345
funcGetBlockNumber: { (context, cwm, sid) in
13341346
precondition (nil != context && nil != cwm)
13351347

13361348
guard let (system, manager) = System.systemExtract (context, cwm)
1337-
else { print ("SYS: BTC: GetBlockNumber: Missed {cwm}"); return }
1349+
else { System.cleanup("SYS: BTC: GetBlockNumber: Missed {cwm}", cwm: cwm); return }
13381350
print ("SYS: BTC: GetBlockNumber")
13391351

13401352
manager.query.getBlockchain (blockchainId: manager.network.uids) { (res: Result<BlockChainDB.Model.Blockchain, BlockChainDB.QueryError>) in
@@ -1348,7 +1360,7 @@ extension System {
13481360
precondition (nil != context && nil != cwm)
13491361

13501362
guard let (system, manager) = System.systemExtract (context, cwm)
1351-
else { print ("SYS: BTC: GetTransactions: Missed {cwm}"); return }
1363+
else { System.cleanup ("SYS: BTC: GetTransactions: Missed {cwm}", cwm: cwm); return }
13521364
print ("SYS: BTC: GetTransactions: Blocks: {\(begBlockNumber), \(endBlockNumber)}")
13531365

13541366
var cAddresses = addresses!
@@ -1392,7 +1404,7 @@ extension System {
13921404
precondition (nil != context && nil != cwm)
13931405

13941406
guard let (system, manager) = System.systemExtract (context, cwm)
1395-
else { print ("SYS: BTC: SubmitTransaction: Missed {cwm}"); return }
1407+
else { System.cleanup ("SYS: BTC: SubmitTransaction: Missed {cwm}", cwm: cwm); return }
13961408
print ("SYS: BTC: SubmitTransaction")
13971409

13981410
let hash = asUTF8String (hashAsHex!)
@@ -1415,7 +1427,7 @@ extension System {
14151427
precondition (nil != context && nil != cwm)
14161428

14171429
guard let (system, manager) = System.systemExtract (context, cwm)
1418-
else { print ("SYS: ETH: GetEtherBalance: Missed {cwm}"); return }
1430+
else { System.cleanup ("SYS: ETH: GetEtherBalance: Missed {cwm}", cwm: cwm); return }
14191431

14201432
let ewm = cryptoWalletManagerAsETH (cwm);
14211433
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1433,7 +1445,7 @@ extension System {
14331445
precondition (nil != context && nil != cwm)
14341446

14351447
guard let (system, manager) = System.systemExtract (context, cwm)
1436-
else { print ("SYS: ETH: GetTokenBalance: Missed {cwm}"); return }
1448+
else { System.cleanup ("SYS: ETH: GetTokenBalance: Missed {cwm}", cwm: cwm); return }
14371449

14381450
let ewm = cryptoWalletManagerAsETH (cwm);
14391451
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1452,7 +1464,7 @@ extension System {
14521464
precondition (nil != context && nil != cwm)
14531465

14541466
guard let (system, manager) = System.systemExtract (context, cwm)
1455-
else { print ("SYS: ETH: GetGasPrice: Missed {cwm}"); return }
1467+
else { System.cleanup ("SYS: ETH: GetGasPrice: Missed {cwm}", cwm: cwm); return }
14561468

14571469
let ewm = cryptoWalletManagerAsETH (cwm);
14581470
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1469,10 +1481,10 @@ extension System {
14691481
precondition (nil != context && nil != cwm)
14701482

14711483
guard let (system, manager) = System.systemExtract (context, cwm)
1472-
else { print ("SYS: ETH: EstimateGas: Missed {cwm}"); return }
1484+
else { System.cleanup ("SYS: ETH: EstimateGas: Missed {cwm}", cwm: cwm); return }
14731485

14741486
guard let price = price.map (asUTF8String)
1475-
else { print ("SYS: ETH: EstimateGas: Missed {price}"); return }
1487+
else { System.cleanup ("SYS: ETH: EstimateGas: Missed {price}", cwm: cwm); return }
14761488

14771489
let ewm = cryptoWalletManagerAsETH (cwm);
14781490
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1493,7 +1505,7 @@ extension System {
14931505
precondition (nil != context && nil != cwm)
14941506

14951507
guard let (system, manager) = System.systemExtract (context, cwm)
1496-
else { print ("SYS: ETH: SubmitTransaction: Missed {cwm}"); return }
1508+
else { System.cleanup ("SYS: ETH: SubmitTransaction: Missed {cwm}", cwm: cwm); return }
14971509

14981510
let ewm = cryptoWalletManagerAsETH (cwm);
14991511
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1511,7 +1523,7 @@ extension System {
15111523
precondition (nil != context && nil != cwm)
15121524

15131525
guard let (system, manager) = System.systemExtract (context, cwm)
1514-
else { print ("SYS: ETH: GetTransactions: Missed {cwm}"); return }
1526+
else { System.cleanup ("SYS: ETH: GetTransactions: Missed {cwm}", cwm: cwm); return }
15151527

15161528
let ewm = cryptoWalletManagerAsETH (cwm);
15171529
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1551,7 +1563,7 @@ extension System {
15511563
precondition (nil != context && nil != cwm)
15521564

15531565
guard let (system, manager) = System.systemExtract (context, cwm)
1554-
else { print ("SYS: ETH: GetLogs: Missed {cwm}"); return }
1566+
else { System.cleanup ("SYS: ETH: GetLogs: Missed {cwm}", cwm: cwm); return }
15551567

15561568
let ewm = cryptoWalletManagerAsETH (cwm);
15571569
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1591,7 +1603,7 @@ extension System {
15911603
precondition (nil != context && nil != cwm)
15921604

15931605
guard let (system, manager) = System.systemExtract (context, cwm)
1594-
else { print ("SYS: ETH: GetBlocks: Missed {cwm}"); return }
1606+
else { System.cleanup ("SYS: ETH: GetBlocks: Missed {cwm}", cwm: cwm); return }
15951607

15961608
let ewm = cryptoWalletManagerAsETH (cwm);
15971609
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1618,7 +1630,7 @@ extension System {
16181630
precondition (nil != context && nil != cwm)
16191631

16201632
guard let (system, manager) = System.systemExtract (context, cwm)
1621-
else { print ("SYS: ETH: GetTokens: Missed {cwm}"); return }
1633+
else { System.cleanup ("SYS: ETH: GetTokens: Missed {cwm}", cwm: cwm); return }
16221634

16231635
manager.query.getTokensAsETH () {
16241636
(res: Result<[BlockChainDB.ETH.Token],BlockChainDB.QueryError>) in
@@ -1642,7 +1654,7 @@ extension System {
16421654
precondition (nil != context && nil != cwm)
16431655

16441656
guard let (system, manager) = System.systemExtract (context, cwm)
1645-
else { print ("SYS: ETH: GetBlockNumber: Missed {cwm}"); return }
1657+
else { System.cleanup ("SYS: ETH: GetBlockNumber: Missed {cwm}", cwm: cwm); return }
16461658

16471659
let ewm = cryptoWalletManagerAsETH (cwm);
16481660
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
@@ -1666,13 +1678,13 @@ extension System {
16661678
precondition (nil != context && nil != cwm)
16671679

16681680
guard let (system, manager) = System.systemExtract (context, cwm)
1669-
else { print ("SYS: ETH: GetNonce: Missed {cwm}"); return }
1681+
else { System.cleanup ("SYS: ETH: GetNonce: Missed {cwm}", cwm: cwm); return }
16701682

16711683
let ewm = cryptoWalletManagerAsETH (cwm);
16721684
let network = asUTF8String (networkGetName (ewmGetNetwork (ewm)))
16731685

16741686
guard let address = address.map (asUTF8String)
1675-
else { print ("SYS: ETH: GetNonce: Missed {address}"); return }
1687+
else { System.cleanup ("SYS: ETH: GetNonce: Missed {address}", cwm: cwm); return }
16761688

16771689
manager.query.getNonceAsETH (network: network, address: address) {
16781690
(res: Result<String, BlockChainDB.QueryError>) in
@@ -1691,7 +1703,7 @@ extension System {
16911703
precondition (nil != context && nil != cwm)
16921704

16931705
guard let (system, manager) = System.systemExtract (context, cwm)
1694-
else { print ("SYS: GEN: GetBlockNumber: Missed {cwm}"); return }
1706+
else { System.cleanup ("SYS: GEN: GetBlockNumber: Missed {cwm}", cwm: cwm); return }
16951707
print ("SYS: GEN: GetBlockNumber")
16961708

16971709
manager.query.getBlockchain (blockchainId: manager.network.uids) {
@@ -1706,7 +1718,7 @@ extension System {
17061718
precondition (nil != context && nil != cwm)
17071719

17081720
guard let (system, manager) = System.systemExtract (context, cwm)
1709-
else { print ("SYS: GEN: GetTransaction: Missed {cwm}"); return }
1721+
else { System.cleanup ("SYS: GEN: GetTransaction: Missed {cwm}", cwm: cwm); return }
17101722
print ("SYS: GEN: GetTransactions: Blocks: {\(begBlockNumber), \(endBlockNumber)}")
17111723

17121724
manager.query.getTransactions (blockchainId: manager.network.uids,
@@ -1743,7 +1755,7 @@ extension System {
17431755
precondition (nil != context && nil != cwm)
17441756

17451757
guard let (system, manager) = System.systemExtract (context, cwm)
1746-
else { print ("SYS: GEN: SubmitTransaction: Missed {cwm}"); return }
1758+
else { System.cleanup ("SYS: GEN: SubmitTransaction: Missed {cwm}", cwm: cwm); return }
17471759
print ("SYS: GEN: SubmitTransaction")
17481760

17491761
let hash = asUTF8String (hashAsHex!)

0 commit comments

Comments
 (0)