Skip to content

Commit 34ba19c

Browse files
committed
Update interface
1 parent d79009e commit 34ba19c

File tree

3 files changed

+66
-56
lines changed

3 files changed

+66
-56
lines changed

contracts/SPVContract.sol

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,13 @@ contract SPVContract is ISPVContract, Initializable {
156156
}
157157

158158
/// @inheritdoc ISPVContract
159-
function getBlockStatus(bytes32 blockHash_) external view returns (bool, uint256) {
160-
if (!isInMainchain(blockHash_)) {
161-
return (false, 0);
162-
}
159+
function getMainchainHead() public view returns (bytes32) {
160+
return _getSPVContractStorage().mainchainHead;
161+
}
163162

164-
return (true, getMainchainHeight() - getBlockHeight(blockHash_));
163+
/// @inheritdoc ISPVContract
164+
function getMainchainHeight() public view returns (uint64) {
165+
return getBlockHeight(_getSPVContractStorage().mainchainHead);
165166
}
166167

167168
/// @inheritdoc ISPVContract
@@ -170,7 +171,7 @@ contract SPVContract is ISPVContract, Initializable {
170171
return blockInfo_;
171172
}
172173

173-
BlockData memory blockData_ = getBlockData(blockHash_);
174+
BlockData memory blockData_ = _getSPVContractStorage().blocksData[blockHash_];
174175

175176
blockInfo_ = BlockInfo({
176177
mainBlockData: blockData_,
@@ -180,23 +181,32 @@ contract SPVContract is ISPVContract, Initializable {
180181
}
181182

182183
/// @inheritdoc ISPVContract
183-
function getLastEpochCumulativeWork() external view returns (uint256) {
184-
return _getSPVContractStorage().lastEpochCumulativeWork;
185-
}
184+
function getBlockHeader(bytes32 blockHash_) public view returns (BlockHeaderData memory) {
185+
BlockData storage blockData = _getSPVContractStorage().blocksData[blockHash_];
186186

187-
/// @inheritdoc ISPVContract
188-
function getBlockMerkleRoot(bytes32 blockHash_) public view returns (bytes32) {
189-
return _getSPVContractStorage().blocksData[blockHash_].merkleRoot;
187+
return
188+
BlockHeaderData({
189+
version: blockData.version,
190+
prevBlockHash: blockData.prevBlockHash,
191+
merkleRoot: blockData.merkleRoot,
192+
time: blockData.time,
193+
bits: blockData.bits,
194+
nonce: blockData.nonce
195+
});
190196
}
191197

192198
/// @inheritdoc ISPVContract
193-
function getMainchainHead() public view returns (bytes32) {
194-
return _getSPVContractStorage().mainchainHead;
199+
function getBlockStatus(bytes32 blockHash_) external view returns (bool, uint64) {
200+
if (!isInMainchain(blockHash_)) {
201+
return (false, 0);
202+
}
203+
204+
return (true, getMainchainHeight() - getBlockHeight(blockHash_));
195205
}
196206

197207
/// @inheritdoc ISPVContract
198-
function getBlockData(bytes32 blockHash_) public view returns (BlockData memory) {
199-
return _getSPVContractStorage().blocksData[blockHash_];
208+
function getBlockMerkleRoot(bytes32 blockHash_) public view returns (bytes32) {
209+
return _getSPVContractStorage().blocksData[blockHash_].merkleRoot;
200210
}
201211

202212
/// @inheritdoc ISPVContract
@@ -215,13 +225,13 @@ contract SPVContract is ISPVContract, Initializable {
215225
}
216226

217227
/// @inheritdoc ISPVContract
218-
function blockExists(bytes32 blockHash_) public view returns (bool) {
219-
return _getBlockHeaderTime(blockHash_) > 0;
228+
function getLastEpochCumulativeWork() public view returns (uint256) {
229+
return _getSPVContractStorage().lastEpochCumulativeWork;
220230
}
221231

222232
/// @inheritdoc ISPVContract
223-
function getMainchainHeight() public view returns (uint256) {
224-
return getBlockHeight(_getSPVContractStorage().mainchainHead);
233+
function blockExists(bytes32 blockHash_) public view returns (bool) {
234+
return _getBlockHeaderTime(blockHash_) > 0;
225235
}
226236

227237
/// @inheritdoc ISPVContract
@@ -352,7 +362,7 @@ contract SPVContract is ISPVContract, Initializable {
352362

353363
function _getStorageMedianTime(
354364
BlockHeaderData memory blockHeader_,
355-
uint256 blockHeight_
365+
uint64 blockHeight_
356366
) internal view returns (uint32) {
357367
if (blockHeight_ == 1) {
358368
return blockHeader_.time;
@@ -383,7 +393,7 @@ contract SPVContract is ISPVContract, Initializable {
383393

384394
function _getMemoryMedianTime(
385395
BlockHeaderData[] memory blockHeaders_,
386-
uint256 to_
396+
uint64 to_
387397
) internal pure returns (uint32) {
388398
if (blockHeaders_.length < MEDIAN_PAST_BLOCKS) {
389399
return 0;

contracts/interfaces/ISPVContract.sol

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.28;
33

4+
import {BlockHeaderData} from "../libs/BlockHeader.sol";
45
import {TxMerkleProof} from "../libs/TxMerkleProof.sol";
56

67
/**
@@ -14,7 +15,7 @@ interface ISPVContract {
1415
* This error indicates that the provided block height is not valid for initialization
1516
* @param blockHeight The invalid block height
1617
*/
17-
error InvalidInitialBlockHeight(uint256 blockHeight);
18+
error InvalidInitialBlockHeight(uint64 blockHeight);
1819
/**
1920
* @notice Emitted when a previous block does not exist.
2021
* This error occurs when a block header references a previous block that is not found
@@ -66,15 +67,15 @@ interface ISPVContract {
6667
* @param newMainchainHead The hash of the new mainchain head
6768
*/
6869
event MainchainHeadUpdated(
69-
uint256 indexed newMainchainHeight,
70+
uint64 indexed newMainchainHeight,
7071
bytes32 indexed newMainchainHead
7172
);
7273
/**
7374
* @notice Emitted when a block header is successfully added
7475
* @param blockHeight The height of the added block
7576
* @param blockHash The hash of the added block
7677
*/
77-
event BlockHeaderAdded(uint256 indexed blockHeight, bytes32 indexed blockHash);
78+
event BlockHeaderAdded(uint64 indexed blockHeight, bytes32 indexed blockHash);
7879

7980
/**
8081
* @notice Represents the data of a block
@@ -138,27 +139,18 @@ interface ISPVContract {
138139
) external view returns (bool);
139140

140141
/**
141-
* @notice Returns the current status of a given block
142-
* @param blockHash_ The hash of the block to check
143-
* @return isInMainchain True if the block is in the mainchain, false otherwise
144-
* @return confirmationsCount The number of blocks that have been mined on top of the given block
145-
*/
146-
function getBlockStatus(bytes32 blockHash_) external view returns (bool, uint256);
147-
148-
/**
149-
* @notice Returns the cumulative work of the last epoch.
150-
* This represents the total difficulty accumulated up to the last epoch boundary
151-
* @return The cumulative work of the last epoch
142+
* @notice Returns the hash of the current mainchain head.
143+
* This represents the highest block on the most accumulated work chain
144+
* @return The hash of the mainchain head
152145
*/
153-
function getLastEpochCumulativeWork() external view returns (uint256);
146+
function getMainchainHead() external view returns (bytes32);
154147

155148
/**
156-
* @notice Returns the Merkle root of a given block hash.
157-
* This function retrieves the Merkle root from the stored block header data
158-
* @param blockHash_ The hash of the block
159-
* @return The Merkle root of the block
149+
* @notice Returns the height of the current mainchain head.
150+
* This represents the highest block number on the most accumulated work chain
151+
* @return The height of the mainchain head
160152
*/
161-
function getBlockMerkleRoot(bytes32 blockHash_) external view returns (bytes32);
153+
function getMainchainHeight() external view returns (uint64);
162154

163155
/**
164156
* @notice Returns detailed information about a block.
@@ -169,26 +161,27 @@ interface ISPVContract {
169161
function getBlockInfo(bytes32 blockHash_) external view returns (BlockInfo memory blockInfo_);
170162

171163
/**
172-
* @notice Returns the basic block data for a given block hash.
173-
* This includes the block header and its height
174-
* @param blockHash_ The hash of the block
175-
* @return The basic block data
164+
* @notice Returns the block header data for a given block hash.
165+
* @param blockHash The hash of the block
166+
* @return The block header data
176167
*/
177-
function getBlockData(bytes32 blockHash_) external view returns (BlockData memory);
168+
function getBlockHeader(bytes32 blockHash) external view returns (BlockHeaderData memory);
178169

179170
/**
180-
* @notice Returns the hash of the current mainchain head.
181-
* This represents the highest block on the most accumulated work chain
182-
* @return The hash of the mainchain head
171+
* @notice Returns the current status of a given block
172+
* @param blockHash_ The hash of the block to check
173+
* @return isInMainchain True if the block is in the mainchain, false otherwise
174+
* @return confirmationsCount The number of blocks that have been mined on top of the given block
183175
*/
184-
function getMainchainHead() external view returns (bytes32);
176+
function getBlockStatus(bytes32 blockHash_) external view returns (bool, uint64);
185177

186178
/**
187-
* @notice Returns the height of the current mainchain head.
188-
* This represents the highest block number on the most accumulated work chain
189-
* @return The height of the mainchain head
179+
* @notice Returns the Merkle root of a given block hash.
180+
* This function retrieves the Merkle root from the stored block header data
181+
* @param blockHash_ The hash of the block
182+
* @return The Merkle root of the block
190183
*/
191-
function getMainchainHeight() external view returns (uint256);
184+
function getBlockMerkleRoot(bytes32 blockHash_) external view returns (bytes32);
192185

193186
/**
194187
* @notice Returns the block height for a given block hash
@@ -214,6 +207,13 @@ interface ISPVContract {
214207
*/
215208
function getBlockTarget(bytes32 blockHash_) external view returns (bytes32);
216209

210+
/**
211+
* @notice Returns the cumulative work of the last epoch.
212+
* This represents the total difficulty accumulated up to the last epoch boundary
213+
* @return The cumulative work of the last epoch
214+
*/
215+
function getLastEpochCumulativeWork() external view returns (uint256);
216+
217217
/**
218218
* @notice Checks if a block exists in the contract's storage.
219219
* This function verifies the presence of a block by its hash

contracts/libs/TargetsHelper.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ library TargetsHelper {
1212
* @notice The ideal expected time for 2016 blocks to be mined, in seconds.
1313
* This is based on a 10-minute block interval
1414
*/
15-
uint256 public constant EXPECTED_TARGET_BLOCKS_TIME = 1209600;
15+
uint32 public constant EXPECTED_TARGET_BLOCKS_TIME = 1209600;
1616
/**
1717
* @notice The number of blocks after which the difficulty target is adjusted.
1818
* This is a fundamental constant in Bitcoin's difficulty adjustment algorithm

0 commit comments

Comments
 (0)