-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinear_optimization_problem_bounty.js
143 lines (126 loc) · 4.55 KB
/
linear_optimization_problem_bounty.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
139
140
141
142
143
const LOPB = artifacts.require("linear_optimization_problem_bounty");
const args = {
durationInBlocks: 10,
}
contract("linear_optimization_problem_bounty", async accounts => {
it("...should set correct initial paramters.", async () => {
const instance = await LOPB.deployed({ ...args });
const owner = await instance.owner();
const bestSolution = await instance.bestSolution();
const durationInBlocks = await instance.durationInBlocks();
const competitionEnd = await instance.competitionEnd();
const addressOfWinner = await instance.addressOfWinner();
const claimPeriodeLength = await instance.claimPeriodeLength();
const minedBlockNumber = await web3.eth.getBlockNumber();
assert.equal(
owner,
accounts[0],
"Owner wasn't correctly set."
);
assert.equal(
bestSolution,
0,
"BestSolution wasn't initialized with 0."
);
assert.equal(
durationInBlocks,
args.durationInBlocks,
"DurationInBlocks wasn't correctly set."
);
assert.equal(
competitionEnd,
minedBlockNumber + durationInBlocks,
"DurationInBlocks wasn't correctly set."
);
assert.equal(
addressOfWinner,
'0x0000000000000000000000000000000000000000',
"AddressOfWinner wasn't correctly initialized with ZERO_ADDRESS."
);
assert.equal(
claimPeriodeLength,
args.durationInBlocks,
"ClaimPeriodeLength wasn't correctly set."
);
});
it("...should set correct values when submitting new valid solution.", async () => {
const instance = await LOPB.deployed({ ...args });
const x1 = 1;
const x2 = 1;
await instance.submitSolution.call(x1, x2);
const instance_x1 = await instance.x1();
const instance_x2 = await instance.x2();
const instance_bestSolution = await instance.bestSolution();
const instance_addressOfWinner = await instance.addressOfWinner();
assert.equal(
instance_x1,
x1,
"x1 value wasn't correctly set."
);
assert.equal(
instance_x2,
x2,
"x2 value wasn't correctly set."
);
// NOTE: update this if the problem in the contract is changed
assert.equal(
instance_bestSolution,
(4 * x1) + (6 * x2),
"BestSolution value wasn't correctly set."
);
assert.equal(
instance_addressOfWinner,
accounts[0],
"AddressOfWinner wasn't correctly set."
);
});
it("...should increase bounty.", async () => {
const instance = await LOPB.deployed({ ...args });
const contractBalance_BeforeTopUp = web3.eth.getBalance(instance);
const transactionValue_inEth = 1;
const transactionValue_inWei = web3.utils.toWei(transactionValue_inEth);
// top up bounty
await instance.topUpBounty.call({}, { from: account[0], value: transactionValue_inWei });
const contractBalance_AfterTopUp = web3.eth.getBalance(instance);
assert.equal(
contractBalance_BeforeTopUp,
contractBalance_AfterTopUp - transactionValue_inWei,
"Bounty was not correctly increased."
);
});
// TODO: this is not complete
// TODO: Hmmm, need to create 'claimPeriodeLength = 6172' blocks
// so that block.number > (self.competitionEnd + self.claimPeriodeLength)
// not sure how to test this...
it("...should claim bounty.", async () => {
const instance = await LOPB.deployed({ ...args });
const contractBalance_Before = web3.eth.getBalance(instance);
const competitionEnd_Before = await instance.competitionEnd();
const x1 = 1;
const x2 = 1;
const transactionValue_inEth = 1;
const transactionValue_inWei = web3.utils.toWei(transactionValue_inEth);
// top up bounty
await instance.topUpBounty.call({}, { from: account[0], value: transactionValue_inWei });
const contractBalance_After = web3.eth.getBalance(instance);
await instance.claimBounty();
const competitionEnd_After = await instance.competitionEnd();
});
// TODO:
it("...should extend competition.", async () => {
const instance = await LOPB.deployed({ durationInBlocks: 1 });
const durationInBlocks = await instance.durationInBlocks();
const competitionEnd_Before = await instance.competitionEnd();
// TODO: Hmmm, need to create 'claimPeriodeLength = 6172' blocks
// so that block.number > (self.competitionEnd + self.claimPeriodeLength)
// not sure how to test this...
/*
// create dummy block
const result = await web3.eth.sendTransaction({
from: account[0],
to: '0x0000000000000000000000000000000000000000',
value: 1, // in wei
});
*/
});
});