-
Notifications
You must be signed in to change notification settings - Fork 20
/
task_exchange.js
138 lines (109 loc) · 4.91 KB
/
task_exchange.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const TaskExchange = artifacts.require('./TaskExchange.sol')
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
const DisputeResolutionLayerDummy = artifacts.require('./DisputeResolutionLayerDummy.sol')
const timeout = require('./helpers/timeout')
const mineBlocks = require('./helpers/mineBlocks')
const BigNumber = require('bignumber.js')
contract('TaskExchange', function(accounts) {
let taskExchange, deposit, bond, tx, log, taskID, intent, oldBalance
const taskGiver = accounts[1]
const solver = accounts[2]
const verifier = accounts[3]
const minDeposit = 500
const reward = web3.utils.toWei('1', 'ether')
const randomBits = 12345
context('incentive layer', () => {
before(async () => {
taskExchange = await TaskExchange.new()
oldBalance = await web3.eth.getBalance(solver)
disputeRes = await DisputeResolutionLayerDummy.at(DisputeResolutionLayerDummy.address)
})
it("should have participants make deposits", async () => {
// taskGiver makes a deposit
await taskExchange.makeDeposit({from: taskGiver, value: 1000})
deposit = await taskExchange.getDeposit.call(taskGiver)
assert(deposit.eq(1000))
// to-be solver makes a deposit
await taskExchange.makeDeposit({from: solver, value: 1000})
deposit = await taskExchange.getDeposit.call(solver)
assert(deposit.eq(1000))
// to-be verifier makes a deposit
await taskExchange.makeDeposit({from: verifier, value: 1000})
deposit = await taskExchange.getDeposit.call(verifier)
assert(deposit.eq(1000))
})
it("should create task", async () => {
// taskGiver creates a task.
// they bond part of their deposit.
tx = await taskExchange.createTask(minDeposit, 0x010203040506070809, [20, 40, 60], 9, DisputeResolutionLayerDummy.address, {from: taskGiver, value: reward})
bond = await taskExchange.getBondedDeposit.call(0, taskGiver)
assert(bond.eq(500))
deposit = await taskExchange.getDeposit.call(taskGiver)
assert(deposit.eq(500))
log = tx.logs.find(log => log.event === 'DepositBonded')
assert(log.args.taskID.eq(0))
assert.equal(log.args.account, taskGiver)
assert(log.args.amount.eq(minDeposit))
log = tx.logs.find(log => log.event === 'TaskCreated')
assert(log.args.taskID.isZero())
assert(log.args.minDeposit.eq(minDeposit))
taskID = log.args.taskID
})
it("should select a solver", async () => {
// solver registers for the task.
// they bond part of their deposit.
tx = await taskExchange.registerForTask(taskID, {from: solver})
log = tx.logs.find(log => log.event === 'DepositBonded')
assert(log.args.taskID.eq(taskID))
assert.equal(log.args.account, solver)
assert(log.args.amount.eq(minDeposit))
deposit = await taskExchange.getDeposit.call(solver)
assert(deposit.eq(500))
log = tx.logs.find(log => log.event === 'SolverSelected')
assert(log.args.taskID.eq(taskID))
assert.equal(log.args.solver, solver)
assert(log.args.minDeposit.eq(minDeposit))
})
it("should commit a solution", async () => {
// solver commits their solutions.
tx = await taskExchange.commitSolution(taskID, web3.utils.soliditySha3(0x12345), web3.utils.soliditySha3(0x12345), {from: solver})
log = tx.logs.find(log => log.event === 'SolutionCommitted')
assert(log.args.taskID.eq(taskID))
assert(log.args.minDeposit.eq(minDeposit))
})
it("should commit to challenge in a verification game", async () => {
// verifier commits a challenge
// they bond part of their deposit.
tx = await taskExchange.commitChallenge(taskID, {from: verifier})
log = tx.logs.find(log => log.event === 'DepositBonded')
assert(log.args.taskID.eq(taskID))
assert.equal(log.args.account, verifier)
assert(log.args.amount.eq(minDeposit))
deposit = await taskExchange.getDeposit.call(verifier)
assert(deposit.eq(500))
})
it("should convict verifier", async () => {
tx = await taskExchange.convictVerifier(taskID, {from: solver})
})
it("should finalize task", async () => {
await mineBlocks(web3, 65)
await taskExchange.finalizeTask(taskID, {from: solver})
})
it('should unbond solver deposit', async () => {
await taskExchange.unbondDeposit(taskID, {from: solver})
assert((await taskExchange.getDeposit.call(solver)).eq(1500))
})
it('should unbond task giver deposit', async () => {
await taskExchange.unbondDeposit(taskID, {from: taskGiver})
assert((await taskExchange.getDeposit.call(taskGiver)).eq(1000))
})
//Uncomment out after making deposit amounts larger
it('should be higher than original balance', async () => {
const newBalance = await web3.eth.getBalance(solver)
//console.log("Old balance: " + oldBalance)
//console.log("New Balance: " + newBalance)
assert((new BigNumber(oldBalance)).isLessThan(new BigNumber(newBalance)))
})
})
})