Skip to content

Commit

Permalink
algod: Minor improvements to simulation support (#749)
Browse files Browse the repository at this point in the history
Co-authored-by: algochoi <86622919+algochoi@users.noreply.github.com>
  • Loading branch information
jasonpaulos and algochoi committed Mar 21, 2023
1 parent 0039284 commit 663dfa5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
14 changes: 7 additions & 7 deletions src/client/v2/algod/simulateTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ export default class SimulateRawTransactions extends JSONRequest<
const res = await this.c.post(
this.path(),
Buffer.from(this.txnBytesToPost),
txHeaders
txHeaders,
this.query,
false
);
return res.body;
return this.prepare(res.body);
}

// eslint-disable-next-line class-methods-use-this
prepare(body: Uint8Array) {
if (body && body.byteLength > 0) {
return encoding.decode(body) as SimulateResponse;
}
return undefined;
prepare(body: Uint8Array): SimulateResponse {
const decoded = encoding.decode(body);
return SimulateResponse.from_obj_for_encoding(decoded);
}
}
31 changes: 17 additions & 14 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,7 @@ export class AtomicTransactionComposer {
for (const [txnIndex, method] of this.methodCalls) {
const txID = this.txIDs[txnIndex];
const pendingInfo =
simulateResponse['txn-groups'][0]['txn-results'][txnIndex][
'txn-result'
];
simulateResponse.txnGroups[0].txnResults[txnIndex].txnResult;

const methodResult: ABIResult = {
txID,
Expand All @@ -649,7 +647,7 @@ export class AtomicTransactionComposer {
AtomicTransactionComposer.parseMethodResponse(
method,
methodResult,
pendingInfo
pendingInfo.get_obj_for_encoding()
)
);
}
Expand Down Expand Up @@ -710,24 +708,29 @@ export class AtomicTransactionComposer {
for (const [txnIndex, method] of this.methodCalls) {
const txID = txIDs[txnIndex];

const methodResult: ABIResult = {
let methodResult: ABIResult = {
txID,
rawReturnValue: new Uint8Array(),
method,
};

const pendingInfo =
txnIndex === firstMethodCallIndex
? confirmedTxnInfo
: // eslint-disable-next-line no-await-in-loop
await client.pendingTransactionInformation(txID).do();
methodResults.push(
AtomicTransactionComposer.parseMethodResponse(
try {
const pendingInfo =
txnIndex === firstMethodCallIndex
? confirmedTxnInfo
: // eslint-disable-next-line no-await-in-loop
await client.pendingTransactionInformation(txID).do();

methodResult = AtomicTransactionComposer.parseMethodResponse(
method,
methodResult,
pendingInfo
)
);
);
} catch (err) {
methodResult.decodeError = err;
}

methodResults.push(methodResult);
}

return {
Expand Down
20 changes: 8 additions & 12 deletions tests/cucumber/steps/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -4644,7 +4644,7 @@ module.exports = function getSteps(options) {
Then(
'the simulation should succeed without any failure message',
async function () {
assert.deepStrictEqual(true, this.simulateResponse['would-succeed']);
assert.deepStrictEqual(true, this.simulateResponse.wouldSucceed);
}
);

Expand All @@ -4656,14 +4656,13 @@ module.exports = function getSteps(options) {
const txnIndexes = stringPath.map((n) => parseInt(n, 10));
const groupNum = parseInt(txnGroupIndex, 10);

assert.deepStrictEqual(false, this.simulateResponse['would-succeed']);
assert.deepStrictEqual(false, this.simulateResponse.wouldSucceed);
// Check for missing signature flag
for (const txnIndex of txnIndexes) {
assert.deepStrictEqual(
true,
this.simulateResponse['txn-groups'][groupNum]['txn-results'][
txnIndex
]['missing-signature']
this.simulateResponse.txnGroups[groupNum].txnResults[txnIndex]
.missingSignature
);
}
}
Expand All @@ -4679,18 +4678,15 @@ module.exports = function getSteps(options) {
const stringPath = failAt.split(',');
const failPath = stringPath.map((n) => parseInt(n, 10));

const failedMessage = this.simulateResponse['txn-groups'][groupNum][
'failure-message'
];
assert.deepStrictEqual(false, this.simulateResponse['would-succeed']);
const failedMessage = this.simulateResponse.txnGroups[groupNum]
.failureMessage;
assert.deepStrictEqual(false, this.simulateResponse.wouldSucceed);
const errorContainsString = failedMessage.includes(errorMsg);
assert.deepStrictEqual(true, errorContainsString);

// Check path array
// deepStrictEqual fails for firefox tests, so compare array manually.
const failedAt = this.simulateResponse['txn-groups'][groupNum][
'failed-at'
];
const { failedAt } = this.simulateResponse.txnGroups[groupNum];
assert.strictEqual(failPath.length, failedAt.length);
for (let i = 0; i < failPath.length; i++) {
assert.strictEqual(failPath[i], failedAt[i]);
Expand Down

0 comments on commit 663dfa5

Please sign in to comment.