-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cw-xc): faster cosmos devnet with fix for interpreter instantiat…
…ion (#4041) So I tuned some bash configs of XCVM, improve startup time of cosmos net for dev, and continue to run transfer. Transfer failed because of CosmWasm/cosmwasm#1810 . So fix was to make short name for interpreter. Hash may fail too, so used counter id. I think in next iter should use network prefixed counter. So that can have global interpreter ids, but no must. Tested by running cosmos devnet oneliner to setup xcvm. And forced create interpreter for user. Also I split XCVM admin key and other admin key, so these processes can do tx in parallel, as it visible in devnet init graph. Also started doing some logging for sanity ``` {"level":"info","process":"centauri","replica":1,"message":"{\"force_instantiate\":{\"user_origin\":\"centauri1e0nh3davd6d3yh73sldwndww7zchetwsae8m9rz9x6hlg7w4ysrqhmaqju\"}}"} {"level":"info","process":"centauri","replica":1,"message":"xcvm:: {\"id\":0,\"result\":{\"ok\":{\"events\":[{\"type\":\"instantiate\",\"attributes\":[{\"key\":\"_contract_address\",\"value\":\"centauri1tlcw3llaxzs3jam7yheqxhr3k0n4y5sfrnah72n6aqt0rlp4xq6qpjy5cc\"},{\"key\":\"code_id\",\"value\":\"2\"}]},{\"type\":\"wasm-xcvm.interpreter\",\"attributes\":[{\"key\":\"_contract_address\",\"value\":\"centauri1tlcw3llaxzs3jam7yheqxhr3k0n4y5sfrnah72n6aqt0rlp4xq6qpjy5cc\"},{\"key\":\"data\",\"value\":\"eyJ1c2VyX29yaWdpbiI6eyJuZXR3b3JrX2lkIjoyLCJ1c2VyX2lkIjoiNjM2NTZlNzQ2MTc1NzI2OTMxNjUzMDZlNjgzMzY0NjE3NjY0MzY2NDMzNzk2ODM3MzM3MzZjNjQ3NzZlNjQ3Nzc3Mzc3YTYzNjg2NTc0Nzc3MzYxNjUzODZkMzk3MjdhMzk3ODM2Njg2YzY3Mzc3NzM0Nzk3MzcyNzE2ODZkNjE3MTZhNzUifSwic2FsdCI6IjY0NjU2NjYxNzU2Yzc0In0=\"}]}],\"data\":\"CkNjZW50YXVyaTF0bGN3M2xsYXh6czNqYW03eWhlcXhocjNrMG40eTVzZnJuYWg3Mm42YXF0MHJscDR4cTZxcGp5NWNj\"}}}"} {"level":"info","process":"centauri","replica":1,"message":"xcvm:: saved interpreter"} ```` Required for merge: - [ ] `pr-workflow-check / draft-release-check` is ✅ success - Other rules GitHub shows you, or can be read in [configuration](../terraform/github.com/branches.tf) Makes review faster: - [x] PR title is my best effort to provide summary of changes and has clear text to be part of release notes - [x] I marked PR by `misc` label if it should not be in release notes - [x] Linked Zenhub/Github/Slack/etc reference if one exists - [x] I was clear on what type of deployment required to release my changes (node, runtime, contract, indexer, on chain operation, frontend, infrastructure) if any in PR title or description - [x] Added reviewer into `Reviewers` - [x] I tagged(`@`) or used other form of notification of one person who I think can handle best review of this PR - [x] I have proved that PR has no general regressions of relevant features and processes required to release into production - [x] Any dependency updates made, was done according guides from relevant dependency - Clicking all checkboxes - Adding detailed description of changes when it feels appropriate (for example when PR is big)
- Loading branch information
1 parent
eccecf5
commit 4f5277f
Showing
16 changed files
with
598 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
code/xcvm/cosmwasm/contracts/gateway/src/interpreter.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use crate::{ | ||
contract::INSTANTIATE_INTERPRETER_REPLY_ID, | ||
error::{ContractError, Result}, | ||
events::make_event, | ||
network::load_this, | ||
state, | ||
}; | ||
use cosmwasm_std::{ | ||
to_binary, Deps, DepsMut, Reply, Response, StdError, StdResult, SubMsg, WasmMsg, | ||
}; | ||
use cw_xc_interpreter::contract::{ | ||
XCVM_INTERPRETER_EVENT_DATA_ORIGIN, XCVM_INTERPRETER_EVENT_PREFIX, | ||
}; | ||
use xc_core::{CallOrigin, InterpreterOrigin}; | ||
|
||
use crate::{auth, prelude::*}; | ||
|
||
pub(crate) fn force_instantiate( | ||
_: auth::Admin, | ||
gateway: Addr, | ||
deps: DepsMut, | ||
user_origin: Addr, | ||
salt: Option<String>, | ||
) -> Result { | ||
let config = load_this(deps.storage)?; | ||
let interpreter_code_id = match config.gateway.expect("expected setup") { | ||
GatewayId::CosmWasm { interpreter_code_id, .. } => interpreter_code_id, | ||
}; | ||
|
||
let call_origin = CallOrigin::Local { user: user_origin }; | ||
let interpreter_origin = InterpreterOrigin { | ||
user_origin: call_origin.user(config.network_id), | ||
salt: salt.clone().map(|x| x.into_bytes()).unwrap_or_default(), | ||
}; | ||
let msg = instantiate( | ||
deps.as_ref(), | ||
gateway, | ||
interpreter_code_id, | ||
&interpreter_origin, | ||
salt.map(|x| x.into_bytes()).unwrap_or_default(), | ||
)?; | ||
Ok(Response::new().add_submessage(msg).add_event(make_event("interpreter.forced"))) | ||
} | ||
|
||
pub fn instantiate( | ||
deps: Deps, | ||
admin: Addr, | ||
interpreter_code_id: u64, | ||
interpreter_origin: &InterpreterOrigin, | ||
salt: Vec<u8>, | ||
) -> Result<SubMsg, ContractError> { | ||
let next_interpreter_id: u128 = | ||
state::interpreter::INTERPRETERS_COUNT.load(deps.storage).unwrap_or_default() + 1; | ||
|
||
let instantiate_msg = WasmMsg::Instantiate2 { | ||
admin: Some(admin.clone().into_string()), | ||
code_id: interpreter_code_id, | ||
msg: to_binary(&cw_xc_interpreter::msg::InstantiateMsg { | ||
gateway_address: admin.clone().into_string(), | ||
interpreter_origin: interpreter_origin.clone(), | ||
})?, | ||
funds: vec![], | ||
// and label has some unknown limits (including usage of special characters) | ||
label: format!("xcvm_interpreter_{}", &next_interpreter_id), | ||
// salt limit is 64 characters | ||
salt: to_binary(&salt)?, | ||
}; | ||
let interpreter_instantiate_submessage = | ||
SubMsg::reply_on_success(instantiate_msg, INSTANTIATE_INTERPRETER_REPLY_ID); | ||
Ok(interpreter_instantiate_submessage) | ||
} | ||
|
||
pub(crate) fn handle_instantiate_reply(deps: DepsMut, msg: Reply) -> StdResult<Response> { | ||
deps.api.debug(&format!( | ||
"xcvm:: {}", | ||
serde_json_wasm::to_string(&msg).map_err(|e| StdError::generic_err(e.to_string()))? | ||
)); | ||
let response = msg.result.into_result().map_err(StdError::generic_err)?; | ||
// Catch the default `instantiate` event which contains `_contract_address` attribute that | ||
// has the instantiated contract's address | ||
let address = &response | ||
.events | ||
.iter() | ||
.find(|event| event.ty == "instantiate") | ||
.ok_or_else(|| StdError::not_found("instantiate event not found"))? | ||
.attributes | ||
.iter() | ||
.find(|attr| &attr.key == "_contract_address") | ||
.ok_or_else(|| StdError::not_found("_contract_address attribute not found"))? | ||
.value; | ||
let interpreter_address = deps.api.addr_validate(address)?; | ||
|
||
// Interpreter provides `network_id, user_id` pair as an event for the router to know which | ||
// pair is instantiated | ||
let event_name = format!("wasm-{}", XCVM_INTERPRETER_EVENT_PREFIX); | ||
let interpreter_origin = &response | ||
.events | ||
.iter() | ||
.find(|event| event.ty.starts_with(&event_name)) | ||
.ok_or_else(|| StdError::not_found("interpreter event not found"))? | ||
.attributes | ||
.iter() | ||
.find(|attr| &attr.key == XCVM_INTERPRETER_EVENT_DATA_ORIGIN) | ||
.ok_or_else(|| StdError::not_found("no data is returned from 'xcvm_interpreter'"))? | ||
.value; | ||
let interpreter_origin = | ||
xc_core::shared::decode_base64::<_, InterpreterOrigin>(interpreter_origin.as_str())?; | ||
|
||
let interpreter_id: u128 = | ||
state::interpreter::INTERPRETERS_COUNT.load(deps.storage).unwrap_or_default() + 1; | ||
let interpreter = state::interpreter::Interpreter { | ||
address: interpreter_address, | ||
interpreter_id: interpreter_id.into(), | ||
}; | ||
|
||
state::interpreter::INTERPRETERS_COUNT.save(deps.storage, &interpreter_id)?; | ||
state::interpreter::INTERPRETERS.save(deps.storage, interpreter_id, &interpreter)?; | ||
state::interpreter::INTERPRETERS_ORIGIN_TO_ID.save( | ||
deps.storage, | ||
interpreter_origin, | ||
&interpreter_id, | ||
)?; | ||
|
||
deps.api.debug("xcvm:: saved interpreter"); | ||
|
||
Ok(Response::new().add_event( | ||
make_event("xcvm.interpreter.instantiated") | ||
.add_attribute("interpreter_id", interpreter_id.to_string()), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
code/xcvm/cosmwasm/contracts/gateway/src/state/interpreter.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,26 @@ | ||
use xc_core::InterpreterOrigin; | ||
use cosmwasm_std::{Deps, StdResult}; | ||
use cw_storage_plus::Item; | ||
use xc_core::{Displayed, InterpreterOrigin}; | ||
|
||
use crate::prelude::*; | ||
|
||
pub type InterpreterId = Displayed<u128>; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
#[cfg_attr(feature = "std", derive(schemars::JsonSchema))] | ||
pub(crate) struct Interpreter { | ||
pub address: Addr, | ||
pub interpreter_id: InterpreterId, | ||
} | ||
|
||
pub(crate) fn get_by_origin(deps: Deps, origin: InterpreterOrigin) -> StdResult<Interpreter> { | ||
let id = INTERPRETERS_ORIGIN_TO_ID.load(deps.storage, origin)?; | ||
INTERPRETERS.load(deps.storage, id) | ||
} | ||
|
||
pub(crate) const INTERPRETERS: Map<InterpreterOrigin, Interpreter> = Map::new("interpreters"); | ||
pub(crate) const INTERPRETERS_COUNT: Item<u128> = Item::new("interpreter_count"); | ||
|
||
pub(crate) const INTERPRETERS_ORIGIN_TO_ID: Map<InterpreterOrigin, u128> = | ||
Map::new("interpreters_origin_to_id"); | ||
|
||
pub(crate) const INTERPRETERS: Map<u128, Interpreter> = Map::new("interpreters"); |
Oops, something went wrong.