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

Slightly better set tests #68

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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;
Copy link
Member Author

Choose a reason for hiding this comment

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

this is removed because we can get the same property from contract object (see get contractTxId() in base/sdk.ts).

Copy link
Member Author

Choose a reason for hiding this comment

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

does not change anything within the code, API is still the same

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
}
}
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);
}
});
});