Author

Topic: [XPM] Mining Explanation Bounty: 1BTC :) (Read 892 times)

member
Activity: 75
Merit: 10
July 17, 2013, 10:20:02 PM
#7
For the process of submitting data to primecoind, I would reference the submitBlock function in the same file on line 346.  Critical to that function is the parseHex function, which converts the parameter from a string to hexidecimal (i.e. the string "10c8e" would become the number 10c8e or 68750 in decimal), the constructor to the CDataStream class, found in serialize.h, and the ProcessBlock function found in main.cpp (line 2236).

From the looks of it the parseHex function is just a simple utility function and the ProcessBlock function just goes through and makes all the appropriate checks to prevent DoS attacks and to make sure the block is valid.  The real magic of submitting the block is in getting the right hex data, in the right order.  For that, I would look at how CDataStream interacts with CBlock, as the former is just a holding cell for the latter (hex data is put into the CDataStream and then fed into the CBlock object, as is shown on line 359 of rpcmining.cpp.  CBlock is defined on line 1340 of main.h.

The relevant function is defined on line 1362 (IMPLEMENT_SERIALIZE), although it is defined by macros.  Looking at these macros (defined in serialize.h, lines 55-93) it would appear that you first must give the block header, followed by a vector of transactions.  The block header is defined in main.h on line 1266 and should have the following fields in order: Version, previous block hash, hash of the Merkle root, time, "nBits" (the target, defined in prime.cpp), the nonce, and finally the prime chain multiplier (i.e. hash * multiplier = origin of the prime chain).  Each item in the vector of transactions should contain, in order: Version, a vector of inputs (CTxIn), a vector of outputs (CTxOut), and finally "nLockTime" has minimal documentation but it says it was implemented in (presumably Bitcoin) version 0.1.6.

This hierarchy continues, where each object that has data members that are, of themselves, objects, until you get to the point of primitives.  CTxIn, for example, has COutPoint prevout (object), then CScript scriptSig (object), and finally nSequence (an integer--primitive).  The order is seen for these on line 347 of main.h as the order of the READWRITE lines.  One can go deeper by finding where COutPoint and CScript are stored and looking at their READWRITE definitions in IMPLEMENT_SERIALIZE.

When one finishes crawling this tree of IMPLEMENT_SERIALIZE functions you get to the point where you have a really long sequence of data that must be submitted.  If you collect all of that data in the right order, represented as its hex values and you pass it to the submitblock function (e.g. primecoind submitblock ) then it will go about checking to make sure that the prime chain is long enough and, if it is, it should broadcast it through the network.



For the process of getting data from primecoind, I would reference the getwork function in rpcmining.cpp on line 86.  Based on its documentation it seems that it should return formatted hash data to work on.  However, every attempt I've made to run "getwork" on my windows QT machine has resulted in the client crashing.  On linux, on the other hand, it nicely returns midstate, data, hash1, and target as separate fields in a JSON format (similar to the output of getmininginfo, but with different fields).  I believe that hash1 and midstate are not needed and are holdovers from the Bitcoin roots of the client.  Target is the difficulty, although its format is a little bit odd (it is little-endian, which means that the least significant digit is first.  Data is a 128 byte array that contains the hash of the version, hash of the previous block, hash of the Merkel root, the time, the difficulty, the nonce, "block", and hash1.  I believe this is the order that the data is listed, and I believe that this is sufficient data to find the hash needed for the proof of work (if one of the hashes already contained isn't already the hash needed). 

I've exhausted my skill (at least until I can get some sleep) as far as looking at the getwork and submitBlock commands are concerned.  There's plenty of fun C/C++ tricks going on and I only hardly know C/C++.  I hope that this is useful towards the development of a GPU miner.  If you're feeling generous and this helped, I can receive donations/bounties at (BTC)1FbWVR3LpGoWp5rc4SJc3cHo42ALrYhcxC or (XPM) AFozUk53j5uZCrKAhkcrM6SjnKtGRXCnov.  I will, of course, attempt to answer any questions about this, although all I know is what I've read and this is a much larger project than I've ever worked with.
legendary
Activity: 1834
Merit: 1020
...bounty?
legendary
Activity: 1834
Merit: 1020
...

Primecoin uses a (almost) linearly-adjusting difficulty algorithm for a chain of length k.  Difficulty is determined by dividing a given prime chain's length by the remainder of the results of a Fermat test.

If prime chain = P0, P1, P2....Pk-1, length = k, and r = test results, then difficulty = Pk/r

Then, compute prime chain length with a fractional length segment...

d = k + (pk-r)/pk

If prime chain length exhibits probable primality, it is a chain of higher integral length.  Difficulty then adjusts according to integral boundaries.

The chain is linked with the block header under the requirement that it is origin is divisible by the block header hash.  What results is a proof-of-work certificate that prevents old work from being reused for future blocks.



Well, I learned a bit about Primecoin just now.  Don't know if I answered your question though lol.
sr. member
Activity: 252
Merit: 250
replied https://bitcointalksearch.org/topic/m.2745077

BTC addy if accepted 13YGmE2CCAxWpAhqQSm5NVz9pU7Q4B22jm
legendary
Activity: 1713
Merit: 1029

Unfortunately since you only answered one, you are not eligible. However, if you answer the 2nd question I put in the other thread (a further question on the sieve), I'll take that question out of the bounty, and give you 0.25 BTC for your efforts. Cheers! Smiley
sr. member
Activity: 252
Merit: 250
legendary
Activity: 1713
Merit: 1029
If you provide a great explanation of all of the following (and are the first to do so), I will give you one BTC:
-What purpose the sieve serves
-What relation the block hash has to the potential block-solving primes
-Process of obtaining all mining data from primecoind
-process of submitting prime chain to mining data
Jump to: