This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
WSPeerGroup.h
168 lines (138 loc) · 5.58 KB
/
WSPeerGroup.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// WSPeerGroup.h
// BitcoinSPV
//
// Created by Davide De Rosa on 24/06/14.
// Copyright (c) 2014 Davide De Rosa. All rights reserved.
//
// https://github.com/keeshux
//
// This file is part of BitcoinSPV.
//
// BitcoinSPV is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// BitcoinSPV is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BitcoinSPV. If not, see <http://www.gnu.org/licenses/>.
//
#import <Foundation/Foundation.h>
#import "WSPeerGroupNotifier.h"
#import "WSPeer.h"
#import "WSReachability.h"
#import "WSBlockChain.h"
@class WSParameters;
@class WSConnectionPool;
@protocol WSPeerGroupDownloader;
@protocol WSPeerGroupDownloadDelegate;
#pragma mark -
@interface WSPeerInfo : NSObject
- (NSString *)host;
- (uint16_t)port;
- (NSString *)userAgent;
- (uint32_t)lastBlockHeight;
- (BOOL)isDownloadPeer;
@end
#pragma mark -
@interface WSPeerGroupStatus : NSObject
- (WSParameters *)parameters;
- (BOOL)isConnected;
- (BOOL)isDownloading;
- (uint32_t)currentHeight;
- (uint32_t)targetHeight;
- (double)downloadProgress;
- (NSArray *)recentBlocks;
- (NSUInteger)sentBytes;
- (NSUInteger)receivedBytes;
- (NSArray *)peersInfo;
- (WSPeerInfo *)downloadPeerInfo;
@end
#pragma mark -
//
// All is done on private queue except public methods that can be run from any queue.
//
// NEVER invoke public methods internally, deadlock is guaranteed due to dispatch_sync.
//
@interface WSPeerGroup : NSObject <WSPeerDelegate, WSReachabilityDelegate>
// WARNING: set properties before starting connections
@property (nonatomic, strong) NSArray *peerHosts; // nil
@property (nonatomic, assign) NSUInteger maxConnections; // 3
@property (nonatomic, assign) NSUInteger maxConnectionFailures; // 20
@property (nonatomic, assign) NSTimeInterval reconnectionDelayOnFailure; // 10.0
@property (nonatomic, assign) NSTimeInterval seedTTL; // 600.0 (10 minutes)
@property (nonatomic, assign) BOOL needsBloomFiltering; // NO
// WARNING: queue must be of type DISPATCH_QUEUE_SERIAL
- (instancetype)initWithParameters:(WSParameters *)parameters;
- (instancetype)initWithParameters:(WSParameters *)parameters pool:(WSConnectionPool *)pool queue:(dispatch_queue_t)queue;
// connection
- (BOOL)startConnections;
- (BOOL)stopConnections;
- (void)stopConnectionsWithCompletionBlock:(void (^)(void))completionBlock;
- (BOOL)isStarted;
- (BOOL)isConnected;
- (NSUInteger)numberOfConnections;
- (BOOL)hasReachedMaxConnections;
// download
- (BOOL)startDownloadWithDownloader:(id<WSPeerGroupDownloader>)downloader;
- (void)stopDownload;
- (BOOL)isDownloading;
- (uint32_t)currentHeight;
- (NSUInteger)numberOfBlocksLeft;
- (BOOL)isSynced;
- (BOOL)reconnectForDownload;
- (BOOL)rescanBlockChain;
// interaction
- (WSPeerGroupStatus *)statusWithNumberOfRecentBlocks:(NSUInteger)numberOfRecentBlocks;
- (BOOL)publishTransaction:(WSSignedTransaction *)transaction;
- (BOOL)publishTransaction:(WSSignedTransaction *)transaction safely:(BOOL)safely;
- (void)saveState;
//
// WARNING: do not nil out peerGroup strong references until disconnection!
//
// WSPeer objects would not call peer:didDisconnectWithError: because peerGroup is
// their (weak) delegate and would be deallocated prematurely
//
// as a consequence, peerGroup wouldn't exist anymore and would never report
// any WSPeerGroupDidDisconnectNotification resulting in completionBlock
// never called
//
@end
#pragma mark -
//
// every method is executed in group queue
//
@protocol WSPeerGroupDownloadDelegate <NSObject>
- (void)peerGroup:(WSPeerGroup *)peerGroup peerDidConnect:(WSPeer *)peer;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didDisconnectWithError:(NSError *)error;
- (void)peerGroup:(WSPeerGroup *)peerGroup peerDidKeepAlive:(WSPeer *)peer;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didReceiveHeaders:(NSArray *)headers;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didReceiveInventories:(NSArray *)inventories;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didReceiveBlock:(WSBlock *)block;
- (BOOL)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer shouldAddTransaction:(WSSignedTransaction *)transaction toFilteredBlock:(WSFilteredBlock *)filteredBlock;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didReceiveFilteredBlock:(WSFilteredBlock *)filteredBlock withTransactions:(NSOrderedSet *)transactions;
- (void)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer didReceiveTransaction:(WSSignedTransaction *)transaction;
- (BOOL)peerGroup:(WSPeerGroup *)peerGroup peer:(WSPeer *)peer shouldAcceptHeader:(WSBlockHeader *)header error:(NSError **)error;
@end
//
// every method is executed in group queue
//
@protocol WSPeerGroupDownloader <WSPeerGroupDownloadDelegate>
- (WSParameters *)parameters;
- (void)startWithPeerGroup:(WSPeerGroup *)peerGroup;
- (void)stop;
- (uint32_t)lastBlockHeight;
- (uint32_t)currentHeight;
- (NSUInteger)numberOfBlocksLeft;
- (BOOL)isSynced;
- (BOOL)isPeerDownloadPeer:(WSPeer *)peer;
- (NSArray *)recentBlocksWithCount:(NSUInteger)count;
- (void)reconnectForDownload;
- (void)rescanBlockChain;
- (void)saveState;
@end