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

Evolve state in rust contracts #388

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rpiszczatowski
Copy link
Contributor

Rust implementation of evolve_state concept

@@ -266,6 +267,8 @@ export interface HandlerApi<State> {
initState(state: State): void;

maybeCallStateConstructor(initialState: State, executionContext: ExecutionContext<State>): Promise<State>;

evolveState(): Promise<EvolveState>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it here? Shouldn't it rather be in HandlerApi interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow. This shouldn't be here. I'll remove it.


#[derive(Serialize, Debug)]
pub struct EvolveState {
pub can_evolve: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

will this map properly to fields in json state, i.e. 'canEvolve' and 'evolve'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it won't. This is supposed to be fetched by SDK WasmHandlerApi and process there. I didn't see reasons to make these field names compatible with our usual State fields. But of course it can be done.

@@ -90,6 +90,14 @@ export class WasmHandlerApi<State> extends AbstractContractHandler<State> {
}
}

evolveState() {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to map the return type? at least sth like EvolveState | State?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand the question. I added this method only to demonstrate how to use new API. Feel free to change it in whatever way you want.

@@ -17,6 +18,15 @@ pub struct PstState {
pub balances: HashMap<String, u64>,
}

impl Evolve for PstState {
Copy link
Contributor

Choose a reason for hiding this comment

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

How will the contract authors know that they need to implement this trait?

I thought about sth like:

pub struct PstState {
    #[serde(flatten)]
    pub evolve: EvolveState,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub owner: String,
    pub balances: HashMap<String, u64>,
}

- the evolve would be a struct that could be composed with contract-specific state...

or maybe a macro?

#[warp-contract(evolve)]
pub struct PstState {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub owner: String,
    pub balances: HashMap<String, u64>,
}

- that automagically adds required fields to the State struct and also implements the Evolve trait (which I believe will always look the same, ie. will have the same mapping:

can_evolve: self.can_evolve.unwrap_or(true),
source_transaction_id: self.evolve.clone(),

)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

compilation will fail saying that required trait is not implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants