根据这个PR,所有可调度的调用都应该使用<T::Lookup as StaticLookup>::Source
而不是T::AccountId
. 为什么用查找来处理转账而不是用他们的账户来处理用户会更好?Substrate 中的查找如何工作,是否有替代方法StaticLookup
?
最后,除了 the 之外还有其他类型IdentityLookup
吗?您将如何使用它们?
type Lookup = IdentityLookup<AccountId>;
StaticLookup 是对地址的抽象,它可以将多种不同的地址类型转换为底层的 AccountId。
想象一个仅使用 AccountId 的外部变量。与该函数交互的唯一方法是为链上帐户提供原始 AccountId。
相反,使用 StaticLookup,您可以提供任何兼容的地址格式,并且将使用其他逻辑将该地址转换为基础帐户。
想象一下:
enum Address {
AccountId([u8; 32]), // 32 byte account id
String(Vec<u8>), // arbitrary string
}
这是一个实际示例,说明如何允许人们在您的链上使用名称服务。例如,您可以这样做:
transfer(b"shawntabrizi", 100 UNIT)
此外:
transfer(5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY, 100 UNIT)
StaticLookup 将包含任何适当的代码,以将已知格式转换为您需要的最终 AccountId。
想象一下下面的代码:
struct NameServiceLookup;
impl StaticLookup for NameServiceLookup {
type Source = Address;
type Target = AccountId;
fn lookup(a: Address) -> Result<AccountId, LookupError> {
match a {
Address::AccountId(id) => Ok(id),
Address::String(string) => {
string_to_account_id(string).ok_or(LookupError)
},
}
}
fn unlookup(a: [u8; 32]) -> Address {
Address::AccountId(a)
}
}
在这里,您可以看到我们有特殊的逻辑来处理地址,如果它是 AccountId 或 String。这最终StaticLookup
将提供。
的实现IdentityLookup
是一个简单的传递,它准确地返回输出的输入。因此,当您没有任何这种花哨的逻辑并且想要使所有内容与直接StaticLookup
完全相同时,这将是您想要使用的:AccountId
pub struct IdentityLookup<T>(PhantomData<T>);
impl<T: Codec + Clone + PartialEq + Debug> StaticLookup for IdentityLookup<T> {
type Source = T;
type Target = T;
fn lookup(x: T) -> Result<T, LookupError> { Ok(x) }
fn unlookup(x: T) -> T { x }
}