Search code examples
cryptocurrencyparitysubstratepolkadotpolkadot-js

Adding additional data fields to account information in Substrate


Very new to Substrate and Rust. My understanding of the ChainState is that it acts sort of like a database which holds account numbers (in this case public keys) and their associated balances. When making a transaction, Substrate basically checks to see that you have a sufficient balance, and if so, the transaction succeeds. (This is different from the UTXO method used in Bitcoin.)

First of all, if I am wrong on the above, please correct me.

If I am correct (or at least close) I would like to find a method for associating other data with each account. I've noticed that in the demos, accounts are also associated with names, like Alice, Bob, etc. Is this kept in the ChainState, or is this something which would only be stored on one's own node?

I am trying to determine a way to associate additional data with accounts in the ChainState. For example, how could I store a name (like Alice, Bob, etc.) in the ChainState (assuming that they are only stored locally) or even other information, such as the birthday of the account owner, or their favorite author, or whatever arbitrary information?


Solution

  • The Chain State is just the state of everything, not necessarily connected to Account IDs. It does, among other things, store balances and such, yes, but also many other things that the chain stored one way or another.

    To add custom data, you would create a new structure (map) and then map account IDs to whatever data you want. As an example:

    decl_storage! {
        trait Store for Module<T: Trait> as TemplateModule {
            /// The storage item for our proofs.
            /// It maps a proof to the user who made the claim and when they made it.
            Proofs: map hasher(blake2_128_concat) Vec<u8> => (T::AccountId, T::BlockNumber);
        }
    }
    

    The above declares a storage map which will associate a hash with a tuple of Account and Block Number. That way, querying the hash will return those two values. You could also do the reverse and associate an AccountID with some other value, like a string (Vec<u8>).

    I recommend going through this tutorial from which I took the above snippet: it will show you exactly how to add custom information into a chain.