Skip to content

Commit

Permalink
Group/v2 (#92)
Browse files Browse the repository at this point in the history
* v2 impl setup

* Updates to contracts

* Compilation updates

* updates

* updates

* updates all existing tests working

* change naming remove comments

* passing in resourceID instead of entire anchor class

* Updates

* Fix vbridge vote proposal

* contracts compiling

* updates

* implement handler proposal in class

* More cleanup

* Updates

* Gets test passing

* Cleanup

* More updates to contracts / tests

* Fix tests

* fixed addEdges tests

* fix updateEdges tests

* fix cancelUpdateProposa.js

* fix createUpdateProposal.js

* fixes

* fixes

* fixes to historicalRootWithdraw.js

* fix sender is not the handler

* cleanup

Co-authored-by: Drew Stone <drewstone329@gmail.com>
  • Loading branch information
akileshtangella and drewstone authored Dec 28, 2021
1 parent 12a14ab commit af083c0
Show file tree
Hide file tree
Showing 79 changed files with 4,321 additions and 4,767 deletions.
1 change: 0 additions & 1 deletion contracts/AnchorProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ contract AnchorProxy {
Instance memory instance = instances[_anchor];
require(instance.state != InstanceState.DISABLED, "The instance is not supported");


instance.token.safeTransferFrom(msg.sender, address(this), _anchor.getDenomination()); //is .denomination correct?

_anchor.deposit{ value: msg.value }(_commitment);
Expand Down
52 changes: 0 additions & 52 deletions contracts/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import "./utils/Pausable.sol";
import "./utils/SafeMath.sol";
import "./utils/SafeCast.sol";
import "./interfaces/IExecutor.sol";
import "./interfaces/IDepositExecute.sol";
import "./interfaces/IERCHandler.sol";

/**
@title Facilitates deposits, creation and voting of deposit proposals, and deposit executions.
Expand Down Expand Up @@ -222,17 +220,6 @@ contract Bridge is Pausable, AccessControl, SafeMath {
handler.setResource(resourceID, executionContextAddress);
}

/**
@notice Sets a resource as burnable for handler contracts that use the IERCHandler interface.
@notice Only callable by an address that currently has the admin role.
@param handlerAddress Address of handler resource will be set for.
@param tokenAddress Address of contract to be called when a deposit is made and a deposited is executed.
*/
function adminSetBurnable(address handlerAddress, address tokenAddress) external onlyAdmin {
IERCHandler handler = IERCHandler(handlerAddress);
handler.setBurnable(tokenAddress);
}

/**
@notice Returns a proposal.
@param originChainID Chain ID deposit originated from.
Expand Down Expand Up @@ -266,45 +253,6 @@ contract Bridge is Pausable, AccessControl, SafeMath {
_fee = newFee.toUint128();
}

/**
@notice Used to manually withdraw funds from ERC safes.
@param handlerAddress Address of handler to withdraw from.
@param tokenAddress Address of token to withdraw.
@param recipient Address to withdraw tokens to.
@param amountOrTokenID Either the amount of ERC20 tokens or the ERC721 token ID to withdraw.
*/
function adminWithdraw(
address handlerAddress,
address tokenAddress,
address recipient,
uint256 amountOrTokenID
) external onlyAdmin {
IERCHandler handler = IERCHandler(handlerAddress);
handler.withdraw(tokenAddress, recipient, amountOrTokenID);
}

/**
@notice Initiates a transfer using a specified handler contract.
@notice Only callable when Bridge is not paused.
@param destinationChainID ID of chain deposit will be bridged to.
@param resourceID ResourceID used to find address of handler to be used for deposit.
@param data Additional data to be passed to specified handler.
@notice Emits {Deposit} event.
*/
function deposit(uint32 destinationChainID, bytes32 resourceID, bytes calldata data) external payable whenNotPaused {
require(msg.value == _fee, "Incorrect fee supplied");

address handler = _resourceIDToHandlerAddress[resourceID];
require(handler != address(0), "resourceID not mapped to handler");

uint64 nonce = ++_counts[destinationChainID];

IDepositExecute depositHandler = IDepositExecute(handler);
depositHandler.deposit(resourceID, destinationChainID, nonce, msg.sender, data);

emit Deposit(destinationChainID, resourceID, nonce);
}

/**
"Creates" the proposal upon first vote.
@notice When called, {msg.sender} will be marked as voting in favor of proposal.
Expand Down
2 changes: 0 additions & 2 deletions contracts/SignatureBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import "./utils/SafeMath.sol";
import "./utils/SafeCast.sol";
import "./utils/Governable.sol";
import "./interfaces/IExecutor.sol";
import "./interfaces/IDepositExecute.sol";
import "./interfaces/IERCHandler.sol";
import "hardhat/console.sol";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

pragma solidity ^0.8.0;

import "../../trees/MerkleTreePoseidon.sol";
import "../../interfaces/IVerifier.sol";
import "../trees/MerkleTreePoseidon.sol";
import "../interfaces/IAnchorVerifier.sol";
import "../interfaces/ILinkableAnchor.sol";
import "./LinkableTree.sol";

abstract contract AnchorBaseV2 is LinkableTree {
IVerifier public immutable verifier;
abstract contract AnchorBase is LinkableTree {
IAnchorVerifier public verifier;
uint32 proposalNonce = 0;

// map to store used nullifier hashes
mapping(bytes32 => bool) public nullifierHashes;
Expand All @@ -26,26 +28,29 @@ abstract contract AnchorBaseV2 is LinkableTree {
@param _merkleTreeHeight the height of deposits' Merkle Tree
*/
constructor(
IVerifier _verifier,
address _handler,
IAnchorVerifier _verifier,
IPoseidonT3 _hasher,
uint32 _merkleTreeHeight,
uint8 _maxEdges
) LinkableTree(_hasher, _merkleTreeHeight, _maxEdges) {
) LinkableTree(_handler, _hasher, _merkleTreeHeight, _maxEdges) {
verifier = _verifier;
}

/**
@dev Inserts a commitment into the tree
@param _commitment the note commitment = Poseidon(chainId, nullifier, secret)
*/
function insert(bytes32 _commitment) external payable nonReentrant {
function insert(bytes32 _commitment) public payable nonReentrant returns(uint32) {
require(!commitments[_commitment], "The commitment has been submitted");

uint32 insertedIndex = _insert(_commitment);
commitments[_commitment] = true;
_processInsertion();

emit Insertion(_commitment, insertedIndex, block.timestamp);

return insertedIndex;
}

/** @dev this function is defined in a child contract */
Expand All @@ -64,7 +69,8 @@ abstract contract AnchorBaseV2 is LinkableTree {
r = verifier.verifyProof(
a, b, c,
_input,
maxEdges
maxEdges,
true
);
require(r, "Invalid withdraw proof");
return r;
Expand All @@ -90,4 +96,31 @@ abstract contract AnchorBaseV2 is LinkableTree {
[_proof[6], _proof[7]]
);
}

/** @dev whether a note is already spent */
function isSpent(bytes32 _nullifierHash) public view returns (bool) {
return nullifierHashes[_nullifierHash];
}

/** @dev whether an array of notes is already spent */
function isSpentArray(bytes32[] calldata _nullifierHashes) external view returns (bool[] memory spent) {
spent = new bool[](_nullifierHashes.length);
for (uint256 i = 0; i < _nullifierHashes.length; i++) {
if (isSpent(_nullifierHashes[i])) {
spent[i] = true;
}
}
}

function setHandler(address newHandler, uint32 nonce) onlyHandler external {
require(newHandler != address(0), "Handler cannot be 0");
require(proposalNonce < nonce, "Invalid nonce");
require(nonce <= proposalNonce + 1, "Nonce must increment by 1");
handler = newHandler;
}

function setVerifier(address newVerifier) onlyHandler external {
require(newVerifier != address(0), "Handler cannot be 0");
verifier = IAnchorVerifier(newVerifier);
}
}
Loading

0 comments on commit af083c0

Please sign in to comment.