-
Notifications
You must be signed in to change notification settings - Fork 11
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
Design Contract
model
#3
Comments
Does a virtual machine need to be provided to execute a method of the contract? |
A method of a Calling the method just outputs a new state but not verifies the transition so a virtual machine is not required. |
old state -> compute -> new state Who will compute? Does the new state can be recomputed by anyone? If not, how to ensure that the new state is correct? |
That's the work of dapp developers. The framework defines the interface of each component and manages how components work together. Framework users(dapp developers) follow the convention and implement their own business logic in the components. How the new state is calculated is determined by framework users, they may adopt various algorithms to generate a new state. And the correctness should be guaranteed by themselves. |
How is it going |
I only defined three interfaces for import { OutPointString, State, Store } from "./store";
export default class Contract<T extends State> extends Store<T> {
create(contract: T) {
// create a contract cell
}
update(key: OutPointString, newContract: T) {
// update a contract cell
}
destory(key: OutPointString) {
// destory a contract cell
}
} |
Contract maybe like this: import { ActorURI, MessagePayload } from "../actor";
import Store from "../store";
enum MethodResponseStatus {
Failed = 0,
Success
}
interface ContractMethodResponse {
status: MethodResponseStatus
reason?: any
data?: any
}
type MethodType = 'call' | 'cast'
interface ContractMethodInterface {
[key: ActorURI]: {
payload: MessagePayload
result?: ContractMethodResponse
}
}
export interface State {}
export default class Contract extends Store {
// This is used to update the state when the contract is updated.
update(oldState: State): State {
return {}
}
run(to: ActorURI, type: 'call' | 'cast', payload: MessagePayload, timeout?: number): Promise<ContractMethodResponse> | void {
let promise
switch (type) {
case 'call':
promise = this.call(to, payload, timeout)
break
case 'cast':
promise = this.cast(to, payload, timeout)
default:
break;
}
if (promise) {
promise.then(res => ({
status: MethodResponseStatus.Success,
data: res
}))
.catch(err => ({
status: MethodResponseStatus.Failed,
reason: err.message ?? err.toString()
}))
}
}
// link others contracts methods, it may be a service
link<T extends ContractMethodInterface>(contractAPIs: {
[P in keyof T]: MethodType
}) {
const attacthMethods: Record<ActorURI, PropertyDescriptor> = {}
Object.keys(contractAPIs).forEach((to: keyof T) => {
attacthMethods[to as string] = {
value: (p: any) => this.run(to as string, contractAPIs[to], p),
enumerable: true
}
})
Object.defineProperties(this, attacthMethods)
return this as unknown as InstanceType<typeof Contract> & {
[P in keyof T]: (param: T[P]['payload']) => Promise<T[P]['result']>
}
}
} And the example is like this If I get the .bit contract interface. I can link .bit contract methods const contract = new Contract()
type BitMethodInterface = {
'transfter': {
payload: {
symbol: symbol
value: {
domain: string
}
}
result: ContractMethodResponse
}
}
const a = contract.link<BitMethodInterface>({
transfter: 'call'
})
a.transfter({
symbol: Symbol('c'),
value: {
domain: 'aaaa'
}
}) |
Store
modeldefault storage layoutContract
model for overridingStore
The text was updated successfully, but these errors were encountered: