Go-Ethereum

如何通過 geth 控制台或 web3 獲取乙太坊區塊鏈的網路雜湊率

  • July 30, 2020

我想知道如何通過 geth 控制台或 web3 獲得乙太坊區塊鍊網路雜湊率。

就像這個 api 的結果一樣 - https://www.etherchain.org/api/miningEstimator

{
"blocktime":"14.4444282592862345",
"difficulty":"1846255676466184.9397",
"hashrate":"137494795284766.6526554599708515"
}

可以在此處找到獲取資訊的方式。所以通過控制台,

function getNetworkStats(
       sampleSize //!< [in] Larger n give more accurate numbers but with longer latency.
   ) {
   blockNum = eth.blockNumber; // Save this value to atomically get a block number.
   blockTime = (eth.getBlock(blockNum).timestamp - eth.getBlock(blockNum - sampleSize).timestamp) / sampleSize;
   difficulty = eth.getBlock(blockNum).difficulty; // You can sum up the last n-blocks and average; this is mathematically sound.

   return {
     "blocktime": blockTime,
     "difficulty": difficulty,
     "hashrate": difficulty / blockTime,
   };
}

值分別以秒、散列和每秒散列為單位。

我用python實現了上面的方法,感覺和全網算力的算力差不多!

eth = create_rpc("ETH")
current_height = int(eth.eth_blockNumber(), 16)
sampleSize = 200
current_block = eth.eth_getBlockByNumber(hex(current_height), False)
pre_height = eth.eth_getBlockByNumber(hex(current_height - sampleSize), False)
current_height_time = int(current_block.get("timestamp"), 16)
pre_height_time = int(pre_height.get("timestamp"), 16)

avg_block_time = (current_height_time - pre_height_time) / sampleSize
print(int(current_block.get("difficulty"), 16) / avg_block_time / 1000000000000)


result: 
177.7550919625694

引用自:https://ethereum.stackexchange.com/questions/34347