Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani committed Jan 17, 2024
1 parent b6b9222 commit 1fdefb2
Showing 1 changed file with 16 additions and 48 deletions.
64 changes: 16 additions & 48 deletions contracts/src/VectorX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
/// @notice The maximum header range that can be requested.
uint32 public constant MAX_HEADER_RANGE = 256;

/// @notice The reset counter.
uint8 public resetCounter;

/// @notice Maps block height to the header hash of the block.
mapping(uint8 => mapping(uint32 => bytes32)) public blockHeightToHeaderHash;
mapping(uint32 => bytes32) public blockHeightToHeaderHash;

/// @notice Maps authority set id to the authority set hash.
mapping(uint8 => mapping(uint64 => bytes32)) public authoritySetIdToHash;
mapping(uint64 => bytes32) public authoritySetIdToHash;

/// @notice Maps block ranges to data commitments. Block ranges are stored as
/// keccak256(abi.encode(startBlock, endBlock)).
mapping(uint8 => mapping(bytes32 => bytes32)) public dataRootCommitments;
mapping(bytes32 => bytes32) public dataRootCommitments;

/// @notice Maps block ranges to state commitments. Block ranges are stored as
/// keccak256(abi.encode(startBlock, endBlock)).
mapping(uint8 => mapping(bytes32 => bytes32)) public stateRootCommitments;
mapping(bytes32 => bytes32) public stateRootCommitments;

struct InitParameters {
address guardian;
Expand All @@ -59,27 +56,8 @@ contract VectorX is IVectorX, TimelockedUpgradeable {

gateway = _params.gateway;

resetCounter = 0;

blockHeightToHeaderHash[resetCounter][_params.height] = _params.header;
authoritySetIdToHash[resetCounter][_params.authoritySetId] = _params
.authoritySetHash;
latestBlock = _params.height;

rotateFunctionId = _params.rotateFunctionId;
headerRangeFunctionId = _params.headerRangeFunctionId;
}

/// @notice Update the address of the gateway contract.
function reinitializeContract(
InitParameters calldata _params
) public onlyGuardian {
// Reset all metadata of this contract.
resetCounter++;

blockHeightToHeaderHash[resetCounter][_params.height] = _params.header;
authoritySetIdToHash[resetCounter][_params.authoritySetId] = _params
.authoritySetHash;
blockHeightToHeaderHash[_params.height] = _params.header;
authoritySetIdToHash[_params.authoritySetId] = _params.authoritySetHash;
latestBlock = _params.height;

rotateFunctionId = _params.rotateFunctionId;
Expand Down Expand Up @@ -115,17 +93,13 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
uint64 _authoritySetId,
uint32 _requestedBlock
) external payable {
bytes32 trustedHeader = blockHeightToHeaderHash[resetCounter][
_trustedBlock
];
bytes32 trustedHeader = blockHeightToHeaderHash[_trustedBlock];
if (trustedHeader == bytes32(0)) {
revert("Trusted header not found");
}
// Note: In the case that the trusted block is an epoch end block, the authority set id will
// be the authority set id of the next epoch.
bytes32 authoritySetHash = authoritySetIdToHash[resetCounter][
_authoritySetId
];
bytes32 authoritySetHash = authoritySetIdToHash[_authoritySetId];
if (authoritySetHash == bytes32(0)) {
revert("Authority set hash not found");
}
Expand Down Expand Up @@ -176,15 +150,11 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
uint64 _authoritySetId,
uint32 _targetBlock
) external {
bytes32 trustedHeader = blockHeightToHeaderHash[resetCounter][
_trustedBlock
];
bytes32 trustedHeader = blockHeightToHeaderHash[_trustedBlock];
if (trustedHeader == bytes32(0)) {
revert("Trusted header not found");
}
bytes32 authoritySetHash = authoritySetIdToHash[resetCounter][
_authoritySetId
];
bytes32 authoritySetHash = authoritySetIdToHash[_authoritySetId];
if (authoritySetHash == bytes32(0)) {
revert("Authority set hash not found");
}
Expand Down Expand Up @@ -213,12 +183,12 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
bytes32 dataRootCommitment
) = abi.decode(output, (bytes32, bytes32, bytes32));

blockHeightToHeaderHash[resetCounter][_targetBlock] = targetHeaderHash;
blockHeightToHeaderHash[_targetBlock] = targetHeaderHash;

// Store the data and state commitments for the range (trustedBlock, targetBlock].
bytes32 key = keccak256(abi.encode(_trustedBlock, _targetBlock));
dataRootCommitments[resetCounter][key] = dataRootCommitment;
stateRootCommitments[resetCounter][key] = stateRootCommitment;
dataRootCommitments[key] = dataRootCommitment;
stateRootCommitments[key] = stateRootCommitment;

// Update latest block.
latestBlock = _targetBlock;
Expand Down Expand Up @@ -246,7 +216,7 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
// This ensures we don't call rotate twice for the same epoch.
require(_epochEndBlock >= latestBlock);

bytes32 currentAuthoritySetHash = authoritySetIdToHash[resetCounter][
bytes32 currentAuthoritySetHash = authoritySetIdToHash[
_currentAuthoritySetId
];
if (currentAuthoritySetHash == bytes32(0)) {
Expand Down Expand Up @@ -286,7 +256,7 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
uint64 _currentAuthoritySetId,
uint32 _epochEndBlock
) external {
bytes32 currentAuthoritySetHash = authoritySetIdToHash[resetCounter][
bytes32 currentAuthoritySetHash = authoritySetIdToHash[
_currentAuthoritySetId
];
// Note: Occurs if requesting a new authority set id that is not the next authority set id.
Expand All @@ -308,9 +278,7 @@ contract VectorX is IVectorX, TimelockedUpgradeable {
bytes32 newAuthoritySetHash = abi.decode(output, (bytes32));

// Store the authority set hash for the next authority set id.
authoritySetIdToHash[resetCounter][
_currentAuthoritySetId + 1
] = newAuthoritySetHash;
authoritySetIdToHash[_currentAuthoritySetId + 1] = newAuthoritySetHash;

emit AuthoritySetStored(
_currentAuthoritySetId + 1,
Expand Down

0 comments on commit 1fdefb2

Please sign in to comment.