Skip to content

Commit bbfa823

Browse files
committed
dropping spy pattern for testing events due to incompatability with HD wallet implementation
1 parent b09dce3 commit bbfa823

9 files changed

Lines changed: 69 additions & 170 deletions

File tree

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
"arg", "npm", "seedrandom", "eql", "sinon", "yaml", "promisify",
3939
"passcode", "geth", "rpc", "rpcmsg","stdev", "stochasm",
4040
"whitelist", "uint", "passcodes", "keystore", "hdwallet",
41+
"formatter",
4142

4243
// shorthand
4344
"eth", "args", "util", "utils", "msg", "prev", "bal",

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
},
3333
"pre-commit": {
3434
"run": [
35+
"precommit",
3536
"lint:js",
3637
"lint:sol",
37-
"trackGasUtilization",
38-
"precommit"
38+
"trackGasUtilization"
3939
]
4040
},
4141
"dependencies": {

scripts/test.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,76 @@ deploy-contracts(){
3939
truffle migrate --reset --network $1
4040
}
4141

42+
deploy-all-contracts(){
43+
deploy-contracts "ganacheUnitTest"
44+
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]
45+
then
46+
deploy-contracts "gethUnitTest"
47+
fi
48+
}
49+
4250
read REF GANACHE_PORT < <(get-network-config ganacheUnitTest)
4351
read REF GETH_PORT < <(get-network-config gethUnitTest)
4452

4553
# Is chain running?
46-
if [ $(process-pid $GANACHE_PORT) ]; then
54+
if [ $(process-pid $GANACHE_PORT) ]
55+
then
4756
REFRESH_GANACHE=0
4857
else
4958
REFRESH_GANACHE=1
5059
fi
51-
if [ $(process-pid $GETH_PORT) ]; then
60+
if [ $(process-pid $GETH_PORT) ]
61+
then
5262
REFRESH_GETH=0
5363
else
5464
REFRESH_GETH=1
5565
fi
5666

5767
echo "------Start blockchain(s)"
5868
start-chain "ganacheUnitTest"
59-
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]; then
69+
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]
70+
then
6071
start-chain "gethUnitTest"
6172
fi
6273

6374
echo "------Deploying contracts"
64-
deploy-contracts "ganacheUnitTest"
65-
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]; then
66-
deploy-contracts "gethUnitTest"
67-
fi
75+
deploy-all-contracts
6876

6977
echo "------Running unit tests"
7078
run-unit-tests "ganacheUnitTest"
71-
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]; then
79+
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]
80+
then
7281
run-unit-tests "gethUnitTest"
7382
fi
7483

7584
echo "------Running gas utilization test"
7685
run-load-tests "ganacheUnitTest"
7786

78-
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]; then
87+
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]
88+
then
7989
echo "------Running simulation tests"
8090
run-simulation-tests "ganacheUnitTest"
8191
run-simulation-tests "gethUnitTest"
8292
fi
8393

8494
# Stop chain
8595
cleanupGanache(){
86-
if [ "$REFRESH_GANACHE" == "1" ]; then
96+
if [ "$REFRESH_GANACHE" == "1" ]
97+
then
8798
stop-chain "ganacheUnitTest"
8899
fi
89100
}
90101

91102
cleanupGeth(){
92-
if [ "$REFRESH_GETH" == "1" ]; then
103+
if [ "$REFRESH_GETH" == "1" ]
104+
then
93105
stop-chain "gethUnitTest"
94106
fi
95107
}
96108

97109
trap cleanupGanache EXIT
98-
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]; then
110+
if [ "${TRAVIS_EVENT_TYPE}" == "cron" ]
111+
then
99112
trap cleanupGeth EXIT
100113
fi
101114

test/load/gas_utilization.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ module.exports = async function (callback) {
148148
console.log('NO SIGNIFICANT CHANGE in gas utilization');
149149
}
150150
console.log('**************************************************************');
151+
process.exit(0);
151152
};

test/simulation/frg_necessary_conditions.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ const UFragments = artifacts.require('UFragments.sol');
1414

