Skip to content

Commit 41b4db0

Browse files
committed
Fix CPUNet after rebase
* Update build instructions to use cmake * Update miner script due to test framework changes * Restore -ntasks parameter in bitcoin-utils (was commented out for some reason?)
1 parent d2fe514 commit 41b4db0

3 files changed

Lines changed: 39 additions & 42 deletions

File tree

contrib/cpunet/README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,46 @@ have `-cpunet` or they will fail.
1818

1919
1. Compile CPUNet:
2020

21-
./configure make -j16
21+
cmake -B build -DWITH_ZMQ=ON -DENABLE_IPC=ON -DENABLE_WALLET=OFF
22+
cmake --build build
2223

2324
2. Run cpunet node:
2425

25-
src/bitcoind -cpunet -zmqpubsequence=tcp://127.0.0.1:28338
26+
build/bin/bitcoind -cpunet -zmqpubsequence=tcp://127.0.0.1:28338
2627

2728
or
2829

2930
# if you have already created the wallet
30-
src/bitcoind -cpunet -wallet=cpunet -zmqpubsequence=tcp://127.0.0.1:28338
31+
build/bin/bitcoind -cpunet -wallet=cpunet -zmqpubsequence=tcp://127.0.0.1:28338
3132

3233
3. Create and open a wallet: (only needs to be done once)
3334

34-
src/bitcoin-cli -cpunet createwallet cpunet
35-
src/bitcoin-cli -cpunet loadwallet cpunet
35+
build/bin/bitcoin-cli -cpunet createwallet cpunet
36+
build/bin/bitcoin-cli -cpunet loadwallet cpunet
3637

3738
4. Generate blocks:
3839

39-
contrib/cpunet/miner --cli=src/bitcoin-cli --ongoing --address `src/bitcoin-cli -cpunet getnewaddress` --grind-cmd="src/bitcoin-util -cpunet -ntasks=1 grind"
40+
contrib/cpunet/miner --cli=build/bin/bitcoin-cli --ongoing --address `build/bin/bitcoin-cli -cpunet getnewaddress` --grind-cmd="build/bin/bitcoin-util -cpunet -ntasks=1 grind"
4041

4142
TODO
4243
====
4344

44-
1. Set up a seed node so we can find peers
45-
46-
2. Create a docker container to run this
47-
48-
3. Add an argument -my-block-latency artificial latency parameter that will
45+
1. Add an argument -my-block-latency artificial latency parameter that will
4946
delay starting mining a new block when the block found is mine.
5047

51-
4. Add an argument -other-block-latency artificial latency parameter that will
48+
2. Add an argument -other-block-latency artificial latency parameter that will
5249
delay starting mining a new block when a new block is found by another miner
5350
and announced to contrib/cpunet/miner via ZMQ. (Related to "Clean Jobs"
5451
stratum parameter)
5552

56-
5. Add SV2 support and use an external SV2 miner instead of bitcoin-utils
53+
3. Add SV2 support and use an external SV2 miner instead of bitcoin-utils
5754

58-
6. Add a -share-difficulty argument to mine shares of lower difficulty than the
55+
4. Add a -share-difficulty argument to mine shares of lower difficulty than the
5956
block target
6057

61-
7. Add a -share-cmd to submit shares to a pool.
58+
5. Add a -share-cmd to submit shares to a pool.
6259

63-
8. The way I changed the PoW hash means that it screws up mainnet, signet, and
60+
6. The way I changed the PoW hash means that it screws up mainnet, signet, and
6461
the testnets and assertions have to be commented out. Therefore this cannot
6562
be merged into Bitcoin unless we find a way to detect `-cpunet` and calculate
6663
the block hash differently. I don't see a way to get the network parameters

