Skip to content

Commit

Permalink
[Fix] audited issues (#115)
Browse files Browse the repository at this point in the history
* fix audit issues

* add audit report
  • Loading branch information
ChainDev931105 authored Jun 4, 2024
1 parent d41f088 commit dfaf775
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
Binary file not shown.
3 changes: 3 additions & 0 deletions contracts/interfaces/ILagrangeCommittee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@ interface ILagrangeCommittee {
event EpochPeriodUpdated(
uint32 indexed chainID, uint32 indexed epochPeriodIndex, uint256 flagBlock, uint256 flagEpoch, uint256 duration
);

// Event fired on updating sign address
event SignAddressUpdated(address indexed operator, address indexed signAddress);
}
59 changes: 31 additions & 28 deletions contracts/protocol/LagrangeCommittee.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit
// @dev This function can be called for the chainID registered in the previous version
function setFirstEpochPeriod(uint32 chainID) public onlyOwner {
uint32 _count = getEpochPeriodCount(chainID);
if (_count > 0) return; // already initialized
if (_count != 0) return; // already initialized
CommitteeDef memory _committeeParam = committeeParams[chainID];

_writeEpochPeriod(chainID, _committeeParam.startBlock, 0, _committeeParam.duration);
Expand Down Expand Up @@ -136,15 +136,18 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit
count++;
}
}
operatorsStatus[operator].blsPubKeys = _blsPubKeys;
for (uint256 i = count; i < _length; i++) {
operatorsStatus[operator].blsPubKeys.pop();
uint256[2][] memory _newBlsPubKeys = new uint256[2][](count);
for (uint256 i; i < count; i++) {
_newBlsPubKeys[i] = _blsPubKeys[i];
}
operatorsStatus[operator].blsPubKeys = _newBlsPubKeys;
}

// Updates an operator's sign address
function updateSignAddress(address operator, address newSignAddress) external onlyService {
require(operatorsStatus[operator].blsPubKeys.length != 0, "Operator is not registered.");
operatorsStatus[operator].signAddress = newSignAddress;
emit SignAddressUpdated(operator, newSignAddress);
}

function subscribeChain(address operator, uint32 chainID) external onlyService {
Expand Down Expand Up @@ -257,14 +260,14 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit

// Checks if a chain's committee is updatable at a given block
function isUpdatable(uint32 chainID, uint256 epochNumber) public view returns (bool) {
(, uint256 _freezeBlock,) = _getEpochInterval(chainID, epochNumber - 1);
(, uint256 _freezeBlock,) = getEpochInterval(chainID, epochNumber - 1);
return block.number > _freezeBlock;
}

// Checks if a chain's committee is locked at a given block
function isLocked(uint32 chainID) public view returns (bool, uint256) {
uint256 _epochNumber = _getEpochNumber(chainID, block.number);
(, uint256 _freezeBlock, uint256 _endBlock) = _getEpochInterval(chainID, _epochNumber);
(, uint256 _freezeBlock, uint256 _endBlock) = getEpochInterval(chainID, _epochNumber);
return (block.number > _freezeBlock, _endBlock);
}

Expand Down Expand Up @@ -440,7 +443,7 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit
) internal {
if (committeeParams[_chainID].duration != _duration) {
uint256 _flagEpoch = _getEpochNumber(_chainID, block.number - 1) + 1;
(,, uint256 _endBlockPrv) = _getEpochInterval(_chainID, _flagEpoch - 1);
(,, uint256 _endBlockPrv) = getEpochInterval(_chainID, _flagEpoch - 1);
_writeEpochPeriod(_chainID, _endBlockPrv, _flagEpoch, _duration);
}

Expand Down Expand Up @@ -471,6 +474,27 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit
);
}

function getEpochInterval(uint32 _chainID, uint256 _epochNumber)
public
view
returns (uint256 _startBlock, uint256 _freezeBlock, uint256 _endBlock)
{
// epoch period would be updated rarely
uint32 _index = getEpochPeriodCount(_chainID);
while (_index > 0) {
(uint256 _flagBlock, uint256 _flagEpoch, uint256 _duration) = getEpochPeriodByIndex(_chainID, _index);
if (_epochNumber >= _flagEpoch) {
_startBlock = (_epochNumber - _flagEpoch) * _duration + _flagBlock;
_endBlock = _startBlock + _duration;
_freezeBlock = _endBlock - committeeParams[_chainID].freezeDuration;
break;
}
unchecked {
_index--;
}
}
}

function _writeEpochPeriod(uint32 _chainID, uint256 _flagBlock, uint256 _flagEpoch, uint256 _duration) internal {
uint32 _index = committees[_chainID][type(uint256).max].leafCount + 1;
committees[_chainID][type(uint256).max - _index] = CommitteeData(
Expand Down Expand Up @@ -499,27 +523,6 @@ contract LagrangeCommittee is Initializable, OwnableUpgradeable, ILagrangeCommit
}
}
}

function _getEpochInterval(uint32 _chainID, uint256 _epochNumber)
internal
view
returns (uint256 _startBlock, uint256 _freezeBlock, uint256 _endBlock)
{
// epoch period would be updated rarely
uint32 _index = getEpochPeriodCount(_chainID);
while (_index > 0) {
(uint256 _flagBlock, uint256 _flagEpoch, uint256 _duration) = getEpochPeriodByIndex(_chainID, _index);
if (_epochNumber >= _flagEpoch) {
_startBlock = (_epochNumber - _flagEpoch) * _duration + _flagBlock;
_endBlock = _startBlock + _duration;
_freezeBlock = _endBlock - committeeParams[_chainID].freezeDuration;
break;
}
unchecked {
_index--;
}
}
}
// ------------------------------------------------------------- //

function _registerOperator(address _operator, address _signAddress, uint256[2][] memory _blsPubKeys) internal {
Expand Down

0 comments on commit dfaf775

Please sign in to comment.