On-chain snapshots of the whole blockchain state

Overview

📸 Snapshop 📸

Snapshop is a tool for creating on-chain snapshots of the whole blockchain state. It lets your smart contracts read the storage of any contract at a given block. It can be used for on-chain voting, airdrops and many more. Works on Ethereum and similar blockchains like Polygon.

The tooling includes:

  • A library with the on-chain logic to use in your smart contract: SnapshopLib.sol
  • A smart contract wrapping that logic into a commonly available generic utility: Snapshop.sol
  • Helpers for Ethers-js: snapshop.js

How it works

  1. Create a snapshot of a block by providing its header. It mustn't be older than 256 blocks which is about 1 hour on Ethereum mainnet.
  2. Extract the block's state root for an arbitrary contract. This is done by providing a Merkle proof of the root.
  3. Access the contract's slots values from the snapshot. This is done by providing a Merkle proof of the slot.

All proofs can be obtained from any client supporting EIP-1186 by calling eth_getProof. As of now, it's been tested with Geth and Infura, but probably other clients support it too.

Usage in on-chain voting

Snapshop can be used to improve on-chain voting with ERC-20 tokens. A simple demo is available in TestGov.sol and its usage is presented in snapshop-test.js. Here are some advantages of Snapshop-based voting over popular checkpoint-based voting.

No self-delegation requirement

The usual approach to checkpoints requires the user to delegate votes to themselves before the voting begins. This excludes holders from participation in even the most important decisions if they haven't declared interest in governance beforehand. It facilitates a clique of regular voters pushing a controversial decision against the will of other holders who haven't been active before.

Snapshop allows dropping the checkpoints system altogether by facilitating reading balances of all the users from an arbitrary block. All users have the right to vote if only they held tokens at the right moment in the past.

Fair gas cost distribution

In checkpointed tokens, self-delegation is disabled by default because it makes each transfer cost considerably more gas. It gets even more expensive when somebody creates a checkpoint. It's very cheap to do so, but then all the delegating users need to use the new checkpoint and pay for the fresh slots to store them. On the other hand, casting a vote is relatively inexpensive. It's not a good balance, because proposals are created only once in a while and votes aren't cast that often but transfers are made all the time, so they shouldn't be an expensive operation.

Snapshop reverses the pricing. Transfers are very cheap because there's no checkpointing or delegation overhead. Casting a vote requires reading state from a snapshot, which is more expensive, but also much less frequent. Creating a proposal is the most expensive because it requires creating a snapshot, but it's also the rarest operation.

Tokens and governance decoupling

Voting based on checkpoints needs to be baked deep into the token contract. It's a complex piece of logic and ideally, it should be put there right at the token deployment to avoid risky updates. It's impossible to choose the state of an arbitrary existing contract as the source of the user's voting power.

Snapshop allows tying voting power with the state of any contract without modifying it or coupling it with the governance system. Any existing contract state can be used, whether it's an ERC-20 token or not, even states of multiple contracts can be used. For example a simple, non-checkpointed ERC-20 token is used but extra votes are granted for being registered as an owner of another contract.

Settled state

Checkpoint creation is done inside a block, which means that it can freeze an intermediate, malicious state. For example a user can have an unsettled flash loan or the miner can sandwich the snapshot creation with a purchase and a sale of the tokens. These aren't undefendable attacks but these risks exist.

Snapshop uses the states of the already mined blocks which by design makes many attack vectors infeasible.

Gas usage

The gas usage has been tested for two extreme cases. Depending on the network and how large is the state of the read contract your use case will require amounts somewhere between these cases.

The most expensive case

It's been tested for Mainnet using the USDT contract. With over 4.6 million holders it has one of the largest states and requires long Merkle proofs. It's practically the most expensive scenario possible.

  • Block snapshot creation: ~39K gas
  • Contract state root extraction: ~163K gas
  • Slot value access: ~155K gas

The cheapest case

Another test has been run for a local testnet using a contract with a single storage slot used. It's the cheapest useful scenario.

  • Block snapshot creation: ~37K gas
  • Contract state root extraction: ~80K gas
  • Slot value access: ~10K gas

Risks

Usage of Snapshop does have some risks.

Storage layout of contracts

Snapshop will blindly return the value from any slot it's requested to read, no matter if it makes sense in the context of the contract's storage layout. The tooling using Snapshop must be very carefully designed to read the right values and interpret them correctly. Any updates of the contract logic may break subsequent uses of a Snapshop-based tool.

Block format changes

If blocks change their internal format, existing deployments of Snapshop may not be able to work with them. It may require updates or migrations to newer versions of deployed contracts. In the worst case, it may not be possible to use Snapshop anymore.

Development

The project uses Hardhat and requires npm and geth to be installed on the machine.

Running tests:

npm test

Running prettier:

npm run prettier

Running solhint:

npm run lint
You might also like...

Open the whole new fancy chapter, in which end-user can interact lively with our quests!

Open the whole new fancy chapter, in which end-user can interact lively with our quests!

