Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Prevent account storage leakage #270

Merged
merged 15 commits into from
Jul 3, 2018
Merged

Prevent account storage leakage #270

merged 15 commits into from
Jul 3, 2018

Conversation

pepyakin
Copy link
Contributor

@pepyakin pepyakin commented Jul 2, 2018

paritytech/polkadot#195 introduced logic of deletion of accounts if their balance is too low.

An account can have not only balances and nonces, but also a key-value storage, that can be used by substrate smart-contracts and this storage should be deleted when account itself gets deleted. However, #195 delayed the removal of the account's storage.

This PR aims to implement deletion of the contract storage along with account itself.

In particular, it adds a clear_prefix function in externalities which takes a prefix, walks runtime storage trie and removes all keys that starts with the given prefix.

Then, the storage of contracts are put in a custom implementation of a storage container: StorageDoubleMap. It basically is a map which uses two keys instead of one to address a value. Also, it provides a special function remove_prefix which removes all entries from the storage that shares a common key (i.e. AccountId).

This custom container is required because we need to use blake2 for hasing contract's storage keys (since they ought to be controlled by smart-contract code which is untrusted) and we have no means to cleanly specify the way of hashing of keys for the storage data containers.

@rphmeier

This comment has been minimized.

@pepyakin

This comment has been minimized.

@pepyakin pepyakin added the A3-in_progress Pull request is in progress. No review needed at this stage. label Jul 2, 2018
@pepyakin pepyakin added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jul 2, 2018
@pepyakin
Copy link
Contributor Author

pepyakin commented Jul 2, 2018

@rphmeier done!

@pepyakin pepyakin changed the title [WIP] Remove prefix to prevent account storage leakage Prevent account storage leakage Jul 2, 2018
@rphmeier rphmeier added A8-looksgood and removed A0-please_review Pull request needs code review. labels Jul 3, 2018
@gavofyork
Copy link
Member

@arkpar mentioned that keys were first hashed before being placed in the merkle tree - if this is true then enumeration won't work. are there tests to ensure it's working as expected?

@pepyakin
Copy link
Contributor Author

pepyakin commented Jul 3, 2018

@arkpar mentioned that keys were first hashed before being placed in the merkle tree

@gavofyork Yes, they are hashed in runtime before calling to the externalities.

Because of this, StorageDoubleMap uses unhashed versions of put/kill/etc functions and do the hashing by itself.

Apart of this, I haven't found any additional hashing on top of that (Except HashDb which isn't relevant here, right?).

are there tests to ensure it's working as expected?

Yeah, there are tests but it seems I forgot to add test for the trie backend specifically, sorry about that. Rebased and added test for prefix walking for the trie backend.

@gavofyork
Copy link
Member

cool - at some point (poc-2 or -3; whenever we restart the chain) we'll want to remove that blanket hashing that StorageDoubleMap circumvents

@rphmeier rphmeier mentioned this pull request Jul 3, 2018
8 tasks
@pepyakin
Copy link
Contributor Author

pepyakin commented Jul 3, 2018

Added comment to #227 about it

@gavofyork gavofyork merged commit 2a8a685 into master Jul 3, 2018
@gavofyork gavofyork deleted the ser-double-map branch July 3, 2018 18:52
JoshOrndorff added a commit to moonbeam-foundation/substrate that referenced this pull request Apr 21, 2021
liuchengxu pushed a commit to chainx-org/substrate that referenced this pull request Aug 23, 2021
* FIx extracter panic

* Change rpc 'chainx_getAddressByAccount'

* Add chainx_getDepositList

* Add withdraw_list

* Update wasm

* Update btc header info

* Flow substrate-rpc

* Fix build error

* Add 'state_getKeys' rpc

* Delete old bind

* Add withdrawal id in result
Fix rebind bug

* Refine a bit

* Fix sign error

* Update wasm
Modify test
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants