从 getwork 函数计算哈希 - 如何做?

发布于 2024-11-14 02:16:35 字数 961 浏览 4 评论 0原文

当我在 bitcoind 服务器上调用 getwork 时,我得到以下信息:

./bitcoind getwork
{
    "midstate" : "695d56ae173bbd0fd5f51d8f7753438b940b7cdd61eb62039036acd1af5e51e3",
    "data" : "000000013d9dcbbc2d120137c5b1cb1da96bd45b249fd1014ae2c2b400001511000000009726fba001940ebb5c04adc4450bdc0c20b50db44951d9ca22fc5e75d51d501f4deec2711a1d932f00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000",
    "hash1" : "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000",
    "target" : "00000000000000000000000000000000000000000000002f931d000000000000"
}

该协议似乎没有记录。如何根据这些数据计算哈希值。我认为这些数据是小端的。那么第一步是将所有内容转换为大端?完成后,我计算数据的 sha256。数据可以分为两个块,每个块 64 字节。第一个卡盘的哈希值由中间状态给出,因此不必计算。

因此,我必须使用 sha256 对 chunk #2 进行哈希处理,并使用中间状态作为初始哈希值。完成后,我最终得到了块 2 的哈希值,即 32 字节。我再次计算该块的哈希值以获得最终哈希值。

然后,我是否将所有内容都转换为小端并提交工作?

hash1有什么用?

when I call getwork on my bitcoind server, I get the following:

./bitcoind getwork
{
    "midstate" : "695d56ae173bbd0fd5f51d8f7753438b940b7cdd61eb62039036acd1af5e51e3",
    "data" : "000000013d9dcbbc2d120137c5b1cb1da96bd45b249fd1014ae2c2b400001511000000009726fba001940ebb5c04adc4450bdc0c20b50db44951d9ca22fc5e75d51d501f4deec2711a1d932f00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000",
    "hash1" : "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000",
    "target" : "00000000000000000000000000000000000000000000002f931d000000000000"
}

This protocol does not seem to be documented. How do I compute the hash from this data. I think that this data is in little endian. So the first step is to convert everything to big endian? Once that is done, I calculate the sha256 of the data. The data can be divided in two chuncks of 64 bytes each. The hash of the first chuck is given by midstate and therefore does not have to be computed.

I must therefore hash the chunck #2 with sha256, using the midstate as the initial hash values. Once that is done, I end up with a hash of chunk 2, which is 32 bytes. I calculate the hash of this chunk one more time to get a final hash.

Then, do I convert everything to little endian and submit the work?

What is hash1 used for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光与爱终年不遇 2024-11-21 02:16:35

哈希计算记录在区块哈希算法中。
从相对简单的基础知识开始。基本数据结构记录在协议规范 - 比特币维基中。请注意,协议定义(以及工作的定义)或多或少假设 SHA-256 哈希值是 256 位小端值,而不是标准所暗示的大端值。另请参阅

Getwork 更复杂,并且会遇到更严重的字节序/字节顺序混乱。

首先请注意,getwork API 经过优化,可以加快挖掘的初始步骤。
midstate 和 hash1 值用于这些性能优化,可以忽略。看看“数据”就知道了。
当使用标准 sha256 实现时,仅对“数据”的前 80 个字节(160 个十六进制字符)进行哈希处理。

不幸的是,getwork 数据结构中呈现的 JSON 数据具有与上面的块示例中的散列所需的不同的字节序特征。

他们都说要到源代码中寻找答案,但 C++ 源代码可能很大且令人困惑。一个简单的替代方案是 poold.py 代码。这里有对此的讨论:用于测试的新矿池。您只需查看“checkwork”例程的前几行以及“bufreverse”和“bytereverse”函数,即可获得正确的字节顺序。最后,只需将数据的每个 32 位段中的字节进行反转即可。是的——很奇怪。但字节序问题很棘手,可能会以这种方式结束……

有关“getwork”工作方式的其他一些有用信息可以在以下讨论中找到:

请注意,在最初的比特币论坛变得非常困难,现在有一个SE 网站

