Skip to content

Commit

Permalink
Simplify tests - remove 'expectAddressEquality' fn
Browse files Browse the repository at this point in the history
  • Loading branch information
krebernisak committed Sep 5, 2022
1 parent 1d31747 commit 26eff08
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 146 deletions.
45 changes: 19 additions & 26 deletions contracts/test/emergency/StarkNetValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from 'hardhat/types'
import { expect } from 'chai'
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'
import { expectAddressEquality } from '../utils'
import { getSelectorFromName } from 'starknet/dist/utils/hash'

describe('StarkNetValidator', () => {
Expand Down Expand Up @@ -63,31 +62,25 @@ describe('StarkNetValidator', () => {

describe('StarkNetValidator', () => {
it('should get the selector from the name successfully', async () => {
const setSelector = getSelectorFromName('update_status')
expect(BigInt(setSelector)).to.equal(
1585322027166395525705364165097050997465692350398750944680096081848180365267n,
)
const actual = getSelectorFromName('update_status')
const expected = 1585322027166395525705364165097050997465692350398750944680096081848180365267n
expect(BigInt(actual)).to.equal(expected)
})

it('reverts if `StarkNetValidator.validate` called by account with no access', async () => {
await expect(starkNetValidator.connect(eoaValidator).validate(0, 0, 1, 1)).to.be.revertedWith(
'No access',
)
const c = starkNetValidator.connect(eoaValidator)
await expect(c.validate(0, 0, 1, 1)).to.be.revertedWith('No access')
})

it('should not revert if `sequencer_uptime_feed.latest_round_data` called by an Account with no explicit access (Accounts are allowed read access)', async () => {
const res = await account.call(l2Contract, 'latest_round_data')
expect(res.round.answer).to.equal(0n)
const { round } = await account.call(l2Contract, 'latest_round_data')
expect(round.answer).to.equal(0n)
})

it('should deploy the messaging contract', async () => {
const {
address: deployedTo,
l1_provider: L1Provider,
} = await starknet.devnet.loadL1MessagingContract(networkUrl)

expect(deployedTo).not.to.be.undefined
expect(L1Provider).to.equal(networkUrl)
const { address, l1_provider } = await starknet.devnet.loadL1MessagingContract(networkUrl)
expect(address).not.to.be.undefined
expect(l1_provider).to.equal(networkUrl)
})

it('should load the already deployed contract if the address is provided', async () => {
Expand All @@ -113,9 +106,9 @@ describe('StarkNetValidator', () => {
expect(msgFromL1).to.have.a.lengthOf(1)
expect(resp.consumed_messages.from_l2).to.be.empty

expectAddressEquality(msgFromL1[0].args.from_address, starkNetValidator.address)
expectAddressEquality(msgFromL1[0].args.to_address, l2Contract.address)
expectAddressEquality(msgFromL1[0].address, mockStarkNetMessaging.address)
expect(msgFromL1[0].args.from_address).to.equal(starkNetValidator.address)
expect(msgFromL1[0].args.to_address).to.equal(l2Contract.address)
expect(msgFromL1[0].address).to.equal(mockStarkNetMessaging.address)

// Assert L2 effects
const res = await account.call(l2Contract, 'latest_round_data')
Expand All @@ -136,9 +129,9 @@ describe('StarkNetValidator', () => {
expect(msgFromL1).to.have.a.lengthOf(1)
expect(resp.consumed_messages.from_l2).to.be.empty

expectAddressEquality(msgFromL1[0].args.from_address, starkNetValidator.address)
expectAddressEquality(msgFromL1[0].args.to_address, l2Contract.address)
expectAddressEquality(msgFromL1[0].address, mockStarkNetMessaging.address)
expect(msgFromL1[0].args.from_address).to.equal(starkNetValidator.address)
expect(msgFromL1[0].args.to_address).to.equal(l2Contract.address)
expect(msgFromL1[0].address).to.equal(mockStarkNetMessaging.address)

// Assert L2 effects
const res = await account.call(l2Contract, 'latest_round_data')
Expand All @@ -163,9 +156,9 @@ describe('StarkNetValidator', () => {
expect(msgFromL1).to.have.a.lengthOf(4)
expect(resp.consumed_messages.from_l2).to.be.empty

expectAddressEquality(msgFromL1[0].args.from_address, starkNetValidator.address)
expectAddressEquality(msgFromL1[0].args.to_address, l2Contract.address)
expectAddressEquality(msgFromL1[0].address, mockStarkNetMessaging.address)
expect(msgFromL1[0].args.from_address).to.equal(starkNetValidator.address)
expect(msgFromL1[0].args.to_address).to.equal(l2Contract.address)
expect(msgFromL1[0].address).to.equal(mockStarkNetMessaging.address)

// Assert L2 effects
const res = await account.call(l2Contract, 'latest_round_data')
Expand Down
16 changes: 3 additions & 13 deletions contracts/test/ocr2/aggregator.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import { assert, expect } from 'chai'
import BN from 'bn.js'
import { starknet } from 'hardhat'
import { constants, ec, encode, hash, number, uint256, stark, KeyPair } from 'starknet'
import { ec, hash, number, uint256, KeyPair } from 'starknet'
import { BigNumberish } from 'starknet/utils/number'
import { Account, StarknetContract, StarknetContractFactory } from 'hardhat/types/runtime'
import { TIMEOUT } from '../constants'

import { toFelt, hexPadStart } from '../utils'

interface Oracle {
signer: KeyPair
transmitter: Account
}

// Required to convert negative values into [0, PRIME) range
const toFelt = (int: number | BigNumberish): BigNumberish => {
const prime = number.toBN(encode.addHexPrefix(constants.FIELD_PRIME))
return number.toBN(int).umod(prime)
}

// NOTICE: Leading zeros are trimmed for an encoded felt (number).
// To decode, the raw felt needs to be start padded up to max felt size (252 bits or < 32 bytes).
const hexPadStart = (data: number, len: number) => {
return `0x${data.toString(16).padStart(len, '0')}`
}

const CHUNK_SIZE = 31

// Observers - max 31 oracles or 31 bytes
Expand Down
25 changes: 10 additions & 15 deletions contracts/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { constants, ec, encode, hash, number, uint256, stark, KeyPair } from 'starknet'
import { BigNumberish } from 'starknet/utils/number'
import { expect } from 'chai'

export const expectInvokeError = async (invoke: Promise<any>, expected?: string) => {
Expand Down Expand Up @@ -43,21 +45,14 @@ export const expectSpecificMsg = (actual: string, expected: string) => {
} else expect.fail(`\nActual: ${actual}\n\nExpected: ${expected}`)
}

/**
* Receives a hex address, converts it to bigint, converts it back to hex.
* This is done to strip leading zeros.
* @param address a hex string representation of an address
* @returns an adapted hex string representation of the address
*/
function adaptAddress(address: string) {
return '0x' + BigInt(address).toString(16)
// Required to convert negative values into [0, PRIME) range
export const toFelt = (int: number | BigNumberish): BigNumberish => {
const prime = number.toBN(encode.addHexPrefix(constants.FIELD_PRIME))
return number.toBN(int).umod(prime)
}

/**
* Expects address equality after adapting them.
* @param actual
* @param expected
*/
export function expectAddressEquality(actual: string, expected: string) {
expect(adaptAddress(actual)).to.equal(adaptAddress(expected))
// NOTICE: Leading zeros are trimmed for an encoded felt (number).
// To decode, the raw felt needs to be start padded up to max felt size (252 bits or < 32 bytes).
export const hexPadStart = (data: number | bigint, len: number) => {
return `0x${data.toString(16).padStart(len, '0')}`
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ export const loadContract_InternalStarkgate = (name: string): any => {
return json.parse(
fs
.readFileSync(
`${__dirname}/../../../node_modules/internals-starkgate-contracts/artifacts/0.0.3/eth/${name}.json`,
`${__dirname}/../../../../node_modules/internals-starkgate-contracts/artifacts/0.0.3/eth/${name}.json`,
)
.toString('ascii'),
)
}

export const loadContract_Openzepplin = (name: string): any => {
export const loadContract_OpenZepplin = (name: string): any => {
return json.parse(
fs
.readFileSync(
`${__dirname}/../../../node_modules/@openzeppelin/contracts/build/contracts/${name}.json`,
`${__dirname}/../../../../node_modules/@openzeppelin/contracts/build/contracts/${name}.json`,
)
.toString('ascii'),
)
Expand All @@ -25,7 +25,7 @@ export const loadContract_Solidity = (name: string): any => {
return json.parse(
fs
.readFileSync(
`${__dirname}/../../../contracts/artifacts/src/chainlink/solidity/mocks/${name}.sol/${name}.json`,
`${__dirname}/../../../../contracts/artifacts/src/chainlink/solidity/mocks/${name}.sol/${name}.json`,
)
.toString('ascii'),
)
Expand Down
Loading

0 comments on commit 26eff08

Please sign in to comment.