1515
const Stochasm = require('stochasm');
1616
const _require = require('app-root-path').require;
17-
const BlockchainCaller = _require('/util/blockchain_caller');
18-
const chain = new BlockchainCaller(web3);
1917

2018
const truffleConfig = _require('/truffle.js');
2119
const accounts = truffleConfig.accounts;
2220

2321
contract('UFragments', async function () {
24-
let uFragments, snapshot, rebaseAmt, inflation;
22+
let uFragments, rebaseAmt, inflation;
2523
const deployer = accounts[0];
2624
const A = accounts[1];
2725
const B = accounts[2];
@@ -32,14 +30,10 @@ contract('UFragments', async function () {
3230
before(async function () {
3331
uFragments = await UFragments.new();
3432
await uFragments.setMonetaryPolicy(deployer, { from: deployer });
35-
snapshot = await chain.snapshotChain();
3633
await uFragments.transfer(A, 3, {from: deployer});
3734
await uFragments.transfer(B, 4, {from: deployer});
3835
await uFragments.transfer(C, 5, {from: deployer});
3936
});
40-
after(async () => {
41-
await chain.revertToSnapshot(snapshot);
42-
});
4337

4438
function printSupply (supply) {
4539
console.log('Total supply is now', supply.toString(), 'UFRG');

test/unit/UFragments.js

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ const UFragments = artifacts.require('UFragments.sol');
22
const _require = require('app-root-path').require;
33
const BlockchainCaller = _require('/util/blockchain_caller');
44
const chain = new BlockchainCaller(web3);
5-
const { ContractEventSpy } = _require('/util/spies');
65

76
const truffleConfig = _require('/truffle.js');
87
const accounts = truffleConfig.accounts;
98

10-
let uFragments, b, eventSpy;
9+
let uFragments, b, r;
1110

1211
async function setContractReferences () {
1312
uFragments = await UFragments.new();
@@ -54,19 +53,14 @@ contract('UFragments:PauseRebase', function () {
5453
before('setup UFragments contract', async function () {
5554
await setContractReferences();
5655
await uFragments.setMonetaryPolicy(policy, {from: deployer});
57-
eventSpy = new ContractEventSpy([uFragments.RebasePaused]);
58-
eventSpy.watch();
59-
await uFragments.setRebasePaused(true);
60-
});
61-
62-
after(async function () {
63-
eventSpy.stopWatching();
56+
r = await uFragments.setRebasePaused(true);
6457
});
6558

6659
it('should emit pause event', async function () {
67-
const pauseEvent = eventSpy.getEventByName('RebasePaused');
68-
expect(pauseEvent).to.exist;
69-
expect(pauseEvent.args.paused);
60+
const log = r.logs[0];
61+
expect(log).to.exist;
62+
expect(log.event).to.eq('RebasePaused');
63+
expect(log.args.paused).to.be.true;
7064
});
7165

7266
it('should not allow calling rebase', async function () {
@@ -117,19 +111,14 @@ contract('UFragments:PauseToken', function () {
117111
before('setup UFragments contract', async function () {
118112
await setContractReferences();
119113
await uFragments.setMonetaryPolicy(policy, {from: deployer});
120-
eventSpy = new ContractEventSpy([uFragments.TokenPaused]);
121-
eventSpy.watch();
122-
await uFragments.setTokenPaused(true);
123-
});
124-
125-
after(async function () {
126-
eventSpy.stopWatching();
114+
r = await uFragments.setTokenPaused(true);
127115
});
128116

129117
it('should emit pause event', async function () {
130-
const pauseEvent = eventSpy.getEventByName('TokenPaused');
131-
expect(pauseEvent).to.exist;
132-
expect(pauseEvent.args.paused);
118+
const log = r.logs[0];
119+
expect(log).to.exist;
120+
expect(log.event).to.eq('TokenPaused');
121+
expect(log.args.paused).to.be.true;
133122
});
134123

135124
it('should allow calling rebase', async function () {
@@ -212,13 +201,7 @@ contract('UFragments:Rebase:Expansion', function () {
212201
await setContractReferences();
213202
await uFragments.setMonetaryPolicy(policy, {from: deployer});
214203
await uFragments.transfer(A, 250, { from: deployer });
215-
eventSpy = new ContractEventSpy([uFragments.Rebase]);
216-
eventSpy.watch();
217-
await uFragments.rebase(1, 500, {from: policy});
218-
});
219-
220-
after(async function () {
221-
eventSpy.stopWatching();
204+
r = await uFragments.rebase(1, 500, {from: policy});
222205
});
223206

224207
it('should increase the totalSupply', async function () {
@@ -235,10 +218,11 @@ contract('UFragments:Rebase:Expansion', function () {
235218
});
236219

237220
it('should emit Rebase', async function () {
238-
const rebaseEvent = eventSpy.getEventByName('Rebase');
239-
expect(rebaseEvent).to.exist;
240-
expect(rebaseEvent.args.epoch.toNumber()).to.eq(1);
241-
expect(rebaseEvent.args.totalSupply.toNumber()).to.eq(1500);
221+
const log = r.logs[0];
222+
expect(log).to.exist;
223+
expect(log.event).to.eq('Rebase');
224+
expect(log.args.epoch.toNumber()).to.eq(1);
225+
expect(log.args.totalSupply.toNumber()).to.eq(1500);
242226
});
243227
});
244228

@@ -252,13 +236,7 @@ contract('UFragments:Rebase:Contraction', function () {
252236
await setContractReferences();
253237
await uFragments.setMonetaryPolicy(policy, {from: deployer});
254238
await uFragments.transfer(A, 250, { from: deployer });
255-
eventSpy = new ContractEventSpy([uFragments.Rebase]);
256-
eventSpy.watch();
257-
await uFragments.rebase(1, -500, {from: policy});
258-
});
259-
260-
after(async function () {
261-
eventSpy.stopWatching();
239+
r = await uFragments.rebase(1, -500, {from: policy});
262240
});
263241

264242
it('should decrease the totalSupply', async function () {
@@ -275,10 +253,11 @@ contract('UFragments:Rebase:Contraction', function () {
275253
});
276254

277255
it('should emit Rebase', async function () {
278-
const rebaseEvent = eventSpy.getEventByName('Rebase');
279-
expect(rebaseEvent).to.exist;
280-
expect(rebaseEvent.args.epoch.toNumber()).to.eq(1);
281-
expect(rebaseEvent.args.totalSupply.toNumber()).to.eq(500);
256+
const log = r.logs[0];
257+
expect(log).to.exist;
258+
expect(log.event).to.eq('Rebase');
259+
expect(log.args.epoch.toNumber()).to.eq(1);
260+
expect(log.args.totalSupply.toNumber()).to.eq(500);
282261
});
283262
});
284263

test/unit/UFragmentsPolicy.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const UFragmentsPolicy = artifacts.require('UFragmentsPolicy.sol');
22
const MockUFragments = artifacts.require('MockUFragments.sol');
33
const MockMarketOracle = artifacts.require('MockMarketOracle.sol');
44

5+
const _ = require('lodash');
56
const BigNumber = require('bignumber.js');
67
const _require = require('app-root-path').require;
7-
const { ContractEventSpy, MockFunctionSpy } = _require('/util/spies');
88
const BlockchainCaller = _require('/util/blockchain_caller');
99
const chain = new BlockchainCaller(web3);
1010

@@ -90,7 +90,6 @@ contract('UFragmentsPolicy:Rebase', async function () {
9090

9191
describe('when minRebaseTimeIntervalSec has passed since the previous rebase', function () {
9292
describe('positive rate', function () {
93-
let uFragSpy, oracleSpy;
9493
before(async function () {
9594
await mockExternalData(1.3e18, 100, 1000);
9695
await uFragmentsPolicy.setMinRebaseTimeIntervalSec(5); // 5 sec
@@ -99,16 +98,8 @@ contract('UFragmentsPolicy:Rebase', async function () {
9998
_epoch = await uFragmentsPolicy.epoch.call();
10099
_time = await uFragmentsPolicy.lastRebaseTimestamp.call();
101100
await mockExternalData(1.6e18, 100, 1010);
102-
uFragSpy = new ContractEventSpy([mockUFragments.FunctionCalled, mockUFragments.FunctionArguments]);
103-
uFragSpy.watch();
104-
oracleSpy = new ContractEventSpy([mockMarketOracle.FunctionCalled, mockMarketOracle.FunctionArguments]);
105-
oracleSpy.watch();
106101
r = await uFragmentsPolicy.rebase();
107102
});
108-
after(async function () {
109-
uFragSpy.stopWatching();
110-
oracleSpy.stopWatching();
111-
});
112103

113104
it('should increment epoch', async function () {
114105
const epoch = await uFragmentsPolicy.epoch.call();
@@ -126,17 +117,20 @@ contract('UFragmentsPolicy:Rebase', async function () {
126117
expect(log.args.volume.toNumber()).to.eq(100);
127118
});
128119
it('should call getPriceAndVolume from the market oracle', async function () {
129-
const fnCalls = new MockFunctionSpy(oracleSpy).getCalledFunctions();
130-
expect(fnCalls[0].fnName).to.eq('MarketOracle:getPriceAndVolume');
131-
expect(fnCalls[0].calledBy).to.eq(uFragmentsPolicy.address);
132-
expect(fnCalls[0].arguments).to.be.empty;
120+
const fnCalled = mockUFragments.FunctionCalled().formatter(r.receipt.logs[0]);
121+
expect(fnCalled.args.functionName).to.eq('MarketOracle:getPriceAndVolume');
122+
expect(fnCalled.args.caller).to.eq(uFragmentsPolicy.address);
133123
});
134124
it('should call uFrag Rebase', async function () {
135-
const fnCalls = new MockFunctionSpy(uFragSpy).getCalledFunctions();
136-
const epoch = await uFragmentsPolicy.epoch.call();
137-
expect(fnCalls[0].fnName).to.eq('UFragments:rebase');
138-
expect(fnCalls[0].calledBy).to.eq(uFragmentsPolicy.address);
139-
expect(fnCalls[0].arguments).to.include.members([epoch.toNumber(), 20]);
125+
_epoch = await uFragmentsPolicy.epoch.call();
126+
const fnCalled = mockUFragments.FunctionCalled().formatter(r.receipt.logs[2]);
127+
expect(fnCalled.args.functionName).to.eq('UFragments:rebase');
128+
expect(fnCalled.args.caller).to.eq(uFragmentsPolicy.address);
129+
const fnArgs = mockUFragments.FunctionArguments().formatter(r.receipt.logs[3]);
130+
const parsedFnArgs = _.reduce(fnArgs.args, function (m, v, k) {
131+
return _.map(v, d => d.toNumber()).concat(m);
132+
}, [ ]);
133+
expect(parsedFnArgs).to.include.members([_epoch.toNumber(), 20]);
140134
});
141135
});
142136
});
@@ -146,16 +140,10 @@ contract('UFragmentsPolicy:Rebase', async function () {
146140
before('setup UFragmentsPolicy contract', setContractReferences);
147141

148142
describe('negative rate', function () {
149-
let uFragSpy;
150143
before(async function () {
151-
uFragSpy = new ContractEventSpy([mockUFragments.FunctionCalled, mockUFragments.FunctionArguments]);
152-
uFragSpy.watch();
153144
await mockExternalData(0.7e18, 100, 1000);
154145
r = await uFragmentsPolicy.rebase();
155146
});
156-
after(async function () {
157-
uFragSpy.stopWatching();
158-
});
159147

160148
it('should emit Rebase with negative appliedSupplyAdjustment', async function () {
161149
const log = r.logs[0];

util/blockchain_caller.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ BlockchainCaller.prototype.sendRawToBlockchain = function (method, params) {
2727
});
2828
};
2929

30-
BlockchainCaller.prototype.waitForSomeTime = function (durationInSec) {
30+
BlockchainCaller.prototype.waitForSomeTime = async function (durationInSec) {
3131
try {
32-
return this.sendRawToBlockchain('evm_increaseTime', [durationInSec]);
32+
const r = await this.sendRawToBlockchain('evm_increaseTime', [durationInSec]);
33+
return r;
3334
} catch (e) {
3435
return new Promise((resolve, reject) => {
3536
setTimeout(() => resolve(), durationInSec * 1000);

0 commit comments

Comments
 (0)