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

Add an xdr.scvMapSorted constructor to automagically sort map ScVals. #787

Merged
merged 5 commits into from
Jan 17, 2025
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
31 changes: 24 additions & 7 deletions src/scval.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,6 @@ export function nativeToScVal(val, opts = {}) {
}

if (Array.isArray(val)) {
if (val.length > 0 && val.some((v) => typeof v !== typeof val[0])) {
throw new TypeError(
`array values (${val}) must have the same type (types: ${val
.map((v) => typeof v)
.join(',')})`
);
}
return xdr.ScVal.scvVec(val.map((v) => nativeToScVal(v, opts)));
}

Expand Down Expand Up @@ -383,3 +376,27 @@ export function scValToNative(scv) {
return scv.value();
}
}

/// Inject a sortable map builder into the xdr module.
xdr.scvSortedMap = (items) => {
const sorted = Array.from(items).sort((a, b) => {
// Both a and b are `ScMapEntry`s, so we need to sort by underlying key.
//
// We couldn't possibly handle every combination of keys since Soroban
// maps don't enforce consistent types, so we do a best-effort and try
// sorting by "number-like" or "string-like."
const nativeA = scValToNative(a.key());
const nativeB = scValToNative(b.key());

switch (typeof nativeA) {
case 'number':
case 'bigint':
return nativeA < nativeB ? -1 : 1;

default:
return nativeA.toString().localeCompare(nativeB.toString());
Comment on lines +383 to +397
Copy link
Member

Choose a reason for hiding this comment

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

It is intriguing how... imperfect this is. Why does this use a different approach to nativeToScVal? nativeToScVal sorting logic appears to look like this:

      return xdr.ScVal.scvMap(
        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))
          .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)
            });
          })
      );

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 think this actually reveals an issue with the way nativeToScVal sorts, instead. It made the assumption that map keys are strings (because in JS all {}s use strings for keys), but if those are being encoded differently (via type), then this might 💥.

Of course this convenience method isn't supposed to be a catch-all for all possible ScVals, but it could be improved there for the easy cases (number-like). Both will work reliably for the common case, which is structs, and that's the primary goal here.

}
});

return xdr.ScVal.scvMap(sorted);
};
73 changes: 69 additions & 4 deletions test/unit/scval_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ describe('parsing and building ScVals', function () {
// iterate for granular errors on failures
targetScv.value().forEach((entry, idx) => {
const actual = scv.value()[idx];
// console.log(idx, 'exp:', JSON.stringify(entry));
// console.log(idx, 'act:', JSON.stringify(actual));
expect(entry).to.deep.equal(actual, `item ${idx} doesn't match`);
});

Expand Down Expand Up @@ -207,8 +205,8 @@ describe('parsing and building ScVals', function () {
);
});

it('throws on arrays with mixed types', function () {
expect(() => nativeToScVal([1, 'a', false])).to.throw(/same type/i);
it('doesnt throw on arrays with mixed types', function () {
expect(nativeToScVal([1, 'a', false]).switch().name).to.equal('scvVec');
});

it('lets strings be small integer ScVals', function () {
Expand Down Expand Up @@ -264,4 +262,71 @@ describe('parsing and building ScVals', function () {
}
]);
});

it('can sort maps by string', function () {
const sample = nativeToScVal(
{ a: 1, b: 2, c: 3 },
{
type: {
a: ['symbol'],
b: ['symbol'],
c: ['symbol']
}
}
);
['a', 'b', 'c'].forEach((val, idx) => {
expect(sample.value()[idx].key().value()).to.equal(val);
});

// nativeToScVal will sort, so we need to "unsort" to make sure it works.
// We'll do this by swapping 0 (a) and 2 (c).
let tmp = sample.value()[0];
sample.value()[0] = sample.value()[2];
sample.value()[2] = tmp;

['c', 'b', 'a'].forEach((val, idx) => {
expect(sample.value()[idx].key().value()).to.equal(val);
});

const sorted = xdr.scvSortedMap(sample.value());
expect(sorted.switch().name).to.equal('scvMap');
['a', 'b', 'c'].forEach((val, idx) => {
expect(sorted.value()[idx].key().value()).to.equal(val);
});
});

it('can sort number-like maps', function () {
const sample = nativeToScVal(
{ 1: 'a', 2: 'b', 3: 'c' },
{
type: {
1: ['i64', 'symbol'],
2: ['i64', 'symbol'],
3: ['i64', 'symbol']
}
}
);
expect(sample.value()[0].key().switch().name).to.equal('scvI64');

[1n, 2n, 3n].forEach((val, idx) => {
let underlyingKey = sample.value()[idx].key().value();
expect(underlyingKey.toBigInt()).to.equal(val);
});

// nativeToScVal will sort, so we need to "unsort" to make sure it works.
// We'll do this by swapping 0th (1n) and 2nd (3n).
let tmp = sample.value()[0];
sample.value()[0] = sample.value()[2];
sample.value()[2] = tmp;

[3n, 2n, 1n].forEach((val, idx) => {
expect(sample.value()[idx].key().value().toBigInt()).to.equal(val);
});

const sorted = xdr.scvSortedMap(sample.value());
expect(sorted.switch().name).to.equal('scvMap');
[1n, 2n, 3n].forEach((val, idx) => {
expect(sorted.value()[idx].key().value().toBigInt()).to.equal(val);
});
});
});
10 changes: 10 additions & 0 deletions types/curr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export namespace xdr {

type Hash = Opaque[]; // workaround, cause unknown

/**
* Returns an {@link ScVal} with a map type and sorted entries.
*
* @param items the key-value pairs to sort.
*
* @warning This only performs "best-effort" sorting, working best when the
* keys are all either numeric or string-like.
*/
function scvSortedMap(items: ScMapEntry[]): ScVal;

interface SignedInt {
readonly MAX_VALUE: 2147483647;
readonly MIN_VALUE: -2147483648;
Expand Down
Loading