Skip to content

Commit

Permalink
Merge pull request #68 from firstbatchxyz/erhant/set-test
Browse files Browse the repository at this point in the history
Slightly better `set` tests
  • Loading branch information
anilaltuner authored Dec 25, 2023
2 parents 8838c15 + e12c47c commit b3644bf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import type {ContractInputGeneric, ContractMode, ContractState} from '../contrac
export class Base<M extends ContractMode> {
readonly contract: Contract<ContractState<M>>;
readonly warp: Warp;
readonly contractTxId: string;
readonly signer: ArWallet | CustomSignature;

constructor(signer: ArWallet | CustomSignature, contractTxId: string, warp: Warp) {
this.signer = signer;
this.contractTxId = contractTxId;
this.warp = warp
// required for proof verification
.use(new SnarkjsExtension())
// required for hashing
.use(new EthersExtension());

this.contract = this.warp
.contract<ContractState<M>>(this.contractTxId)
.contract<ContractState<M>>(contractTxId)
.setEvaluationOptions({
allowBigInt: true, // bigInt is required for circuits
useKVStorage: true,
Expand Down
9 changes: 7 additions & 2 deletions src/base/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
return this.base.warp;
}

/** Contract transaction id. */
get contractTxId(): string {
return this.base.contract.txId();
}

/** Signer. */
get signer(): ArWallet | CustomSignature {
return this.base.signer;
Expand Down Expand Up @@ -100,7 +105,7 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
* @param options optional range
* @returns a key-value `Map`
*/
async getKVMap(options?: SortKeyCacheRangeOptions): Promise<Map<string, V>> {
async getKVMap(options?: SortKeyCacheRangeOptions): Promise<Map<string, V | null>> {
return await this.base.safeReadInteraction<GetKVMapInput, Map<string, V>>({
function: 'getKVMap',
value: {
Expand All @@ -114,7 +119,7 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
* @param key the key of the value to be returned
* @returns the value of the given key
*/
async get(key: string): Promise<V> {
async get(key: string): Promise<V | null> {
return await this.base.safeReadInteraction<GetInput, V>({
function: 'get',
value: {
Expand Down
21 changes: 21 additions & 0 deletions src/contracts/states/hollowdb-set.state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "0.0.0",
"owner": "",
"verificationKeys": {
"auth": null
},
"isProofRequired": {
"auth": false
},
"canEvolve": true,
"whitelists": {
"put": {},
"update": {},
"set": {}
},
"isWhitelistRequired": {
"put": false,
"update": false,
"set": false
}
}
4 changes: 2 additions & 2 deletions src/hollowdb-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class SetSDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelist
/**
* Inserts the given value into database.
*
* There must not be a value at the given key.
* Overwrites the existing values at the given key.
*
* @param key the key of the value to be inserted
* @param value the value to be inserted
Expand All @@ -28,7 +28,7 @@ export class SetSDK<V = unknown> extends BaseSDK<V, {proofs: ['auth']; whitelist
/**
* Inserts an array of value into database.
*
* There must not be a value at the given key.
* Overwrites the existing values at the given keys.
*
* @param keys the keys of the values to be inserted
* @param values the values to be inserted
Expand Down
22 changes: 17 additions & 5 deletions tests/set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,38 @@ describe('set tests', () => {

it('should allow putting a value', async () => {
await owner.put(KEY, VALUE);
const value = await owner.get(KEY);
expect(value?.val).toBe(VALUE.val);
});

it('should NOT allow putting a value again', async () => {
await expect(owner.put(KEY, NEXT_VALUE)).rejects.toThrow('Contract Error [put]: Key already exists.');
});

it('should allow setting a value at an existing key', async () => {
await owner.set(KEY, VALUE);
await owner.set(KEY, NEXT_VALUE);
const value = await owner.get(KEY);
expect(value?.val).toBe(NEXT_VALUE.val);
});

it('should allow setting a value at a new key', async () => {
const newKV = createValues();
await owner.set(newKV.KEY, newKV.VALUE);
const value = await owner.get(newKV.KEY);
expect(value?.val).toBe(newKV.VALUE.val);
});

it('should allow setting many values', async () => {
const kvs = Array.from({length: 5}, () => createValues());
await owner.setMany(
kvs.map(kv => kv.KEY),
kvs.map(kv => kv.VALUE)
);

const keys = kvs.map(kv => kv.KEY);
const values = kvs.map(kv => kv.VALUE);
await owner.setMany(keys, values);

const results = await owner.getMany(keys);
expect(results.length).toBe(values.length);
for (let i = 0; i < results.length; i++) {
expect(results[i]?.val).toBe(values[i].val);
}
});
});

0 comments on commit b3644bf

Please sign in to comment.