Skip to content

Commit

Permalink
[Framework] Refactor multi-chain address generate (#546)
Browse files Browse the repository at this point in the history
* [Framework] Refactor multi-chain address generate
  • Loading branch information
jolestar authored Jul 31, 2023
1 parent 7e654b4 commit 7541a8f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
15 changes: 4 additions & 11 deletions crates/rooch-executor/src/actor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,13 @@ impl ExecutorActor {
})
}

pub fn resolve_address(
pub fn resolve_or_generate(
&self,
multi_chain_address_sender: MultiChainAddress,
) -> Result<AccountAddress> {
let resolved_sender = {
let address_mapping = self.moveos.as_module_bundle::<AddressMapping>();
address_mapping
.resolve(multi_chain_address_sender.clone())?
.ok_or_else(|| {
anyhow::anyhow!(
"the multiaddress sender({}) mapping record is not exists.",
multi_chain_address_sender
)
})?
address_mapping.resovle_or_generate(multi_chain_address_sender)?
};

Ok(resolved_sender)
Expand All @@ -78,7 +71,7 @@ impl ExecutorActor {
pub fn validate<T: AbstractTransaction>(&self, tx: T) -> Result<VerifiedMoveOSTransaction> {
let multi_chain_address_sender = tx.sender();

let resolved_sender = self.resolve_address(multi_chain_address_sender.clone());
let resolved_sender = self.resolve_or_generate(multi_chain_address_sender.clone());
let authenticator = tx.authenticator_info();

let mut moveos_tx = tx.construct_moveos_transaction(resolved_sender?)?;
Expand Down Expand Up @@ -216,7 +209,7 @@ impl Handler<ResolveMessage> for ExecutorActor {
msg: ResolveMessage,
_ctx: &mut ActorContext,
) -> Result<AccountAddress, anyhow::Error> {
self.resolve_address(msg.address)
self.resolve_or_generate(msg.address)
}
}

Expand Down
33 changes: 32 additions & 1 deletion crates/rooch-framework/doc/address_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Constants](#@Constants_0)
- [Function `is_rooch_address`](#0x3_address_mapping_is_rooch_address)
- [Function `resolve`](#0x3_address_mapping_resolve)
- [Function `resolve_or_generate`](#0x3_address_mapping_resolve_or_generate)
- [Function `exists_mapping`](#0x3_address_mapping_exists_mapping)
- [Function `bind`](#0x3_address_mapping_bind)
- [Function `bind_no_check`](#0x3_address_mapping_bind_no_check)
Expand Down Expand Up @@ -178,7 +179,37 @@ Resolve a multi-chain address to a rooch address
<b>let</b> addr = <a href="_borrow">table::borrow</a>(&am.mapping, maddress);
<a href="_some">option::some</a>(*addr)
}<b>else</b>{
<a href="address_mapping.md#0x3_address_mapping_default_rooch_address">default_rooch_address</a>(maddress)
<a href="_none">option::none</a>()
}
}
</code></pre>



</details>

<a name="0x3_address_mapping_resolve_or_generate"></a>

## Function `resolve_or_generate`

Resolve a multi-chain address to a rooch address, if not exists, generate a new rooch address


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">address_mapping::MultiChainAddress</a>): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="address_mapping.md#0x3_address_mapping_resolve_or_generate">resolve_or_generate</a>(ctx: &StorageContext, maddress: <a href="address_mapping.md#0x3_address_mapping_MultiChainAddress">MultiChainAddress</a>): <b>address</b> {
<b>let</b> addr = <a href="address_mapping.md#0x3_address_mapping_resolve">resolve</a>(ctx, maddress);
<b>if</b>(<a href="_is_none">option::is_none</a>(&addr)){
<a href="address_mapping.md#0x3_address_mapping_generate_rooch_address">generate_rooch_address</a>(maddress)
}<b>else</b>{
<a href="_extract">option::extract</a>(&<b>mut</b> addr)
}
}
</code></pre>
Expand Down
16 changes: 13 additions & 3 deletions crates/rooch-framework/sources/address_mapping.move
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,23 @@ module rooch_framework::address_mapping{
let addr = table::borrow(&am.mapping, maddress);
option::some(*addr)
}else{
default_rooch_address(maddress)
option::none()
}
}

/// Resolve a multi-chain address to a rooch address, if not exists, generate a new rooch address
public fun resolve_or_generate(ctx: &StorageContext, maddress: MultiChainAddress): address {
let addr = resolve(ctx, maddress);
if(option::is_none(&addr)){
generate_rooch_address(maddress)
}else{
option::extract(&mut addr)
}
}

fun default_rooch_address(maddress: MultiChainAddress): Option<address> {
fun generate_rooch_address(maddress: MultiChainAddress): address {
let hash = blake2b256(&maddress.raw_address);
option::some(moveos_std::bcs::to_address(hash))
moveos_std::bcs::to_address(hash)
}

/// Check if a multi-chain address is bound to a rooch address
Expand Down
22 changes: 22 additions & 0 deletions crates/rooch-framework/src/bindings/address_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AddressMapping<'a> {

impl<'a> AddressMapping<'a> {
const RESOLVE_FUNCTION_NAME: &'static IdentStr = ident_str!("resolve");
const RESOLVE_OR_GENERATE_FUNCTION_NAME: &'static IdentStr = ident_str!("resolve_or_generate");

pub fn resolve(&self, multichain_address: MultiChainAddress) -> Result<Option<AccountAddress>> {
if multichain_address.is_rooch_address() {
Expand All @@ -40,6 +41,27 @@ impl<'a> AddressMapping<'a> {
})
}
}

pub fn resovle_or_generate(
&self,
multichain_address: MultiChainAddress,
) -> Result<AccountAddress> {
if multichain_address.is_rooch_address() {
let rooch_address: RoochAddress = multichain_address.try_into()?;
Ok(rooch_address.into())
} else {
let ctx = TxContext::zero();
let call = FunctionCall::new(
Self::function_id(Self::RESOLVE_OR_GENERATE_FUNCTION_NAME),
vec![],
vec![multichain_address.to_bytes()],
);
self.caller.call_function(&ctx, call).map(|values| {
let value = values.get(0).expect("Expected return value");
AccountAddress::from_bytes(&value.value).expect("Expected return address")
})
}
}
}

impl<'a> ModuleBundle<'a> for AddressMapping<'a> {
Expand Down
Binary file modified crates/rooch-genesis/genesis/genesis
Binary file not shown.

0 comments on commit 7541a8f

Please sign in to comment.