Skip to content

Commit

Permalink
Regroups assert statements as described in raiden-network#47.
Browse files Browse the repository at this point in the history
  • Loading branch information
err508 committed Aug 23, 2018
1 parent 8f1b366 commit d571ee3
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions raiden_contracts/contracts/TokenNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,6 @@ contract TokenNetwork is Utils {

Channel storage channel = channels[channel_identifier];

// We always increase the channel counter, therefore no channel data can already exist,
// corresponding to this channel_identifier. This check must never fail.
assert(channel.settle_block_number == 0);
assert(channel.state == ChannelState.NonExistent);

// Store channel information
channel.settle_block_number = settle_timeout;
channel.state = ChannelState.Opened;
Expand All @@ -259,6 +254,10 @@ contract TokenNetwork is Utils {
participant2,
settle_timeout
);
// We always increase the channel counter, therefore no channel data can already exist,
// corresponding to this channel_identifier. This check must never fail.
assert(channel.settle_block_number == 0);
assert(channel.state == ChannelState.NonExistent);

return channel_identifier;
}
Expand Down Expand Up @@ -298,9 +297,6 @@ contract TokenNetwork is Utils {
require(added_deposit > 0);
// Underflow check; we use <= because added_deposit == total_deposit for the first deposit
require(added_deposit <= total_deposit);
// This should never fail at this point. Added check for security, because we directly set
// the participant_state.deposit = total_deposit, while we transfer `added_deposit` tokens.
assert(participant_state.deposit + added_deposit == total_deposit);

// Update the participant's channel deposit
participant_state.deposit = total_deposit;
Expand All @@ -316,6 +312,10 @@ contract TokenNetwork is Utils {
participant_state.deposit
);

// This should never fail at this point. Added check for security, because we directly set
// the participant_state.deposit = total_deposit, while we transfer `added_deposit` tokens.
assert(participant_state.deposit + added_deposit == total_deposit);

// Do the transfer
require(token.transferFrom(msg.sender, address(this), added_deposit));
}
Expand Down Expand Up @@ -365,9 +365,6 @@ contract TokenNetwork is Utils {
Participant storage partner_state = channel.participants[partner];

total_deposit = participant_state.deposit + partner_state.deposit;
// This should never happen, as we have an overflow check in setTotalDeposit
assert(total_deposit >= participant_state.deposit);
assert(total_deposit >= partner_state.deposit);

// Entire withdrawn amount must not be bigger than the current channel deposit
require(total_withdraw <= (total_deposit - partner_state.withdrawn_amount));
Expand Down Expand Up @@ -397,6 +394,10 @@ contract TokenNetwork is Utils {
// Do the tokens transfer
require(token.transfer(participant, current_withdraw));

// This should never happen, as we have an overflow check in setTotalDeposit
assert(total_deposit >= participant_state.deposit);
assert(total_deposit >= partner_state.deposit);

// A withdraw should never happen if a participant already has a
// balance proof in storage
assert(participant_state.nonce == 0);
Expand Down Expand Up @@ -1191,9 +1192,6 @@ contract TokenNetwork is Utils {
// ordered values
require(participant2_max_transferred >= participant1_max_transferred);

assert(participant1_max_transferred >= participant1_settlement.transferred);
assert(participant2_max_transferred >= participant2_settlement.transferred);

// This is the maximum amount that participant1 can receive at settlement time
participant1_net_max_received = (
participant2_max_transferred -
Expand All @@ -1212,6 +1210,10 @@ contract TokenNetwork is Utils {
participant1_max_amount,
participant1_settlement.withdrawn
);

assert(participant1_max_transferred >= participant1_settlement.transferred);
assert(participant2_max_transferred >= participant2_settlement.transferred);

return participant1_max_amount;
}

Expand Down

0 comments on commit d571ee3

Please sign in to comment.