move deposits length to a separate var
This commit is contained in:
parent
c4b81058ed
commit
b59fbcf169
@ -25,10 +25,12 @@ contract TornadoTrees is EnsResolve {
|
|||||||
uint256 public constant BYTES_SIZE = 32 + 32 + 4 + CHUNK_SIZE * ITEM_SIZE;
|
uint256 public constant BYTES_SIZE = 32 + 32 + 4 + CHUNK_SIZE * ITEM_SIZE;
|
||||||
uint256 public constant SNARK_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
uint256 public constant SNARK_FIELD = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
|
||||||
|
|
||||||
bytes32[] public deposits;
|
mapping(uint256 => bytes32) public deposits;
|
||||||
|
uint256 public depositsLength;
|
||||||
uint256 public lastProcessedDepositLeaf;
|
uint256 public lastProcessedDepositLeaf;
|
||||||
|
|
||||||
bytes32[] public withdrawals;
|
mapping(uint256 => bytes32) public withdrawals;
|
||||||
|
uint256 public withdrawalsLength;
|
||||||
uint256 public lastProcessedWithdrawalLeaf;
|
uint256 public lastProcessedWithdrawalLeaf;
|
||||||
|
|
||||||
bool public initialized;
|
bool public initialized;
|
||||||
@ -86,8 +88,6 @@ contract TornadoTrees is EnsResolve {
|
|||||||
lastProcessedWithdrawalLeaf = withdrawalLeaf;
|
lastProcessedWithdrawalLeaf = withdrawalLeaf;
|
||||||
|
|
||||||
uint256 i = depositLeaf;
|
uint256 i = depositLeaf;
|
||||||
|
|
||||||
// todo deposits.length = _tornadoTreesV1.deposits.length
|
|
||||||
while (true) {
|
while (true) {
|
||||||
(bool success, bytes memory data) = address(_tornadoTreesV1).staticcall{ gas: 3000 }( // todo define more specise gas value.
|
(bool success, bytes memory data) = address(_tornadoTreesV1).staticcall{ gas: 3000 }( // todo define more specise gas value.
|
||||||
abi.encodeWithSignature("deposits(uint256)", i)
|
abi.encodeWithSignature("deposits(uint256)", i)
|
||||||
@ -96,9 +96,10 @@ contract TornadoTrees is EnsResolve {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bytes32 deposit = abi.decode(data, (bytes32));
|
bytes32 deposit = abi.decode(data, (bytes32));
|
||||||
|
deposits[i] = deposit;
|
||||||
i++;
|
i++;
|
||||||
deposits.push(deposit);
|
|
||||||
}
|
}
|
||||||
|
depositsLength = depositLeaf + i;
|
||||||
|
|
||||||
i = withdrawalLeaf;
|
i = withdrawalLeaf;
|
||||||
while (true) {
|
while (true) {
|
||||||
@ -110,25 +111,28 @@ contract TornadoTrees is EnsResolve {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bytes32 withdrawal = abi.decode(data, (bytes32));
|
bytes32 withdrawal = abi.decode(data, (bytes32));
|
||||||
|
withdrawals[i] = withdrawal;
|
||||||
i++;
|
i++;
|
||||||
withdrawals.push(withdrawal);
|
|
||||||
}
|
}
|
||||||
|
withdrawalsLength = withdrawalLeaf + i;
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerDeposit(address _instance, bytes32 _commitment) external onlyTornadoProxy onlyInitialized {
|
function registerDeposit(address _instance, bytes32 _commitment) external onlyTornadoProxy onlyInitialized {
|
||||||
deposits.push(keccak256(abi.encode(_instance, _commitment, blockNumber())));
|
uint256 _depositsLength = depositsLength;
|
||||||
emit DepositData(_instance, _commitment, blockNumber(), deposits.length - 1);
|
deposits[_depositsLength] = keccak256(abi.encode(_instance, _commitment, blockNumber()));
|
||||||
|
emit DepositData(_instance, _commitment, blockNumber(), _depositsLength - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerWithdrawal(address _instance, bytes32 _nullifierHash) external onlyTornadoProxy onlyInitialized {
|
function registerWithdrawal(address _instance, bytes32 _nullifierHash) external onlyTornadoProxy onlyInitialized {
|
||||||
withdrawals.push(keccak256(abi.encode(_instance, _nullifierHash, blockNumber())));
|
uint256 _withdrawalsLength = withdrawalsLength;
|
||||||
emit WithdrawalData(_instance, _nullifierHash, blockNumber(), withdrawals.length - 1);
|
withdrawals[_withdrawalsLength] = keccak256(abi.encode(_instance, _nullifierHash, blockNumber()));
|
||||||
|
emit WithdrawalData(_instance, _nullifierHash, blockNumber(), _withdrawalsLength - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function migrate(TreeLeaf[] calldata _depositEvents, TreeLeaf[] calldata _withdrawalEvents) external {
|
function migrate(TreeLeaf[] calldata _depositEvents, TreeLeaf[] calldata _withdrawalEvents) external {
|
||||||
require(!initialized, "Already migrated");
|
require(!initialized, "Already migrated");
|
||||||
uint256 _lastProcessedDepositLeaf = lastProcessedDepositLeaf;
|
uint256 _lastProcessedDepositLeaf = lastProcessedDepositLeaf;
|
||||||
uint256 _depositLength = deposits.length;
|
uint256 _depositLength = depositsLength;
|
||||||
for (uint256 i = 0; i < _depositLength - _lastProcessedDepositLeaf; i++) {
|
for (uint256 i = 0; i < _depositLength - _lastProcessedDepositLeaf; i++) {
|
||||||
bytes32 leafHash = keccak256(abi.encode(_depositEvents[i].instance, _depositEvents[i].hash, _depositEvents[i].block));
|
bytes32 leafHash = keccak256(abi.encode(_depositEvents[i].instance, _depositEvents[i].hash, _depositEvents[i].block));
|
||||||
require(leafHash == deposits[_lastProcessedDepositLeaf + i], "Incorrect deposit");
|
require(leafHash == deposits[_lastProcessedDepositLeaf + i], "Incorrect deposit");
|
||||||
@ -140,7 +144,7 @@ contract TornadoTrees is EnsResolve {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 _withdrawalLength = withdrawals.length;
|
uint256 _withdrawalLength = withdrawalsLength;
|
||||||
uint256 _lastProcessedWithdrawalLeaf = lastProcessedWithdrawalLeaf;
|
uint256 _lastProcessedWithdrawalLeaf = lastProcessedWithdrawalLeaf;
|
||||||
for (uint256 i = 0; i < _withdrawalLength - _lastProcessedWithdrawalLeaf; i++) {
|
for (uint256 i = 0; i < _withdrawalLength - _lastProcessedWithdrawalLeaf; i++) {
|
||||||
bytes32 leafHash = keccak256(
|
bytes32 leafHash = keccak256(
|
||||||
@ -245,16 +249,16 @@ contract TornadoTrees is EnsResolve {
|
|||||||
require(_withdrawalRoot == withdrawalRoot || _withdrawalRoot == previousWithdrawalRoot, "Incorrect withdrawal tree root");
|
require(_withdrawalRoot == withdrawalRoot || _withdrawalRoot == previousWithdrawalRoot, "Incorrect withdrawal tree root");
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRegisteredDeposits() external view returns (uint256 count, bytes32[] memory _deposits) {
|
function getRegisteredDeposits() external view returns (bytes32[] memory _deposits) {
|
||||||
count = deposits.length - lastProcessedDepositLeaf;
|
uint256 count = depositsLength - lastProcessedDepositLeaf;
|
||||||
_deposits = new bytes32[](count);
|
_deposits = new bytes32[](count);
|
||||||
for (uint256 i = 0; i < count; i++) {
|
for (uint256 i = 0; i < count; i++) {
|
||||||
_deposits[i] = deposits[lastProcessedDepositLeaf + i];
|
_deposits[i] = deposits[lastProcessedDepositLeaf + i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRegisteredWithdrawals() external view returns (uint256 count, bytes32[] memory _withdrawals) {
|
function getRegisteredWithdrawals() external view returns (bytes32[] memory _withdrawals) {
|
||||||
count = withdrawals.length - lastProcessedWithdrawalLeaf;
|
uint256 count = withdrawalsLength - lastProcessedWithdrawalLeaf;
|
||||||
_withdrawals = new bytes32[](count);
|
_withdrawals = new bytes32[](count);
|
||||||
for (uint256 i = 0; i < count; i++) {
|
for (uint256 i = 0; i < count; i++) {
|
||||||
_withdrawals[i] = withdrawals[lastProcessedWithdrawalLeaf + i];
|
_withdrawals[i] = withdrawals[lastProcessedWithdrawalLeaf + i];
|
||||||
|
@ -37,9 +37,12 @@ contract TornadoTreesMock is TornadoTrees {
|
|||||||
uint256 _withdrawBlockNumber
|
uint256 _withdrawBlockNumber
|
||||||
) public {
|
) public {
|
||||||
setBlockNumber(_depositBlockNumber);
|
setBlockNumber(_depositBlockNumber);
|
||||||
deposits.push(keccak256(abi.encode(_instance, _commitment, blockNumber())));
|
deposits[depositsLength] = keccak256(abi.encode(_instance, _commitment, blockNumber()));
|
||||||
|
depositsLength++;
|
||||||
|
|
||||||
setBlockNumber(_withdrawBlockNumber);
|
setBlockNumber(_withdrawBlockNumber);
|
||||||
withdrawals.push(keccak256(abi.encode(_instance, _nullifier, blockNumber())));
|
withdrawals[withdrawalsLength] = keccak256(abi.encode(_instance, _nullifier, blockNumber()));
|
||||||
|
withdrawalsLength++;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDepositTreeMock(
|
function updateDepositTreeMock(
|
||||||
|
@ -122,7 +122,8 @@ describe('TornadoTrees', function () {
|
|||||||
describe('#getRegisteredDeposits', () => {
|
describe('#getRegisteredDeposits', () => {
|
||||||
it('should work', async () => {
|
it('should work', async () => {
|
||||||
const abi = new ethers.utils.AbiCoder()
|
const abi = new ethers.utils.AbiCoder()
|
||||||
let { count, _deposits } = await tornadoTrees.getRegisteredDeposits()
|
const count = await tornadoTrees.depositsLength()
|
||||||
|
const _deposits = await tornadoTrees.getRegisteredDeposits()
|
||||||
expect(count).to.be.equal(notes.length)
|
expect(count).to.be.equal(notes.length)
|
||||||
_deposits.forEach((hash, i) => {
|
_deposits.forEach((hash, i) => {
|
||||||
const encodedData = abi.encode(
|
const encodedData = abi.encode(
|
||||||
@ -149,7 +150,8 @@ describe('TornadoTrees', function () {
|
|||||||
describe('#getRegisteredWithdrawals', () => {
|
describe('#getRegisteredWithdrawals', () => {
|
||||||
it('should work', async () => {
|
it('should work', async () => {
|
||||||
const abi = new ethers.utils.AbiCoder()
|
const abi = new ethers.utils.AbiCoder()
|
||||||
let { count, _withdrawals } = await tornadoTrees.getRegisteredWithdrawals()
|
const count = await tornadoTrees.withdrawalsLength()
|
||||||
|
const _withdrawals = await tornadoTrees.getRegisteredWithdrawals()
|
||||||
expect(count).to.be.equal(notes.length)
|
expect(count).to.be.equal(notes.length)
|
||||||
_withdrawals.forEach((hash, i) => {
|
_withdrawals.forEach((hash, i) => {
|
||||||
const encodedData = abi.encode(
|
const encodedData = abi.encode(
|
||||||
|
Loading…
Reference in New Issue
Block a user