Fancy Chapter - Here comes the fancy adventure! Fancy Chapter is the User Interface (UI) of TheNewsQuest app, in which user can freely interact tons o

Aug 5, 2022

we learn the whole concept of JS including Basics like Object, Functions, Array etc. And Advance JS - Understanding DOMs, JQuery, Ajax, Prototypes etc.

JavaScript-for-Complete-Web Development. we learn the whole concept of JS including Basics like Object, Functions, Array etc. And Advance JS - Underst

Jul 22, 2022

Get started with GatsbyJS straight away without having to spend a whole day configuring your usual addons.

Get started with GatsbyJS straight away without having to spend a whole day configuring your usual addons.

Gatsby Starter Infinite Get started with GatsbyJS straight away without having to spend a whole day configuring your usual addons. This starter includ

Jun 27, 2022

Application which will allow users to find hotels, hostels and restaurants and feedback about those in whole over of world.

Gatsby minimal TypeScript starter 🚀 Quick start Create a Gatsby site. Use the Gatsby CLI to create a new site, specifying the minimal TypeScript star

Jul 22, 2022

Your whole team, changing the world one stroke at a time.

Your whole team, changing the world one stroke at a time.

Collanvas — Your whole team, changing the world one stroke at a time 🎨 With an online whiteboard, you can brainstorm 🧠 , draw art 🖌️ , and even pla

Dec 29, 2022

Evmos chain

Evmos Evmos is a scalable, high-throughput Proof-of-Stake blockchain that is fully compatible and interoperable with Ethereum. It's built using the Co

Jan 3, 2023

On-chain defense against hostile takeovers

Poison pill On-chain defense against hostile takeovers. In layman's terms, this smart contract only facilitates a discounted sale of shares to a white

Jul 19, 2022

WAMpage - A WebOS root LPE exploit chain

WAMpage - A WebOS root LPE exploit chain

WAMpage WAMpage - A WebOS root LPE exploit chain This exploit is mainly of interest to other researchers - if you just want to root your TV, you proba

Dec 2, 2022

Nouns On-Chain Proposal Simulation and Analysis

Nouns Diligence Nouns On-Chain Proposal Simulation and Analysis For Voters Technical reports for all reviewed proposals can be found in the reports fo

Dec 26, 2022
Owner
Igor Żuk
Igor Żuk
chain-syncer is a module which allows you to synchronize your app with any ethereum-compatible blockchain/contract state. Fast. Realtime. Reliable.

Chain Syncer Chain Syncer is a JS module which allows you to synchronize your app with any ethereum-compatible blockchain/contract state. Fast. Realti

Miroslaw Shpak 10 Dec 15, 2022
API and CLI tool to fetch and query Chome DevTools heap snapshots.

Puppeteer Heap Snapshot Capture heap snapshots and query the snapshot for objects matching a set of properties. Read more about it in this blog post.

Adrian Cooney 858 Jan 3, 2023
A TypeScript library for creating dependency snapshots.

Dependency Submission Toolkit @github/dependency-submission-toolkit is a TypeScript library for creating dependency snapshots and submitting them to t

GitHub 19 Nov 22, 2022
CDK construct to periodically take snapshots of RDS databases, sanitize them, and share with selected accounts.

CDK Construct for RDS Sanitized Snapshots Periodically take snapshots of RDS databases, sanitize them, and share with selected accounts. Use this to a

CloudSnorkel 6 Dec 7, 2022
🥁 Batch contract/on-chain queries to the same block. Multicall SDK for the Klaytn blockchain.

Klaytn Multicall Built for inevitable-changes/bento Inspired by makerdao/multicall and dopex-io/web3-multicall ?? Installation # Yarn yarn install kla

Inevitable (Bento) 4 Nov 7, 2022
It is a very basic implementation of how blockchain works, mainly how the bitcoin blockchain.

How to run this program npm install node core/blockchain.js What is this It is a very basic implementation of how blockchain works, mainly how the bit

Anish Jain 12 May 9, 2022
HackMIT 2022. 2nd Place in Blockchain for Society sponsored by Jump Crypto. A revolutionary web application that leverages machine learning and blockchain technology to improve the crowdsourcing experience!

?? Wikisafe ?? Wikisafe is a revolutionary new crowdsourcing web application that innovates the process of crowdsourcing information. This application

Benson Liu 5 Dec 8, 2022
A mod for Mindustry that adds a few items and liquids, and a whole new tier of upgrades, along with arc

A mod that adds a few items and liquids, and a whole new tier of upgrades, along with arc. Made by Chickenloser and Oreo with help from Duvent.

null 5 Nov 25, 2022
A website that acts as a guide about the universities to potential students whole throughout the globe.

A website that acts as a guide about the universities to potential students whole throughout the globe.

null 1 Apr 15, 2022
Given a list of items, only render what's visible on the screen while allowing scrolling the whole list.

Solid Windowed Given a list of items, only render what's visible on the screen while allowing scrolling the whole list. A Solid component. See https:/

Tito 40 Dec 21, 2022