migration logic (#687)

This commit is contained in:
Dev Kalra 2023-03-14 14:58:57 +05:30 committed by GitHub
parent de0096efd4
commit 93ca9c427a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 3 deletions

View File

@ -26,6 +26,8 @@ use {
state::{ state::{
config, config,
config_read, config_read,
deprecated_config,
deprecated_config_read,
price_feed_bucket, price_feed_bucket,
price_feed_read_bucket, price_feed_read_bucket,
ConfigInfo, ConfigInfo,
@ -49,6 +51,7 @@ use {
OverflowOperation, OverflowOperation,
QueryRequest, QueryRequest,
Response, Response,
StdError,
StdResult, StdResult,
WasmMsg, WasmMsg,
WasmQuery, WasmQuery,
@ -90,8 +93,30 @@ use {
/// this function can safely be implemented as: /// this function can safely be implemented as:
/// `Ok(Response::default())` /// `Ok(Response::default())`
#[cfg_attr(not(feature = "library"), entry_point)] #[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> { pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
Ok(Response::default()) let depreceated_cfg_result = deprecated_config_read(deps.storage).load();
match depreceated_cfg_result {
Ok(depreceated_cfg) => {
let cfg = ConfigInfo {
wormhole_contract: depreceated_cfg.wormhole_contract,
data_sources: depreceated_cfg.data_sources,
governance_source: depreceated_cfg.governance_source,
governance_source_index: depreceated_cfg.governance_source_index,
governance_sequence_number: depreceated_cfg.governance_sequence_number,
chain_id: depreceated_cfg.chain_id,
valid_time_period: depreceated_cfg.valid_time_period,
fee: depreceated_cfg.fee,
};
config(deps.storage).save(&cfg)?;
deprecated_config(deps.storage).remove();
Ok(Response::default())
}
Err(_) => Err(StdError::GenericErr {
msg: String::from("Error reading config"),
}),
}
} }
#[cfg_attr(not(feature = "library"), entry_point)] #[cfg_attr(not(feature = "library"), entry_point)]

View File

@ -27,7 +27,8 @@ use {
}, },
}; };
pub static CONFIG_KEY: &[u8] = b"config"; pub static DEPRECATED_CONFIG_KEY: &[u8] = b"config";
pub static CONFIG_KEY: &[u8] = b"config_v1";
pub static PRICE_FEED_KEY: &[u8] = b"price_feed"; pub static PRICE_FEED_KEY: &[u8] = b"price_feed";
/// A `PythDataSource` identifies a specific contract (given by its Wormhole `emitter`) on /// A `PythDataSource` identifies a specific contract (given by its Wormhole `emitter`) on
@ -78,3 +79,40 @@ pub fn price_feed_bucket(storage: &mut dyn Storage) -> Bucket<PriceFeed> {
pub fn price_feed_read_bucket(storage: &dyn Storage) -> ReadonlyBucket<PriceFeed> { pub fn price_feed_read_bucket(storage: &dyn Storage) -> ReadonlyBucket<PriceFeed> {
bucket_read(storage, PRICE_FEED_KEY) bucket_read(storage, PRICE_FEED_KEY)
} }
// this code is only added to facilititate migration
// once migrated this code can be removed
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct DepreceatedConfigInfo {
pub owner: Addr,
pub wormhole_contract: Addr,
pub data_sources: HashSet<PythDataSource>,
pub governance_source: PythDataSource,
// Index for preventing replay attacks on governance data source transfers.
// This index increases every time the governance data source is changed, which prevents old
// transfer request VAAs from being replayed.
pub governance_source_index: u32,
// The wormhole sequence number for governance messages. This value is increased every time the
// a governance instruction is executed.
//
// This field differs from the one above in that it is generated by wormhole and applicable to all
// governance messages, whereas the one above is generated by Pyth and only applicable to governance
// source transfers.
pub governance_sequence_number: u64,
// Warning: This id needs to agree with the wormhole chain id.
// We should read this directly from wormhole, but their contract doesn't expose it.
pub chain_id: u16,
pub valid_time_period: Duration,
// The fee to pay, denominated in fee_denom (typically, the chain's native token)
pub fee: Coin,
}
pub fn deprecated_config(storage: &mut dyn Storage) -> Singleton<DepreceatedConfigInfo> {
singleton(storage, DEPRECATED_CONFIG_KEY)
}
pub fn deprecated_config_read(storage: &dyn Storage) -> ReadonlySingleton<DepreceatedConfigInfo> {
singleton_read(storage, DEPRECATED_CONFIG_KEY)
}