Skip to content

Commit

Permalink
Fix withdrawal test
Browse files Browse the repository at this point in the history
  • Loading branch information
AllFi committed Nov 23, 2023
1 parent 9b62c7c commit c84bee8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
43 changes: 24 additions & 19 deletions src/zkbob/sequencer/ZkBobSequencer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol
import {IERC20Permit} from "../../interfaces/IERC20Permit.sol";
import {EIP1967Admin} from "../../proxy/EIP1967Admin.sol";
import {Ownable} from "../../utils/Ownable.sol";
import {console2} from "forge-std/console2.sol";

contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {
using PriorityQueue for PriorityQueue.Queue;
Expand Down Expand Up @@ -130,9 +129,6 @@ contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {

(bool success, string memory revertReason) = _propagateToPool(ZkBobPool.transact.selector);

// We remove the commitment from the queue regardless of the result of the call to pool contract
// If we check that the prover is not malicious then the tx is not valid because of the limits or
// absence of funds so it can't be proved
if (success) {
emit Proved();
} else {
Expand Down Expand Up @@ -175,6 +171,16 @@ contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {
emit DirectDepositCommited();
}

/**
* @dev This function verifies that a direct deposit batch is the current pending operation and
* propagates the call to the pool contract. The signature of this function must be the same
* as the signature of the `ZkBobPool.appendDirectDeposits` function.
* @param _root_after new merkle tree root after append.
* @param _indices list of indices for queued pending deposits.
* @param _out_commit out commitment for output notes serialized from direct deposits.
* @param _batch_deposit_proof snark proof for batch deposit verifier.
* @param _tree_proof snark proof for tree update verifier.
*/
function proveDirectDeposit(
uint256 _root_after,
uint256[] calldata _indices,
Expand All @@ -197,7 +203,6 @@ contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {
}

(bool success, string memory revertReason) = _propagateToPool(ZkBobPool.appendDirectDeposits.selector);

if (success) {
emit Proved();
} else {
Expand All @@ -208,6 +213,20 @@ contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {
}
}

function pendingOperation() external view returns (PriorityOperation memory op) {
require(!priorityQueue.isEmpty(), "ZkBobSequencer: queue is empty");

uint256 head = priorityQueue.getFirstUnprocessedPriorityTx();
uint256 tail = priorityQueue.getTotalPriorityTxs();
for (uint256 i = head; i <= tail; i++) {
if (op.timestamp + expirationTime >= block.timestamp) {
op = priorityQueue.get(i);
break;
}
}
require(op.commitHash != bytes32(0), "ZkBobSequencer: no pending operations");
}

function _claimDepositProxyFee(uint16 _txType, bytes calldata _memo, uint256 _nullifier) internal {
int256 fee = int64(_parseProxyFee(_memo));
if (_txType == DEPOSIT) {
Expand Down Expand Up @@ -310,18 +329,4 @@ contract ZkBobSequencer is SequencerABIDecoder, EIP1967Admin, Ownable {
function _revertIfEquals(string memory reason, string memory revertReason) internal pure {
require(keccak256(abi.encodePacked((reason))) != keccak256(abi.encodePacked((revertReason))), revertReason);
}

function pendingOperation() external view returns (PriorityOperation memory op) {
require(!priorityQueue.isEmpty(), "ZkBobSequencer: queue is empty");

uint256 head = priorityQueue.getFirstUnprocessedPriorityTx();
uint256 tail = priorityQueue.getTotalPriorityTxs();
for (uint256 i = head; i <= tail; i++) {
if (op.timestamp + expirationTime >= block.timestamp) {
op = priorityQueue.get(i);
break;
}
}
require(op.commitHash != bytes32(0), "ZkBobSequencer: no pending operations");
}
}
32 changes: 18 additions & 14 deletions test/zkbob/ZkBobSequencer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ abstract contract AbstractZkBobPoolSequencerTest is AbstractForkTest {

assert(success);

vm.prank(prover1);
(success, ) = address(sequencer).call(abi.encodePacked(ZkBobSequencer.prove.selector, proveData));

assert(success);
Expand All @@ -587,16 +588,16 @@ abstract contract AbstractZkBobPoolSequencerTest is AbstractForkTest {
uint64 _proxyFee,
uint64 _proverFee,
address _prover,
address receiver) internal view returns (bytes memory commitData, bytes memory proveData) {

bytes32 nullifier = bytes32(_randFR());
// commitData = abi.encodePacked(nullifier);
bytes memory transfer_delta_bytes = abi.encodePacked(//28
uint48(0), //index 6
uint112(0), //energy 14
int64(-int256((_amount)))//8
);
commitData = abi.encodePacked(
address receiver
) internal view returns (bytes memory commitData, bytes memory proveData) {
bytes32 nullifier = bytes32(_randFR());
// commitData = abi.encodePacked(nullifier);
bytes memory transfer_delta_bytes = abi.encodePacked(//28
uint48(0), //index 6
uint112(0), //energy 14
int64(-int256((_amount)))//8
);
commitData = abi.encodePacked(
// ZkBobSequencer.commit.selector, //4
nullifier, //32 nullifier
new bytes(32), //32 out_commit
Expand Down Expand Up @@ -629,14 +630,17 @@ abstract contract AbstractZkBobPoolSequencerTest is AbstractForkTest {
commitData,
uint16(2), //withdrawal,
uint16(memo.length),
memo);
memo
);

proveData = abi.encodePacked(
proveData,
uint16(2), //withdrawal,
memo.length,
memo);
}
uint16(memo.length),
memo
);
}

function _encodeDeposit(
int256 _amount,
uint64 _proxyFee,
Expand Down

0 comments on commit c84bee8

Please sign in to comment.