From 7541a8f771b2711c293eedd50be2db7715210f5f Mon Sep 17 00:00:00 2001 From: jolestar Date: Mon, 31 Jul 2023 17:21:01 +0800 Subject: [PATCH] [Framework] Refactor multi-chain address generate (#546) * [Framework] Refactor multi-chain address generate --- crates/rooch-executor/src/actor/executor.rs | 15 +++----- crates/rooch-framework/doc/address_mapping.md | 33 +++++++++++++++++- .../sources/address_mapping.move | 16 +++++++-- .../src/bindings/address_mapping.rs | 22 ++++++++++++ crates/rooch-genesis/genesis/genesis | Bin 38894 -> 39019 bytes 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/crates/rooch-executor/src/actor/executor.rs b/crates/rooch-executor/src/actor/executor.rs index e16997d480..0f7eaadfbd 100644 --- a/crates/rooch-executor/src/actor/executor.rs +++ b/crates/rooch-executor/src/actor/executor.rs @@ -56,20 +56,13 @@ impl ExecutorActor { }) } - pub fn resolve_address( + pub fn resolve_or_generate( &self, multi_chain_address_sender: MultiChainAddress, ) -> Result { let resolved_sender = { let address_mapping = self.moveos.as_module_bundle::(); - 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) @@ -78,7 +71,7 @@ impl ExecutorActor { pub fn validate(&self, tx: T) -> Result { 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?)?; @@ -216,7 +209,7 @@ impl Handler for ExecutorActor { msg: ResolveMessage, _ctx: &mut ActorContext, ) -> Result { - self.resolve_address(msg.address) + self.resolve_or_generate(msg.address) } } diff --git a/crates/rooch-framework/doc/address_mapping.md b/crates/rooch-framework/doc/address_mapping.md index a9696841b9..4e973e4717 100644 --- a/crates/rooch-framework/doc/address_mapping.md +++ b/crates/rooch-framework/doc/address_mapping.md @@ -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) @@ -178,7 +179,37 @@ Resolve a multi-chain address to a rooch address let addr = table::borrow(&am.mapping, maddress); option::some(*addr) }else{ - default_rooch_address(maddress) + option::none() + } +} + + + + + + + + +## Function `resolve_or_generate` + +Resolve a multi-chain address to a rooch address, if not exists, generate a new rooch address + + +
public fun resolve_or_generate(ctx: &storage_context::StorageContext, maddress: address_mapping::MultiChainAddress): address
+
+ + + +
+Implementation + + +
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)
     }
 }
 
diff --git a/crates/rooch-framework/sources/address_mapping.move b/crates/rooch-framework/sources/address_mapping.move index 0444bc9f4a..255001b042 100644 --- a/crates/rooch-framework/sources/address_mapping.move +++ b/crates/rooch-framework/sources/address_mapping.move @@ -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
{ + 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 diff --git a/crates/rooch-framework/src/bindings/address_mapping.rs b/crates/rooch-framework/src/bindings/address_mapping.rs index 321fd159af..fdbaf2fa6a 100644 --- a/crates/rooch-framework/src/bindings/address_mapping.rs +++ b/crates/rooch-framework/src/bindings/address_mapping.rs @@ -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> { if multichain_address.is_rooch_address() { @@ -40,6 +41,27 @@ impl<'a> AddressMapping<'a> { }) } } + + pub fn resovle_or_generate( + &self, + multichain_address: MultiChainAddress, + ) -> Result { + 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> { diff --git a/crates/rooch-genesis/genesis/genesis b/crates/rooch-genesis/genesis/genesis index 9e1d42a68a669c5504440c4f590aecbb417c6d10..7617716774e06ba61bd7bd95e8e3c346e9be642c 100644 GIT binary patch delta 786 zcmY*XJxml)5T2R0`*!#3vh3dN{lLNTqX_;LkZ3lVSQ!%&6I&{d0~T0?TXJ_QjZzu^ z3N;%$F|p7DEg*qH6Sa_Dqa}@%HY6lUXQFWqXne)YeDl8fCNuBttkU)xt!<6=Zj5i< zz3}wj#oYHd?>}ar6-O?8+SpzGQ67A~^I~&l_}cpK(U;EIuj+Z%wk-j`K?o=)i;+91 zy~Vu#8CTKxOS{7SDaYj(W}Id77q&fAvi*rlx1#=~z(4?5)JPzpafkq1;7mKQ z@q`B8>nxsWv9cz^oUj1$LIRbP1kvTRp((rDkpzgc=QHTZuozgq*#NY@K!Vw?5cI?O z0?2_vk%T6WbRe2ISn5I;lS7$qrsGj$gvWX`w#R!*2$ro}+jd;v&DdEPxH*}Z1t|=Z zq+=qq0^LNCCYi{GjZS-)C^@zT>L{J`-E?=U#bICpNn0ZeEVKZnB2<+AnI0-LVtOlQ zA?f>-`w4?xdh>oUJvZ}e%#9Y~Mzc|wuP!XawVC`(REz4>W>l#s$>eOMIyF^~8VxP3 z#Z516{GXMV7n*TWGnkRgU5~N{zLL}{2QumK?D_Q3Y{69%bJeTS$i&F0)2f-IUbQFv zFngj;Yb5iLoJi{R+FYqSj-gw82K- zOjqK8^jn-;B9Cz>c<+3vyWpYU=DWnt3pq!j->$&-Ld~I8olO}Cb>855MLloZe~j)k;Zy*uyBGpm`CJ^J;E_70XNcDgVA zZXYLy^Xa$mzYUd-^v=TNqnnR+AI|jKl=1!0&zJ#UD2JryFocEea3-P=Xp?k^CDN&nlC|{E zP7c<_r%`G`J}&f9UFhZ7D0{+KIV-N7ls*I)>d+LZwr;KCixg(blRd%^n_+=PBYfZnSKKm704qy&lBbk(*Do& H?Mwdvx5#wZ