Skip to content

Commit

Permalink
feat(.sol): Change channelID array to singular channelID and remove u…
Browse files Browse the repository at this point in the history
…nnecessary functions

Signed-off-by: Sophia Koehler <sophia@perun.network>
  • Loading branch information
sophia1ch committed Jan 14, 2025
1 parent 1410701 commit 7aebc7a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 109 deletions.
52 changes: 14 additions & 38 deletions contracts/Adjudicator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract Adjudicator {
locked[s],
_channel.state
);
require(Channel.areBytes32ArraysEqual(subAlloc.ID, _state.channelID), "invalid sub-channel id");
require(subAlloc.ID == _state.channelID, "invalid sub-channel id");

Check warning on line 124 in contracts/Adjudicator.sol

View workflow job for this annotation

GitHub Actions / Build & Test

GC: Use Custom Errors instead of require statements

uint256[] memory _outcome;
(_outcome, nextIndex) = registerRecursive(
Expand Down Expand Up @@ -159,10 +159,7 @@ contract Adjudicator {
}

// If registered, require newer version and refutation timeout not passed.
(Dispute memory dispute, bool registered) = getDispute(state.channelID[Channel.findBackendIndex(
state.channelID,
state.outcome.backends
)]);
(Dispute memory dispute, bool registered) = getDispute(state.channelID);
if (registered) {
if (dispute.stateHash == hashState(state)) {
// Skip if same state.
Expand Down Expand Up @@ -206,10 +203,7 @@ contract Adjudicator {
uint256 actorIdx,
bytes memory sig
) external {
Dispute storage dispute = requireGetDispute(state.channelID[Channel.findBackendIndex(
state.channelID,
state.outcome.backends
)]);
Dispute storage dispute = requireGetDispute(state.channelID);
if (dispute.phase == uint8(DisputePhase.DISPUTE)) {
// solhint-disable-next-line not-rely-on-time
require(block.timestamp >= dispute.timeout, "timeout not passed");
Expand Down Expand Up @@ -292,10 +286,7 @@ contract Adjudicator {
Channel.validateSignatures(params, state, sigs);

// If registered, require not concluded.
(Dispute storage dispute, bool registered) = getDispute(state.channelID[Channel.findBackendIndex(
state.channelID,
state.outcome.backends
)]);
(Dispute storage dispute, bool registered) = getDispute(state.channelID);
if (registered) {
require(
dispute.phase != uint8(DisputePhase.CONCLUDED),
Expand Down Expand Up @@ -343,10 +334,7 @@ contract Adjudicator {
Channel.Params memory params,
Channel.State memory state
) internal pure {
require(state.channelID[Channel.findBackendIndex(
state.channelID,
state.outcome.backends
)] == channelID(params), "invalid params");
require(state.channelID == channelID(params), "invalid params");
}

/**
Expand All @@ -361,11 +349,7 @@ contract Adjudicator {
Channel.State memory state,
DisputePhase disputePhase
) internal {
uint256 zeroIndex = Channel.findBackendIndex(
state.channelID,
state.outcome.backends
);
(Dispute storage dispute, bool registered) = getDispute(state.channelID[zeroIndex]);
(Dispute storage dispute, bool registered) = getDispute(state.channelID);

dispute.challengeDuration = uint64(params.challengeDuration);
dispute.version = state.version;
Expand All @@ -386,7 +370,7 @@ contract Adjudicator {
dispute.timeout = uint64(block.timestamp) + dispute.challengeDuration;
}

setDispute(state.channelID[zeroIndex], dispute);
setDispute(state.channelID, dispute);
}

/**
Expand Down Expand Up @@ -465,11 +449,7 @@ contract Adjudicator {
* Reverts if the channel is already concluded.
*/
function concludeSingle(Channel.State memory state) internal {
uint64 zeroIndex = Channel.findBackendIndex(
state.channelID,
state.outcome.backends
);
Dispute storage dispute = requireGetDispute(state.channelID[zeroIndex]);
Dispute storage dispute = requireGetDispute(state.channelID);
require(dispute.stateHash == hashState(state), "invalid state");
require(
dispute.phase != uint8(DisputePhase.CONCLUDED),
Expand All @@ -485,7 +465,7 @@ contract Adjudicator {
require(block.timestamp >= dispute.timeout, "timeout not passed yet");
dispute.phase = uint8(DisputePhase.CONCLUDED);

setDispute(state.channelID[zeroIndex], dispute);
setDispute(state.channelID, dispute);
}

/**
Expand Down Expand Up @@ -520,7 +500,7 @@ contract Adjudicator {
for (uint256 i = 0; i < locked.length; ++i) {
Channel.SubAlloc memory subAlloc = locked[i];
Channel.State memory subState = subStates[nextIndex++];
require(Channel.areBytes32ArraysEqual(subAlloc.ID, subState.channelID), "invalid subchannel id");
require(subAlloc.ID == subState.channelID, "invalid subchannel id");

uint256[][] memory subOutcome;
(subOutcome, nextIndex) = forceConcludeRecursive(
Expand All @@ -546,23 +526,19 @@ contract Adjudicator {
* Reverts if the channel is not registered.
*/
function forceConcludeSingle(Channel.State memory state) internal {
uint64 zeroIndex = Channel.findBackendIndex(
state.channelID,
state.outcome.backends
);
Dispute storage dispute = requireGetDispute(state.channelID[zeroIndex]);
Dispute storage dispute = requireGetDispute(state.channelID);
require(dispute.stateHash == hashState(state), "invalid state");
if (dispute.phase != uint8(DisputePhase.CONCLUDED)) {
dispute.phase = uint8(DisputePhase.CONCLUDED);
setDispute(state.channelID[zeroIndex], dispute);
setDispute(state.channelID, dispute);
}
}

/**
* @dev pushOutcome sets the outcome at the asset holders.
*/
function pushOutcome(
bytes32[] memory channel,
bytes32 channel,
Channel.Asset[] memory assets,
Channel.Participant[] memory participants,
uint256[][] memory outcome
Expand All @@ -573,7 +549,7 @@ contract Adjudicator {
if (asset.ethHolder != address(0)) {
// solhint-disable-next-line calls-loop
AssetHolder(asset.ethHolder).setOutcome(
channel[a],
channel,
participants,
outcome[a]
);
Expand Down
2 changes: 1 addition & 1 deletion contracts/AssetHolder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract contract AssetHolder {
* @notice Address of the adjudicator contract that can call setOutcome.
* @dev Set by the constructor.
*/
address public immutable adjudicator;
address public adjudicator; // solhint-disable-line immutable-states

/**
* @notice The onlyAdjudicator modifier specifies functions that can only be called from the adjudicator contract.
Expand Down
33 changes: 3 additions & 30 deletions contracts/Channel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ library Channel {
}

struct State {
bytes32[] channelID;
bytes32 channelID;
uint64 version;
Allocation outcome;
bytes appData;
Expand All @@ -59,7 +59,7 @@ library Channel {

struct SubAlloc {
// ID is the channelID of the subchannel
bytes32[] ID; // solhint-disable-line var-name-mixedcase
bytes32 ID; // solhint-disable-line var-name-mixedcase
// balances holds the total balance of the subchannel of every asset.
uint256[] balances;
// indexMap maps each sub-channel participant to a parent channel
Expand Down Expand Up @@ -125,38 +125,11 @@ library Channel {
SubAlloc memory a,
SubAlloc memory b
) internal pure {
require(areBytes32ArraysEqual(a.ID, b.ID), "SubAlloc: unequal ID");
require(a.ID == b.ID, "SubAlloc: unequal ID");
Array.requireEqualUint256Array(a.balances, b.balances);
Array.requireEqualUint16Array(a.indexMap, b.indexMap);
}

// @dev areBytes32ArraysEqual checks if two arrays of bytes32 are equal.
function areBytes32ArraysEqual(bytes32[] memory arr1, bytes32[] memory arr2) internal pure returns (bool) {
if (arr1.length != arr2.length) {
return false;
}
for (uint i = 0; i < arr1.length; ++i) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}

/**
* @dev findBackendIndex finds out which of the channelIDs corresponds to the ethereum encoded channelID.
* Reverts if none of the channelIDs has a zero backend.
* Optimized for the common case of only one zero backend.
*/
function findBackendIndex(bytes32[] memory channel, uint256[] memory backends) internal pure returns (uint64) {
require(channel.length == backends.length, "Array lengths mismatch");

if (backends[0] == 1) return 0;
if (backends.length > 1 && backends[1] == 1) return 1;

return 0;
}

/// @dev Asserts that a and b are equal.
function requireEqualAsset(Asset memory a, Asset memory b) internal pure {
require(a.chainID == b.chainID, "unequal chainID");
Expand Down
6 changes: 0 additions & 6 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import "solidity-coverage";
const config: HardhatUserConfig = {
solidity: {
version: "0.8.28",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
};

Expand Down
Loading

0 comments on commit 7aebc7a

Please sign in to comment.