libplanet
libplanet copied to clipboard
Prevent passing not-fully-instantiated `BlockChain<T>` to `IBlockPolicy<T>`
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.
This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.
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
This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.