Skip to content

Commit

Permalink
full eslint, fix #51
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivshti committed Apr 2, 2019
1 parent 361ec73 commit d84040b
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 251 deletions.
2 changes: 1 addition & 1 deletion contracts/Identity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ contract Identity {
internal
{
assembly {
let result := call(sub(gas, 10000), to, value, add(data, 0x20), mload(data), 0, 0)
let result := call(gas, to, value, add(data, 0x20), mload(data), 0, 0)

switch result case 0 {
let size := returndatasize
Expand Down
2 changes: 1 addition & 1 deletion migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const AdExCore = artifacts.require('./AdExCore.sol')
module.exports = function(deployer) {
deployer.deploy(AdExCore)
deployer.deploy(Migrations)
};
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
"solc": "^0.5.7",
"truffle": "^5.0.10"
},
"devDependencies": {},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"prettier": "^1.16.4"
},
"scripts": {
"test": "truffle test",
"lint": "eslint . -c .eslintrc.js --fix",
"preversion": "mkdir -p temp && solc --abi contracts/AdExCore.sol -o temp && mkdir -p abi && mv temp/AdExCore.abi abi/AdExCore.json && rm -r temp/"
},
"repository": {
Expand Down
132 changes: 95 additions & 37 deletions test/TestAdExCore.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const promisify = require('util').promisify
const { providers, Contract } = require('ethers')

const AdExCore = artifacts.require('AdExCore')
const MockToken = artifacts.require('./mocks/Token')
const MockLibs = artifacts.require('./mocks/Libs')

const { moveTime, sampleChannel, expectEVMError } = require('./')
const promisify = require('util').promisify

const ethSign = promisify(web3.eth.sign.bind(web3))

const { ChannelState, Channel, MerkleTree, splitSig } = require('../js')
const { providers, Contract } = require('ethers')

const web3Provider = new providers.Web3Provider(web3.currentProvider)

contract('AdExCore', function(accounts) {
Expand All @@ -16,7 +19,7 @@ contract('AdExCore', function(accounts) {
let libMock

const tokens = 2000
const userAcc = accounts[0]
const userAcc = accounts[0]

before(async function() {
const tokenWeb3 = await MockToken.new()
Expand All @@ -34,33 +37,50 @@ contract('AdExCore', function(accounts) {
it('SignatureValidator', async function() {
const hash = '0x0202020202020202020202020202020202020202020202020202020202020202'
const sig = splitSig(await ethSign(hash, userAcc))
assert.isTrue(await libMock.isValidSig(hash, userAcc, sig), 'isValidSig returns true for the signer')
assert.isNotTrue(await libMock.isValidSig(hash, accounts[1], sig), 'isValidSig returns true for a non-signer')
assert.isTrue(
await libMock.isValidSig(hash, userAcc, sig),
'isValidSig returns true for the signer'
)
assert.isNotTrue(
await libMock.isValidSig(hash, accounts[1], sig),
'isValidSig returns true for a non-signer'
)
})

it('channelOpen', async function() {
const blockTime = (await web3.eth.getBlock('latest')).timestamp

const channelWrongCreator = sampleChannel(accounts, token.address, accounts[1], tokens, blockTime+50, 0)
const channelWrongCreator = sampleChannel(
accounts,
token.address,
accounts[1],
tokens,
blockTime + 50,
0
)
await expectEVMError(core.channelOpen(channelWrongCreator.toSolidityTuple()), 'INVALID_CREATOR')

const channel = sampleChannel(accounts, token.address, userAcc, tokens, blockTime+50, 0)
const channel = sampleChannel(accounts, token.address, userAcc, tokens, blockTime + 50, 0)
const receipt = await (await core.channelOpen(channel.toSolidityTuple())).wait()
const ev = receipt.events.find(x => x.event === 'LogChannelOpen')
const ev = receipt.events.find(x => x.event === 'LogChannelOpen')
assert.ok(ev, 'has LogChannelOpen event')

assert.equal(await token.balanceOf(userAcc), 0, 'account balance is 0')
assert.equal(await token.balanceOf(core.address), tokens, 'contract balance is correct')

assert.equal(ev.args.channelId, channel.hashHex(core.address), 'channel hash matches')
assert.equal(await core.states(channel.hash(core.address)), ChannelState.Active, 'channel state is correct')
assert.equal(
await core.states(channel.hash(core.address)),
ChannelState.Active,
'channel state is correct'
)

await expectEVMError(core.channelOpen(channel.toSolidityTuple()), 'INVALID_STATE')
})

it('channelWithdrawExpired', async function() {
const blockTime = (await web3.eth.getBlock('latest')).timestamp
const channel = sampleChannel(accounts, token.address, userAcc, tokens, blockTime+50, 1)
const channel = sampleChannel(accounts, token.address, userAcc, tokens, blockTime + 50, 1)

await (await core.channelOpen(channel.toSolidityTuple())).wait()
const initialBal = await token.balanceOf(userAcc)
Expand All @@ -72,9 +92,20 @@ contract('AdExCore', function(accounts) {
// Ensure we can do this when the time comes
await moveTime(web3, 100)
const receipt = await (await channelWithdrawExpired()).wait()
assert.ok(receipt.events.find(x => x.event === 'LogChannelWithdrawExpired'), 'has LogChannelWihtdrawExpired event')
assert.equal(await core.states(channel.hash(core.address)), ChannelState.Expired, 'channel state is correct')
assert.equal(await token.balanceOf(userAcc), initialBal.toNumber() + tokens, 'funds are returned')
assert.ok(
receipt.events.find(x => x.event === 'LogChannelWithdrawExpired'),
'has LogChannelWihtdrawExpired event'
)
assert.equal(
await core.states(channel.hash(core.address)),
ChannelState.Expired,
'channel state is correct'
)
assert.equal(
await token.balanceOf(userAcc),
initialBal.toNumber() + tokens,
'funds are returned'
)

// cannot do it again
await expectEVMError(channelWithdrawExpired(), 'INVALID_STATE')
Expand All @@ -83,21 +114,22 @@ contract('AdExCore', function(accounts) {
it('channelWithdraw', async function() {
const blockTime = (await web3.eth.getBlock('latest')).timestamp
const totalDeposit = tokens
const channel = sampleChannel(accounts, token.address, userAcc, totalDeposit, blockTime+50, 2)
const channel = sampleChannel(accounts, token.address, userAcc, totalDeposit, blockTime + 50, 2)
const channelWithdraw = core.channelWithdraw.bind(core, channel.toSolidityTuple())
await (await core.channelOpen(channel.toSolidityTuple())).wait()

// Prepare the tree and sign the state root
const userLeafAmnt = totalDeposit/2
const userLeafAmnt = totalDeposit / 2
const [stateRoot, validSigs, proof] = await balanceTreeToWithdrawArgs(
channel,
{ [userAcc]: userLeafAmnt },
userAcc, userLeafAmnt
userAcc,
userLeafAmnt
)

// Can't withdraw an amount that is not in the tree
await expectEVMError(
channelWithdraw(stateRoot, validSigs, proof, userLeafAmnt+1),
channelWithdraw(stateRoot, validSigs, proof, userLeafAmnt + 1),
'BALANCELEAF_NOT_FOUND'
)

Expand All @@ -112,27 +144,43 @@ contract('AdExCore', function(accounts) {
const validWithdraw = () => channelWithdraw(stateRoot, validSigs, proof, userLeafAmnt)
const tx = await validWithdraw()
const receipt = await tx.wait()
assert.ok(receipt.events.find(x => x.event === 'LogChannelWithdraw'), 'has LogChannelWithdraw event')
assert.ok(
receipt.events.find(x => x.event === 'LogChannelWithdraw'),
'has LogChannelWithdraw event'
)
assert.equal(await token.balanceOf(userAcc), userLeafAmnt, 'user has a proper token balance')

const channelId = channel.hash(core.address)
assert.equal(await core.withdrawn(channelId), userLeafAmnt, 'channel has the right withdrawn value')
assert.equal(await core.withdrawnPerUser(channelId, userAcc), userLeafAmnt, 'channel has right withdrawnPerUser')
assert.equal(
await core.withdrawn(channelId),
userLeafAmnt,
'channel has the right withdrawn value'
)
assert.equal(
await core.withdrawnPerUser(channelId, userAcc),
userLeafAmnt,
'channel has right withdrawnPerUser'
)

// if we try with less, it won't work
const decWithdrawArgs = await balanceTreeToWithdrawArgs(
channel,
{ [userAcc]: userLeafAmnt-1 },
userAcc, userLeafAmnt-1
{ [userAcc]: userLeafAmnt - 1 },
userAcc,
userLeafAmnt - 1
)
await expectEVMError(channelWithdraw.apply(null, decWithdrawArgs))
await expectEVMError(channelWithdraw(...decWithdrawArgs))

// we can do it again, but it's not gonna give us more tokens
const receipt2 = await (await validWithdraw()).wait()
const withdrawEvent = receipt2.events.find(x => x.event === 'LogChannelWithdraw')
assert.ok(withdrawEvent, 'has LogChannelWithdraw event')
assert.equal(withdrawEvent.args.amount, 0, 'withdrawn amount is 0')
assert.equal(await core.withdrawn(channelId), userLeafAmnt, 'channel has the right withdrawn value')
assert.equal(
await core.withdrawn(channelId),
userLeafAmnt,
'channel has the right withdrawn value'
)

// add more balances and withdraw; make sure that only the difference (to the last withdrawal) is withdrawn
// also, test a tree that has more elements
Expand All @@ -142,53 +190,64 @@ contract('AdExCore', function(accounts) {
{
[userAcc]: incUserLeafAmnt,
[accounts[1]]: 10,
[accounts[2]]: 10,
[accounts[2]]: 10
},
userAcc, incUserLeafAmnt
userAcc,
incUserLeafAmnt
)
const receipt3 = await (await channelWithdraw.apply(null, incWithdrawArgs)).wait()
const receipt3 = await (await channelWithdraw(...incWithdrawArgs)).wait()
const incWithdrawEvent = receipt3.events.find(x => x.event === 'LogChannelWithdraw')
assert.ok(incWithdrawEvent, 'has LogChannelWithdraw event')
assert.equal(incWithdrawEvent.args.amount, 10, 'withdrawn amount is 10')
assert.equal(await core.withdrawn(channelId), incUserLeafAmnt, 'channel has the right withdrawn value')
assert.equal(
await core.withdrawn(channelId),
incUserLeafAmnt,
'channel has the right withdrawn value'
)
assert.equal(await token.balanceOf(userAcc), incUserLeafAmnt, 'user has the right token amount')

await moveTime(web3, 100)
await expectEVMError(validWithdraw(), 'EXPIRED')

// Now we withdrawExpired, and we can only get the rest
const expiredReceipt = await (await core.channelWithdrawExpired(channel.toSolidityTuple())).wait()
const expiredReceipt = await (await core.channelWithdrawExpired(
channel.toSolidityTuple()
)).wait()
const expiredEv = expiredReceipt.events.find(x => x.event === 'LogChannelWithdrawExpired')
assert.equal(expiredEv.args.amount.toNumber() + incUserLeafAmnt, totalDeposit, 'withdrawExpired returned the rest of the funds')
assert.equal(
expiredEv.args.amount.toNumber() + incUserLeafAmnt,
totalDeposit,
'withdrawExpired returned the rest of the funds'
)
assert.equal(await token.balanceOf(userAcc), totalDeposit, 'totalDeposit is returned')
})

it('channelWithdraw: cannot withdraw more than the channel', async function() {
const blockTime = (await web3.eth.getBlock('latest')).timestamp
const totalDeposit = tokens
const channel = sampleChannel(accounts, token.address, userAcc, totalDeposit, blockTime+50, 3)
const channelWithdraw = core.channelWithdraw.bind(core, channel.toSolidityTuple())
const channel = sampleChannel(accounts, token.address, userAcc, totalDeposit, blockTime + 50, 3)
await (await core.channelOpen(channel.toSolidityTuple())).wait()

const leafAmnt = totalDeposit + 1
const [stateRoot, validSigs, proof] = await balanceTreeToWithdrawArgs(
channel,
{ [userAcc]: leafAmnt },
userAcc, leafAmnt
userAcc,
leafAmnt
)
await expectEVMError(
core.channelWithdraw(channel.toSolidityTuple(), stateRoot, validSigs, proof, leafAmnt),
'WITHDRAWING_MORE_THAN_CHANNEL'
)
})


// Bench: creating these: (elem1, elem2, elem3, tree, proof, stateRoot, hashToSignHex, sig1), 1000 times, takes ~6000ms
// Bench: creating these: (elem1, elem2, elem3, tree, proof, stateRoot, hashtoSignHex), 1000 times, takes ~300ms
// Bench: creating these: (tree, proof, stateRoot, hashtoSignHex), 1000 times, takes ~300ms
async function balanceTreeToWithdrawArgs(channel, balances, acc, amnt) {
const elements = Object.entries(balances)
.map(([ acc, amnt ]) => Channel.getBalanceLeaf(acc, amnt))
const elements = Object.entries(balances).map(([leafAcc, leafAmnt]) =>
Channel.getBalanceLeaf(leafAcc, leafAmnt)
)
const tree = new MerkleTree(elements)
const elemToWithdraw = Channel.getBalanceLeaf(acc, amnt)
const proof = tree.proof(elemToWithdraw)
Expand All @@ -199,4 +258,3 @@ contract('AdExCore', function(accounts) {
return [stateRoot, [sig1, sig2], proof, amnt]
}
})

Loading

0 comments on commit d84040b

Please sign in to comment.