The hash calculation is documented at Block hashing algorithm.
Start there for the relatively simple basics. The basic data structures are documented in Protocol specification - Bitcoin Wiki. Note that the protocol definition (and the definition of work) more or less assumes that SHA-256 hashes are 256-bit little-endian values, rather than big-endian as the standard implies. See also

Getwork is more complicated and runs into more serious endian/byte ordering confusion.

First note that the getwork API is optimized to speed up the initial steps of mining.
The midstate and hash1 values are for these performance optimizations and can be ignored. Just look at the "data".
And when a standard sha256 implementation is used, only the first 80 bytes (160 hex characters) of the "data" are hashed.

Unfortunately, the JSON data presented in the getwork data structure has different endian characteristics than what is needed for hashing in the block example above.

They all say to go to the source for the answer, but the C++ source can be big and confusing. A simple alternative is the poold.py code. There is discussion of it here: New mining pool for testing. You only need to look at the first few lines of the "checkwork" routine, and the "bufreverse" and "bytereverse" functions, to get the byte ordering right. In the end it is just a matter of doing a reversal of the bytes in each 32-bit segment of the data. Yes - very odd. But endian issues are tricky and can end up that way....

Some other helpful information on the way "getwork" works can be found in discussions at:

Note that finding the signal to noise in the original Bitcoin forum is getting very hard, and there is now an SE site for this.

清醇 2024-11-21 02:16:35

听起来不错,javascript中有一个脚本可以计算哈希值,但我不完全理解它,所以我不知道,如果你看一下,也许你会更好地理解它。

this.tryHash = function(midstate, half, data, hash1, target, nonce){  
    data[3] = nonce;
    this.sha.reset();

    var h0 = this.sha.update(midstate, data).state;   // compute first hash
    for (var i = 0; i < 8; i++) hash1[i] = h0[i];   // place it in the h1 holder
    this.sha.reset();                 // reset to initial state
    var h = this.sha.update(hash1).state;       // compute final hash
    if (h[7] == 0) {
      var ret = [];
      for (var i = 0; i < half.length; i++)
        ret.push(half[i]);
      for (var i = 0; i < data.length; i++)
        ret.push(data[i]);
      return ret;
    } else return null;
  };

来源:https://github。 com/jwhitehorn/jsMiner/blob/4fcdd9042a69b309035dfe9c9ddf716119831a16/engine.js#L149-165

It sounds right, there is a script in javascript that do calculate the hash but I do not fully understand it so I don't know, maybe you understand it better if you look.

this.tryHash = function(midstate, half, data, hash1, target, nonce){  
    data[3] = nonce;
    this.sha.reset();

    var h0 = this.sha.update(midstate, data).state;   // compute first hash
    for (var i = 0; i < 8; i++) hash1[i] = h0[i];   // place it in the h1 holder
    this.sha.reset();                 // reset to initial state
    var h = this.sha.update(hash1).state;       // compute final hash
    if (h[7] == 0) {
      var ret = [];
      for (var i = 0; i < half.length; i++)
        ret.push(half[i]);
      for (var i = 0; i < data.length; i++)
        ret.push(data[i]);
      return ret;
    } else return null;
  };

SOURCE: https://github.com/jwhitehorn/jsMiner/blob/4fcdd9042a69b309035dfe9c9ddf716119831a16/engine.js#L149-165

简单气质女生网名 2024-11-21 02:16:35

坦白说
任何来源都没有正式描述比特币块哈希算法。


哈希计算记录在块哈希算法中。

应该阅读

哈希计算在块哈希算法中“描述”。

en.bitcoin.it/wiki/Block_hashing_algorithm

顺便说一句,PHP 中的示例代码存在一个错误(打字错误) )
Python 中的示例代码在 Windows XP 32 的 Python3.3 上运行时会生成错误
(缺少对 string.decode 的支持)

Frankly speaking
Bitcoin block hashing algorithm is not officially described by any source.

"
The hash calculation is documented at Block hashing algorithm.
"
should read

The hash calculation is "described" at Block hashing algorithm.

en.bitcoin.it/wiki/Block_hashing_algorithm

btw the example code in PHP comes with a bug (typo)
the example code in Python generates errors when run by Python3.3 for Windows XP 32
(missing support for string.decode)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文