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
base: main
Are you sure you want to change the base?
Conversation
@@ -266,6 +267,8 @@ export interface HandlerApi<State> { | |||
initState(state: State): void; | |||
|
|||
maybeCallStateConstructor(initialState: State, executionContext: ExecutionContext<State>): Promise<State>; | |||
|
|||
evolveState(): Promise<EvolveState>; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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'?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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(),
)
There was a problem hiding this comment.
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.
b7cefc0
to
c94cf64
Compare
Rust implementation of evolve_state concept