Skip to content

Commit

Permalink
Merge pull request thehubbleproject#88 from thehubbleproject/rm-burnc…
Browse files Browse the repository at this point in the history
…onsent-cancel

rm burn consent cancel
  • Loading branch information
ChihChengLiang authored Jul 24, 2020
2 parents dbc11fe + f6a76bc commit f4b06dd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 49 deletions.
14 changes: 1 addition & 13 deletions contracts/BurnConsent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ contract BurnConsent is FraudProofHelpers {
Types.BurnConsent memory _tx
) public view returns (bytes memory updatedAccount, bytes32 newRoot) {
Types.UserAccount memory account = _merkle_proof.accountIP.account;
if (_tx.cancel) {
account.burn -= _tx.amount;
} else {
account.burn += _tx.amount;
}
account.burn = _tx.amount;
account.nonce++;
newRoot = UpdateAccountWithSiblings(account, _merkle_proof);
updatedAccount = RollupUtils.BytesFromAccount(account);
Expand Down Expand Up @@ -166,14 +162,6 @@ contract BurnConsent is FraudProofHelpers {
false
);
}
if (_tx.cancel && account.burn < _tx.amount) {
return (
ZERO_BYTES32,
"",
Types.ErrorCode.InvalidCancelBurnAmount,
false
);
}

if (_tx.nonce != account.nonce.add(1)) {
return (ZERO_BYTES32, "", Types.ErrorCode.BadNonce, false);
Expand Down
30 changes: 11 additions & 19 deletions contracts/libs/RollupUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -514,17 +514,15 @@ library RollupUtils {
pure
returns (bytes memory)
{
return
abi.encodePacked(_tx.fromIndex, _tx.amount, _tx.nonce, _tx.cancel);
return abi.encodePacked(_tx.fromIndex, _tx.amount, _tx.nonce);
}

function BytesFromBurnConsentNoStruct(
uint256 fromIndex,
uint256 amount,
uint256 nonce,
bool cancel
uint256 nonce
) public pure returns (bytes memory) {
return abi.encode(fromIndex, amount, nonce, cancel);
return abi.encode(fromIndex, amount, nonce);
}

function BurnConsentFromBytes(bytes memory txBytes)
Expand All @@ -533,50 +531,45 @@ library RollupUtils {
returns (Types.BurnConsent memory)
{
Types.BurnConsent memory _tx;
(_tx.fromIndex, _tx.amount, _tx.nonce, _tx.cancel) = abi.decode(
(_tx.fromIndex, _tx.amount, _tx.nonce) = abi.decode(
txBytes,
(uint256, uint256, uint256, bool)
(uint256, uint256, uint256)
);
return _tx;
}

function BurnConsentSignBytes(
uint256 fromIndex,
uint256 amount,
uint256 nonce,
bool cancel
uint256 nonce
) public pure returns (bytes32) {
return
keccak256(
BytesFromBurnConsentNoStruct(fromIndex, amount, nonce, cancel)
);
keccak256(BytesFromBurnConsentNoStruct(fromIndex, amount, nonce));
}

function CompressBurnConsent(Types.BurnConsent memory _tx)
public
pure
returns (bytes memory)
{
return abi.encode(_tx.fromIndex, _tx.amount, _tx.cancel, _tx.signature);
return abi.encode(_tx.fromIndex, _tx.amount, _tx.signature);
}

function CompressBurnConsentNoStruct(
uint256 fromIndex,
uint256 amount,
uint256 nonce,
bool cancel,
bytes memory sig
) public pure returns (bytes memory) {
return abi.encode(fromIndex, amount, nonce, cancel, sig);
return abi.encode(fromIndex, amount, nonce, sig);
}

function CompressBurnConsentWithMessage(
bytes memory message,
bytes memory sig
) public pure returns (bytes memory) {
Types.BurnConsent memory _tx = BurnConsentFromBytes(message);
return
abi.encode(_tx.fromIndex, _tx.amount, _tx.nonce, _tx.cancel, sig);
return abi.encode(_tx.fromIndex, _tx.amount, _tx.nonce, sig);
}

function DecompressBurnConsent(bytes memory txBytes)
Expand All @@ -586,11 +579,10 @@ library RollupUtils {
uint256 fromIndex,
uint256 amount,
uint256 nonce,
bool cancel,
bytes memory signature
)
{
return abi.decode(txBytes, (uint256, uint256, uint256, bool, bytes));
return abi.decode(txBytes, (uint256, uint256, uint256, bytes));
}

function HashFromBurnConsent(Types.BurnConsent memory _tx)
Expand Down
2 changes: 0 additions & 2 deletions contracts/libs/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ library Types {
uint256 fromIndex;
uint256 amount;
uint256 nonce;
bool cancel;
bytes signature;
}

Expand Down Expand Up @@ -135,7 +134,6 @@ library Types {
NotEnoughTokenBalance,
BadFromTokenType,
BadToTokenType,
InvalidCancelBurnAmount,
BadFromIndex,
BurnAlreadyExecuted,
NotCreatingOnZeroAccount,
Expand Down
2 changes: 0 additions & 2 deletions scripts/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export enum ErrorCode {
NotEnoughTokenBalance,
BadFromTokenType,
BadToTokenType,
InvalidCancelBurnAmount,
BadFromIndex,
BurnAlreadyExecuted,
NotCreatingOnZeroAccount,
Expand All @@ -84,7 +83,6 @@ export interface BurnConsentTx {
fromIndex: number;
amount: number;
nonce: number;
cancel: boolean;
signature: string;
}

Expand Down
10 changes: 3 additions & 7 deletions test/Reddit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,18 @@ contract("Reddit", async function() {
const tx = {
fromIndex: User.AccID,
amount: 5,
nonce: userMP.accountIP.account.nonce + 1,
cancel: false
nonce: userMP.accountIP.account.nonce + 1
} as BurnConsentTx;
const signBytes = await RollupUtilsInstance.BurnConsentSignBytes(
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel
tx.nonce
);
tx.signature = utils.sign(signBytes, User.Wallet);
const txBytes = await RollupUtilsInstance.BytesFromBurnConsentNoStruct(
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel
tx.nonce
);
await RollupUtilsInstance.BurnConsentFromBytes(txBytes);

Expand Down Expand Up @@ -480,7 +477,6 @@ contract("Reddit", async function() {
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel,
tx.signature
);
await RollupUtilsInstance.DecompressBurnConsent(compressedTx);
Expand Down
8 changes: 2 additions & 6 deletions test/utils/rollup-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,23 @@ contract("RollupUtils", async function(accounts) {
fromIndex: 1,
amount: 5,
nonce: 3,
cancel: false,
signature: "0xabcd"
};
const signBytes = await RollupUtilsInstance.BurnConsentSignBytes(
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel
tx.nonce
);
const txBytes = await RollupUtilsInstance.BytesFromBurnConsentNoStruct(
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel
tx.nonce
);
await RollupUtilsInstance.BurnConsentFromBytes(txBytes);
const compressedTx = await RollupUtilsInstance.CompressBurnConsentNoStruct(
tx.fromIndex,
tx.amount,
tx.nonce,
tx.cancel,
tx.signature
);
await RollupUtilsInstance.DecompressBurnConsent(compressedTx);
Expand Down

0 comments on commit f4b06dd

Please sign in to comment.