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

Automatically sort maps to match Soroban environment expectations #759

Merged
merged 6 commits into from
Jun 28, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* Improve the efficiency and portability of asset type retrieval ([#758](https://github.com/stellar/js-stellar-base/pull/758)).
* `nativeToScVal` now correctly sorts maps lexicographically based on the keys to match what the Soroban environment expects ([#759](https://github.com/stellar/js-stellar-base/pull/759)).


## [`v12.0.1`](https://github.com/stellar/js-stellar-base/compare/v12.0.0...v12.0.1)
Expand Down
34 changes: 20 additions & 14 deletions src/scval.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ import { ScInt, XdrLargeInt, scValToBigInt } from './numbers/index';
*/
export function nativeToScVal(val, opts = {}) {
switch (typeof val) {
case 'object':
case 'object': {
if (val === null) {
return xdr.ScVal.scvVoid();
}
Expand Down Expand Up @@ -191,20 +191,26 @@ export function nativeToScVal(val, opts = {}) {
}

return xdr.ScVal.scvMap(
Object.entries(val).map(([k, v]) => {
// the type can be specified with an entry for the key and the value,
// e.g. val = { 'hello': 1 } and opts.type = { hello: [ 'symbol',
// 'u128' ]} or you can use `null` for the default interpretation
const [keyType, valType] = (opts?.type ?? {})[k] ?? [null, null];
const keyOpts = keyType ? { type: keyType } : {};
const valOpts = valType ? { type: valType } : {};

return new xdr.ScMapEntry({
key: nativeToScVal(k, keyOpts),
val: nativeToScVal(v, valOpts)
});
})
Object.entries(val)
// The Soroban runtime expects maps to have their keys in sorted
// order, so let's do that here as part of the conversion to prevent
// confusing error messages on execution.
.sort(([key1], [key2]) => key1.localeCompare(key2))
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
.map(([k, v]) => {
// the type can be specified with an entry for the key and the value,
// e.g. val = { 'hello': 1 } and opts.type = { hello: [ 'symbol',
// 'u128' ]} or you can use `null` for the default interpretation
const [keyType, valType] = (opts?.type ?? {})[k] ?? [null, null];
const keyOpts = keyType ? { type: keyType } : {};
const valOpts = valType ? { type: valType } : {};

return new xdr.ScMapEntry({
key: nativeToScVal(k, keyOpts),
val: nativeToScVal(v, valOpts)
});
})
);
}

case 'number':
case 'bigint':
Expand Down
28 changes: 14 additions & 14 deletions test/unit/scval_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,36 @@ describe('parsing and building ScVals', function () {
const targetScv = xdr.ScVal.scvMap(
[
['bool', xdr.ScVal.scvBool(true)],
['void', xdr.ScVal.scvVoid()],
['u32', xdr.ScVal.scvU32(1)],
['i32', xdr.ScVal.scvI32(1)],
['u64', xdr.ScVal.scvU64(new xdr.Uint64(1))],
['i64', xdr.ScVal.scvI64(new xdr.Int64(-1))],
['u128', new ScInt(1, { type: 'u128' }).toScVal()],
['i128', new ScInt(1, { type: 'i128' }).toScVal()],
['u256', new ScInt(1, { type: 'u256' }).toScVal()],
['i256', new ScInt(1, { type: 'i256' }).toScVal()],
['i32', xdr.ScVal.scvI32(1)],
['i64', xdr.ScVal.scvI64(new xdr.Int64(-1))],
[
'map',
xdr.ScVal.scvMap([
new xdr.ScMapEntry({
key: xdr.ScVal.scvString('arbitrary'),
val: xdr.ScVal.scvU64(new xdr.Uint64(1))
}),
new xdr.ScMapEntry({
key: xdr.ScVal.scvString('nested'),
val: xdr.ScVal.scvString('values')
}),
new xdr.ScMapEntry({
key: xdr.ScVal.scvString('etc'),
val: xdr.ScVal.scvBool(false)
}),
new xdr.ScMapEntry({
key: xdr.ScVal.scvString('nested'),
val: xdr.ScVal.scvString('values')
})
])
],
['u128', new ScInt(1, { type: 'u128' }).toScVal()],
['u256', new ScInt(1, { type: 'u256' }).toScVal()],
['u32', xdr.ScVal.scvU32(1)],
['u64', xdr.ScVal.scvU64(new xdr.Uint64(1))],
[
'vec',
xdr.ScVal.scvVec(['same', 'type', 'list'].map(xdr.ScVal.scvString))
]
],
['void', xdr.ScVal.scvVoid()]
].map(([type, scv]) => {
return new xdr.ScMapEntry({
key: new xdr.ScVal.scvString(type),
Expand Down Expand Up @@ -182,12 +182,12 @@ describe('parsing and building ScVals', function () {
scv = nativeToScVal(
{
hello: 'world',
goodbye: [1, 2, 3]
there: [1, 2, 3]
Copy link
Contributor

Choose a reason for hiding this comment

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

it's tough to grasp how expectations are validating the new sorting of keys from this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the main test for order was the earlier one. This test was evaluating that type was being used correctly, and there are some assertions about scv[0] and scv[1] that become out of order since goodbye comes before hello lexicographically, so instead of modifying the assertion checks I just changed the key name so it'd stay in the same order as before.

},
{
type: {
hello: ['symbol', null],
goodbye: [null, 'i32']
there: [null, 'i32']
}
}
);
Expand Down
Loading