contrib/cpunet/miner

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ logging.basicConfig(
3838
# "cpunet" to the end of the block header and hash that instead. It should match
3939
# src/primitives/block.cpp:13, CBlockHeader::GetHash()
4040
# This is copied/modifed from test/functional/test_framework/messages.py:732
41-
def CBlockHeader_calc_sha256(self):
42-
if self.sha256 is None:
43-
r = b""
44-
r += self.nVersion.to_bytes(4, "little", signed=True)
45-
r += ser_uint256(self.hashPrevBlock)
46-
r += ser_uint256(self.hashMerkleRoot)
47-
r += self.nTime.to_bytes(4, "little")
48-
r += self.nBits.to_bytes(4, "little")
49-
r += self.nNonce.to_bytes(4, "little")
50-
r += b"cpunet\x00"
51-
self.sha256 = uint256_from_str(hash256(r))
52-
self.hash = hash256(r)[::-1].hex()
53-
54-
CBlock.old_calc_sha256 = CBlock.calc_sha256
55-
CBlockHeader.calc_sha256 = CBlockHeader_calc_sha256
41+
def CBlockHeader_hash_hex(self):
42+
r = self._serialize_header()
43+
r += b"cpunet\x00"
44+
return hash256(r)[::-1].hex()
45+
46+
def CBlockHeader_hash_int(self):
47+
r = self._serialize_header()
48+
r += b"cpunet\x00"
49+
return uint256_from_str(hash256(r))
50+
51+
CBlockHeader.old_hash_hex = CBlockHeader.hash_hex
52+
CBlockHeader.hash_hex = CBlockHeader_hash_hex
53+
54+
CBlockHeader.old_hash_int = CBlockHeader.hash_int
55+
CBlockHeader.hash_int = CBlockHeader_hash_int
5656

5757
class CPUMiner():
5858
def __init__(self, args):
@@ -183,7 +183,6 @@ class CPUMiner():
183183
script_BIP34_coinbase_height(tmpl["height"]), 0xffffffff)]
184184
cbtx.vout = [CTxOut(tmpl["coinbasevalue"], self.reward_spk)]
185185
cbtx.vin[0].nSequence = 2**32-2
186-
cbtx.rehash()
187186

188187
# Construct block
189188
block = CBlock()
@@ -221,17 +220,18 @@ class CPUMiner():
221220
self.mined_blocks += 1
222221
newhead = from_hex(CBlockHeader(), stdout.decode('utf8'))
223222
block.nNonce = newhead.nNonce
224-
block.rehash()
225223

226224
# Submit block
227225
r = await self.bitcoin_cli("-stdin", "submitblock", input=block.serialize().hex().encode('utf8'))
228226
logging.debug("Submitting block: %s", block.serialize().hex())
229227
if r != "":
230-
logging.warning("submitblock returned %s for height %d hash %s", r, tmpl["height"], block.hash)
228+
logging.warning("submitblock returned %s for height %d hash %s",
229+
r, tmpl["height"], block.hash_hex())
231230

232231
# Report
233-
logging.info("Mined a block solution at height %d with hash: %s"%(tmpl["height"], block.hash))
234-
logging.debug("Block hash %s payout to %s", block.hash, self.reward_addr)
232+
logging.info("Mined a block solution at height %d with hash: %s"%(tmpl["height"],
233+
block.hash_hex()))
234+
logging.debug("Block hash %s payout to %s", block.hash_hex(), self.reward_addr)
235235
logging.debug("Solved block is %s", block)
236236

237237
# Run the next iteration

src/bitcoin-util.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
132132

133133
std::vector<std::thread> threads;
134134
uint16_t n_tasks = std::max(1u, std::thread::hardware_concurrency());
135-
// if(gArgs.IsArgSet("-ntasks")) {
136-
// if(!ParseUInt16(gArgs.GetArg("-ntasks", "1"), &n_tasks)) {
137-
// strPrint = strprintf("Argument to -ntasks should be a number, found %s\n", *gArgs.GetArg("-ntasks"));
138-
// return EXIT_FAILURE;
139-
// }
140-
// }
135+
if(gArgs.IsArgSet("-ntasks")) {
136+
if(!ParseUInt16(gArgs.GetArg("-ntasks", "1"), &n_tasks)) {
137+
strPrint = strprintf("Argument to -ntasks should be a number, found %s\n", *gArgs.GetArg("-ntasks"));
138+
return EXIT_FAILURE;
139+
}
140+
}
141141
threads.reserve(n_tasks);
142142
for (int i = 0; i < n_tasks; ++i) {
143143
threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));

0 commit comments

Comments
 (0)