Skip to content

Commit

Permalink
test: Add secp256r1, transient storage, code oracle integration tests (
Browse files Browse the repository at this point in the history
…matter-labs#1630)

## What ❔

Add secp256r1, transient storage, code oracle integration tests

## Why ❔

Better integration test coverage

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
perekopskiy authored Apr 10, 2024
1 parent 7c8ae40 commit 96939ce
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 12 deletions.
3 changes: 2 additions & 1 deletion core/tests/ts-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ts-jest": "^29.0.1",
"ts-node": "^10.1.0",
"typescript": "^4.3.5",
"zksync-web3": "^0.15.5"
"zksync-web3": "^0.15.5",
"elliptic": "^6.5.5"
}
}
66 changes: 66 additions & 0 deletions core/tests/ts-integration/tests/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { shouldOnlyTakeFee } from '../src/modifiers/balance-checker';

import * as ethers from 'ethers';
import * as zksync from 'zksync-web3';
import * as elliptic from 'elliptic';
import { Provider } from 'zksync-web3';
import { RetryProvider } from '../src/retry-provider';

Expand Down Expand Up @@ -330,6 +331,71 @@ describe('Smart contract behavior checks', () => {
).toBeRejected('not enough gas to publish compressed bytecodes');
});

test('Should check secp256r1 precompile', async () => {
const ec = new elliptic.ec('p256');

const privateKeyHex = '519b423d715f8b581f4fa8ee59f4771a5b44c8130b4e3eacca54a56dda72b464';
const privateKey = Buffer.from(privateKeyHex, 'hex');

const message =
'0x5905238877c77421f73e43ee3da6f2d9e2ccad5fc942dcec0cbd25482935faaf416983fe165b1a045ee2bcd2e6dca3bdf46c4310a7461f9a37960ca672d3feb5473e253605fb1ddfd28065b53cb5858a8ad28175bf9bd386a5e471ea7a65c17cc934a9d791e91491eb3754d03799790fe2d308d16146d5c9b0d0debd97d79ce8';
const digest = ethers.utils.arrayify(ethers.utils.keccak256(message));
const signature = ec.sign(digest, privateKey);

const publicKeyHex =
'0x1ccbe91c075fc7f4f033bfa248db8fccd3565de94bbfb12f3c59ff46c271bf83ce4014c68811f9a21a1fdb2c0e6113e06db7ca93b7404e78dc7ccd5ca89a4ca9';

// Check that verification succeeds.
const res = await alice.provider.call({
to: '0x0000000000000000000000000000000000000100',
data: ethers.utils.concat([
digest,
'0x' + signature.r.toString('hex'),
'0x' + signature.s.toString('hex'),
publicKeyHex
])
});
expect(res).toEqual('0x0000000000000000000000000000000000000000000000000000000000000001');

// Send the transaction.
const tx = await alice.sendTransaction({
to: '0x0000000000000000000000000000000000000100',
data: ethers.utils.concat([
digest,
'0x' + signature.r.toString('hex'),
'0x' + signature.s.toString('hex'),
publicKeyHex
])
});
const receipt = await tx.wait();
expect(receipt.status).toEqual(1);
});

test('Should check transient storage', async () => {
const artifact = require(`${process.env.ZKSYNC_HOME}/etc/contracts-test-data/artifacts-zk/contracts/storage/storage.sol/StorageTester.json`);
const contractFactory = new zksync.ContractFactory(artifact.abi, artifact.bytecode, alice);
const storageContract = await contractFactory.deploy();
await storageContract.deployed();
// Tests transient storage, see contract code for details.
await expect(storageContract.testTransientStore()).toBeAccepted([]);
// Checks that transient storage is cleaned up after each tx.
await expect(storageContract.assertTValue(0)).toBeAccepted([]);
});

test('Should check code oracle works', async () => {
// Deploy contract that calls CodeOracle.
const artifact = require(`${process.env.ZKSYNC_HOME}/etc/contracts-test-data/artifacts-zk/contracts/precompiles/precompiles.sol/Precompiles.json`);
const contractFactory = new zksync.ContractFactory(artifact.abi, artifact.bytecode, alice);
const contract = await contractFactory.deploy();
await contract.deployed();

// Check that CodeOracle can decommit code of just deployed contract.
const versionedHash = zksync.utils.hashBytecode(artifact.bytecode);
const expectedBytecodeHash = ethers.utils.keccak256(artifact.bytecode);

await expect(contract.callCodeOracle(versionedHash, expectedBytecodeHash)).toBeAccepted([]);
});

afterAll(async () => {
await testMaster.deinitialize();
});
Expand Down
Loading

0 comments on commit 96939ce

Please sign in to comment.