-
Notifications
You must be signed in to change notification settings - Fork 172
Block Checking
Currently the block validation is split among functions:
ProcessBlock
, CheckBlock
, AcceptBlock
and ConnectBlock
.
Block validation can be further split based on when they are performed and what data they require. Some can be done as soon as receiving a block, like format and version validation. Some checks can be done in-between. The most complex checks, like transaction, stake and reward validation, are done when attaching a block to the current best chain.
CheckBlock is not the right function to perform context-sensitive checks. Eg validating research reward requires reading blocks from the current best chain (previous stake of this cpid) and the current superblock. In CheckBlock, the previous block might not be accepted in main chain, the transactions are inaccurate, wrong superblocks loaded, cpid data is inaccurate. Even the prev block (according to bitcoin) might not even be available, that is for AcceptBlock, but gridcoin's orphan handling erases that.
CheckBlock should do some context-free checks, like formatting, max money, bounds checking, format checking. When CheckBlock fails, the block is not saved to index, to blockfile, not propagated, the sender banned and should be freed from memory. And CheckBlock should be fast and cheap to reduce ddos.
AcceptBlock saves it to index, blockfile and memory. In this function the prev block is already in index, but the transactions in it are not yet added to transaction index. Data from previous block related to cpid, superblock, beacon, message may not be in grc memory structures. The prev block may not be in main chain.
ConnectBlock adds transactions from a bloc to transaction index. Previous block is in index, on disk and it's transactions are in tx index. Gridcoin specific data should be processed here. Superblocks loaded and verified, dpor rewards verified, beacons processed.
Bevare that also DisConnectBlock exists. It must undo effect of ConnectBlock. Remeber that if superblock, beacon, dpor contract is disconnected, the previous one should be loaded (this is challenging).
todo