]>
Commit | Line | Data |
---|---|---|
2341e9b5 | 1 | Coding |
2 | ==================== | |
3 | ||
2887bffc PW |
4 | Various coding styles have been used during the history of the codebase, |
5 | and the result is not very consistent. However, we're now trying to converge to | |
6 | a single style, so please use it in new code. Old code will be converted | |
7 | gradually. | |
8 | - Basic rules specified in src/.clang-format. Use a recent clang-format-3.5 to format automatically. | |
9 | - Braces on new lines for namespaces, classes, functions, methods. | |
10 | - Braces on the same line for everything else. | |
11 | - 4 space indentation (no tabs) for every block except namespaces. | |
12 | - No indentation for public/protected/private or for namespaces. | |
13 | - No extra spaces inside parenthesis; don't do ( this ) | |
14 | - No space after function names; one space after if, for and while. | |
86fe1b86 | 15 | |
2887bffc PW |
16 | Block style example: |
17 | ```c++ | |
18 | namespace foo | |
19 | { | |
20 | class Class | |
21 | { | |
22 | bool Function(char* psz, int n) | |
23 | { | |
24 | // Comment summarising what this section of code does | |
25 | for (int i = 0; i < n; i++) { | |
26 | // When something fails, return early | |
27 | if (!Something()) | |
28 | return false; | |
29 | ... | |
30 | } | |
2341e9b5 | 31 | |
2887bffc PW |
32 | // Success return is usually at the end |
33 | return true; | |
34 | } | |
35 | } | |
36 | } | |
86fe1b86 | 37 | ``` |
2341e9b5 | 38 | |
8414cb04 WL |
39 | Doxygen comments |
40 | ----------------- | |
41 | ||
42 | To facilitate the generation of documentation, use doxygen-compatible comment blocks for functions, methods and fields. | |
43 | ||
44 | For example, to describe a function use: | |
45 | ```c++ | |
46 | /** | |
47 | * ... text ... | |
48 | * @param[in] arg1 A description | |
49 | * @param[in] arg2 Another argument description | |
50 | * @pre Precondition for function... | |
51 | */ | |
52 | bool function(int arg1, const char *arg2) | |
53 | ``` | |
54 | A complete list of `@xxx` commands can be found at http://www.stack.nl/~dimitri/doxygen/manual/commands.html. | |
55 | As Doxygen recognizes the comments by the delimiters (`/**` and `*/` in this case), you don't | |
7e6d23b1 | 56 | *need* to provide any commands for a comment to be valid; just a description text is fine. |
8414cb04 WL |
57 | |
58 | To describe a class use the same construct above the class definition: | |
59 | ```c++ | |
4fbfebea | 60 | /** |
8414cb04 WL |
61 | * Alerts are for notifying old versions if they become too obsolete and |
62 | * need to upgrade. The message is displayed in the status bar. | |
63 | * @see GetWarnings() | |
64 | */ | |
65 | class CAlert | |
66 | { | |
67 | ``` | |
68 | ||
69 | To describe a member or variable use: | |
70 | ```c++ | |
71 | int var; //!< Detailed description after the member | |
72 | ``` | |
73 | ||
74 | Also OK: | |
75 | ```c++ | |
76 | /// | |
77 | /// ... text ... | |
78 | /// | |
79 | bool function2(int arg1, const char *arg2) | |
80 | ``` | |
81 | ||
82 | Not OK (used plenty in the current source, but not picked up): | |
83 | ```c++ | |
84 | // | |
85 | // ... text ... | |
86 | // | |
87 | ``` | |
88 | ||
89 | A full list of comment syntaxes picked up by doxygen can be found at http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html, | |
90 | but if possible use one of the above styles. | |
91 | ||
3bf5f528 MF |
92 | Development tips and tricks |
93 | --------------------------- | |
94 | ||
95 | **compiling for debugging** | |
96 | ||
97 | Run configure with the --enable-debug option, then make. Or run configure with | |
98 | CXXFLAGS="-g -ggdb -O0" or whatever debug flags you need. | |
99 | ||
100 | **debug.log** | |
101 | ||
102 | If the code is behaving strangely, take a look in the debug.log file in the data directory; | |
103 | error and debugging messages are written there. | |
104 | ||
73280baa | 105 | The -debug=... command-line option controls debugging; running with just -debug or -debug=1 will turn |
3bf5f528 MF |
106 | on all categories (and give you a very large debug.log file). |
107 | ||
3bf5f528 MF |
108 | **testnet and regtest modes** |
109 | ||
0fa4dcc0 | 110 | Run with the -testnet option to run with "play zcash" on the test network, if you |
3bf5f528 MF |
111 | are testing multi-machine code that needs to operate across the internet. |
112 | ||
113 | If you are testing something that can run on one machine, run with the -regtest option. | |
114 | In regression test mode, blocks can be created on-demand; see qa/rpc-tests/ for tests | |
115 | that run in -regtest mode. | |
116 | ||
117 | **DEBUG_LOCKORDER** | |
118 | ||
0fa4dcc0 | 119 | Zcash is a multithreaded application, and deadlocks or other multithreading bugs |
3bf5f528 MF |
120 | can be very difficult to track down. Compiling with -DDEBUG_LOCKORDER (configure |
121 | CXXFLAGS="-DDEBUG_LOCKORDER -g") inserts run-time checks to keep track of which locks | |
122 | are held, and adds warnings to the debug.log file if inconsistencies are detected. | |
123 | ||
2341e9b5 | 124 | Locking/mutex usage notes |
8414cb04 | 125 | ------------------------- |
2341e9b5 | 126 | |
127 | The code is multi-threaded, and uses mutexes and the | |
128 | LOCK/TRY_LOCK macros to protect data structures. | |
129 | ||
130 | Deadlocks due to inconsistent lock ordering (thread 1 locks cs_main | |
131 | and then cs_wallet, while thread 2 locks them in the opposite order: | |
132 | result, deadlock as each waits for the other to release its lock) are | |
133 | a problem. Compile with -DDEBUG_LOCKORDER to get lock order | |
134 | inconsistencies reported in the debug.log file. | |
135 | ||
136 | Re-architecting the core code so there are better-defined interfaces | |
137 | between the various components is a goal, with any necessary locking | |
138 | done by the components (e.g. see the self-contained CKeyStore class | |
139 | and its cs_KeyStore lock for example). | |
140 | ||
2341e9b5 | 141 | Threads |
8414cb04 | 142 | ------- |
2341e9b5 | 143 | |
25ef6ace | 144 | - ThreadScriptCheck : Verifies block scripts. |
145 | ||
146 | - ThreadImport : Loads blocks from blk*.dat files or bootstrap.dat. | |
147 | ||
2341e9b5 | 148 | - StartNode : Starts other threads. |
149 | ||
25ef6ace | 150 | - ThreadDNSAddressSeed : Loads addresses of peers from the DNS. |
151 | ||
152 | - ThreadMapPort : Universal plug-and-play startup/shutdown | |
2341e9b5 | 153 | |
3985a40d | 154 | - ThreadSocketHandler : Sends/Receives data from peers on port 8233. |
25ef6ace | 155 | |
156 | - ThreadOpenAddedConnections : Opens network connections to added nodes. | |
157 | ||
2341e9b5 | 158 | - ThreadOpenConnections : Initiates new connections to peers. |
159 | ||
25ef6ace | 160 | - ThreadMessageHandler : Higher-level message handling (sending and receiving). |
161 | ||
162 | - DumpAddresses : Dumps IP addresses of nodes to peers.dat. | |
2341e9b5 | 163 | |
164 | - ThreadFlushWalletDB : Close the wallet.dat file if it hasn't been used in 500ms. | |
25ef6ace | 165 | |
3985a40d | 166 | - ThreadRPCServer : Remote procedure call handler, listens on port 8232 for connections and services them. |
25ef6ace | 167 | |
0fa4dcc0 | 168 | - ZcashMiner : Generates zcash (if wallet is enabled). |
25ef6ace | 169 | |
170 | - Shutdown : Does an orderly shutdown of everything. | |
3bf5f528 MF |
171 | |
172 | Pull Request Terminology | |
173 | ------------------------ | |
174 | ||
7e6d23b1 | 175 | Concept ACK - Agree with the idea and overall direction, but have neither reviewed nor tested the code changes. |
3bf5f528 MF |
176 | |
177 | utACK (untested ACK) - Reviewed and agree with the code changes but haven't actually tested them. | |
178 | ||
179 | Tested ACK - Reviewed the code changes and have verified the functionality or bug fix. | |
180 | ||
181 | ACK - A loose ACK can be confusing. It's best to avoid them unless it's a documentation/comment only change in which case there is nothing to test/verify; therefore the tested/untested distinction is not there. | |
182 | ||
7e6d23b1 | 183 | NACK - Disagree with the code changes/concept. Should be accompanied by an explanation. |