|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {BlockHeader} from "@solarity/solidity-lib/libs/bitcoin/BlockHeader.sol"; |
| 5 | +import {TxMerkleProof} from "@solarity/solidity-lib/libs/bitcoin/TxMerkleProof.sol"; |
| 6 | +import {EndianConverter} from "@solarity/solidity-lib/libs/utils/EndianConverter.sol"; |
| 7 | + |
| 8 | +import {TargetsHelper} from "./libs/TargetsHelper.sol"; |
| 9 | +import {BlockHistory} from "./libs/BlockHistory.sol"; |
| 10 | + |
| 11 | +import {IHistoricalSPVGateway} from "./interfaces/IHistoricalSPVGateway.sol"; |
| 12 | + |
| 13 | +import {SPVGateway} from "./SPVGateway.sol"; |
| 14 | + |
| 15 | +contract HistoricalSPVGateway is IHistoricalSPVGateway, SPVGateway { |
| 16 | + using BlockHeader for bytes; |
| 17 | + using TargetsHelper for bytes32; |
| 18 | + using EndianConverter for bytes32; |
| 19 | + using BlockHistory for bytes32[]; |
| 20 | + |
| 21 | + bytes32 public constant HISTORICAL_SPV_GATEWAY_STORAGE_SLOT = |
| 22 | + keccak256("spv.gateway.historical.spv.gateway.storage"); |
| 23 | + |
| 24 | + struct HistoricalSPVGatewayStorage { |
| 25 | + uint256 historyBlocksCount; |
| 26 | + bytes32 historyBlocksTreeRoot; |
| 27 | + } |
| 28 | + |
| 29 | + function _getHistoricalSPVGatewayStorage() |
| 30 | + private |
| 31 | + pure |
| 32 | + returns (HistoricalSPVGatewayStorage storage _hspvs) |
| 33 | + { |
| 34 | + bytes32 slot_ = HISTORICAL_SPV_GATEWAY_STORAGE_SLOT; |
| 35 | + |
| 36 | + assembly { |
| 37 | + _hspvs.slot := slot_ |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + function __HistoricalSPVGateway_init( |
| 42 | + bytes calldata blockHeaderRaw_, |
| 43 | + uint64 blockHeight_, |
| 44 | + uint256 cumulativeWork_, |
| 45 | + bytes32 historyBlocksTreeRoot_, |
| 46 | + BlockHistory.HistoryProofData calldata proofData_ |
| 47 | + ) external initializer { |
| 48 | + (BlockHeader.HeaderData memory blockHeader_, bytes32 blockHash_) = _parseBlockHeaderRaw( |
| 49 | + blockHeaderRaw_ |
| 50 | + ); |
| 51 | + |
| 52 | + BlockHistory.verifyHistoryProof( |
| 53 | + historyBlocksTreeRoot_, |
| 54 | + blockHash_, |
| 55 | + blockHeight_, |
| 56 | + cumulativeWork_, |
| 57 | + proofData_ |
| 58 | + ); |
| 59 | + |
| 60 | + _initialize(blockHeader_, blockHash_, blockHeight_, cumulativeWork_); |
| 61 | + |
| 62 | + _getHistoricalSPVGatewayStorage().historyBlocksCount = blockHeight_; |
| 63 | + _getHistoricalSPVGatewayStorage().historyBlocksTreeRoot = historyBlocksTreeRoot_; |
| 64 | + } |
| 65 | + |
| 66 | + /// @inheritdoc IHistoricalSPVGateway |
| 67 | + function checkHistoryTxInclusion( |
| 68 | + bytes32[] calldata merkleProof_, |
| 69 | + bytes calldata blockHeaderRaw_, |
| 70 | + bytes32 txId_, |
| 71 | + uint256 txIndex_, |
| 72 | + HistoryBlockInclusionProofData calldata blockInclusionProofData_ |
| 73 | + ) external view returns (bool) { |
| 74 | + (BlockHeader.HeaderData memory blockHeader_, bytes32 blockHash_) = blockHeaderRaw_ |
| 75 | + .parseBlockHeader(true); |
| 76 | + |
| 77 | + require( |
| 78 | + blockHash_ == blockInclusionProofData_.blockHash, |
| 79 | + DifferentBlockHashes(blockHash_, blockInclusionProofData_.blockHash) |
| 80 | + ); |
| 81 | + |
| 82 | + require( |
| 83 | + checkHistoryBlockInclusion(blockInclusionProofData_), |
| 84 | + BlockHashNotInHistory(blockHash_) |
| 85 | + ); |
| 86 | + |
| 87 | + bytes32 leRoot_ = blockHeader_.merkleRoot.bytes32BEtoLE(); |
| 88 | + |
| 89 | + return TxMerkleProof.verify(merkleProof_, leRoot_, txId_, txIndex_); |
| 90 | + } |
| 91 | + |
| 92 | + /// @inheritdoc IHistoricalSPVGateway |
| 93 | + function checkHistoryBlockInclusion( |
| 94 | + HistoryBlockInclusionProofData calldata inclusionProofData_ |
| 95 | + ) public view returns (bool) { |
| 96 | + bytes32 level1Root_ = inclusionProofData_.level1MerkleProof.processLevel1Proof( |
| 97 | + inclusionProofData_.blockHash, |
| 98 | + inclusionProofData_.blockHeight |
| 99 | + ); |
| 100 | + |
| 101 | + return |
| 102 | + inclusionProofData_.level2MerkleProof.verifyLevel2Proof( |
| 103 | + getHistoryBlocksTreeRoot(), |
| 104 | + level1Root_, |
| 105 | + BlockHistory.getChunkNumber(getHistoryBlocksCount()), |
| 106 | + BlockHistory.getChunkNumber(inclusionProofData_.blockHeight) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /// @inheritdoc IHistoricalSPVGateway |
| 111 | + function getHistoryBlocksCount() public view returns (uint256) { |
| 112 | + return _getHistoricalSPVGatewayStorage().historyBlocksCount; |
| 113 | + } |
| 114 | + |
| 115 | + /// @inheritdoc IHistoricalSPVGateway |
| 116 | + function getHistoryBlocksTreeRoot() public view returns (bytes32) { |
| 117 | + return _getHistoricalSPVGatewayStorage().historyBlocksTreeRoot; |
| 118 | + } |
| 119 | +} |
0 commit comments