Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
test: set zero-gas network with besu
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Apr 20, 2021
1 parent ed2de51 commit 25c59f1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.
9 changes: 9 additions & 0 deletions besu.config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
network="dev"
miner-enabled=true
miner-coinbase="0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"
rpc-http-cors-origins=["all"]
host-allowlist=["*"]
rpc-ws-enabled=true
rpc-http-enabled=true
data-path="/tmp/tmpdata-path"
min-gas-price=0
15 changes: 7 additions & 8 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ const hardhatConfig: HardhatUserConfig = {
cache: config.paths.cache,
artifacts: config.paths.build.contracts
},
defaultNetwork: "besu",
networks: {
besu: {
url: "http://151.56.86.147:18545",
accounts: [
"e227d26044b03bdb29ef637638a4a153cd6afb6572753d3ba4dbb3367bb6ebba",
"1a0bb26879365926c14777a58dcc259e241a71c2546f7eb86f723e1cdf683d73",
"2a8664ee7022cf75e2cfa04636fe77fc45b735d9ff9d7c514f8ae75114fe0ee6",
"f6eb9c7034b57fb692e2c1f4177fc01b28eea9e6e5e32ae64d0f62f3937623ad",
"ec5a056be5c22f3ef4d1bfa02371dcc0cf1394f9ea4b5ce8e9c4f5bc1f0d7704"
]
url: "http://127.0.0.1:8545",
gasPrice: 0,
gas: 112450000,
accounts: {
mnemonic: "test test test test test test test test test test test junk"
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"README.md"
],
"scripts": {
"start": "hardhat node",
"start": "besu --config-file=besu.config.toml",
"compile": "hardhat compile",
"clean": "hardhat clean",
"test": "hardhat test",
"deploy:localhost": "hardhat --network localhost deploy",
"deploy": "hardhat deploy",
"snark": "ts-node scripts/create-snark-artifacts.ts",
"lint": "solhint contracts/**/*.sol",
"commit": "git-cz",
Expand Down
23 changes: 8 additions & 15 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createBlakeHash from "blake-hash"
import { eddsa, poseidon } from "circomlib"
import { SMT } from "@cedoor/smt"
import crypto from "crypto"
import { Contract, providers } from "ethers"
import { Contract, providers, Wallet } from "ethers"
import { Scalar, utils } from "ffjavascript"
import { ethers } from "hardhat"
import { groth16 } from "snarkjs"
Expand All @@ -13,21 +13,14 @@ export function getProjectConfig() {
return config
}

export async function getAccounts() {
const signers = await ethers.getSigners()
const voters = createVoterAccounts(signers.length)

return signers.map((signer, i) => ({
signer,
voter: voters[i]
}))
}

function createVoterAccounts(n: number) {
export async function createAccounts(n = 10) {
const accounts = []

for (let i = 0; i < n; i++) {
accounts.push(createVoterAccount())
accounts.push({
wallet: ethers.Wallet.createRandom().connect(ethers.provider),
voter: createVoterAccount()
})
}

return accounts
Expand Down Expand Up @@ -135,9 +128,9 @@ function padNumberAs64Hex(n: any) {
return `0x${hex}`
}

export async function deployContract(contractName: string): Promise<Contract> {
export async function deployContract(contractName: string, wallet: Wallet): Promise<Contract> {
const ContractFactory = await ethers.getContractFactory(contractName)
const instance = await ContractFactory.deploy()
const instance = await ContractFactory.connect(wallet).deploy()

await instance.deployed()

Expand Down
65 changes: 35 additions & 30 deletions test/core-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from "chai"
import { Contract } from "ethers"
import { Contract, Wallet } from "ethers"
import {
createElektonProof,
delay,
deployContract,
getAccounts,
createAccounts,
getLastBlockTimestamp,
getTreeRoot,
stringToBytes32,
Expand All @@ -13,28 +13,28 @@ import {

describe("Core tests", () => {
let elekton: Contract
let accounts: any[]
let accounts: { wallet: Wallet; voter: any }[]
let elektonUser1: Contract

before(async () => {
elekton = await deployContract("Elekton")
accounts = await getAccounts()
elektonUser1 = elekton.connect(accounts[1].signer)
accounts = await createAccounts()
elekton = await deployContract("Elekton", accounts[0].wallet)
elektonUser1 = elekton.connect(accounts[1].wallet)
})

describe("#createUser()", () => {
let user1Address: string

before(async () => {
user1Address = await accounts[1].signer.getAddress()
user1Address = await accounts[1].wallet.getAddress()
})

for (let i = 1; i < 5; i++) {
const data = stringToBytes32(i.toString())

it(`User ${i} should add himself`, async () => {
const elektonUser = elekton.connect(accounts[i].signer)
const address = await accounts[i].signer.getAddress()
const elektonUser = elekton.connect(accounts[i].wallet)
const address = await accounts[i].wallet.getAddress()

await expect(waitConfirmations(elektonUser.createUser(data)))
.to.emit(elekton, "UserCreated")
Expand All @@ -45,7 +45,7 @@ describe("Core tests", () => {
it(`User 1 should not update his data with the same reference`, async () => {
const data = stringToBytes32("1")

await expect(elektonUser1.createUser(data)).to.be.revertedWith("E000")
await expect(waitConfirmations(elektonUser1.createUser(data))).to.be.reverted
})

it(`User 1 should update his data with another reference`, async () => {
Expand All @@ -71,29 +71,32 @@ describe("Core tests", () => {
const startDate = timestamp + 2
const endDate = timestamp + 12

await expect(elekton.createBallot(ballotData, treeRoot, startDate, endDate)).to.be.revertedWith("E100")
await expect(waitConfirmations(elekton.createBallot(ballotData, treeRoot, startDate, endDate))).to.be
.reverted
})

it("A ballot should not start in the past", async () => {
const timestamp = await getLastBlockTimestamp(elekton.provider)
const startDate = timestamp - 2
const endDate = timestamp + 10
const endDate = timestamp + 2

await expect(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate)).to.be.revertedWith("E101")
await expect(waitConfirmations(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate))).to.be
.reverted
})

it("A ballot should not last less than 10 seconds", async () => {
it("A ballot start date should not be less than its end date", async () => {
const timestamp = await getLastBlockTimestamp(elekton.provider)
const startDate = timestamp + 2
const endDate = timestamp + 10
const endDate = timestamp + 1

await expect(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate)).to.be.revertedWith("E102")
await expect(waitConfirmations(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate))).to.be
.reverted
})

it("User 1 should create ballot 0 with 4 voters (users 1, 2, 3, 4)", async () => {
const timestamp = await getLastBlockTimestamp(elekton.provider)
const startDate = timestamp + 2
const endDate = timestamp + 54
const startDate = timestamp + 5
const endDate = timestamp + 100

await expect(waitConfirmations(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate)))
.to.emit(elekton, "BallotCreated")
Expand All @@ -118,7 +121,7 @@ describe("Core tests", () => {
const ballotIndex = 1n
const proof = await createElektonProof(voterPublicKeys, ballotIndex, accounts[1].voter, vote)

await expect(elekton.vote(...proof)).to.be.revertedWith("E200")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})

it("An user should not vote in advance", async () => {
Expand All @@ -130,29 +133,29 @@ describe("Core tests", () => {

await waitConfirmations(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate))

await expect(elekton.vote(...proof)).to.be.revertedWith("E201")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})

it("An user should not vote late", async () => {
const ballotIndex = 2n
const timestamp = await getLastBlockTimestamp(elekton.provider)
const startDate = timestamp + 4
const endDate = timestamp + 14
const startDate = timestamp + 5
const endDate = timestamp + 6
const proof = await createElektonProof(voterPublicKeys, ballotIndex, accounts[1].voter, vote)

await waitConfirmations(elektonUser1.createBallot(ballotData, treeRoot, startDate, endDate))

await delay(14000)
await delay(6000)

await expect(elekton.vote(...proof)).to.be.revertedWith("E202")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})

it("An user should not vote on ballot with a wrong smt root", async () => {
const ballotIndex = 0n
const wrongVoterPublicKeys = accounts.slice(1, 6).map((account) => account.voter.publicKey)
const proof = await createElektonProof(wrongVoterPublicKeys, ballotIndex, accounts[1].voter, vote)

await expect(elekton.vote(...proof)).to.be.revertedWith("E203")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})

for (let i = 1; i < 5; i++) {
Expand All @@ -170,14 +173,14 @@ describe("Core tests", () => {
const ballotIndex = 0n
const proof = await createElektonProof(voterPublicKeys, ballotIndex, accounts[1].voter, vote)

await expect(elekton.vote(...proof)).to.be.revertedWith("E204")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})

it("An user should not vote with a invalid proof", async () => {
const ballotIndex = 0n
const proof = await createElektonProof(voterPublicKeys, ballotIndex, accounts[0].voter, vote)

await expect(elekton.vote(...proof)).to.be.revertedWith("E205")
await expect(waitConfirmations(elekton.vote(...proof))).to.be.reverted
})
})

Expand All @@ -191,13 +194,15 @@ describe("Core tests", () => {
})

it("An user should not publish a decryption key on a ballot of another user", async () => {
const elektonUser2 = elekton.connect(accounts[2].signer)
const elektonUser2 = elekton.connect(accounts[2].wallet)

await expect(elektonUser2.publishDecryptionKey(ballotIndex, decryptionKey)).to.be.revertedWith("E104")
await expect(waitConfirmations(elektonUser2.publishDecryptionKey(ballotIndex, decryptionKey))).to.be
.reverted
})

it("User 1 should not publish the decryption key before his ballot ends", async () => {
await expect(elektonUser1.publishDecryptionKey(ballotIndex, decryptionKey)).to.be.revertedWith("E105")
await expect(waitConfirmations(elektonUser1.publishDecryptionKey(ballotIndex, decryptionKey))).to.be
.reverted
})

it("User 1 should publish the decryption key when his ballot ends", async () => {
Expand Down

0 comments on commit 25c59f1

Please sign in to comment.