Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change height to leaf index in Solidity and tests #35

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/anchors/bridged/2/AnchorBase2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract contract AnchorBase2 is MerkleTreePoseidon, ReentrancyGuard {
struct Edge {
uint256 chainID;
bytes32 root;
uint256 height;
uint256 latestLeafIndex;
}

// maps sourceChainID to the index in the edge list
Expand All @@ -54,8 +54,8 @@ abstract contract AnchorBase2 is MerkleTreePoseidon, ReentrancyGuard {
event Deposit(bytes32 indexed commitment, uint32 leafIndex, uint256 timestamp);
event Withdrawal(address to, bytes32 nullifierHash, address indexed relayer, uint256 fee);
// bridge events
event EdgeAddition(uint256 chainID, uint256 height, bytes32 merkleRoot);
event EdgeUpdate(uint256 chainID, uint256 height, bytes32 merkleRoot);
event EdgeAddition(uint256 chainID, uint256 latestLeafIndex, bytes32 merkleRoot);
event EdgeUpdate(uint256 chainID, uint256 latestLeafIndex, bytes32 merkleRoot);
event RootHistoryRecorded(uint timestamp, bytes32[1] roots);
event RootHistoryUpdate(uint timestamp, bytes32[1] roots);

Expand Down
14 changes: 7 additions & 7 deletions contracts/anchors/bridged/2/LinkableAnchor2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ abstract contract LinkableAnchor2 is AnchorBase2, ILinkableAnchor {
function addEdge(
uint256 sourceChainID,
bytes32 root,
uint256 height
uint256 leafIndex
) onlyHandler override external payable nonReentrant {
require(edgeList.length < 1, "This Anchor is at capacity");
edgeExistsForChain[sourceChainID] = true;
uint index = edgeList.length;
Edge memory edge = Edge({
chainID: sourceChainID,
root: root,
height: height
latestLeafIndex: leafIndex
});
edgeList.push(edge);
edgeIndex[sourceChainID] = index;
// add to root histories
uint32 neighborRootIndex = 0;
neighborRoots[sourceChainID][neighborRootIndex] = root;
emit EdgeAddition(sourceChainID, height, root);
emit EdgeAddition(sourceChainID, leafIndex, root);
// emit update event
bytes32[1] memory neighbors = getLatestNeighborRoots();
emit RootHistoryUpdate(block.timestamp, neighbors);
Expand All @@ -62,22 +62,22 @@ abstract contract LinkableAnchor2 is AnchorBase2, ILinkableAnchor {
function updateEdge(
uint256 sourceChainID,
bytes32 root,
uint256 height
uint256 leafIndex
) onlyHandler override external payable nonReentrant {
require(edgeExistsForChain[sourceChainID], "Chain must be integrated from the bridge before updates");
require(edgeList[edgeIndex[sourceChainID]].height < height, "New height must be greater");
require(edgeList[edgeIndex[sourceChainID]].latestLeafIndex < leafIndex, "New leaf index must be greater");
uint index = edgeIndex[sourceChainID];
// update the edge in the edge list
edgeList[index] = Edge({
chainID: sourceChainID,
root: root,
height: height
latestLeafIndex: leafIndex
});
// add to root histories
uint32 neighborRootIndex = (currentNeighborRootIndex[sourceChainID] + 1) % ROOT_HISTORY_SIZE;
currentNeighborRootIndex[sourceChainID] = neighborRootIndex;
neighborRoots[sourceChainID][neighborRootIndex] = root;
emit EdgeUpdate(sourceChainID, height, root);
emit EdgeUpdate(sourceChainID, leafIndex, root);
// emit update event
bytes32[1] memory neighbors = getLatestNeighborRoots();
emit RootHistoryUpdate(block.timestamp, neighbors);
Expand Down
18 changes: 9 additions & 9 deletions contracts/handlers/AnchorHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract AnchorHandler is IExecutor, HandlerHelpers {
uint256 _sourceChainID;
bytes32 _resourceID;
bytes32 _merkleRoot;
uint256 _blockHeight;
uint256 _leafIndex;
}

// sourceChainID => height => Update Record
Expand Down Expand Up @@ -73,18 +73,18 @@ contract AnchorHandler is IExecutor, HandlerHelpers {
@notice Proposal execution should be initiated when a proposal is finalized in the Bridge contract.
by a relayer on the deposit's destination chain.
@param resourceID ResourceID corresponding to a particular set of Anchors
@param data Consists of {sourceChainID}, {blockHeight}, {merkleRoot} all padded to 32 bytes.
@param data Consists of {sourceChainID}, {leafIndex}, {merkleRoot} all padded to 32 bytes.
@notice Data passed into the function should be constructed as follows:
chainID uint256 bytes 0 - 32
blockHeight uint256 bytes 32 - 64
chainID uint256 bytes 0 - 32
leafIndex uint256 bytes 32 - 64
merkleRoot uint256 bytes 64 - 96
*/
function executeProposal(bytes32 resourceID, bytes calldata data) external override onlyBridge {
uint256 sourceChainId;
uint256 blockHeight;
uint256 leafIndex;
uint256 merkleRoot;

(sourceChainId, blockHeight, merkleRoot) = abi.decode(data, (uint256, uint, uint));
(sourceChainId, leafIndex, merkleRoot) = abi.decode(data, (uint256, uint, uint));

address anchorAddress = _resourceIDToContractAddress[resourceID];

Expand All @@ -96,13 +96,13 @@ contract AnchorHandler is IExecutor, HandlerHelpers {
anchor.updateEdge(
sourceChainId,
bytes32(merkleRoot),
blockHeight
leafIndex
);
} else {
anchor.addEdge(
sourceChainId,
bytes32(merkleRoot),
blockHeight
leafIndex
);
}

Expand All @@ -112,7 +112,7 @@ contract AnchorHandler is IExecutor, HandlerHelpers {
sourceChainId,
resourceID,
bytes32(merkleRoot),
blockHeight
leafIndex
);
}
}
4 changes: 2 additions & 2 deletions contracts/interfaces/ILinkableAnchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface ILinkableAnchor {
function addEdge(
uint256 sourceChainID,
bytes32 root,
uint256 height
uint256 latestLeafIndex
) external payable;
function updateEdge(
uint256 sourceChainID,
bytes32 root,
uint256 height
uint256 latestLeafIndex
) external payable;
}
6 changes: 3 additions & 3 deletions scripts/evm/linkAnchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ async function depositAndProposeAndExecute({
const data = receipt.events[2].args;
// @ts-ignore
const updateNonce = data[1];
const originBlockHeight = receipt.blockNumber;
const originLatestLeafIndex = data.leafIndex;

const originChainID = await originWallet.getChainId();
const originMerkleRoot = await originAnchor.getLastRoot();
// create correct update proposal data for the deposit on origin chain
const updateData = helpers.createUpdateProposalData(originChainID, originBlockHeight, originMerkleRoot);
console.log('Created update data w/ args', originChainID, originBlockHeight, originMerkleRoot, updateData)
const updateData = helpers.createUpdateProposalData(originChainID, originLatestLeafIndex, originMerkleRoot);
console.log('Created update data w/ args', originChainID, originLatestLeafIndex, originMerkleRoot, updateData)
const dataHash = ethers.utils.keccak256(destAnchorHandler.address + updateData.substr(2));
// create destination resourceID to create proposals to update against
const destChainId = await destWallet.getChainId();
Expand Down
20 changes: 10 additions & 10 deletions test/anchor/addEdges.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ contract('LinkableAnchor - [add edges]', async accounts => {
addEdge = (edge, sender) => AnchorInstance.addEdge(
edge.sourceChainID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: sender }
)

updateEdge = (edge, sender) => AnchorInstance.updateEdge(
edge.sourceChainID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: sender }
)
});
Expand All @@ -86,7 +86,7 @@ contract('LinkableAnchor - [add edges]', async accounts => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

await TruffleAssert.passes(addEdge(edge, accounts[0]));
Expand All @@ -101,7 +101,7 @@ contract('LinkableAnchor - [add edges]', async accounts => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

await TruffleAssert.passes(addEdge(edge, accounts[0]));
Expand All @@ -113,13 +113,13 @@ contract('LinkableAnchor - [add edges]', async accounts => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

const edge1 = {
sourceChainID: '0x02',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

await TruffleAssert.passes(addEdge(edge, accounts[0]));
Expand All @@ -132,7 +132,7 @@ contract('LinkableAnchor - [add edges]', async accounts => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

await TruffleAssert.passes(addEdge(edge, accounts[0]));
Expand All @@ -146,22 +146,22 @@ contract('LinkableAnchor - [add edges]', async accounts => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

const result = await addEdge(edge, accounts[0]);

TruffleAssert.eventEmitted(result, 'EdgeAddition', (ev) => {
return ev.chainID == parseInt(edge.sourceChainID, 16) &&
ev.height == edge.height && ev.merkleRoot == edge.root
ev.latestLeafIndex == edge.latestLeafIndex && ev.merkleRoot == edge.root
});
});

it('Adding edge should emit correct RootHistoryUpdate event', async () => {
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 1,
latestLeafIndex: 1,
};

const result = await addEdge(edge, accounts[0]);
Expand Down
4 changes: 2 additions & 2 deletions test/anchor/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ contract('Anchor2', (accounts) => {
edge.destChainID,
edge.destResourceID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: _sender }
)

updateEdge = (edge, _sender) => AnchorInstance.updateEdge(
edge.destChainID,
edge.destResourceID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: _sender }
)

Expand Down
24 changes: 12 additions & 12 deletions test/anchor/updateEdges.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ const Token = artifacts.require("ERC20Mock");
addEdge = (edge, sender) => AnchorInstance.addEdge(
edge.sourceChainID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: sender }
)

updateEdge = (edge, sender) => AnchorInstance.updateEdge(
edge.sourceChainID,
edge.root,
edge.height,
edge.latestLeafIndex,
{ from: sender }
)
});
Expand All @@ -85,12 +85,12 @@ const Token = artifacts.require("ERC20Mock");
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 100,
latestLeafIndex: 100,
};
const edgeUpdated = {
sourceChainID: '0x01',
root: '0x2222111111111111111111111111111111111111111111111111111111111111',
height: 101,
latestLeafIndex: 101,
};

await TruffleAssert.passes(addEdge(edge, accounts[0]));
Expand All @@ -102,12 +102,12 @@ const Token = artifacts.require("ERC20Mock");
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 100,
latestLeafIndex: 100,
};
const edgeUpdated = {
sourceChainID: '0x02',
root: '0x2222111111111111111111111111111111111111111111111111111111111111',
height: 101,
latestLeafIndex: 101,
};
await TruffleAssert.passes(addEdge(edge, accounts[0]));
await TruffleAssert.reverts(updateEdge(edgeUpdated, accounts[0]));
Expand All @@ -117,12 +117,12 @@ const Token = artifacts.require("ERC20Mock");
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 100,
latestLeafIndex: 100,
};
const edgeUpdated = {
sourceChainID: '0x01',
root: '0x2222111111111111111111111111111111111111111111111111111111111111',
height: 101,
latestLeafIndex: 101,
};
await TruffleAssert.passes(addEdge(edge, accounts[0]));

Expand All @@ -141,12 +141,12 @@ const Token = artifacts.require("ERC20Mock");
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 100,
latestLeafIndex: 100,
};
const edgeUpdated = {
sourceChainID: '0x01',
root: '0x2222111111111111111111111111111111111111111111111111111111111111',
height: 101,
latestLeafIndex: 101,
};
await addEdge(edge, accounts[0]);
const result = await updateEdge(edgeUpdated, accounts[0]);
Expand All @@ -160,12 +160,12 @@ const Token = artifacts.require("ERC20Mock");
const edge = {
sourceChainID: '0x01',
root: '0x1111111111111111111111111111111111111111111111111111111111111111',
height: 100,
latestLeafIndex: 100,
};
const edgeUpdated = {
sourceChainID: '0x01',
root: '0x2222111111111111111111111111111111111111111111111111111111111111',
height: 101,
latestLeafIndex: 101,
};
await addEdge(edge, accounts[0]);
const result = await updateEdge(edgeUpdated, accounts[0]);
Expand Down
6 changes: 3 additions & 3 deletions test/anchorbridge/cancelUpdateProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ contract('Bridge - [CancelUpdateProposal with relayerThreshold == 3]', async (ac
const expectedUpdateNonce = 1;
const relayerThreshold = 3;
const merkleTreeHeight = 31;
const blockHeight = 1;
const sender = accounts[5]
let merkleRoot;
let AnchorInstance;
Expand Down Expand Up @@ -76,7 +75,8 @@ contract('Bridge - [CancelUpdateProposal with relayerThreshold == 3]', async (ac

await token.mint(sender, tokenDenomination);
await token.increaseAllowance(AnchorInstance.address, 1000000000, {from: sender});
await AnchorInstance.deposit('0x1111111111111111111111111111111111111111111111111111111111111111', {from: sender});
let { logs } = await AnchorInstance.deposit('0x1111111111111111111111111111111111111111111111111111111111111111', {from: sender});
let latestLeafIndex = logs[0].args.leafIndex;
merkleRoot = await AnchorInstance.getLastRoot();

resourceID = Helpers.createResourceID(AnchorInstance.address, originChainID);
Expand All @@ -89,7 +89,7 @@ contract('Bridge - [CancelUpdateProposal with relayerThreshold == 3]', async (ac
initialContractAddresses,
);

data = Helpers.createUpdateProposalData(originChainID, blockHeight, merkleRoot);
data = Helpers.createUpdateProposalData(originChainID, latestLeafIndex, merkleRoot);
dataHash = Ethers.utils.keccak256(DestinationAnchorHandlerInstance.address + data.substr(2));

await Promise.all([
Expand Down
Loading