Skip to content

Commit 3f0250a

Browse files
authored
Merge pull request #2 from distributed-lab/feature/optimizations
Feature/optimizations
2 parents 6e4e564 + 3116e09 commit 3f0250a

27 files changed

+1954
-1242
lines changed

contracts/SPVContract.sol

Lines changed: 330 additions & 77 deletions
Large diffs are not rendered by default.

contracts/interfaces/ISPVContract.sol

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.28;
3+
4+
import {BlockHeaderData} from "../libs/BlockHeader.sol";
5+
6+
interface ISPVContract {
7+
error InvalidInitialBlockHeight(uint256 blockHeight);
8+
error PrevBlockDoesNotExist(bytes32 prevBlockHash);
9+
error BlockAlreadyExists(bytes32 blockHash);
10+
11+
error EmptyBlockHeaderArray();
12+
error InvalidBlockHeadersOrder();
13+
14+
error InvalidTarget(bytes32 blockTarget, bytes32 networkTarget);
15+
error InvalidBlockHash(bytes32 actualBlockHash, bytes32 blockTarget);
16+
error InvalidBlockTime(uint32 blockTime, uint32 medianTime);
17+
18+
event MainchainHeadUpdated(
19+
uint256 indexed newMainchainHeight,
20+
bytes32 indexed newMainchainHead
21+
);
22+
event BlockHeaderAdded(uint256 indexed blockHeight, bytes32 indexed blockHash);
23+
24+
struct BlockData {
25+
BlockHeaderData header;
26+
uint256 blockHeight;
27+
}
28+
29+
struct BlockInfo {
30+
BlockData mainBlockData;
31+
bool isInMainchain;
32+
uint256 cumulativeWork;
33+
}
34+
35+
function addBlockHeaderBatch(bytes[] calldata blockHeaderRawArray_) external;
36+
37+
function addBlockHeader(bytes calldata blockHeaderRaw_) external;
38+
39+
function getLastEpochCumulativeWork() external view returns (uint256);
40+
41+
function getBlockMerkleRoot(bytes32 blockHash_) external view returns (bytes32);
42+
43+
function getBlockInfo(bytes32 blockHash_) external view returns (BlockInfo memory blockInfo_);
44+
45+
function getBlockData(bytes32 blockHash_) external view returns (BlockData memory);
46+
47+
function getMainchainHead() external view returns (bytes32);
48+
49+
function getBlockHeight(bytes32 blockHash_) external view returns (uint256);
50+
51+
function getBlockHash(uint256 blockHeight_) external view returns (bytes32);
52+
53+
function getBlockTarget(bytes32 blockHash_) external view returns (bytes32);
54+
55+
function blockExists(bytes32 blockHash_) external view returns (bool);
56+
57+
function getMainchainBlockHeight() external view returns (uint256);
58+
59+
function isInMainchain(bytes32 blockHash_) external view returns (bool);
60+
}

contracts/libs/BlockHeader.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ library BlockHeader {
2222
function getBlockHeaderHash(bytes calldata blockHeaderRaw_) internal pure returns (bytes32) {
2323
bytes32 rawBlockHash_ = sha256(abi.encode(sha256(blockHeaderRaw_)));
2424

25-
return reverseHash(rawBlockHash_);
25+
return _reverseHash(rawBlockHash_);
2626
}
2727

2828
function parseBlockHeaderData(
@@ -35,8 +35,8 @@ library BlockHeader {
3535

3636
headerData_ = BlockHeaderData({
3737
version: uint32(_reverseBytes(blockHeaderRaw_[0:4])),
38-
prevBlockHash: reverseHash(bytes32(blockHeaderRaw_[4:36])),
39-
merkleRoot: reverseHash(bytes32(blockHeaderRaw_[36:68])),
38+
prevBlockHash: _reverseHash(bytes32(blockHeaderRaw_[4:36])),
39+
merkleRoot: _reverseHash(bytes32(blockHeaderRaw_[36:68])),
4040
time: uint32(_reverseBytes(blockHeaderRaw_[68:72])),
4141
bits: bytes4(uint32(_reverseBytes(blockHeaderRaw_[72:76]))),
4242
nonce: uint32(_reverseBytes(blockHeaderRaw_[76:80]))
@@ -48,15 +48,15 @@ library BlockHeader {
4848
return
4949
abi.encodePacked(
5050
_reverseUint32(headerData_.version),
51-
reverseHash(headerData_.prevBlockHash),
52-
reverseHash(headerData_.merkleRoot),
51+
_reverseHash(headerData_.prevBlockHash),
52+
_reverseHash(headerData_.merkleRoot),
5353
_reverseUint32(headerData_.time),
5454
_reverseUint32(uint32(headerData_.bits)),
5555
_reverseUint32(headerData_.nonce)
5656
);
5757
}
5858

59-
function reverseHash(bytes32 blockHash_) internal pure returns (bytes32) {
59+
function _reverseHash(bytes32 blockHash_) private pure returns (bytes32) {
6060
return bytes32(uint256(blockHash_).reverseBytes());
6161
}
6262

contracts/libs/BlocksStorage.sol

Lines changed: 0 additions & 287 deletions
This file was deleted.

0 commit comments

Comments
 (0)