# CPU Mining with Getrue To mine at truechain, you'll just need truechain client, Getrue. <br/> _**NOTE:** Ensure your blockchain is fully synchronised with the main chain before starting to mine, otherwise you will not be mining on the main chain._ <br/> When you start up your truechain node with `getrue` it is not mining by default. To start it in mining mode, you use the `--mine` [command line option](https://github.com/truechain/truechain-engineering-code/wiki/Command-Line-Options). The `-minerthreads` parameter can be used to set the number parallel mining threads (defaulting to the total number of processor cores). `getrue --mine --minerthreads=4` <br/> only mine fruit `getrue --minefruit --minerthreads=4` <br/> You can also start and stop CPU mining at runtime using the console. `miner.start` takes an optional parameter for the number of miner threads. ``` > miner.start(8) null > miner.stop() true ``` or ``` > miner.startFruit(8) null > miner.stop() true ``` <br/> Note that mining for real TRUE only makes sense if you are in sync with the network (since you mine on top of the consensus block). Therefore the truechain downloader/synchroniser will delay mining until syncing is complete, and after that mining automatically starts unless you cancel your intention with `miner.stop()`. <br/> In order to earn TRUE you must have your **coinbase** address set. This coinbase defaults to your [primary account](https://github.com/truechain/truechain-engineering-code/wiki/Managing-your-accounts). If you don't have an coinbase address, then `Getrue --mine` or `Getrue --minefruit`will not start up. <br/> You can set your coinbase on the command line: ``` getrue --coinbase '0xa4d8e9cae4d04b093aac82e6cd355b6b963fb7ff' --mine 2>> getrue.log ``` or ``` getrue --coinbase '0xa4d8e9cae4d04b093aac82e6cd355b6b963fb7ff' --minefruit 2>> getrue.log ``` <br/> You can reset your coinbase on the console too: ``` miner.setCoinbase(etrue.accounts[2]) ``` <br/> Note that your coinbase does not need to be an address of a local account, just an existing one. <br/> There is an option to add extra Data (32 bytes only) to your mined blocks. By convention this is interpreted as a unicode string, so you can set your short vanity tag. ``` miner.setExtra("trueminer") ... debug.printBlock(131805) BLOCK(be465b020fdbedc4063756f0912b5a89bbb4735bd1d1df84363e05ade0195cb1): Size: 531.00 B TD: 643485290485 { NoNonce: ee48752c3a0bfe3d85339451a5f3f411c21c8170353e450985e1faab0a9ac4cc Header: [ ... Miner: a4d8e9cae4d04b093aac82e6cd355b6b963fb7ff Number: 131805 Extra: trueminer ... } ``` You can check your hashrate with miner.getHashRate(), the result is in H/s (Hash operations per second). ``` > miner.getHashRate() 30 ``` <br/> After you successfully mined some blocks, you can check the true balance of your coinbase account. Now assuming your coinbase is a local account: ``` > etrue.getBalance(etrue.coinbase).toNumber(); '34698870000000' ``` <br/> In order to spend your earnings you will need to have this account unlocked. ``` > personal.unlockAccount(etrue.coinbase) Password true ``` <br/> You can check which blocks are mined by a particular miner (address) with the following code snippet on the console: ``` function minedBlocks(lastn, addr) { addrs = []; if (!addr) { addr = etrue.coinbase } limit = etrue.blockNumber - lastn for (i = etrue.blockNumber; i >= limit; i--) { if (etrue.getBlock(i).miner == addr) { addrs.push(i) } } return addrs } // scans the last 1000 blocks and returns the blocknumbers of blocks mined by your coinbase // (more precisely blocks the mining reward for which is sent to your coinbase). minedBlocks(1000, etrue.coinbase); //[352708, 352655, 352559] ``` <br/> Note that it will happen often that you find a block yet it never makes it to the canonical chain. This means when you locally include your mined block, the current state will show the mining reward credited to your account, however, after a while, the better chain is discovered and we switch to a chain in which your block is not included and therefore no mining reward is credited. Therefore it is quite possible that as a miner monitoring their coinbase balance will find that it may fluctuate quite a bit. <br/> Mining success depends on the set block difficulty. Block difficulty dynamically adjusts each block in order to regulate the network hashing power to produce a 10 minute blocktime. Your chances of finding a block therefore follows from your hashrate relative to difficulty. The time you need to wait you are expected to find a block can be estimated with the following code: <br/> **INCORRECT...CHECKING** ``` etrue = etrue.getBlock("latest").difficulty/miner.getHashRate(); // estimated time in seconds Math.floor(etrue / 3600.) + "h " + Math.floor((etrue % 3600)/60) + "m " + Math.floor(etrue % 60) + "s"; // 1h 3m 30s ``` <br/>