PPC switching between PoS and PoW

When I run PPC I have the option to run both ThreadBitcoinMiner() to mine PoW coins and ThreadStakeMinter() to mine PoS coins.

I cannot see in the source code where it decides to switch between mining PoW and PoS as these seem to be only selectable at the command prompt when the program is run.

Have I understood this correctly or can someone point me at what part of the code decides between the selection/priority of the two functions to generate the next block? Is this perhaps decided by the network rather than locally?

Do you mean in the ppc client? You should never pow mine with the client, because it runs on your cpu. The elctricity you spend would cost way more than the coins it would produce. pow mining is only worth it when you have up-to-date specialized asic hardware.

Thank you.

I’m not mining but trying to work out how PPC (the client or the network) decides whether to mine a PoW or PoS block for the next block, or indeed which is valid for the next block.

The client software can try to mint and mine at the same time.
If the minting thread finds a valid kernel from one of your mature transactions, it can generate a block with it.
If a mining thread finds a valid nonce, it can generate a block with it.

Whatever the last blocks of the blockchain are, the next block can be either a POS or a POW block, and the network will accept it as long as it passes the POS difficulty test for POS blocks or the POW difficulty test for POW blocks.

And if a node receives a POS block and a POW block at the same time as candidates for the next block, it will choose the POS block, because POS blocks have a higher ‘trust’ and the chain with the highest chain trust wins.

Thank you that’s a good explanation.

In the PPC source code is bnChainTrust applicable for proof-of-work blocks?

bnChainTrust is the global trust of the blockchain (the sum of the trusts of all the blocks in the blockchain).

Every proof-of-work block has a trust of 1.

The trust of a proof-of-stake block depends on the value of the difficulty when the block was found. The trust is approximately: difficulty * 2^32.
If a proof-of-stake block was generated now (with difficulty = 13), its trust would be 55834574848.

If you want to see in detail how it is done in the source code, you can check the functions GetBlockTrust in main.h and AddToBlockIndex in main.cpp.

Thanks again that’s very helpful.

So if I’m understanding this correctly PPC has a PoW component, which will kick in when no PoS blocks are being mined.

Can I check I have understood the following correctly:

When you say ‘difficulty’ what do you mean? Can you point me at where ‘difficulty’ is defined in the code? Is it network difficulty? If so would that mean if the hashing power applied to PPC goes up it becomes harder to successfully mine PoW blocks as PoS blocks have a higher weighting so are more readily accepted by the network?

From what I understood PoW is supposed to atrophy gracefully in PPC, but will never reach zero PoW. Is this correct or will PoW blocks one day no longer be valid on the network?

As PPC matures will block generation time remain roughly constant, I understand blocks are spaced at about every 10 minutes, but as the system matures what enforces that blocks are generated every 10 minutes on average by PoS? Or have I misunderstood something.

The likelihood of mining a block is how many coins you have over 30 days old. (From what I can tell this is true and it’s not calculated as say number of coins * number of days). If so why was this implementation decision taken? It means people can split their coins into multiple wallets to get a better spread of successfully mining PoS blocks. Unless I’ve misunderstood something here.

If a part of the blockchain has not been checkpointed and it contains one PoW block and then a chain of PoS blocks what’s to stop someone who holds a sizeable amount of coins (in different transactions) creating a new branch replacing the PoW block and orphaning that branch with their coin age, by spending multiple transactions to generate a new branch of blocks? If this is possible does it have any detrimental effect?

Do you know of any existing tools with which to look at the current block chain and check the ratios of PoW to PoS blocks going back over time?

[quote=“No_2, post:7, topic:3043”]1.
When you say ‘difficulty’ what do you mean? Can you point me at where ‘difficulty’ is defined in the code? Is it network difficulty? If so would that mean if the hashing power applied to PPC goes up it becomes harder to successfully mine PoW blocks as PoS blocks have a higher weighting so are more readily accepted by the network?[/quote]

