0

I'm creating an AI controlled bitcoin miner but I'm having trouble manually & locally creating a midstate function to be used for optimizing the mining process. Basically I've created independent functions that handle the steps involved in mining bitcoin manually.... all except the midstate creation function - inline std::string GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);.

I've searched everywhere and have been unable to find a detailed implementation guide and I've also been unsuccessful in doing it myself. The closest I came to understanding the logic behind it was THIS - of which my untested attempt has been wiped along with my laptop and android. Just got a Desktop and I REALLY NEED HELP!!!!

All I really need is an accurate implementation of the midstate function using C++

#include <iostream>
#include ALL LIBRARIES USED IN SOLUTION CODE

// DD - Declare and Define
// DD Variable, DD Function, DD Class, DD Struct ....
// Got the idea from Luffy from ONE PIECE - Gum Gum: Jet Pistol, Gum Gum: Gattling Gun ....
// Anyways .... MOVING ON


// Assume all data types are std::string
// This should help with faster debugging/testing of code/function

std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variables
  std::string midstate_hash;

  // Concatenate chunks of data -- 4 bytes, 32 bytes , 32 bytes
  midstate_hash = nVersion + nPrevHash + nMerkleRoot;

  // Then call sha256 to do whatever vodoo it does
  sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(midstate_hash);

  // ---- OR ----
  midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);


  // Then return string to caller function
  return midstate_hash;

}

// SAME FUNCTION BUT OPTIMIZED
std::string GenerateMidstate(std::string nVersion, std::string nPrevHash, std::string nMerkleRoot)
{
  // DD variable, Concatenate data and assign variable to the result of midstate function
  std::string midstate_hash = sha256_Magically_Transform_BitChunks_To_Usable_MIDSTATE(nVersion + nPrevHash + nMerkleRoot);

  return midstate_hash; // return midstate
}



// The implementation of the **sha256 midstate** generation can be done within same function, independent function e.g **SHA256_TRANSFORM_MIDSTATE()** or it can exist within an external header file.
// But please all header files used in creating a working function should be clearly stated and not be left to my imagination.. Please and THANKS in advance.


int main()
{
  std::string MIDSTATE, nVersion, nPrevHash, nMerkleRoot, nTime, nBits, nNonce;
  nVersion = 00000002;
  nPrevHash = "8975c5797431243........";
  nMerkleRoot = "ac363c68a97320f039ef4392ac2337754b65bfbb4790faf84d3a27c974c3ab31";
  nTime = "3ab1dd30";
  nBits = "1b9033c2";
  nNonce = "001b32bc";

  MIDSTATE = GenerateMidstate(nVersion, nPrevHash, nMerkleRoot);
  std::cout<< "Midstate of block data is "<< MIDSTATE << std::endl;
  std::cout << "Begin mining attempts" << std::endl;

  return 0;

}

At the end of the day the output should have successfully and accurately generated a midstate which obviously should not remain the same when merkleRootHash is changed/updated.

That's it guys... It's really that simple. The code template is as straight forward as shown above. The rest of the program of course is as technical as you can imagine (lol...)

And Oh.. please do feel free to point out any and all possible issue(s)(and their solutions) that might come up when converting std::string to uint32_t, int32_t, uint256 ..... I wouldn't mind optimizing my untested fix for the possible and inevitable incompatible conversion of string to BTYE error flags

This is really URGENT and extremely important to me. Thanks

ORIGINAL BITCOIN DATA TYPES

int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;

A side question which can be ignored... P.S please don't ignore

Q. If a midstate is made up of only (nVersion & nPrevHash) or (nPrevHash & nMerkleRoot) - will it still be an accurate midstate if hashing function called sha256(sha256(midstate, nMerkleRoot, nTime, nBits, nNonce)); or sha256(sha256(nVersion, midstate, nTime, nBits, nNonce));. Would such a midtate be valid??

2 Answers 2

1

Probably not the right stack exchange for this question. It is only tangentially related to bitcoin. You probably want Programming, or Computer Science.

0

You can check this github project for the midstate optimization code. The project is in C# but you can easily port to c++. Works well for existing blocks.

https://github.com/aseemgautam/bitcoin-sha256

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.