Skip to content

How to compute a worker's hashrate | 如何计算矿工的哈希速率

YihaoPeng edited this page Sep 11, 2019 · 8 revisions

From https://github.com/btccom/btcpool/issues/130

Q:

How is possible to count hashrate of miners? I see in database only accepted/rejected shares. But as I know also share difficulty needed to count hashrate, insn't it?

A:

accepted/rejected shares themselves in database are the sum of all shares' difficulty in a period of time. So you just need the elapsed time to compute the hashrate. For Bitcoin, it is:

hashrate (hash/s) = accepted_share * 4294967296 (hash) / elapsed_time (s)

4294967296 is 2^32. It is a constant derived from Bitcoin's TargetToDifficulty function.

Example (for Bitcoin):

accept_1h = 1048576
hashrate = 1048576 * 4294967296 / (60 * 60) = 1250999896491 (hash/s) = 1.25 TH/s

Why the constant is 2^32?

Bitcoin's pool difficulty 1 (pdiff) is

0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

To achieve this target, you need to have a result of the hash function with a 32 bits leading 0, and subsequent 224 bits can be any value.

So we ignore the subsequent 224 bits, only consider the first 32 bits, then it can be regarded as a hash function with a 32bits output. At this point the problem translates to: given any input, get the specific output of the hash function (0x00000000).

Assuming that the value of the hash function is evenly distributed within the range, the probability of obtaining a particular output value B for any input A is 1/(2^32).

So when you try to change the input and compute the hash, after 2^32 attempts, the probability of getting a specific input 0x00000000 will be 1.

This does not guarantee that you will get that particular output, but after so many attempts, the probability that you will get the output is positive infinity (this is the meaning of probability 1).

Let's go back to the original question. Reaching 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF (difficulty 1) requires 2^32 hash operations. So if the share submitted by a miner reaches this difficulty, it can be considered that it has performed 2^32 hash operations.

So the constant is 2^32.

And if the miner submited a share for difficulty 1 in 10 seconds, its hashrate is: (2^32) hash / 10 s = 429496729.6 hash/s


For Ethereum, the constant is 1.

Our pool difficulty 1 of Ethereum is

0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

A example for Ethereum:

accept_1h = 1048576
hashrate = 1048576 * 1 / (60 * 60) = 291(hash/s)

The hash rate calculation method of DCR is exactly the same as Bitcoin.

Fields in the database

The following fields in the database can be used to calculate the hash rate:

-- in `bpool_local_db`.`mining_workers`
  `accept_1m` bigint(20) NOT NULL DEFAULT '0',
  `accept_5m` bigint(20) NOT NULL DEFAULT '0',
  `accept_15m` bigint(20) NOT NULL DEFAULT '0',
  `reject_15m` bigint(20) NOT NULL DEFAULT '0',
  `accept_1h` bigint(20) NOT NULL DEFAULT '0',
  `reject_1h` bigint(20) NOT NULL DEFAULT '0',

-- in `bpool_local_stats_db`.`stats_[pool|user|worker]_[hour|day]`
  `share_accept` bigint(20) NOT NULL DEFAULT '0',
  `share_reject` bigint(20) NOT NULL DEFAULT '0',

The value of these fields are the sum of the difficulty of shares over a period of time. The specific time period is reflected in the field name/table name.

Hashrate Computing Table

hashrate = share_diff_sum * factor / time
(hash/s) =   (non-unit)   * (hash) / (s)
Blockchain Algorithm Diff 1 (bits) Factor
BTC/BCH/UBTC/SBTC SHA256d 0x1d00ffff 2 ^ 32
ETH Ethash 0x2100ffff 1
DCR Blake256 0x1d00ffff 2 ^ 32
LTC Scrypt 0x1f00ffff 2 ^ 16
Grin Cuckaroo29 42
Cuckatoo31+ 42
ZCash EquiHash (n=144, k=5) 0x1f07ffff 2 ^ 13
Beam EquiHash (n=150, k=5) 1

A python version of BitsToTarget:

bits=0x187fffff
exp=bits >> 24
base = bits & 0x00ffffff
target = base * 2**(8*(exp-3))
print '%064x' % target