UNPKG

godprotocol

Version:

A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.

208 lines (168 loc) 5.62 kB
import Utils from "./Utils.js"; import Compiler from "aircode4"; import Chain from "./Chain.js"; import Virtual_machine from "./Virtual_machine.js"; import register from "godprotocol/callables/register.js"; import { hash } from "godprotocol/utils/hash.js"; class Account extends Utils { constructor(name, options) { super(); this.name = name; this.options = options; this.manager = options.manager; this.vm = new Virtual_machine(this); this.compiler = new Compiler(this); } load = async (payload, options) => { let { address, sequence, compiler, from_reload, _id } = payload; let phy = `${this.physical_address}/${address}`; let chain = await this.chain.chain(phy); if (compiler) { !from_reload && (await this.manager.oracle.write(`.codes/${phy}.air`, sequence)); sequence = await this.compiler.to_sequence(sequence, address); sequence = sequence[0]; } let res = await chain.add_config(sequence, { ...options, _id }); return res; }; run = async (payload, socket) => { let { address, _id, env, config, thread, callback } = payload; if (!config) { let phy = `${this.physical_address}/${address}`; let chain = await this.chain.chain(phy); let folder = await chain.folder(".configs"); config = await folder.readone(_id ? { _id } : null); if (!config) { let code = await this.manager.oracle.read(`.codes/${phy}.air`); if (code) { code = await this.compiler.to_sequence(code, address); code = code[0]; config = code; await this.load({ address, sequence: config, _id }); } } } if (!config) { return { message: "Code not found!" }; } let result = await this.vm.nodelist(config); let thread_id = await this.manager.load(result, this, { spawn: thread && thread._id, pointer: thread && thread.pointer, callback: async (res) => { if (typeof callback === "string") { if (callback === "store" && !Array.isArray(res)) { let store_fn = await this.vm.callable("store"); await store_fn.callable( { object: await this.vm.cloth(res) }, { vm: this.vm, chain: this.chain } ); callback = res.value; } else { let folder = await this.ds.folder(callback); await folder.write(res, { replace: true }); } } else if (typeof callback === "function") { await callback(res); } if (socket) { // handle socket } }, }); if (env) { this.vm.envs[thread_id] = env; } return { thread_id, callback }; }; parse = async (payload) => { let { address, query, retrieve, from } = payload; if (!address) { return { message: "Invalid payload.", }; } let phy = `${this.physical_address}/${address}`; if (from === ".codes") { let res = await this.manager.oracle.read(`.codes/${phy}.air`); if (res) { return { result: res }; } else return { message: "Code not found" }; } else if (from === "thread") { // is thread_id; let thread_info = this.manager.accounts_pairs[this.name]; let thread = thread_info.threads[address]; return thread ? { _id: thread._id, status: thread.status, spawn: thread.spawn, callback: thread.callback, pointer: thread.pointer, total: thread.bullets.length, } : {}; } let folder = await this.ds.folder( phy, from === "ram" ? null : { account: this.physical_address } ); let result = await folder.readone(query.query, { ...query.options }); if (!result) return result; if (from !== "ram") { if (this.vm.datatypes.includes(result.type)) { let obj = await this.vm.retrieve(result, true); if (retrieve) { result = await obj.to_address(); } else result.literal = await obj.literal(); } else if (retrieve) { result = await this.vm.retrieve(result); } } else { if (this.vm.datatypes.includes(result.type)) { if (retrieve) { result = await (await this.vm.cloth_content(result)).to_address(); } else result.literal = await ( await this.vm.cloth_content(result) ).literal(); } else if (retrieve) { result = await this.vm.cloth_content(result); } } return result; }; verify = async (password) => { let passes = await this.ds.folder(`${this.physical_address}/__passwords`, { account: this.physical_address, }); let pass = await passes.readone(), passed = false; if (pass) { passed = pass && pass.key === hash(password); } else { passed = await passes.write({ key: hash(password) }); } this.servers = passed && (await this.manager.oracle.update_servers(this.physical_address)); return passed; }; sync = async () => { this.physical_address = `${"Accounts"}/${this.name}`; this.chain = new Chain(`${this.physical_address}`, this); this.ds = this.manager.ds; let pass = this.options.password, res = true; if (pass) { res = await this.verify(pass); } await register(this); if (res) { console.log(`Account:${this.name} initiation successful!`); } else console.log(`Account:${this.name} initiation failed!`); return res; }; } export default Account;