Skip to content

Commit

Permalink
CORE-207: BRFileService error handling (part #1, on fileServiceLoad).
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Gamble committed Jan 25, 2019
1 parent 1a79689 commit 03d0439
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 68 deletions.
68 changes: 58 additions & 10 deletions BRWalletManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ fileServiceTypeTransactionV1Reader (BRFileServiceContext context,
static BRArrayOf(BRTransaction*)
initialTransactionsLoad (BRWalletManager manager) {
BRSetOf(BRTransaction*) transactionSet = BRSetNew(BRTransactionHash, BRTransactionEq, 100);
fileServiceLoad (manager->fileService, transactionSet, fileServiceTypeTransactions, 1);
if (1 != fileServiceLoad (manager->fileService, transactionSet, fileServiceTypeTransactions, 1)) {
BRSetFree(transactionSet);
return NULL;
}

size_t transactionsCount = BRSetCount(transactionSet);

Expand Down Expand Up @@ -235,7 +238,10 @@ fileServiceTypeBlockV1Reader (BRFileServiceContext context,
static BRArrayOf(BRMerkleBlock*)
initialBlocksLoad (BRWalletManager manager) {
BRSetOf(BRTransaction*) blockSet = BRSetNew(BRMerkleBlockHash, BRMerkleBlockEq, 100);
fileServiceLoad (manager->fileService, blockSet, fileServiceTypeBlocks, 1);
if (1 != fileServiceLoad (manager->fileService, blockSet, fileServiceTypeBlocks, 1)) {
BRSetFree (blockSet);
return NULL;
}

size_t blocksCount = BRSetCount(blockSet);

Expand Down Expand Up @@ -301,7 +307,10 @@ static BRArrayOf(BRPeer)
initialPeersLoad (BRWalletManager manager) {
/// Load peers for the wallet manager.
BRSetOf(BRPeer*) peerSet = BRSetNew(BRPeerHash, BRPeerEq, 100);
fileServiceLoad (manager->fileService, peerSet, fileServiceTypePeers, 1);
if (1 != fileServiceLoad (manager->fileService, peerSet, fileServiceTypePeers, 1)) {
BRSetFree(peerSet);
return NULL;
}

size_t peersCount = BRSetCount(peerSet);
BRPeer *peersRefs[peersCount];
Expand All @@ -319,6 +328,32 @@ initialPeersLoad (BRWalletManager manager) {
return peers;
}

static void
bwmFileServiceErrorHandler (BRFileServiceContext context,
BRFileService fs,
BRFileServiceError error) {
BRWalletManager bwm = (BRWalletManager) context;

switch (error.type) {
case FILE_SERVICE_IMPL:
// This actually a FATAL - an unresolvable coding error.
_peer_log ("bread: FileService Error: IMPL: %s", error.u.impl.reason);
break;
case FILE_SERVICE_UNIX:
_peer_log ("bread: FileService Error: UNIX: %s", strerror(error.u.unix.error));
break;
case FILE_SERVICE_ENTITY:
// This is likely a coding error too.
_peer_log ("bread: FileService Error: ENTITY (%s); %s",
error.u.entity.type,
error.u.entity.reason);
break;
}
_peer_log ("bread: FileService Error: FORCED SYNC%s", "");

if (NULL != bwm->peerManager)
BRPeerManagerRescan (bwm->peerManager);
}
///
/// MARK: Wallet Manager
///
Expand All @@ -341,7 +376,9 @@ BRWalletManagerNew (BRWalletManagerClient client,
//
// Create the File Service w/ associated types.
//
manager->fileService = fileServiceCreate (baseStoragePath, networkName, currencyName);
manager->fileService = fileServiceCreate (baseStoragePath, networkName, currencyName,
manager,
bwmFileServiceErrorHandler);
if (NULL == manager->fileService) { free (manager); return NULL; }

/// Transaction
Expand Down Expand Up @@ -376,6 +413,22 @@ BRWalletManagerNew (BRWalletManagerClient client,

/// Load transactions for the wallet manager.
BRArrayOf(BRTransaction*) transactions = initialTransactionsLoad(manager);
/// Load blocks and peers for the peer manager.
BRArrayOf(BRMerkleBlock*) blocks = initialBlocksLoad(manager);
BRArrayOf(BRPeer) peers = initialPeersLoad(manager);

// If any of these are NULL, then there was a failure; on a failure they all need to be cleared
// which will cause a *FULL SYNC*
if (NULL == transactions || NULL == blocks || NULL == peers) {
if (NULL == transactions) array_new (transactions, 1);
else array_clear(transactions);

if (NULL == blocks) array_new (blocks, 1);
else array_clear(blocks);

if (NULL == peers) array_new (peers, 1);
else array_clear(peers);
}

manager->wallet = BRWalletNew (transactions, array_count(transactions), mpk, fork);
BRWalletSetCallbacks (manager->wallet, manager,
Expand All @@ -384,18 +437,13 @@ BRWalletManagerNew (BRWalletManagerClient client,
_BRWalletManagerTxUpdated,
_BRWalletManagerTxDeleted);

array_free(transactions);

client.funcWalletEvent (manager,
manager->wallet,
(BRWalletEvent) {
BITCOIN_WALLET_CREATED
});

/// Load blocks and peers for the peer manager.
BRArrayOf(BRMerkleBlock*) blocks = initialBlocksLoad(manager);
BRArrayOf(BRPeer) peers = initialPeersLoad(manager);

manager->peerManager = BRPeerManagerNew (params, manager->wallet, earliestKeyTime,
blocks, array_count(blocks),
peers, array_count(peers));
Expand All @@ -409,7 +457,7 @@ BRWalletManagerNew (BRWalletManagerClient client,
_BRWalletManagerNetworkIsReachabele,
_BRWalletManagerThreadCleanup);

array_free(blocks); array_free(peers);
array_free(transactions); array_free(blocks); array_free(peers);

return manager;
}
Expand Down
16 changes: 9 additions & 7 deletions Swift/BRCrypto/BRBitcoin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public class BitcoinWallet: Wallet {
/// BRCorePeerManager.
///
public class BitcoinWalletManager: WalletManager {
#if false
#if true
internal let coreWalletManager: BRCoreWalletManager
#endif

Expand Down Expand Up @@ -379,7 +379,7 @@ public class BitcoinWalletManager: WalletManager {
self.mode = mode
self.state = WalletManagerState.created

#if false
#if true
let client = BRWalletManagerClient (
funcTransactionEvent: { (this, coreWallet, coreTransaction, event) in
if let bwm = BitcoinWalletManager.lookup (manager: this),
Expand Down Expand Up @@ -505,12 +505,14 @@ public class BitcoinWalletManager: WalletManager {
}})

self.coreWalletManager = BRWalletManagerNew (client,
forkId,
account.masterPublicKey,
params,
UInt32 (timestamp),
path)
#endif

self.corePeerManager = BRWalletManagerGetPeerManager (self.coreWalletManager)
self.coreWallet = BRWalletManagerGetWallet(self.coreWalletManager)
#else

self.coreWallet = BRWalletNew (nil, 0, account.masterPublicKey, Int32(forkId.rawValue))
self.corePeerManager = BRPeerManagerNew (params, coreWallet, UInt32(timestamp),
Expand Down Expand Up @@ -662,22 +664,22 @@ public class BitcoinWalletManager: WalletManager {
{ (this) in // threadCleanup
if let _ = BitcoinWalletManager.lookup (ptr: this) {
}})

#endif

BitcoinWalletManager.managers.append(self)
self.listener.handleManagerEvent(manager: self, event: WalletManagerEvent.created)
let _ = self.primaryWallet
}

public func connect() {
#if false
#if true
BRWalletManagerConnect(self.coreWalletManager)
#endif
BRPeerManagerConnect (self.corePeerManager)
}

public func disconnect() {
#if false
#if true
BRWalletManagerDisconnect(self.coreWalletManager)
#endif
BRPeerManagerDisconnect (self.corePeerManager)
Expand Down
6 changes: 6 additions & 0 deletions Swift/BRCryptoDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
.appendingPathComponent("Core").path

do {
if FileManager.default.fileExists(atPath: storagePath) {
try FileManager.default.removeItem(atPath: storagePath)
}

try FileManager.default.createDirectory (atPath: storagePath,
withIntermediateDirectories: true,
attributes: nil)
Expand All @@ -71,6 +75,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
print("Error: \(error.localizedDescription)")
}

NSLog ("StoragePath: \(storagePath)");

self.listener = CoreDemoListener ()

self.btcManager = BitcoinWalletManager (listener: listener,
Expand Down
Loading

0 comments on commit 03d0439

Please sign in to comment.