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

algod: Minor improvements to simulation support #749

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this parseBody parameter seems to parse to JSON by default:

https://github.com/algorand/js-algorand-sdk/blob/develop/src/client/client.ts#L261

Copy link
Contributor Author

@jasonpaulos jasonpaulos Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the underlying problem is that post and delete assume the response is always application/json. Probably they should use getAcceptFormat(query) like get does to figure out the actual response format.

But I'm ok with leaving this as-is for now, since passing parseBody as false is sufficient for the time being. We can address that problem with a separate issue/PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #751

);
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