libplanet icon indicating copy to clipboard operation
libplanet copied to clipboard

Prevent passing not-fully-instantiated `BlockChain<T>` to `IBlockPolicy<T>`

Open greymistcube opened this issue 4 years ago • 3 comments

Currently, IBlockChain<T>.GetNextBlockDifficulty() takes a BlockChain<T> object that is not fully instantiated when creating BlockChain<T> from scratch, i.e. empty. Since API specification of BlockChain<T> cannot be reliable while under instantiation, this should be prevented. As there are plenty of "special" implicit logic happening when dealing with a genesis block, separating out the handling of a genesis block entirely might be better for runtime safety.

greymistcube avatar Sep 10 '21 10:09 greymistcube

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

stale[bot] avatar Nov 09 '21 20:11 stale[bot]

The easiest way to fix it would be make IBlockChain<T>.GetNextBlockDifficulty()'s first parameter nullable:

https://github.com/planetarium/libplanet/blob/668f535f00eb2e5e55be0902473a3df8e3cf6bcb/Libplanet/Blockchain/Policies/IBlockPolicy.cs#L85-L94

Then pass null to the parameter when the chain is being initialized:

https://github.com/planetarium/libplanet/blob/668f535f00eb2e5e55be0902473a3df8e3cf6bcb/Libplanet/Blockchain/BlockChain.cs#L1350

A chain's emptiness can be determined by seeing BlockChain<T>.Count property. Note that there are already several conditionals like Count < 1 or Count == 0:

https://github.com/planetarium/libplanet/blob/668f535f00eb2e5e55be0902473a3df8e3cf6bcb/Libplanet/Blockchain/BlockChain.cs#L172

https://github.com/planetarium/libplanet/blob/668f535f00eb2e5e55be0902473a3df8e3cf6bcb/Libplanet/Blockchain/BlockChain.cs#L473-L479

https://github.com/planetarium/libplanet/blob/668f535f00eb2e5e55be0902473a3df8e3cf6bcb/Libplanet/Blockchain/BlockChain.cs#L504-L510

dahlia avatar Feb 28 '22 09:02 dahlia

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.

stale[bot] avatar Apr 29 '22 10:04 stale[bot]