-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move wallet manager "zero balance" check to transaction handlers (…
- Loading branch information
Showing
31 changed files
with
841 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
__tests__/e2e/tests/scenarios/scenario1/htlc-claim/0.transfer-new-wallet.action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use strict"; | ||
|
||
const { Managers, Utils } = require("@arkecosystem/crypto"); | ||
const utils = require("./utils"); | ||
const testUtils = require("../../../../lib/utils/test-utils"); | ||
const { delegates } = require("../../../../lib/utils/testnet"); | ||
const { TransactionFactory } = require('../../../../../helpers/transaction-factory'); | ||
|
||
/** | ||
* Creates a transaction to a new wallet | ||
* @param {Object} options = { } | ||
* @return {void} | ||
*/ | ||
module.exports = async options => { | ||
Managers.configManager.setFromPreset("testnet"); | ||
|
||
const senderWallet = delegates[7]; // better use a different delegate for each scenario initial transfer | ||
let transaction1 = TransactionFactory.transfer(utils.htlcSender.address, 1000 * Math.pow(10, 8), "send coins to htlc sender") | ||
.withFee(0.1 * Math.pow(10, 8)) | ||
.withNonce(Utils.BigNumber.make(2)) | ||
.withPassphrase(senderWallet.passphrase) | ||
.createOne(); | ||
|
||
await testUtils.POST("transactions", { | ||
transactions: [transaction1], | ||
}); | ||
}; |
92 changes: 92 additions & 0 deletions
92
__tests__/e2e/tests/scenarios/scenario1/htlc-claim/1.create-lock-txs.action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"use strict"; | ||
|
||
const { Managers, Crypto, Utils } = require("@arkecosystem/crypto"); | ||
const utils = require("./utils"); | ||
const shared = require("./shared"); | ||
const testUtils = require("../../../../lib/utils/test-utils"); | ||
const { TransactionFactory } = require('../../../../../helpers/transaction-factory'); | ||
|
||
/** | ||
* Send lock transactions, we need 4 lock transactions to then test : | ||
* - associated lock tx does not exist | ||
* - secret hash does not match | ||
* - not recipient of lock tx | ||
* - lock expired | ||
* @param {Object} options = { } | ||
* @return {void} | ||
*/ | ||
module.exports = async options => { | ||
Managers.configManager.setFromPreset("testnet"); | ||
|
||
const nodesHeight = await testUtils.getNodesHeight(); | ||
const lastHeight = Math.max(...nodesHeight); | ||
|
||
// "normal" htlc lock transaction that will allow to claim without issue | ||
shared.lockTransactions.normal = TransactionFactory.htlcLock( | ||
{ | ||
secretHash: Crypto.HashAlgorithms.sha256(utils.htlcRecipient1.address.slice(0, 32)).toString("hex"), | ||
expiration: { | ||
type: 2, | ||
value: lastHeight + 12, | ||
}, | ||
}, | ||
utils.htlcRecipient1.address, | ||
3 * Math.pow(10, 8) | ||
) | ||
.withFee(0.1 * Math.pow(10, 8)) | ||
.withPassphrase(utils.htlcSender.passphrase) | ||
.createOne(); | ||
|
||
// htlc lock transaction that we will claim with a wrong secret hash | ||
shared.lockTransactions.wrongSecret = TransactionFactory.htlcLock( | ||
{ | ||
secretHash: Crypto.HashAlgorithms.sha256(utils.htlcRecipient2.address.slice(0, 32)).toString("hex"), | ||
expiration: { | ||
type: 2, | ||
value: lastHeight + 12, | ||
}, | ||
}, | ||
utils.htlcRecipient2.address, | ||
3 * Math.pow(10, 8) | ||
) | ||
.withFee(0.1 * Math.pow(10, 8)) | ||
.withNonce(Utils.BigNumber.make(1)) | ||
.withPassphrase(utils.htlcSender.passphrase) | ||
.createOne(); | ||
|
||
// htlc lock transaction that we will claim with a wallet not recipient of the lock tx | ||
shared.lockTransactions.notRecipient = TransactionFactory.htlcLock( | ||
{ | ||
secretHash: Crypto.HashAlgorithms.sha256(utils.htlcRecipient3.address.slice(0, 32)).toString("hex"), | ||
expiration: { | ||
type: 2, | ||
value: lastHeight + 12, | ||
}, | ||
}, | ||
utils.htlcRecipient3.address, | ||
3 * Math.pow(10, 8) | ||
) | ||
.withFee(0.1 * Math.pow(10, 8)) | ||
.withNonce(Utils.BigNumber.make(2)) | ||
.withPassphrase(utils.htlcSender.passphrase) | ||
.createOne(); | ||
|
||
// htlc lock transaction that we will claim after lock expiration | ||
shared.lockTransactions.lockExpired = TransactionFactory.htlcLock( | ||
{ | ||
secretHash: Crypto.HashAlgorithms.sha256(utils.htlcRecipient4.address.slice(0, 32)).toString("hex"), | ||
expiration: { | ||
type: 2, | ||
value: lastHeight + 1, | ||
}, | ||
}, | ||
utils.htlcRecipient4.address, | ||
3 * Math.pow(10, 8) | ||
) | ||
.withFee(0.1 * Math.pow(10, 8)) | ||
.withNonce(Utils.BigNumber.make(3)) | ||
.withPassphrase(utils.htlcSender.passphrase) | ||
.createOne(); | ||
|
||
await testUtils.POST("transactions", { transactions: Object.values(shared.lockTransactions) }, 1); // to node 1 | ||
}; |
25 changes: 25 additions & 0 deletions
25
__tests__/e2e/tests/scenarios/scenario1/htlc-claim/2.check-lock.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
|
||
const testUtils = require("../../../../lib/utils/test-utils"); | ||
const utils = require("./utils"); | ||
const shared = require("./shared"); | ||
|
||
describe("Check confirmed and unconfirmed transactions", () => { | ||
it("should have no unconfirmed transaction", async () => { | ||
const response = await testUtils.GET("transactions/unconfirmed", {}, 1); | ||
testUtils.expectSuccessful(response); | ||
const transactions = response.data.data; | ||
|
||
expect(transactions.length).toBe(0); | ||
}); | ||
|
||
it("should have all lock transactions forged", async () => { | ||
const response = await testUtils.GET("transactions"); | ||
testUtils.expectSuccessful(response); | ||
const transactions = response.data.data; | ||
|
||
for (const recipientName of ["htlcRecipient1", "htlcRecipient2", "htlcRecipient3", "htlcRecipient4"]) { | ||
expect(transactions.filter(transaction => transaction.recipient === utils[recipientName].address).length).toBe(1); | ||
} | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
__tests__/e2e/tests/scenarios/scenario1/htlc-claim/3.create-claim-txs.action.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
|
||
const { Managers, Utils } = require("@arkecosystem/crypto"); | ||
const utils = require("./utils"); | ||
const shared = require("./shared"); | ||
const testUtils = require("../../../../lib/utils/test-utils"); | ||
const { TransactionFactory } = require('../../../../../helpers/transaction-factory'); | ||
|
||
/** | ||
* Send claim transactions, we need 4 claim transactions to test when : | ||
* - associated lock tx does not exist | ||
* - secret hash does not match | ||
* - not recipient of lock tx | ||
* - lock expired | ||
* @param {Object} options = { } | ||
* @return {void} | ||
*/ | ||
module.exports = async options => { | ||
Managers.configManager.setFromPreset("testnet"); | ||
|
||
// 1st transaction : "normal" htlc lock transaction that we claim without issue | ||
shared.claimTransactions.normal = TransactionFactory.htlcClaim( | ||
{ | ||
lockTransactionId: shared.lockTransactions.normal.id, | ||
unlockSecret: shared.lockTransactions.normal.recipientId.slice(0, 32), | ||
} | ||
) | ||
.withPassphrase(utils.htlcRecipient1.passphrase) | ||
.createOne(); | ||
|
||
// 2nd transaction : htlc lock transaction that we claim with a wrong secret hash | ||
shared.claimTransactions.wrongSecret = TransactionFactory.htlcClaim( | ||
{ | ||
lockTransactionId: shared.lockTransactions.wrongSecret.id, | ||
unlockSecret: "thatisasecretthatissooooooowrong", | ||
} | ||
) | ||
.withPassphrase(utils.htlcRecipient2.passphrase) | ||
.createOne(); | ||
|
||
// 3rd transaction : htlc lock transaction that we claim with a wallet not recipient of the lock tx | ||
shared.claimTransactions.notRecipient = TransactionFactory.htlcClaim( | ||
{ | ||
lockTransactionId: shared.lockTransactions.notRecipient.id, | ||
unlockSecret: shared.lockTransactions.notRecipient.recipientId.slice(0, 32), | ||
} | ||
) | ||
.withPassphrase(utils.htlcNotRecipient.passphrase) | ||
.createOne(); | ||
|
||
// 4th transaction : htlc lock transaction that we will claim after lock expiration | ||
shared.claimTransactions.lockExpired = TransactionFactory.htlcClaim( | ||
{ | ||
lockTransactionId: shared.lockTransactions.lockExpired.id, | ||
unlockSecret: shared.lockTransactions.lockExpired.recipientId.slice(0, 32), | ||
} | ||
) | ||
.withPassphrase(utils.htlcRecipient4.passphrase) | ||
.createOne(); | ||
|
||
await testUtils.POST("transactions", { transactions: Object.values(shared.claimTransactions) }, 1); // to node 1 | ||
}; |
31 changes: 31 additions & 0 deletions
31
__tests__/e2e/tests/scenarios/scenario1/htlc-claim/4.check-claim.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
const testUtils = require("../../../../lib/utils/test-utils"); | ||
const utils = require("./utils"); | ||
const shared = require("./shared"); | ||
|
||
describe("Check confirmed and unconfirmed transactions", () => { | ||
it("should have no unconfirmed transaction", async () => { | ||
const response = await testUtils.GET("transactions/unconfirmed", {}, 1); | ||
testUtils.expectSuccessful(response); | ||
const transactions = response.data.data; | ||
|
||
expect(transactions.length).toBe(0); | ||
}); | ||
|
||
it("should have valid transactions forged and invalid not forged", async () => { | ||
const response = await testUtils.GET("transactions"); | ||
testUtils.expectSuccessful(response); | ||
const transactions = response.data.data; | ||
|
||
// recipients 1 and htlcNotRecipient sent valid htlc claim transactions | ||
for (const senderName of ["htlcRecipient1", "htlcNotRecipient"]) { | ||
expect(transactions.filter(transaction => transaction.sender === utils[senderName].address).length).toBe(1); | ||
} | ||
|
||
// recipients 2 and 4 sent invalid htlc claim transactions | ||
for (const senderName of ["htlcRecipient2", "htlcRecipient4"]) { | ||
expect(transactions.filter(transaction => transaction.sender === utils[senderName].address).length).toBe(0); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.