cosmwasm: tokenbridge: Add chain registration query

Add a query to get the registered emitterr address for a given chain id.
This will be used by accounting.
This commit is contained in:
Chirantan Ekbote 2022-10-24 14:49:19 +09:00 committed by Evan Gray
parent a3682cc158
commit 09459fcf9e
3 changed files with 42 additions and 3 deletions

View File

@ -31,8 +31,8 @@ use cosmwasm_std::{
use crate::{
msg::{
ExecuteMsg, ExternalIdResponse, InstantiateMsg, IsVaaRedeemedResponse, MigrateMsg,
QueryMsg, TransferInfoResponse, WrappedRegistryResponse,
ChainRegistrationResponse, ExecuteMsg, ExternalIdResponse, InstantiateMsg,
IsVaaRedeemedResponse, MigrateMsg, QueryMsg, TransferInfoResponse, WrappedRegistryResponse,
},
state::{
bridge_contracts, bridge_contracts_read, bridge_deposit, config, config_read,
@ -1449,6 +1449,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::TransferInfo { vaa } => to_binary(&query_transfer_info(deps, env, &vaa)?),
QueryMsg::ExternalId { external_id } => to_binary(&query_external_id(deps, external_id)?),
QueryMsg::IsVaaRedeemed { vaa } => to_binary(&query_is_vaa_redeemed(deps, env, &vaa)?),
QueryMsg::ChainRegistration { chain } => {
query_chain_registration(deps, chain).and_then(|r| to_binary(&r))
}
}
}
@ -1526,6 +1529,13 @@ fn query_is_vaa_redeemed(deps: Deps, _env: Env, vaa: &Binary) -> StdResult<IsVaa
})
}
fn query_chain_registration(deps: Deps, chain: u16) -> StdResult<ChainRegistrationResponse> {
bridge_contracts_read(deps.storage)
.load(&chain.to_be_bytes())
.map(Binary::from)
.map(|address| ChainRegistrationResponse { address })
}
fn is_governance_emitter(cfg: &ConfigInfo, emitter_chain: u16, emitter_address: &[u8]) -> bool {
cfg.gov_chain == emitter_chain && cfg.gov_address == emitter_address
}

View File

@ -79,6 +79,7 @@ pub enum QueryMsg {
TransferInfo { vaa: Binary },
ExternalId { external_id: Binary },
IsVaaRedeemed { vaa: Binary },
ChainRegistration { chain: u16 },
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
@ -110,3 +111,9 @@ pub struct ExternalIdResponse {
pub struct IsVaaRedeemedResponse {
pub is_redeemed: bool,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ChainRegistrationResponse {
pub address: Binary,
}

View File

@ -262,6 +262,7 @@ test("Deploy Contracts", (done) => {
})();
});
passOnceTests(false, 1);
alwaysPassTests(false, 1);
describe("Upgrade to shutdown contracts Tests", () => {
@ -350,7 +351,7 @@ describe("Upgrade to shutdown contracts Tests", () => {
});
alwaysPassTests(true, 2);
passOnceTests(true, 1);
passOnceTests(true, 2);
failInShutdownModeTests(true, 1);
describe("Upgrade to previous non-shutdown contracts Tests", () => {
@ -1374,6 +1375,27 @@ function alwaysPassTests(shutdownMode: boolean, pass: number) {
}
})();
});
test("Query Chain Registration", (done) => {
(async () => {
try {
const [client] = await makeProviderAndWallet();
const tokenbridge = contracts.get("tokenBridge")!;
const result: any = await client.wasm.contractQuery(tokenbridge, {
chain_registration: {
chain: FOREIGN_CHAIN,
},
});
expect(result).toStrictEqual({
address: Buffer.from(FOREIGN_TOKEN_BRIDGE, "hex").toString("base64")
});
done();
} catch (e) {
console.error(e);
done("Failed to Query Chain Registration");
}
})();
});
});
}