test: adds tests for Common contract (fixed history) (#57)

* test: adds output function to print hashes used in solidity/test/domainHashTestCases.json

* test: adds domainHashTestCases.json file

* test: adds signature and domainHash calculation tests to common tests

* fix: runs prettier on contracts
buddies-main-deployment
Luke Tchang 4 years ago committed by GitHub
parent 188178263b
commit 1f22e023eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      rust/optics-core/Cargo.toml
  2. 13
      rust/optics-core/src/utils.rs
  3. 32
      solidity/contracts/test/TestCommon.sol
  4. 77
      solidity/test/Common.test.js
  5. 16
      solidity/test/domainHashTestCases.json

@ -17,7 +17,6 @@ tracing-futures = "0.2.4"
serde = {version = "1.0", features = ["derive"]}
serde_json = {version = "1.0"}
[dev-dependencies]
tokio = {version = "1.0.1", features = ["rt", "time"]}

@ -10,3 +10,16 @@ pub(crate) fn domain_hash(origin_slip44_id: u32) -> H256 {
.as_slice(),
)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
// Prints domain hashes used in solidity/test/domainHashTestCases.sol
fn output_domain_hashes() {
for n in 1..=3 {
println!("Domain hash for originSlip44 of {}: {:?}", n, domain_hash(n));
}
}
}

@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
import "../Common.sol";
contract TestCommon is Common {
constructor(
uint32 _originSLIP44,
address _updater,
bytes32 _current
) Common(_originSLIP44, _updater, _current) {}
function testCheckSig(
bytes32 _oldRoot,
bytes32 _newRoot,
bytes memory _signature
) external view returns (bool) {
return checkSig(_oldRoot, _newRoot, _signature);
}
function fail() internal override {
_setFailed();
}
function testDomainHash(uint32 _originSLIP44)
external
pure
returns (bytes32)
{
return keccak256(abi.encodePacked(_originSLIP44, "OPTICS"));
}
}

@ -0,0 +1,77 @@
const { waffle, ethers } = require('hardhat');
const { provider, deployMockContract } = waffle;
const { expect } = require('chai');
const { testCases } = require('./domainHashTestCases.json');
const ACTIVE = 0;
const FAILED = 1;
const originSLIP44 = 1234;
describe('Common', async () => {
let common, signer, fakeSigner, updater, fakeUpdater, initialRoot;
before(async () => {
[signer, fakeSigner] = provider.getWallets();
updater = await optics.Updater.fromSigner(signer, originSLIP44);
fakeUpdater = await optics.Updater.fromSigner(fakeSigner, originSLIP44);
initialRoot = ethers.utils.formatBytes32String('initial root');
});
beforeEach(async () => {
const CommonFactory = await ethers.getContractFactory('TestCommon');
common = await CommonFactory.deploy(
originSLIP44,
updater.signer.address,
initialRoot,
);
await common.deployed();
});
it('Accepts updater signature', async () => {
const oldRoot = ethers.utils.formatBytes32String('old root');
const newRoot = ethers.utils.formatBytes32String('new root');
const { signature } = await updater.signUpdate(oldRoot, newRoot);
expect(await common.testCheckSig(oldRoot, newRoot, signature)).to.be.true;
});
it('Rejects non-updater signature', async () => {
const oldRoot = ethers.utils.formatBytes32String('old root');
const newRoot = ethers.utils.formatBytes32String('new root');
const { signature: fakeSignature } = await fakeUpdater.signUpdate(
oldRoot,
newRoot,
);
expect(await common.testCheckSig(oldRoot, newRoot, fakeSignature)).to.be
.false;
});
it('Fails on valid double update proof', async () => {
const oldRoot = ethers.utils.formatBytes32String('old root');
const newRoot = ethers.utils.formatBytes32String('new root 1');
const newRoot2 = ethers.utils.formatBytes32String('new root 2');
const { signature } = await updater.signUpdate(oldRoot, newRoot);
const { signature: signature2 } = await updater.signUpdate(
oldRoot,
newRoot2,
);
await expect(
common.doubleUpdate(oldRoot, [newRoot, newRoot2], signature, signature2),
).to.emit(common, 'DoubleUpdate');
expect(await common.state()).to.equal(FAILED);
});
it('Calculates domain hashes from originSLIP44', async () => {
// Compare Rust output in json file to solidity output
for (let testCase of testCases) {
const { originSlip44, expectedDomainHash } = testCase;
const solidityDomainHash = await common.testDomainHash(originSlip44);
expect(solidityDomainHash).to.equal(expectedDomainHash);
}
});
});

@ -0,0 +1,16 @@
{
"testCases": [
{
"originSlip44": 1,
"expectedDomainHash": "0xfa923b9e3942a539792474b49136600ed6690d0e51c6993e9238819047dfea63"
},
{
"originSlip44": 2,
"expectedDomainHash": "0x8caa11ba0b38c460170899d835c99eb38b9319603fe5d391c469da2657e01039"
},
{
"originSlip44": 3,
"expectedDomainHash": "0xa2718888038cf3886089211e47a6f2fb003fc887a4f748fd6278156f23f9ecbf"
}
]
}
Loading…
Cancel
Save