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 deploy config and set contaner status #44

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 19 additions & 6 deletions src/ainize.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import Ain from "@ainblockchain/ain-js";
import * as NodeCache from "node-cache";
import Middleware from "./middlewares/middleware";
import { getBlockChainEndpoint } from "./constants";
import { DEFAULT_BILLING_CONFIG, getBlockChainEndpoint } from "./constants";
import Handler from "./handlers/handler";
import AppController from "./controller/appController";
import AppController from "./controllers/appController";
import Model from "./model";
import { deployConfig } from "./types/type";
import AinModule from "./ain";
export default class Ainize {
private cache: NodeCache;
ain: Ain;
middleware: Middleware;
handler: Handler;
appController: AppController = AppController.getInstance();

constructor(chainId: 1|0) {
constructor(chainId: 1 | 0) {
const blockChainEndpoint = getBlockChainEndpoint(chainId);
this.ain = new Ain(blockChainEndpoint, chainId);
this.cache = new NodeCache();
Expand All @@ -21,10 +23,21 @@ export default class Ainize {
}

// FIXME(yoojin): add config type and change param type.
deploy(modelName: string, config: any) {
deploy({modelName, billingConfig, serviceUrl}: deployConfig) {
// TODO(yoojin, woojae): Deploy container, advanced.
// TODO(yoojin): add createApp
// this.appController.createApp(modelName, )
const deployer = AinModule.getInstance().getAddress();
if (!billingConfig) {
billingConfig = {
...DEFAULT_BILLING_CONFIG,
depositAddress: deployer,
}
}
// NOTE(yoojin): For test. We make fixed url on service.
if (!serviceUrl) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

현재로썬 저 규칙을 안따를 가능성이 높기 때문에 그냥 필수로 하는게 더 좋아 보입니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ditto

serviceUrl = `https://${modelName}.ainetwork.xyz`;
}

this.appController.createApp({ appName: modelName, serviceUrl, billingConfig })
}

model(modelName: string) {
Expand Down
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appBillingConfig } from "./types/type"

export const getBlockChainEndpoint = (chainId:number) =>{
return chainId === 1 ? "https://mainnet-event.ainetwork.ai" : "https://testnet-event.ainetwork.ai"
}
Expand Down Expand Up @@ -91,5 +93,10 @@ export const defaultAppRules = (appName: string): { [type: string]: { ref: strin
}
}

export const DEFAULT_BILLING_CONFIG: Omit<appBillingConfig, "depositAddress"> = {
costPerToken: 0,
minCost: 0,
}

export const SECOND = 1000;
export const HANDLER_HEARBEAT_INTERVAL = 15 * SECOND;
28 changes: 18 additions & 10 deletions src/controllers/appController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SetOperation } from "@ainblockchain/ain-js/lib/types";
import { Path, defaultAppRules } from "../constants";
import { appBillingConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { ContainerStatus, appBillingConfig, createAppConfig, setRuleParam, setTriggerFunctionParm, triggerFunctionConfig } from "../types/type";
import { buildSetOperation, buildTxBody } from "../utils/builder";
import AinModule from '../ain';

Expand All @@ -22,7 +22,7 @@ export default class AppController {
* @param {setDefaultFlag} setDefaultFlag - Set true which you wan to set config as default.
* @returns Result of transaction.
*/
async createApp(appName: string, serviceUrl: string) {
async createApp({ appName, serviceUrl, billingConfig }: createAppConfig) {
const setRuleOps: SetOperation[] = [];
const setFunctionOps: SetOperation[] = [];
const setBillingConfigOps: SetOperation[] = [] ;
Expand All @@ -39,20 +39,17 @@ export default class AppController {
const value = this.buildSetFunctionValue(depositParam);
const funcOp = buildSetOperation("SET_FUNCTION", depositParam.ref, value);
setFunctionOps.push(funcOp);
const depositAddress = this.ain.getDefaultAccount()!.address;
const defaultConfig: appBillingConfig = {
depositAddress,
costPerToken: 0,
minCost: 0,
}
const configOp = this.buildSetAppBillingConfigOp(appName, defaultConfig);
const configOp = this.buildSetAppBillingConfigOp(appName, billingConfig);
setBillingConfigOps.push(configOp);

const statusOp = this.buildSetContainerStatusOp(appName, ContainerStatus.RUNNING);

const txBody = buildTxBody([
createAppOp,
...setRuleOps,
...setFunctionOps,
...setBillingConfigOps,
statusOp,
]);
return await this.ain.sendTransaction(txBody);
}
Expand Down Expand Up @@ -118,6 +115,12 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

async setContainerStatus(appName: string, status: ContainerStatus) {
const op = this.buildSetContainerStatusOp(appName, status);
const txBody = buildTxBody(op);
return await this.ain.sendTransaction(txBody);
}

/**
* Add admin on app.
* @param {string} appName
Expand All @@ -142,7 +145,7 @@ export default class AppController {
return await this.ain.sendTransaction(txBody);
}

/**
/**
* Check cost of request and check if account can pay. You should use this function before send or handle request.
* If you don't set address, it will use default account's address.
* @param {string} appName - App name you want to request service to.
Expand Down Expand Up @@ -172,6 +175,11 @@ export default class AppController {
return await this.ain.getValue(balancePath);
}

private buildSetContainerStatusOp(appName: string, status: ContainerStatus) {
const path = Path.app(appName).status();
return buildSetOperation("SET_VALUE", path, status);
}

private buildSetAppBillingConfigOp(appName: string, config: appBillingConfig) {
const path = Path.app(appName).billingConfig();
return buildSetOperation("SET_VALUE", path, config);
Expand Down
19 changes: 18 additions & 1 deletion src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ export type deposit = {
transferValue: number,
appName: string,
requesterAddress: string,
}
}

export type deployConfig = {
modelName: string,
billingConfig?: appBillingConfig,
serviceUrl?: string,
Copy link
Collaborator

Choose a reason for hiding this comment

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

현재는 serviceUrl필수로 해두는게 맞는 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

저희 테스트 용으로 임시로 두었습니다. logic에는 주석 썼는데 type에 안써놨네요. 주석 추가하겠습니다.

}

export type createAppConfig = {
appName: string,
billingConfig: appBillingConfig,
serviceUrl: string,
}

export enum ContainerStatus {
RUNNING = "RUNNING",
STOP = "STOP",
}