ChainSafe / ChainSafe/gossamer

Implement read-only `StorageState` functionality to translation shim type

Open
#4,467 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
454
Forks
144
PR merge metrics
No merged PRs in 30d

Description

## Issue Summary

From design doc:
> The following `StorageState` methods are relatively easy to implement in the [translation shim](https://github.com/ChainSafe/gossamer/blob/development/internal/client/adapter/client_adapter.go) by utilizing `db.Backend`.
`internal/client/db/backend.go`

```go
GetStorage(root *common.Hash, key []byte) ([]byte, error)
GetStorageByBlockHash(bhash *common.Hash, key []byte) ([]byte, error)
GetKeysWithPrefix(root *common.Hash, prefix []byte) ([][]byte, error)
GetStorageChild(root *common.Hash, keyToChild []byte) (trie.Trie, error)
GetStorageFromChild(root *common.Hash, keyToChild, key []byte) ([]byte, error)
GetStateRootFromBlock(bhash *common.Hash) (*common.Hash, error)

StorageRoot() (common.Hash, error)

Entries(root *common.Hash) (map[string][]byte, error)

LoadCode(hash *common.Hash) ([]byte, error)
LoadCodeHash(hash *common.Hash) (common.Hash, error)
```

> `GetStorageXXX` methods are easily accessible by translating and implementing the [`StorageProvider`](https://github.com/paritytech/polkadot-sdk/blob/030cb4a71b0b390626a586bfe7117b7c66b4700c/substrate/client/api/src/backend.rs#L418) trait for `Client` `internal/client/client.go`

> `GetStateRootFromBlock` can be retrieved from already introduced `HeaderBackend` interface which `db.Backend` implements. Same for `StorageRoot`.

> `Entries` should be refactored to utilize an iterator where `Entries` is called. I propose refactoring this method to return a [`PairsIter`](https://github.com/ChainSafe/gossamer/blob/83edb1e7bc65b5c1d569be8e9d5acaea891a3531/internal/primitives/state-machine/backend.go#L53) which iterates through the keys and values.

> `LoadCode` and `LoadCodeHash` should be trivially implemented by accessing the state trie for a given hash and retrieving the entry with key ":code".

## Acceptance Criteria
- [x] Introduce `StorageProvider` interface and implement it
- [x] Implement `GetXXX` methods
- [x] Implement `StorageRoot` method
- Revise `Entries` function call to return iterator. (removed from scope)
- Update all references to `Entries` to use iterator.
- [x] Implement `LoadCode` and `LoadCodeHash`.
- [x] Unit tests.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.