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

3.0.0: Improve object encoding and decoding #862

Merged
merged 33 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d003221
Improved encoding & decoding work, still work in progress
jasonpaulos Mar 25, 2024
6bdc9ba
Fix up transactions unit test
jasonpaulos Mar 27, 2024
6e80bd6
Fixing type errors
jasonpaulos Mar 28, 2024
71999a6
Update cucumber test branch
jasonpaulos Mar 28, 2024
6f58347
Fix more type errors
jasonpaulos Mar 29, 2024
312fd65
Untyped model
jasonpaulos Apr 2, 2024
04af841
Fixed untyped value conversions
jasonpaulos Apr 2, 2024
564c889
Temporarily disable block parsing unit tests
jasonpaulos Apr 3, 2024
6637ead
Fix integration test errors
jasonpaulos Apr 4, 2024
6235396
Update examples
jasonpaulos Apr 4, 2024
1ecb5e2
Initial work on unifying msgp and json paths with a schema object
jasonpaulos Apr 5, 2024
abcbd9a
Make everything Encodable, including generated models
jasonpaulos Apr 11, 2024
d6c4913
Fix most unit tests
jasonpaulos Apr 30, 2024
54aeb78
Update json decoding references
jasonpaulos May 1, 2024
3b99c59
Passing unit tests
jasonpaulos May 2, 2024
3d911fc
Type issue
jasonpaulos May 2, 2024
65e0724
Fixing integration test failures
jasonpaulos May 2, 2024
381347d
fix mistake
jasonpaulos May 2, 2024
7e6ca55
Simplifying code
jasonpaulos May 10, 2024
0a493ae
Remove BaseModel, introduce tests for Schemas
jasonpaulos May 10, 2024
272df51
Fix issues
jasonpaulos May 10, 2024
b11917b
Clean up box translation
jasonpaulos May 10, 2024
3016e9a
Regenerate types, simplify toEncodingData
jasonpaulos May 23, 2024
ce0d857
Clean up generation of toEncodingData and fromEncodingData functions
jasonpaulos May 28, 2024
d72e724
Mark types.ts as generated following https://github.com/github-lingui…
ohill May 29, 2024
b864c24
set linguist-generated=true https://docs.github.com/en/repositories/w…
ohill May 29, 2024
f79528e
Remove `required` from NamedMapEntry & introduce OptionalSchema in it…
jasonpaulos May 30, 2024
6c90132
Fix integration test error
jasonpaulos May 31, 2024
8a29264
Expose and test encode/decodeJSON
jasonpaulos Jun 4, 2024
f67acb6
Migration guide update
jasonpaulos Jun 4, 2024
d11db00
typo
jasonpaulos Jun 4, 2024
c82a856
docstrings
jasonpaulos Jun 4, 2024
86a6e11
Update .test-env
jasonpaulos Jun 5, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/esm/*
.vscode/*
!.vscode/settings.json
!.vscode/extensions.json
!.vscode/launch.json

# npm
node_modules/
Expand Down
2 changes: 1 addition & 1 deletion .test-env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configs for testing repo download:
SDK_TESTING_URL="https://github.com/algorand/algorand-sdk-testing"
SDK_TESTING_BRANCH="V2"
SDK_TESTING_BRANCH="fix-mock-response-txn-encoding-types"
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
SDK_TESTING_HARNESS="test-harness"

INSTALL_ONLY=0
Expand Down
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug unit tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/tests/mocha.js",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/tsx",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"env": {
"NODE_ENV": "testing",
"MOCHA_TIMEOUT": "0"
},
"skipFiles": [
// Node.js internal core modules
"<node_internals>/**",
// Ignore all dependencies (optional)
"${workspaceFolder}/node_modules/**"
]
}
]
}
2 changes: 1 addition & 1 deletion examples/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function main() {
txns: signedTxns,
});

console.log('Dryrun:', dryrunRequest.get_obj_for_encoding());
console.log('Dryrun:', dryrunRequest);
// example: DEBUG_DRYRUN_DUMP

// example: DEBUG_DRYRUN_SUBMIT
Expand Down
4 changes: 2 additions & 2 deletions examples/participation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function main() {
suggestedParams: params,
});

console.log(onlineKeyreg.get_obj_for_encoding());
console.log(onlineKeyreg);
// example: TRANSACTION_KEYREG_ONLINE_CREATE

// example: TRANSACTION_KEYREG_OFFLINE_CREATE
Expand All @@ -55,7 +55,7 @@ async function main() {
suggestedParams,
nonParticipation: true,
});
console.log(offlineKeyReg.get_obj_for_encoding());
console.log(offlineKeyReg);
// example: TRANSACTION_KEYREG_OFFLINE_CREATE
}

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"url": "git://github.com/algorand/js-algorand-sdk.git"
},
"dependencies": {
"algo-msgpack-with-bigint": "^2.1.1",
"algorand-msgpack": "^1.0.1",
"hi-base32": "^0.5.1",
"js-sha256": "^0.9.0",
"js-sha3": "^0.8.0",
Expand Down
23 changes: 9 additions & 14 deletions src/boxStorage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { EncodedBoxReference } from './types/transactions/index.js';
import { BoxReference } from './types/transactions/base.js';

function translateBoxReference(
function boxReferenceToEncodingData(
reference: BoxReference,
foreignApps: bigint[],
appIndex: bigint
): EncodedBoxReference {
): Map<string, unknown> {
gmalouf marked this conversation as resolved.
Show resolved Hide resolved
const referenceId = BigInt(reference.appIndex);
const referenceName = reference.name;
const isOwnReference = referenceId === BigInt(0) || referenceId === appIndex;
Expand All @@ -22,28 +21,24 @@ function translateBoxReference(
throw new Error(`Box ref with appId ${referenceId} not in foreign-apps`);
}

const encodedReference: EncodedBoxReference = {};
if (index !== 0) {
encodedReference.i = index;
}
if (referenceName.length) {
encodedReference.n = referenceName;
}
return encodedReference;
return new Map<string, number | Uint8Array>([
['i', index],
['n', referenceName],
]);
}

/**
* translateBoxReferences translates an array of BoxReferences with app IDs
* into an array of EncodedBoxReferences with foreign indices.
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
*/
export function translateBoxReferences(
export function boxReferencesToEncodingData(
references: ReadonlyArray<BoxReference>,
foreignApps: ReadonlyArray<number | bigint>,
appIndex: number | bigint
): EncodedBoxReference[] {
): Array<Map<string, unknown>> {
const appIndexBigInt = BigInt(appIndex);
const foreignAppsBigInt = foreignApps.map(BigInt);
return references.map((bx) =>
translateBoxReference(bx, foreignAppsBigInt, appIndexBigInt)
boxReferenceToEncodingData(bx, foreignAppsBigInt, appIndexBigInt)
);
}
1 change: 0 additions & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Generics
export { default as JSONRequest } from './v2/jsonrequest.js';
export { default as BaseModel } from './v2/basemodel.js';
export { default as ServiceClient } from './v2/serviceClient.js';
export * from './baseHTTPClient.js';
export * from './urlTokenBaseHTTPClient.js';
Expand Down
4 changes: 3 additions & 1 deletion src/client/v2/algod/accountApplicationInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class AccountApplicationInformation extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AccountApplicationResponse {
return AccountApplicationResponse.from_obj_for_encoding(body);
return AccountApplicationResponse.fromEncodingData(
Copy link
Contributor

Choose a reason for hiding this comment

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

imo this interface from user point of view is too verbose. Can't fromEncodingData check if it is getting raw json or a encodingSchema-compatible dict ?
This pattern repeats over and over and over except few places where dicts supplied into fromEncodingData.
Well, unless all these verboseness is only in auto-generated and not exposed to end user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is pretty verbose and not very nice.

When decoding from a serialized JSON string, decodeJSON can be used, e.g. decodeJSON(str, AccountApplicationResponse). This is what I expect users will need the vast majority of the time, not the verbose code present in the request classes.

In these request classes, we are in a bit of weird situation when prepare function is called, since the JSON response is already parsed into an object. I have a desire to change this so that the JSON string is the input to prepare, then decodeJSON can be used and the code would be cleaner. But this PR has already grown large, so I didn't want to attempt that now.

AccountApplicationResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/accountAssetInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default class AccountAssetInformation extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): AccountAssetResponse {
return AccountAssetResponse.from_obj_for_encoding(body);
return AccountAssetResponse.fromEncodingData(
AccountAssetResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/accountInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class AccountInformation extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): Account {
return Account.from_obj_for_encoding(body);
return Account.fromEncodingData(
Account.encodingSchema.fromPreparedJSON(body)
);
}
}
8 changes: 4 additions & 4 deletions src/client/v2/algod/algod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import SetBlockOffsetTimestamp from './setBlockOffsetTimestamp.js';
import GetBlockOffsetTimestamp from './getBlockOffsetTimestamp.js';
import Disassemble from './disassemble.js';
import SimulateRawTransactions from './simulateTransaction.js';
import { EncodedSignedTransaction } from '../../../types/transactions/index.js';
import { SignedTransaction } from '../../../signedTransaction.js';
import * as encoding from '../../../encoding/encoding.js';
import Ready from './ready.js';
import UnsetSyncRound from './unsetSyncRound.js';
Expand Down Expand Up @@ -614,13 +614,13 @@ export class AlgodClient extends ServiceClient {
* @category POST
*/
simulateRawTransactions(stxOrStxs: Uint8Array | Uint8Array[]) {
const txnObjects: EncodedSignedTransaction[] = [];
const txnObjects: SignedTransaction[] = [];
if (Array.isArray(stxOrStxs)) {
for (const stxn of stxOrStxs) {
txnObjects.push(encoding.decode(stxn) as EncodedSignedTransaction);
txnObjects.push(encoding.decodeMsgpack(stxn, SignedTransaction));
}
} else {
txnObjects.push(encoding.decode(stxOrStxs) as EncodedSignedTransaction);
txnObjects.push(encoding.decodeMsgpack(stxOrStxs, SignedTransaction));
}
const request = new modelsv2.SimulateRequest({
txnGroups: [
Expand Down
4 changes: 1 addition & 3 deletions src/client/v2/algod/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export default class Block extends JSONRequest<BlockResponse, Uint8Array> {

// eslint-disable-next-line class-methods-use-this
prepare(body: Uint8Array): BlockResponse {
return BlockResponse.from_obj_for_encoding(
encoding.decode(body) as Record<string, any>
);
return encoding.decodeMsgpack(body, BlockResponse);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class Compile extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): CompileResponse {
return CompileResponse.from_obj_for_encoding(body);
return CompileResponse.fromEncodingData(
CompileResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/disassemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export default class Disassemble extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): DisassembleResponse {
return DisassembleResponse.from_obj_for_encoding(body);
return DisassembleResponse.fromEncodingData(
DisassembleResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
6 changes: 4 additions & 2 deletions src/client/v2/algod/dryrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Dryrun extends JSONRequest<

constructor(c: HTTPClient, dr: modelsv2.DryrunRequest) {
super(c);
this.blob = encoding.encode(dr.get_obj_for_encoding(true, true)!);
this.blob = encoding.encodeMsgpack(dr);
}

// eslint-disable-next-line class-methods-use-this
Expand All @@ -40,6 +40,8 @@ export default class Dryrun extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): DryrunResponse {
return DryrunResponse.from_obj_for_encoding(body);
return DryrunResponse.fromEncodingData(
DryrunResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
2 changes: 1 addition & 1 deletion src/client/v2/algod/getApplicationBoxByName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ export default class GetApplicationBoxByName extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): Box {
return Box.from_obj_for_encoding(body);
return Box.fromEncodingData(Box.encodingSchema.fromPreparedJSON(body));
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getApplicationBoxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export default class GetApplicationBoxes extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): BoxesResponse {
return BoxesResponse.from_obj_for_encoding(body);
return BoxesResponse.fromEncodingData(
BoxesResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getApplicationByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class GetApplicationByID extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): Application {
return Application.from_obj_for_encoding(body);
return Application.fromEncodingData(
Application.encodingSchema.fromPreparedJSON(body)
);
}
}
2 changes: 1 addition & 1 deletion src/client/v2/algod/getAssetByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export default class GetAssetByID extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): Asset {
return Asset.from_obj_for_encoding(body);
return Asset.fromEncodingData(Asset.encodingSchema.fromPreparedJSON(body));
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getBlockHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class GetBlockHash extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): BlockHashResponse {
return BlockHashResponse.from_obj_for_encoding(body);
return BlockHashResponse.fromEncodingData(
BlockHashResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getBlockOffsetTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default class GetBlockOffsetTimestamp extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): GetBlockTimeStampOffsetResponse {
return GetBlockTimeStampOffsetResponse.from_obj_for_encoding(body);
return GetBlockTimeStampOffsetResponse.fromEncodingData(
GetBlockTimeStampOffsetResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getSyncRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default class GetSyncRound extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): GetSyncRoundResponse {
return GetSyncRoundResponse.from_obj_for_encoding(body);
return GetSyncRoundResponse.fromEncodingData(
GetSyncRoundResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export default class GetTransactionGroupLedgerStateDeltasForRound extends JSONRe
prepare(
body: Record<string, any>
): TransactionGroupLedgerStateDeltasForRoundResponse {
return TransactionGroupLedgerStateDeltasForRoundResponse.from_obj_for_encoding(
body
return TransactionGroupLedgerStateDeltasForRoundResponse.fromEncodingData(
TransactionGroupLedgerStateDeltasForRoundResponse.encodingSchema.fromPreparedJSON(
body
)
);
}
}
4 changes: 3 additions & 1 deletion src/client/v2/algod/getTransactionProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export default class GetTransactionProof extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): TransactionProofResponse {
return TransactionProofResponse.from_obj_for_encoding(body);
return TransactionProofResponse.fromEncodingData(
TransactionProofResponse.encodingSchema.fromPreparedJSON(body)
);
}
}
2 changes: 1 addition & 1 deletion src/client/v2/algod/lightBlockHeaderProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export default class LightBlockHeaderProof extends JSONRequest<

// eslint-disable-next-line class-methods-use-this
prepare(body: Record<string, any>): LBHP {
return LBHP.from_obj_for_encoding(body);
return LBHP.fromEncodingData(LBHP.encodingSchema.fromPreparedJSON(body));
}
}
Loading