Skip to content

Commit

Permalink
Merge pull request #119 from ainize-team/bugfix/jiyoung/gc-rule-test
Browse files Browse the repository at this point in the history
Set GC rule for requestKey
  • Loading branch information
jiyoung-an authored May 28, 2024
2 parents 336a508 + 0498e79 commit 1cb73a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TransactionBody } from "@ainblockchain/ain-util";
import { txResult } from "./types/type";
import { Signer } from "@ainblockchain/ain-js/lib/signer/signer";
import { DefaultSigner } from "@ainblockchain/ain-js/lib/signer/default-signer"
import { GetOptions } from "@ainblockchain/ain-js/lib/types";

// NOTE(yoojin): Plz suggest a good name.
export default class AinModule {
Expand All @@ -20,7 +21,7 @@ export default class AinModule {
initAin(chainId: 0 | 1) {
const blockchainAPIEndpoint = getBlockChainAPIEndpoint(chainId);
const blockchainEventEndpoint = getBlockChainEventEndpoint(chainId);
this.ain = new Ain(blockchainAPIEndpoint,blockchainEventEndpoint, chainId);
this.ain = new Ain(blockchainAPIEndpoint, blockchainEventEndpoint, chainId);
}

createAccount() {
Expand Down Expand Up @@ -89,9 +90,9 @@ export default class AinModule {
return await this.ain!.db.ref(balancePath).getValue();
}

async getValue(path: string) {
async getValue(path: string, options?: GetOptions) {
this.checkAinInitiated();
return await this.ain!.db.ref(path).getValue();
return await this.ain!.db.ref().getValue(path, options);
}

private async _sendTransaction(txBody: TransactionBody) {
Expand Down
2 changes: 1 addition & 1 deletion src/ainize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class Ainize {
*/
async getService(serviceName: string): Promise<Service> {
const servicePath = Path.app(serviceName).root();
const serviceData = await this.ain.getValue(servicePath);
const serviceData = await this.ain.getValue(servicePath, { is_shallow: true });
if(!serviceData) {
throw new Error("Service not found");
}
Expand Down
22 changes: 17 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ export const Path = {
service: () => `${Path.app(appName).root()}/service`,
userOfService: (userAddress: string) =>
`${Path.app(appName).service()}/${userAddress}`,
requestKey: (userAddress: string, requestKey: string) =>
`${Path.app(appName).userOfService(userAddress)}/${requestKey}`,
request: (userAddress: string, requestKey: string) =>
`${Path.app(appName).userOfService(userAddress)}/${requestKey}/request`,
`${Path.app(appName).requestKey(userAddress, requestKey)}/request`,
response: (userAddress: string, requestKey: string) =>
`${Path.app(appName).userOfService(userAddress)}/${requestKey}/response`,
`${Path.app(appName).requestKey(userAddress, requestKey)}/response`,
}
},
transfer: (from: string, to: string, transferKey: string) =>
`/transfer/${from}/${to}/${transferKey}/value`,
}

export const defaultAppRules = (appName: string): { [type: string]: { ref: string, value: object } } => {
const rootRef = Path.app(appName).root();
return {
deposit: {
ref: `${Path.app(appName).depositOfUser("$userAddress")}/$transferKey`,
Expand All @@ -54,7 +55,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
},
},
balanceHistory: {
ref: `${rootRef}/balance/$userAddress/history/$timestamp`,
ref: `${Path.app(appName).balance()}/$userAddress/history/$timestamp`,
value: {
".rule": {
write:
Expand All @@ -64,6 +65,17 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
},
},
},
requestKey: {
ref: Path.app(appName).requestKey("$userAddress", "$requestKey"),
value: {
".rule": {
state: {
gc_max_siblings: 20,
gc_num_siblings_deleted: 10,
},
},
}
},
request: {
ref: Path.app(appName).request("$userAddress", "$requestKey"),
value: {
Expand All @@ -77,7 +89,7 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
},
},
response: {
ref: Path.app(appName).response("userAddress", "$requestKey"),
ref: Path.app(appName).response("$userAddress", "$requestKey"),
value: {
".rule": {
write: "util.isAppAdmin(`" + `${appName}` + "`, auth.addr, getValue) === true && util.isDict(newData) && util.isString(newData.status)"
Expand Down

0 comments on commit 1cb73a7

Please sign in to comment.