3

The docs say that StaticLookup does not require "context" whereas Lookup does. What does this mean? Is there an example or a pallet where we can look at both Lookup and StaticLookup implementations to see how they differ?

/// Means of changing one type into another in a manner dependent on the source type.
pub trait Lookup {
    /// Type to lookup from.
    type Source;
    /// Type to lookup into.
    type Target;
    /// Attempt a lookup.
    fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError>;
}

/// Means of changing one type into another in a manner dependent on the source type.
/// This variant is different to `Lookup` in that it doesn't (can cannot) require any
/// context.
pub trait StaticLookup {
    /// Type to lookup from.
    type Source: Codec + Clone + PartialEq + Debug + TypeInfo;
    /// Type to lookup into.
    type Target;
    /// Attempt a lookup.
    fn lookup(s: Self::Source) -> Result<Self::Target, LookupError>;
    /// Convert from Target back to Source.
    fn unlookup(t: Self::Target) -> Self::Source;
}

2 Answers 2

1

IIUC, this only difference is about the revert.

Lookup:

  • A -> B
  • B -> A 𝘟

StaticLookup:

  • A -> B
  • B -> A

For example, you define an account id conversion algorithm.

AccountId32 -> AccountId20, truncate the first 20 bytes.

This is not recoverable. You should use Lookup here.

1

Lookup::lookup has a self parameter and StaticLookup does not.
So you can carry around some context in the self of the Lookup trait.

Otherwise inverting a value may not be defined for different contexts.
But yea, the implications in the doc are a bit weird.

2
  • Could you point to a non trivial implementation of Lookup ? Commented Sep 13, 2022 at 7:56
  • 1
    Imaging converting an EVM address to a Substrate AccountId but you want to take the Ethereum-ChainId into account. Could be part of the context. But i don't have good example code. Commented Sep 13, 2022 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.