This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
DepositFraudTest.js
374 lines (312 loc) · 11 KB
/
DepositFraudTest.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const {deployAndLinkAll} = require("./helpers/testDeployer.js")
const {states, bytes32zero} = require("./helpers/utils.js")
const {createSnapshot, restoreSnapshot} = require("./helpers/snapshot.js")
const {AssertBalance} = require("./helpers/assertBalance.js")
const {accounts, web3} = require("@openzeppelin/test-environment")
const [owner] = accounts
const {BN, constants, expectRevert} = require("@openzeppelin/test-helpers")
const {ZERO_ADDRESS} = constants
const {expect} = require("chai")
describe("DepositFraud", async function() {
let tbtcConstants
let tbtcSystemStub
let tbtcDepositToken
let testDeposit
let ecdsaKeepStub
let beneficiary
let assertBalance
before(async () => {
;({
tbtcConstants,
tbtcToken,
tbtcSystemStub,
tbtcDepositToken,
testDeposit,
ecdsaKeepStub,
} = await deployAndLinkAll())
beneficiary = accounts[4]
assertBalance = new AssertBalance(tbtcToken)
tbtcDepositToken.forceMint(
beneficiary,
web3.utils.toBN(testDeposit.address),
)
})
beforeEach(async () => {
await testDeposit.reset()
await ecdsaKeepStub.reset()
await testDeposit.setKeepAddress(ecdsaKeepStub.address)
})
describe("provideFundingECDSAFraudProof", async () => {
const bond = 1000
before(async () => {
timer = await tbtcConstants.getFundingTimeout.call()
})
beforeEach(async () => {
await ecdsaKeepStub.setSuccess(true)
await testDeposit.setState(states.AWAITING_BTC_FUNDING_PROOF)
await ecdsaKeepStub.send(bond, {from: owner})
})
it("updates to awaiting fraud funding proof, distributes signer bond to funder, and logs FraudDuringSetup", async () => {
const blockNumber = await web3.eth.getBlock("latest").number
await testDeposit.provideFundingECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
)
await assertBalance.eth(ecdsaKeepStub.address, new BN(0))
await assertBalance.eth(testDeposit.address, new BN(bond))
const withdrawable = await testDeposit.getWithdrawAllowance.call({
from: beneficiary,
})
expect(withdrawable).to.eq.BN(new BN(bond))
const depositState = await testDeposit.getState.call()
expect(depositState).to.eq.BN(states.FAILED_SETUP)
const eventList = await tbtcSystemStub.getPastEvents("FraudDuringSetup", {
fromBlock: blockNumber,
toBlock: "latest",
})
expect(eventList.length, "bad event length").to.equal(1)
})
it("reverts if not awaiting funding proof", async () => {
await testDeposit.setState(states.START)
await expectRevert(
testDeposit.provideFundingECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Signer fraud during funding flow only available while awaiting funding",
)
})
it("reverts if the signature is not fraud", async () => {
await ecdsaKeepStub.setSuccess(false)
await expectRevert(
testDeposit.provideFundingECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Signature is not fraudulent",
)
})
})
describe("startSignerFraudLiquidation", async () => {
let signerBond
before(async () => {
signerBond = 10000000
await ecdsaKeepStub.send(signerBond, {from: accounts[1]})
})
beforeEach(async () => {
await createSnapshot()
})
afterEach(async () => {
await restoreSnapshot()
})
it("executes and emits StartedLiquidation event", async () => {
const block = await web3.eth.getBlock("latest")
await testDeposit.startSignerFraudLiquidation({from: owner})
const events = await tbtcSystemStub.getPastEvents("StartedLiquidation", {
fromBlock: block.number,
toBlock: "latest",
})
const initiator = await testDeposit.getLiquidationInitiator()
const initiated = await testDeposit.getLiquidationTimestamp()
expect(events[0].returnValues[0]).to.equal(testDeposit.address)
expect(events[0].returnValues[1]).to.be.true
expect(events[0].returnValues[2]).to.eq.BN(block.timestamp)
expect(initiator).to.equal(owner)
expect(initiated).to.eq.BN(block.timestamp)
})
it("liquidates immediately with bonds going to the redeemer if we came from the redemption flow", async () => {
// setting redeemer address suggests we are coming from redemption flow
testDeposit.setRedeemerAddress(owner)
const currentBond = await web3.eth.getBalance(ecdsaKeepStub.address)
const block = await web3.eth.getBlock("latest")
await testDeposit.startSignerFraudLiquidation()
const events = await tbtcSystemStub.getPastEvents("Liquidated", {
fromBlock: block.number,
toBlock: "latest",
})
const withdrawable = await testDeposit.getWithdrawAllowance.call({
from: owner,
})
expect(withdrawable).to.eq.BN(new BN(currentBond))
expect(events[0].returnValues[0]).to.equal(testDeposit.address)
const depositState = await testDeposit.getState.call()
expect(depositState).to.eq.BN(states.LIQUIDATED)
})
})
describe("startSignerAbortLiquidation", async () => {
let signerBond
before(async () => {
signerBond = 10000000
await ecdsaKeepStub.send(signerBond, {from: accounts[1]})
})
beforeEach(async () => {
await createSnapshot()
})
afterEach(async () => {
await restoreSnapshot()
})
it("executes and emits StartedLiquidation event", async () => {
const block = await web3.eth.getBlock("latest")
await testDeposit.startSignerAbortLiquidation({from: owner})
const events = await tbtcSystemStub.getPastEvents("StartedLiquidation", {
fromBlock: block.number,
toBlock: "latest",
})
const initiator = await testDeposit.getLiquidationInitiator()
const initiated = await testDeposit.getLiquidationTimestamp()
expect(events[0].returnValues[0]).to.equal(testDeposit.address)
expect(events[0].returnValues[1]).to.be.false
expect(events[0].returnValues[2]).to.eq.BN(block.timestamp)
expect(initiator).to.equal(owner)
expect(initiated).to.eq.BN(block.timestamp)
})
})
describe("provideECDSAFraudProof", async () => {
before(async () => {
await testDeposit.setState(states.ACTIVE)
await ecdsaKeepStub.send(1000000, {from: owner})
})
beforeEach(async () => {
await createSnapshot()
})
afterEach(async () => {
await restoreSnapshot()
})
it("executes and moves state to FRAUD_LIQUIDATION_IN_PROGRESS", async () => {
await testDeposit.provideECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
)
const depositState = await testDeposit.getState.call()
expect(depositState).to.eq.BN(states.FRAUD_LIQUIDATION_IN_PROGRESS)
})
it("reverts if in the funding flow", async () => {
await testDeposit.setState(states.AWAITING_BTC_FUNDING_PROOF)
await expectRevert(
testDeposit.provideECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Use provideFundingECDSAFraudProof instead",
)
})
it("reverts if already in signer liquidation", async () => {
await testDeposit.setState(states.LIQUIDATION_IN_PROGRESS)
await expectRevert(
testDeposit.provideECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Signer liquidation already in progress",
)
})
it("reverts if the contract has halted", async () => {
await testDeposit.setState(states.REDEEMED)
await expectRevert(
testDeposit.provideECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Contract has halted",
)
})
it("reverts if signature is not fraud according to Keep", async () => {
await ecdsaKeepStub.setSuccess(false)
await expectRevert(
testDeposit.provideECDSAFraudProof(
0,
bytes32zero,
bytes32zero,
bytes32zero,
"0x00",
),
"Signature is not fraud",
)
})
})
describe("validateRedeemerNotPaid", async () => {
const _txOutputVector =
"0x012040351d0000000016001486e7303082a6a21d5837176bc808bf4828371ab6"
const redeemerOutputScript =
"0x16001486e7303082a6a21d5837176bc808bf4828371ab6"
const prevoutValueBytes = "0xf078351d00000000"
const outpoint =
"0x913e39197867de39bff2c93c75173e086388ee7e8707c90ce4a02dd23f7d2c0d00000000"
const _longTxOutputVector = `0x034897070000000000220020a4333e5612ab1a1043b25755c89b16d55184a42f81799e623e6bc39db8539c180000000000000000166a14edb1b5c2f39af0fec151732585b1049b078952112040351d0000000016001486e7303082a6a21d5837176bc808bf4828371ab6`
beforeEach(async () => {
await testDeposit.setUTXOInfo(prevoutValueBytes, 0, outpoint)
await testDeposit.setRequestInfo(
ZERO_ADDRESS,
redeemerOutputScript,
2424,
0,
bytes32zero,
)
})
it("returns false if redeemer is paid and value is sufficient", async () => {
const success = await testDeposit.validateRedeemerNotPaid(_txOutputVector)
expect(success).to.be.false
})
it("returns false if redeemer is paid, value is sufficient and output is at 3rd position", async () => {
const success = await testDeposit.validateRedeemerNotPaid(
_longTxOutputVector,
)
expect(success).to.be.false
})
it("returns true if redeemer is not paid", async () => {
await testDeposit.setRequestInfo(
ZERO_ADDRESS,
"0x" + "0".repeat(20),
2424,
0,
bytes32zero,
)
const success = await testDeposit.validateRedeemerNotPaid(_txOutputVector)
expect(success).to.be.true
})
it("returns true if value is not sufficient", async () => {
await testDeposit.setUTXOInfo("0xf078351d00000001", 0, outpoint)
const success = await testDeposit.validateRedeemerNotPaid(_txOutputVector)
expect(success).to.be.true
})
it("returns true if there is no witness flag", async () => {
const _txOutputVectorNoWitness =
"0x024897070000000000220020a4333e5612ab1a1043b25755c89b16d55184a42f81799e623e6bc39db8539c180000000000000000166a14edb1b5c2f39af0fec151732585b1049b07895211"
const newPKH =
"0xa4333e5612ab1a1043b25755c89b16d55184a42f81799e623e6bc39db8539c18" // note length > 20
await testDeposit.setRequestInfo(
ZERO_ADDRESS,
newPKH,
2424,
0,
bytes32zero,
)
await testDeposit.setUTXOInfo("0xffff", 0, outpoint)
const success = await testDeposit.validateRedeemerNotPaid(
_txOutputVectorNoWitness,
)
expect(success).to.be.true
})
})
})