substrate 为什么有的调用的地方用<T::Lookup as StaticLookup>::Source 代替 T::AccountId

  StaticLookup是对地址的抽象,可以将多种不同的地址类型转换为基础的AccountId。

  如果一个仅使用AccountId的extrinsic。 与该调用函数进行交互的唯一方法是为链上帐户提供原始AccountId。相反,使用StaticLookup,您可以提供任何兼容的地址格式,并且将使用其他逻辑将该地址转换为基础AccountId。

看如下示例代码:

// 假设有如下结构体代表地址信息

enum Address {
  AccountId([u8; 32]), // 32 byte account id
  String(Vec<u8>), // arbitrary string
}
// 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)
    }
}

  IdentityLookup的实现是一个简单的传递,它完全原样返回和输入一样的输出。 因此,当您没有任何此类奇特的逻辑并且想要使所有StaticLookup与AccountId直接完全相同时,可以使用IdentityLookup, substrate中很多pallet的测试代码中都有  

type Lookup = IdentityLookup<Self::AccountId>;
原文地址:https://www.cnblogs.com/kundij/p/14672189.html