|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; |
| 5 | + |
| 6 | +import {BlockHeader, BlockHeaderData} from "./libs/BlockHeader.sol"; |
| 7 | +import {BlocksStorage} from "./libs/BlocksStorage.sol"; |
| 8 | +import {TargetsStorage} from "./libs/targets/TargetsStorage.sol"; |
| 9 | +import {TargetsHelper} from "./libs/targets/TargetsHelper.sol"; |
| 10 | + |
| 11 | +contract SPVContract is Initializable { |
| 12 | + using BlockHeader for bytes; |
| 13 | + using BlocksStorage for BlocksStorage.BlocksData; |
| 14 | + using TargetsStorage for TargetsStorage.TargetsData; |
| 15 | + |
| 16 | + bytes32 public constant SPV_CONTRACT_STORAGE_SLOT = |
| 17 | + keccak256("spv.contract.spv.contract.storage"); |
| 18 | + |
| 19 | + error PrevBlockDoesNotExist(bytes32 prevBlockHash); |
| 20 | + error BlockAlreadyExists(bytes32 blockHash); |
| 21 | + |
| 22 | + error InvalidTarget(bytes32 blockTarget, bytes32 networkTarget); |
| 23 | + error InvalidBlockHash(bytes32 actualBlockHash, bytes32 blockTarget); |
| 24 | + error InvalidBlockTime(uint32 blockTime, uint32 medianTime); |
| 25 | + |
| 26 | + event BlockHeaderAdded(uint256 indexed blockHeight, bytes32 indexed blockHash); |
| 27 | + |
| 28 | + struct SPVContractStorage { |
| 29 | + BlocksStorage.BlocksData blocksData; |
| 30 | + TargetsStorage.TargetsData targets; |
| 31 | + uint256 pendingTargetHeightCount; |
| 32 | + } |
| 33 | + |
| 34 | + function __SPVContract_init( |
| 35 | + uint256 pendingBlockCount_, |
| 36 | + uint256 pendingTargetHeightCount_ |
| 37 | + ) external initializer { |
| 38 | + SPVContractStorage storage $ = _getSPVContractStorage(); |
| 39 | + |
| 40 | + $.blocksData.initialize(pendingBlockCount_); |
| 41 | + $.targets.initialize(); |
| 42 | + |
| 43 | + $.pendingTargetHeightCount = pendingTargetHeightCount_; |
| 44 | + } |
| 45 | + |
| 46 | + function _getSPVContractStorage() internal pure returns (SPVContractStorage storage _spvs) { |
| 47 | + bytes32 slot_ = SPV_CONTRACT_STORAGE_SLOT; |
| 48 | + |
| 49 | + assembly { |
| 50 | + _spvs.slot := slot_ |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function addBlockHeader(bytes calldata blockHeaderRaw_) external { |
| 55 | + SPVContractStorage storage $ = _getSPVContractStorage(); |
| 56 | + |
| 57 | + (BlockHeaderData memory blockHeader_, bytes32 blockHash_) = blockHeaderRaw_ |
| 58 | + .parseBlockHeaderData(); |
| 59 | + |
| 60 | + require(!$.blocksData.blockExists(blockHash_), BlockAlreadyExists(blockHash_)); |
| 61 | + require( |
| 62 | + $.blocksData.blockExists(blockHeader_.prevBlockHash), |
| 63 | + PrevBlockDoesNotExist(blockHeader_.prevBlockHash) |
| 64 | + ); |
| 65 | + |
| 66 | + uint256 blockHeight_ = $.blocksData.getBlockHeight(blockHeader_.prevBlockHash) + 1; |
| 67 | + |
| 68 | + _tryConfirmPendingTarget(blockHeight_); |
| 69 | + |
| 70 | + bytes32 target_ = _getRequiredTarget(blockHeight_, blockHeader_.prevBlockHash); |
| 71 | + |
| 72 | + _validateBlockRules(blockHeader_, blockHash_, target_); |
| 73 | + |
| 74 | + $.blocksData.addBlock( |
| 75 | + blockHeader_, |
| 76 | + blockHash_, |
| 77 | + blockHeight_, |
| 78 | + TargetsStorage.getBlockWork(target_) |
| 79 | + ); |
| 80 | + |
| 81 | + if (TargetsStorage.isTargetAdjustmentBlock(blockHeight_)) { |
| 82 | + $.targets.updatePendingTarget( |
| 83 | + blockHeight_, |
| 84 | + blockHash_, |
| 85 | + $.blocksData.getBlockTimeByBlockHeight( |
| 86 | + blockHeight_ - TargetsStorage.DIFFICULTY_ADJSTMENT_INTERVAL |
| 87 | + ), |
| 88 | + blockHeader_.time |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + emit BlockHeaderAdded(blockHeight_, blockHash_); |
| 93 | + } |
| 94 | + |
| 95 | + function _getRequiredTarget( |
| 96 | + uint256 blockHeight_, |
| 97 | + bytes32 prevBlockHash_ |
| 98 | + ) internal view returns (bytes32 target_) { |
| 99 | + SPVContractStorage storage $ = _getSPVContractStorage(); |
| 100 | + |
| 101 | + uint256 blockTargetEpoch_ = TargetsStorage.countTargetEpoch(blockHeight_); |
| 102 | + bool isPendingTargetRequired_ = blockTargetEpoch_ == $.targets.getPendingEpoch(); |
| 103 | + |
| 104 | + if (isPendingTargetRequired_) { |
| 105 | + uint256 epochBlockNumber_ = TargetsStorage.getEpochBlockNumber(blockHeight_); |
| 106 | + |
| 107 | + for (uint256 i = 0; i < epochBlockNumber_ - 1; ++i) { |
| 108 | + prevBlockHash_ = $.blocksData.getPrevBlockHash(prevBlockHash_); |
| 109 | + } |
| 110 | + |
| 111 | + target_ = $.targets.getPendingTarget(prevBlockHash_); |
| 112 | + } else { |
| 113 | + target_ = $.targets.getTarget(blockTargetEpoch_); |
| 114 | + } |
| 115 | + |
| 116 | + assert(target_ > 0); |
| 117 | + } |
| 118 | + |
| 119 | + function _tryConfirmPendingTarget(uint256 blockHeight_) internal { |
| 120 | + SPVContractStorage storage $ = _getSPVContractStorage(); |
| 121 | + |
| 122 | + if (!$.targets.hasPendingTarget() || blockHeight_ < $.pendingTargetHeightCount) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + uint256 lastActiveBlockHeight_ = blockHeight_ - $.pendingTargetHeightCount; |
| 127 | + |
| 128 | + if (TargetsStorage.isTargetAdjustmentBlock(lastActiveBlockHeight_)) { |
| 129 | + $.targets.confirmPendingTarget( |
| 130 | + $.blocksData.getBlockHashByBlockHeight(lastActiveBlockHeight_) |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + function _validateBlockRules( |
| 136 | + BlockHeaderData memory blockHeader_, |
| 137 | + bytes32 blockHash_, |
| 138 | + bytes32 target_ |
| 139 | + ) internal view { |
| 140 | + SPVContractStorage storage $ = _getSPVContractStorage(); |
| 141 | + |
| 142 | + bytes32 blockTarget_ = TargetsHelper.bitsToTarget(blockHeader_.bits); |
| 143 | + |
| 144 | + require(target_ == blockTarget_, InvalidTarget(blockTarget_, target_)); |
| 145 | + require(blockHash_ <= blockTarget_, InvalidBlockHash(blockHash_, blockTarget_)); |
| 146 | + |
| 147 | + uint32 medianTime_ = $.blocksData.getMedianTime(blockHeader_.prevBlockHash); |
| 148 | + |
| 149 | + require(blockHeader_.time > medianTime_, InvalidBlockTime(blockHeader_.time, medianTime_)); |
| 150 | + } |
| 151 | +} |
0 commit comments