Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Use Dev mode network for cucumber tests #614

Merged
merged 10 commits into from
Aug 1, 2022
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ unit:
node_modules/.bin/cucumber-js --tags "@unit.offline or @unit.algod or @unit.indexer or @unit.rekey or @unit.tealsign or @unit.dryrun or @unit.applications or @unit.responses or @unit.transactions or @unit.transactions.keyreg or @unit.transactions.payment or @unit.responses.231 or @unit.feetest or @unit.indexer.logs or @unit.abijson or @unit.abijson.byname or @unit.atomic_transaction_composer or @unit.responses.unlimited_assets or @unit.indexer.ledger_refactoring or @unit.algod.ledger_refactoring or @unit.dryrun.trace.application or @unit.sourcemap" tests/cucumber/features --require-module ts-node/register --require tests/cucumber/steps/index.js

integration:
node_modules/.bin/cucumber-js --tags "@algod or @assets or @auction or @kmd or @send or @indexer or @rekey or @send.keyregtxn or @dryrun or @compile or @applications or @indexer.applications or @applications.verified or @indexer.231 or @abi or @c2c or @compile.sourcemap" tests/cucumber/features --require-module ts-node/register --require tests/cucumber/steps/index.js
node_modules/.bin/cucumber-js --tags "@algod or @assets or @auction or @kmd or @send or @indexer or @rekey_v1 or @send.keyregtxn or @dryrun or @compile or @applications or @indexer.applications or @applications.verified or @indexer.231 or @abi or @c2c or @compile.sourcemap" tests/cucumber/features --require-module ts-node/register --require tests/cucumber/steps/index.js

docker-test:
./tests/cucumber/docker/run_docker.sh
Expand Down
94 changes: 89 additions & 5 deletions tests/cucumber/steps/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ module.exports = function getSteps(options) {
steps.then[name] = fn;
}

// Dev Mode State
const DEV_MODE_INITIAL_MICROALGOS = 10_000_000;

/*
* waitForAlgodInDevMode is a Dev mode helper method that waits for a transaction to resolve.
* Since Dev mode produces blocks on a per transaction basis, it's possible
* algod generates a block _before_ the corresponding SDK call to wait for a block.
* Without _any_ wait, it's possible the SDK looks for the transaction before algod completes processing.
* So, the method performs a local sleep to simulate waiting for a block.
*/
function waitForAlgodInDevMode() {
return new Promise((resolve) => setTimeout(resolve, 500));
}

const { algod_token: algodToken, kmd_token: kmdToken } = options;

Given('an algod client', async function () {
Expand Down Expand Up @@ -185,6 +199,26 @@ module.exports = function getSteps(options) {
});

When('I get status after this block', async function () {
// Send a transaction to advance blocks in dev mode.
const sp = await this.acl.getTransactionParams();
if (sp.firstRound === 0) sp.firstRound = 1;
const fundingTxnArgs = {
from: this.accounts[0],
to: this.accounts[0],
amount: 0,
fee: sp.fee,
firstRound: sp.lastRound + 1,
lastRound: sp.lastRound + 1000,
genesisHash: sp.genesishashb64,
genesisID: sp.genesisID,
};
const stxKmd = await this.kcl.signTransaction(
this.handle,
this.wallet_pswd,
fundingTxnArgs
);
await this.acl.sendRawTransaction(stxKmd);

this.statusAfter = await this.acl.statusAfterBlock(this.status.lastRound);
return this.statusAfter;
});
Expand Down Expand Up @@ -359,6 +393,35 @@ module.exports = function getSteps(options) {
return this.pk;
});

When(
'I generate a key using kmd for rekeying and fund it',
async function () {
this.rekey = await this.kcl.generateKey(this.handle);
this.rekey = this.rekey.address;
// Fund the rekey address with some Algos
const sp = await this.acl.getTransactionParams();
if (sp.firstRound === 0) sp.firstRound = 1;
const fundingTxnArgs = {
from: this.accounts[0],
to: this.rekey,
amount: DEV_MODE_INITIAL_MICROALGOS,
fee: sp.fee,
firstRound: sp.lastRound + 1,
lastRound: sp.lastRound + 1000,
genesisHash: sp.genesishashb64,
genesisID: sp.genesisID,
};

const stxKmd = await this.kcl.signTransaction(
this.handle,
this.wallet_pswd,
fundingTxnArgs
);
await this.acl.sendRawTransaction(stxKmd);
return this.rekey;
}
);

Then('the key should be in the wallet', async function () {
let keys = await this.kcl.listKeys(this.handle);
keys = keys.addresses;
Expand Down Expand Up @@ -431,6 +494,27 @@ module.exports = function getSteps(options) {
}
);

Given(
'default transaction with parameters {int} {string} and rekeying key',
async function (amt, note) {
this.pk = this.rekey;
const result = await this.acl.getTransactionParams();
this.lastRound = result.lastRound;
this.txn = {
from: this.rekey,
to: this.accounts[1],
fee: result.fee,
firstRound: result.lastRound + 1,
lastRound: result.lastRound + 1000,
genesisHash: result.genesishashb64,
genesisID: result.genesisID,
note: makeUint8Array(Buffer.from(note, 'base64')),
amount: parseInt(amt),
};
return this.txn;
}
);

Given(
'default multisig transaction with parameters {int} {string}',
async function (amt, note) {
Expand Down Expand Up @@ -801,13 +885,13 @@ module.exports = function getSteps(options) {
assert.deepStrictEqual(true, 'type' in info);
// let localParams = await this.acl.getTransactionParams();
// this.lastRound = localParams.lastRound;
await this.acl.statusAfterBlock(this.lastRound + 2);
await waitForAlgodInDevMode();
info = await this.acl.transactionById(this.txid);
assert.deepStrictEqual(true, 'type' in info);
});

Then('I can get the transaction by ID', async function () {
await this.acl.statusAfterBlock(this.lastRound + 2);
await waitForAlgodInDevMode();
const info = await this.acl.transactionById(this.txid);
assert.deepStrictEqual(true, 'type' in info);
});
Expand Down Expand Up @@ -3900,7 +3984,7 @@ module.exports = function getSteps(options) {
const info = await algosdk.waitForConfirmation(
this.v2Client,
fundingResponse.txId,
2
1
);
assert.ok(info['confirmed-round'] > 0);
}
Expand Down Expand Up @@ -4256,7 +4340,7 @@ module.exports = function getSteps(options) {
const info = await algosdk.waitForConfirmation(
this.v2Client,
fundingResponse.txId,
2
1
);
assert.ok(info['confirmed-round'] > 0);
}
Expand Down Expand Up @@ -4377,7 +4461,7 @@ module.exports = function getSteps(options) {
const info = await algosdk.waitForConfirmation(
this.v2Client,
this.appTxid.txId,
2
1
);
assert.ok(info['confirmed-round'] > 0);
});
Expand Down