Skip to content

Commit

Permalink
Refactored if statement logic for readability
Browse files Browse the repository at this point in the history
removed needless change in btoken contract
  • Loading branch information
keirongulrajani committed Aug 1, 2022
1 parent dc359c2 commit fd3db78
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 63 deletions.
1 change: 0 additions & 1 deletion bridge/contracts/BToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ contract BToken is
if (totalSupply_ < numBTK_) {
revert BTokenErrors.BurnAmountExceedsSupply(numBTK_, totalSupply_);
}

return _min(poolBalance_, _pInverse(totalSupply_) - _pInverse(totalSupply_ - numBTK_));
}

Expand Down
4 changes: 2 additions & 2 deletions bridge/contracts/ETHDKG.sol
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ contract ETHDKG is
}

if (
!(validatorsAccounts_.length == validatorIndexes_.length &&
validatorsAccounts_.length == validatorShares_.length)
validatorsAccounts_.length != validatorIndexes_.length ||
validatorsAccounts_.length != validatorShares_.length
) {
revert ETHDKGErrors.MigrationInputDataMismatch(
validatorsAccounts_.length,
Expand Down
2 changes: 1 addition & 1 deletion bridge/contracts/Snapshots.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ contract Snapshots is Initializable, SnapshotsStorage, ISnapshots {
if (_epoch != 0) {
revert SnapshotsErrors.MigrationNotAllowedAtCurrentEpoch();
}
if (!(groupSignature_.length == bClaims_.length && groupSignature_.length >= 1)) {
if (groupSignature_.length != bClaims_.length || groupSignature_.length == 0) {
revert SnapshotsErrors.MigrationInputDataMismatch(
groupSignature_.length,
bClaims_.length
Expand Down
2 changes: 1 addition & 1 deletion bridge/contracts/libraries/StakingNFT/StakingNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ abstract contract StakingNFT is
// collect state
Position memory p = _positions[tokenID_];
// enforce freeAfter to prevent burn during lock
if (!(p.freeAfter < block.number && p.withdrawFreeAfter < block.number)) {
if (p.freeAfter >= block.number || p.withdrawFreeAfter >= block.number) {
revert StakingNFTErrors.FreeAfterTimeNotReached();
}

Expand Down
68 changes: 34 additions & 34 deletions bridge/contracts/libraries/ethdkg/ETHDKGAccusations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function accuseParticipantNotRegistered(address[] memory dishonestAddresses) external {
if (
!(_ethdkgPhase == Phase.RegistrationOpen &&
((block.number >= _phaseStartBlock + _phaseLength) &&
(block.number < _phaseStartBlock + 2 * _phaseLength)))
_ethdkgPhase != Phase.RegistrationOpen ||
block.number < _phaseStartBlock + _phaseLength ||
block.number >= _phaseStartBlock + 2 * _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInPostRegistrationAccusationPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -47,9 +47,9 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function accuseParticipantDidNotDistributeShares(address[] memory dishonestAddresses) external {
if (
!(_ethdkgPhase == Phase.ShareDistribution &&
((block.number >= _phaseStartBlock + _phaseLength) &&
(block.number < _phaseStartBlock + 2 * _phaseLength)))
_ethdkgPhase != Phase.ShareDistribution ||
block.number < _phaseStartBlock + _phaseLength ||
block.number >= _phaseStartBlock + 2 * _phaseLength
) {
revert ETHDKGErrors.NotInPostSharedDistributionPhase(_ethdkgPhase);
}
Expand All @@ -74,8 +74,8 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
revert ETHDKGErrors.AccusedDistributedSharesInRound(dishonestAddresses[i]);
}
if (
!(dishonestParticipant.commitmentsFirstCoefficient[0] == 0 &&
dishonestParticipant.commitmentsFirstCoefficient[1] == 0)
dishonestParticipant.commitmentsFirstCoefficient[0] != 0 ||
dishonestParticipant.commitmentsFirstCoefficient[1] != 0
) {
revert ETHDKGErrors.AccusedHasCommitments(dishonestAddresses[i]);
}
Expand All @@ -96,12 +96,12 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
) external {
// We should allow accusation, even if some of the participants didn't participate
if (
!((_ethdkgPhase == Phase.DisputeShareDistribution &&
!(_ethdkgPhase == Phase.DisputeShareDistribution &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength) ||
(_ethdkgPhase == Phase.ShareDistribution &&
(block.number >= _phaseStartBlock + _phaseLength) &&
(block.number < _phaseStartBlock + 2 * _phaseLength)))
block.number < _phaseStartBlock + _phaseLength) &&
!(_ethdkgPhase == Phase.ShareDistribution &&
block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength)
) {
revert ETHDKGErrors.ETHDKGNotInDisputePhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -200,9 +200,9 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function accuseParticipantDidNotSubmitKeyShares(address[] memory dishonestAddresses) external {
if (
!(_ethdkgPhase == Phase.KeyShareSubmission &&
(block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength))
_ethdkgPhase != Phase.KeyShareSubmission ||
block.number < _phaseStartBlock + _phaseLength ||
block.number >= _phaseStartBlock + 2 * _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInPostKeyshareSubmissionPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -237,9 +237,9 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function accuseParticipantDidNotSubmitGPKJ(address[] memory dishonestAddresses) external {
if (
!(_ethdkgPhase == Phase.GPKJSubmission &&
(block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength))
_ethdkgPhase != Phase.GPKJSubmission ||
block.number < _phaseStartBlock + _phaseLength ||
block.number >= _phaseStartBlock + 2 * _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInPostGPKJSubmissionPhase(_ethdkgPhase);
}
Expand All @@ -262,10 +262,10 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

// todo: being paranoic, check if we need this or if it's expensive
if (
!(dishonestParticipant.gpkj[0] == 0 &&
dishonestParticipant.gpkj[1] == 0 &&
dishonestParticipant.gpkj[2] == 0 &&
dishonestParticipant.gpkj[3] == 0)
dishonestParticipant.gpkj[0] != 0 ||
dishonestParticipant.gpkj[1] != 0 ||
dishonestParticipant.gpkj[2] != 0 ||
dishonestParticipant.gpkj[3] != 0
) {
revert ETHDKGErrors.AccusedDistributedGPKJ(dishonestAddresses[i]);
}
Expand All @@ -285,12 +285,12 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
) external {
// We should allow accusation, even if some of the participants didn't participate
if (
!((_ethdkgPhase == Phase.DisputeGPKJSubmission &&
!(_ethdkgPhase == Phase.DisputeGPKJSubmission &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength) ||
(_ethdkgPhase == Phase.GPKJSubmission &&
(block.number >= _phaseStartBlock + _phaseLength) &&
(block.number < _phaseStartBlock + 2 * _phaseLength)))
block.number < _phaseStartBlock + _phaseLength) &&
!(_ethdkgPhase == Phase.GPKJSubmission &&
block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength)
) {
revert ETHDKGErrors.ETHDKGNotInPostGPKJSubmissionPhase(_ethdkgPhase);
}
Expand All @@ -303,13 +303,13 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
Participant memory disputer = _participants[msg.sender];

if (
!(dishonestParticipant.nonce == _nonce &&
dishonestParticipant.phase == Phase.GPKJSubmission)
dishonestParticipant.nonce != _nonce ||
dishonestParticipant.phase != Phase.GPKJSubmission
) {
revert ETHDKGErrors.AccusedDidNotSubmitGPKJInRound(dishonestAddress);
}

if (!(disputer.nonce == _nonce && disputer.phase == Phase.GPKJSubmission)) {
if (disputer.nonce != _nonce || disputer.phase != Phase.GPKJSubmission) {
revert ETHDKGErrors.DisputerDidNotSubmitGPKJInRound(msg.sender);
}

Expand Down Expand Up @@ -347,9 +347,9 @@ contract ETHDKGAccusations is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
bytes32 commitmentsHash = keccak256(abi.encodePacked(commitments[k]));
Participant memory participant = _participants[validators[k]];
if (
!(participant.nonce == nonce &&
participant.index <= type(uint8).max &&
!_isBitSet(bitMap, uint8(participant.index)))
participant.nonce != nonce ||
participant.index > type(uint8).max ||
_isBitSet(bitMap, uint8(participant.index))
) {
revert ETHDKGErrors.InvalidOrDuplicatedParticipant(validators[k]);
}
Expand Down
42 changes: 21 additions & 21 deletions bridge/contracts/libraries/ethdkg/ETHDKGPhases.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function register(uint256[2] memory publicKey) external {
if (
!(_ethdkgPhase == Phase.RegistrationOpen &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength)
_ethdkgPhase != Phase.RegistrationOpen ||
block.number < _phaseStartBlock ||
block.number >= _phaseStartBlock + _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInRegistrationPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -64,9 +64,9 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
external
{
if (
!(_ethdkgPhase == Phase.ShareDistribution &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength)
_ethdkgPhase != Phase.ShareDistribution ||
block.number < _phaseStartBlock ||
block.number >= _phaseStartBlock + _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInSharedDistributionPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -136,13 +136,13 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
// Only progress if all participants distributed their shares
// and no bad participant was found
if (
!((_ethdkgPhase == Phase.KeyShareSubmission &&
!(_ethdkgPhase == Phase.KeyShareSubmission &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength) ||
(_ethdkgPhase == Phase.DisputeShareDistribution &&
block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength &&
_badParticipants == 0))
block.number < _phaseStartBlock + _phaseLength) &&
!(_ethdkgPhase == Phase.DisputeShareDistribution &&
block.number >= _phaseStartBlock + _phaseLength &&
block.number < _phaseStartBlock + 2 * _phaseLength &&
_badParticipants == 0)
) {
revert ETHDKGErrors.ETHDKGNotInKeyshareSubmissionPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -227,9 +227,9 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {

function submitMasterPublicKey(uint256[4] memory masterPublicKey_) external {
if (
!(_ethdkgPhase == Phase.MPKSubmission &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength)
_ethdkgPhase != Phase.MPKSubmission ||
block.number < _phaseStartBlock ||
block.number >= _phaseStartBlock + _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInMasterPublicKeySubmissionPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -265,9 +265,9 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
function submitGPKJ(uint256[4] memory gpkj) external {
//todo: should we evict all validators if no one sent the master public key in time?
if (
!(_ethdkgPhase == Phase.GPKJSubmission &&
block.number >= _phaseStartBlock &&
block.number < _phaseStartBlock + _phaseLength)
_ethdkgPhase != Phase.GPKJSubmission ||
block.number < _phaseStartBlock ||
block.number >= _phaseStartBlock + _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInGPKJSubmissionPhase(_ethdkgPhase);
}
Expand Down Expand Up @@ -315,9 +315,9 @@ contract ETHDKGPhases is ETHDKGStorage, IETHDKGEvents, ETHDKGUtils {
function complete() external {
//todo: should we reward ppl here?
if (
!((_ethdkgPhase == Phase.DisputeGPKJSubmission &&
block.number >= _phaseStartBlock + _phaseLength) &&
block.number < _phaseStartBlock + 2 * _phaseLength)
_ethdkgPhase != Phase.DisputeGPKJSubmission ||
block.number < _phaseStartBlock + _phaseLength ||
block.number >= _phaseStartBlock + 2 * _phaseLength
) {
revert ETHDKGErrors.ETHDKGNotInPostGPKJDisputePhase(_ethdkgPhase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ library MerkleProofParserLibrary {
}
mProof.included = BaseParserLibrary.extractBool(src, 0);
mProof.keyHeight = BaseParserLibrary.extractUInt16FromBigEndian(src, 1);
if (!(mProof.keyHeight >= 0 && mProof.keyHeight <= 256)) {
if (mProof.keyHeight > 256) {
revert MerkleProofParserLibraryErrors.InvalidKeyHeight(mProof.keyHeight);
}
mProof.key = BaseParserLibrary.extractBytes32(src, 3);
Expand Down
4 changes: 2 additions & 2 deletions bridge/contracts/utils/MerkleProofLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ library MerkleProofLibrary {
bytes32 value,
uint16 proofHeight
) internal pure returns (bytes32) {
if (!(proofHeight <= 256 && proofHeight >= 0)) {
if (proofHeight > 256) {
revert MerkleProofLibraryErrors.InvalidProofHeight(proofHeight);
}

Expand Down Expand Up @@ -146,7 +146,7 @@ library MerkleProofLibrary {
);

uint16 proofIdx = 0;
if (!(proofHeight <= 256 && proofHeight >= 0)) {
if (proofHeight > 256) {
revert MerkleProofLibraryErrors.InvalidProofHeight(proofHeight);
}
for (uint256 i = 0; i < proofHeight; i++) {
Expand Down

0 comments on commit fd3db78

Please sign in to comment.