Skip to content

Commit

Permalink
fix: kvStorage does not return properly falsy values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Oct 18, 2023
1 parent 46fe5b9 commit 88c21dc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/__tests__/integration/basic/pst-kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,35 @@ describe('Testing the Profit Sharing Token', () => {
).toEqual(23111222);
expect((await pst.getStorageValues(['foo'])).cachedValue.get('foo')).toBeNull();
});

it('should properly store false and falsy values in kv', async () => {
const falsyEntries = {
'false': false,
'emptyString': '',
'zero': 0,
'emptyArray': [],
'nan': NaN,
'null': null,
'undefined': undefined,
} as const;

await pst.writeInteraction({
function: 'kvPut',
kvPut: falsyEntries
});
await mineBlock(warp);

const { kvGet } =
(await pst.viewState<unknown, { kvGet: typeof falsyEntries }>({
function: 'kvGet',
kvGet: Object.keys(falsyEntries) })).result;

expect(kvGet.emptyArray).toEqual([]);
expect(kvGet.emptyString).toEqual('');
expect(kvGet.zero).toEqual(0);
expect(kvGet.false).toEqual(false);
expect(kvGet.nan).toBeNull();
expect(kvGet.null).toBeNull();
expect(kvGet.undefined).toBeNull();
});
});
18 changes: 18 additions & 0 deletions src/__tests__/integration/data/kv-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,23 @@ export async function handle(state, action) {
return {result: {target, ticker, balance: result ? result : 0}};
}

if (input.function === 'kvPut') {
for (const [key, value] of Object.entries(input.kvPut)) {
await SmartWeave.kv.put(key, value);
}

return {state};
}

if (input.function === 'kvGet') {
const kvGet = {};

for (const key of input.kvGet) {
kvGet[key] = await SmartWeave.kv.get(key);
}

return {result: {kvGet}};
}

throw new ContractError(`No function supplied or function not recognised: "${input.function}"`);
}
2 changes: 1 addition & 1 deletion src/cache/impl/LevelDbCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class LevelDbCache<V> implements SortKeyCache<V> {
const keys = await contractCache.keys({ reverse: true, lte: sortKey, limit: 1 }).all();
if (keys.length) {
const subLevelValue = await this.getValueFromLevel(keys[0], contractCache);
if (subLevelValue) {
if (subLevelValue != null) {
return new SortKeyCacheResult<V>(keys[0], subLevelValue);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/smartweave-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class KV {
}

const result = await this._storage.getLessOrEqual(key, this._transaction.sortKey);
return result?.cachedValue || null;
return result?.cachedValue ?? null;
}

async del(key: string): Promise<void> {
Expand Down

0 comments on commit 88c21dc

Please sign in to comment.