Minting and mining each have their own difficulty.
When more coins are used to mint, the POS difficulty increases.
When more hashing power is used to mine, the POW difficulty increases.
And of course, the higher the difficulty, the harder it is to find a block.
In the source code, the difficulty of a block is calculated from the nBits variable of this block (GetBlockDifficulty function in main.h).

[quote=“No_2, post:7, topic:3043”]2.
From what I understood PoW is supposed to atrophy gracefully in PPC, but will never reach zero PoW. Is this correct or will PoW blocks one day no longer be valid on the network?[/quote]

The reward of a POW block decreases when the POW difficulty increases.
As the hashrate is supposed to increase with time (because of more efficient mining hardware), on average the POW reward is supposed to decline continuously, but that doesn’t change the validity of POW blocks.
The community could decide at some point to modify the protocol and disable POW blocks, but that’s not the case for the moment.

[quote=“No_2, post:7, topic:3043”]3.
As PPC matures will block generation time remain roughly constant, I understand blocks are spaced at about every 10 minutes, but as the system matures what enforces that blocks are generated every 10 minutes on average by PoS? Or have I misunderstood something.[/quote]

The POS difficulty adapts continuously so that the average time between two POS blocks is 10 minutes.

[quote=“No_2, post:7, topic:3043”]4.
The likelihood of mining a block is how many coins you have over 30 days old. (From what I can tell this is true and it’s not calculated as say number of coins * number of days). If so why was this implementation decision taken? It means people can split their coins into multiple wallets to get a better spread of successfully mining PoS blocks. Unless I’ve misunderstood something here.[/quote]

In fact the probability of minting a POS block depends on number_of_coins * day_weight, where: day_weight = min(days - 30, 60).
You can check the CheckStakeKernelHash function in kernel.cpp, it contains a few comments explaining the formula used for minting.
You can also see the probability of finding a POS block with the calculator at http://poscalculator.peercointalk.org.

[quote=“No_2, post:7, topic:3043”]5.
If a part of the blockchain has not been checkpointed and it contains one PoW block and then a chain of PoS blocks what’s to stop someone who holds a sizeable amount of coins (in different transactions) creating a new branch replacing the PoW block and orphaning that branch with their coin age? If this is possible does it have any detrimental effect?[/quote]

This attacker would also have to replace all the POS blocks after the POW blocks for his chain to try to become the main chain. It would require a huge amount of mature coins.
You can check the topic at http://www.peercointalk.org/index.php?topic=3141.0 which contains calculations on the probability of blockchain reorganization.

[quote=“No_2, post:7, topic:3043”]6.
Do you know of any existing tools with which to look at the current block chain and check the ratios of PoW to PoS blocks going back over time?[/quote]

There’s http://peerchain.net where you can see the ratio on the last 2016 blocks, as well as charts of inflation, rewards, number of POS or POW blocks, supply, difficulties, etc…

[quote=“glv, post:8, topic:3043”][quote=“No_2, post:7, topic:3043”]1.
When you say ‘difficulty’ what do you mean? Can you point me at where ‘difficulty’ is defined in the code? Is it network difficulty? If so would that mean if the hashing power applied to PPC goes up it becomes harder to successfully mine PoW blocks as PoS blocks have a higher weighting so are more readily accepted by the network?[/quote]

Minting and mining each have their own difficulty.
When more coins are used to mint, the POS difficulty increases.
When more hashing power is used to mine, the POW difficulty increases.
And of course, the higher the difficulty, the harder it is to find a block.
In the source code, the difficulty of a block is calculated from the nBits variable of this block (GetBlockDifficulty function in main.h).[/quote]

Did you mean GetDifficulty() in main.h?

The calculation is done in the GetBlockDifficulty function in main.h in the source code of Peerunity.
For Ppcoin, it is in the GetDifficulty function in bitcoinrpc.cpp.