Skip to content

Commit

Permalink
refactor(Test): verifiers refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Apr 16, 2024
1 parent e440f3c commit b941a86
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 383 deletions.
198 changes: 76 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

48 changes: 20 additions & 28 deletions scripts/GasEstimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
SupportedSmartWallet,
SupportedSmartWalletName,
getSmartWalletAddress,
addSwapHash,
} from '../test/utils/TestUtils';
import { TestSwap, TestVerifierEverythingAccepted } from 'typechain-types';
import { Penalizer, RelayHub } from '@rsksmart/rif-relay-contracts';
Expand Down Expand Up @@ -385,18 +386,6 @@ async function estimateRelayCost(fees = NO_FEES, payment: Payment = 'erc20') {
},
};

const {
smartWalletTokenBalance: smartWalletInitialBalance,
relayWorkerTokenBalance: workerInitialBalance,
relayWorkerRBTCBalance: workerInitialBalanceRBTC,
ownerRBTCBalance: ownerInitialBalanceRBTC,
} = await getBalances({
owner,
smartWalletAddress: smartWallet.address,
token,
relayWorker,
});

const transferReceiver = ethers.Wallet.createRandom();

const { data, to, gas } = await getDestinationContractCallParams(
Expand All @@ -421,6 +410,18 @@ async function estimateRelayCost(fees = NO_FEES, payment: Payment = 'erc20') {
owner
);

const {
smartWalletTokenBalance: smartWalletInitialBalance,
relayWorkerTokenBalance: workerInitialBalance,
relayWorkerRBTCBalance: workerInitialBalanceRBTC,
ownerRBTCBalance: ownerInitialBalanceRBTC,
} = await getBalances({
owner,
smartWalletAddress: smartWallet.address,
token,
relayWorker,
});

const relayCallResult = await relayHub
.connect(relayWorker)
.relayCall(completeReq, signature, {
Expand Down Expand Up @@ -694,22 +695,13 @@ async function getExecutionParameters(
swap: TestSwap,
swAddress: string
): Promise<DestinationContractCallParams> {
const timelock = 500;
const preimageHash = utils.soliditySha256(['bytes32'], [constants.HashZero]);

const encodedFunction = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,uint256)',
[constants.HashZero, CLAIMED_AMOUNT, constants.AddressZero, timelock]
);

const hash = await swap.hashValues(
preimageHash,
CLAIMED_AMOUNT,
swAddress,
constants.AddressZero,
timelock
);
await swap.addSwap(hash);
const encodedFunction = await addSwapHash({
swap,
amount: CLAIMED_AMOUNT,
claimAddress: swAddress,
refundAddress: Wallet.createRandom().address,
external: true,
});

const estimatedDestinationCallGasCorrected =
await getEstimatedGasWithCorrection(
Expand Down
73 changes: 17 additions & 56 deletions test/RelayHub.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { ethers } from 'hardhat';
import { BigNumber, Wallet, constants, providers, utils } from 'ethers';
import { BigNumber, Wallet, constants, providers } from 'ethers';
import {
UtilToken,
SmartWalletFactory,
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
evmMineMany,
deployRelayHub,
deployContract,
addSwapHash,
} from './utils/TestUtils';
import { RelayWorkersAddedEvent } from 'typechain-types/@rsksmart/rif-relay-contracts/contracts/RelayHub';

Expand Down Expand Up @@ -1006,32 +1007,13 @@ describe('RelayHub', function () {
constants.AddressZero,
nextWalletIndex
);

claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('should fail if revert from destination contract', async function () {
Expand Down Expand Up @@ -1223,9 +1205,6 @@ describe('RelayHub', function () {
let minimalBoltzFactory: MinimalBoltzSmartWalletFactory;
let claimedValue: BigNumber;
let smartWalletAddress: string;
const timelock = 500;
let refundAddress: string;
let preimageHash: string;

beforeEach(async function () {
const smartWalletTemplate =
Expand All @@ -1248,42 +1227,24 @@ describe('RelayHub', function () {
constants.AddressZero,
nextWalletIndex
);

refundAddress = Wallet.createRandom().address;
claimedValue = ethers.utils.parseEther('0.5');
preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('should fail if revert from destination contract', async function () {
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
preimageHash,
constants.HashZero,
ethers.utils.parseEther('2'),
smartWalletAddress,
refundAddress,
timelock,
constants.AddressZero,
500,
]
);

Expand Down
33 changes: 8 additions & 25 deletions test/relayclient/RelayClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import {
addSwapHash,
assertLog,
createSmartWalletFactory,
createSupportedSmartWallet,
Expand Down Expand Up @@ -29,7 +30,7 @@ import {
HttpClient,
HttpWrapper,
} from '@rsksmart/rif-relay-client';
import { constants, utils, Wallet } from 'ethers';
import { constants, Wallet } from 'ethers';
import { ethers } from 'hardhat';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { loadConfiguration } from '../relayserver/ServerTestUtils';
Expand Down Expand Up @@ -560,31 +561,13 @@ describe('RelayClient', function () {
constants.AddressZero,
nextWalletIndex
);
const refundAddress = Wallet.createRandom().address;
const claimedValue = ethers.utils.parseEther('0.5');
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
const timelock = 500;
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('without tokenGas', async function () {
Expand Down
70 changes: 17 additions & 53 deletions test/relayserver/RelayServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
createRelayUserDefinedRequest,
createDeployUserDefinedRequest,
getSmartWalletTemplate,
addSwapHash,
} from '../utils/TestUtils';
import config from 'config';
import {
Expand All @@ -56,7 +57,7 @@ import {
RelayRequestBody,
setEnvelopingConfig,
} from '@rsksmart/rif-relay-client';
import { BigNumber, constants, utils, Wallet } from 'ethers';
import { BigNumber, constants, Wallet } from 'ethers';
import { spy, match } from 'sinon';
import {
TestDeployVerifierConfigurableMisbehavior,
Expand Down Expand Up @@ -728,39 +729,20 @@ describe('RelayServer', function () {
let index = 1;

beforeEach(async function () {
index++;
swap = await deployContract<TestSwap>('TestSwap');
const smartWalletAddress = await boltzFactory.getSmartWalletAddress(
owner.address,
constants.AddressZero,
index
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
encodedData = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
encodedData = await addSwapHash({
swap,
amount: ethers.utils.parseEther('0.5'),
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
await boltzVerifier.acceptContract(swap.address);
index++;
});

it('should relay deploy transaction with contract execution', async function () {
Expand Down Expand Up @@ -823,7 +805,7 @@ describe('RelayServer', function () {
relayServer.createRelayTransaction(
stringifyEnvelopingTx(envelopingTxRequest)
)
).to.be.rejectedWith('Claiming value lower than fees');
).to.be.rejectedWith('Native balance too lo');
});

// FIXME - Should bubble up error but its failing with a different error
Expand Down Expand Up @@ -868,30 +850,12 @@ describe('RelayServer', function () {
index
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
encodedData = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
encodedData = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
await minimalBoltzVerifier.acceptContract(swap.address);
index++;
});
Expand Down Expand Up @@ -956,7 +920,7 @@ describe('RelayServer', function () {
relayServer.createRelayTransaction(
stringifyEnvelopingTx(envelopingTxRequest)
)
).to.be.rejectedWith('Claiming value lower than fees');
).to.be.rejectedWith('Native balance too low');
});
});
});
Expand Down
Loading

0 comments on commit b941a86

Please sign in to comment.