3

How can my pallet access a substrate chain's storage at a previous block?

For example: storage_name::get(&key, &block_number);

Possible, is there documentation? Not possible, can we request this feature?

1 Answers1

4

It is not possible to query the storage of older blocks from within the runtime, nor would it be a feature that really makes sens to include as you describe it.

Each block should only rely on the data available in that block, else you start to make larger assumptions about the clients you are working with and what data is actually available to them.

The solution is simple here, just store any data you need into your own storage item that persists from block to block. We do this for a number of storages where we need the info from previous blocks like the validator and nominator information in the staking pallet.

When you don't need that data anymore, you can clean it up.

Here is an example: https://github.com/paritytech/substrate/blob/master/frame/staking/src/lib.rs#L969

Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
  • However, our concern is that storing that data on chain will add additional weight to the chain. i.e. if I store the_last_value and the_value on the chain this would double the chain space for this parameter. If I can query it's previous state, it wont? – Jacob R. Steeves Nov 18 '20 at 00:25
  • For both `last_value` and `value` to exist, any computer will need to store both copies of the variable. Our underlying DB is smart such that we wont make a new copy of duplicate values between two blocks, so you will not actually be saving any storage space on the computer running your blockchain. You can get an idea why the same amount of space is used by watching this "pruning" section of the Substrate Storage Deep Dive: https://youtu.be/9S8rmW8LD5o?t=730 – Shawn Tabrizi Nov 18 '20 at 04:20