Nightshade
Nightshade
0
Alex Skidanov Illia Polosukhin Bowen Wang
7/AlexSkidanov 7/ilblackdragon 7/BowenWang18
[email protected] [email protected] [email protected]
July 2019
Updated February 2024
Contents
1 Sharding Basics 4
1.1 Validator partitioning and beacon chains . . . . . . . . . . . . . . 4
1.2 Quadratic sharding . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 State sharding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Cross-shard transactions . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Malicious behavior . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.1 Malicious forks . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.2 Approving invalid blocks . . . . . . . . . . . . . . . . . . . 9
3 Nightshade 23
3.1 From shard chains to shard chunks . . . . . . . . . . . . . . . . . 23
3.2 Consensus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.3 Block production . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.4 Ensuring data availability . . . . . . . . . . . . . . . . . . . . . . 26
3.4.1 Dealing with lazy block producers . . . . . . . . . . . . . 26
3.5 State transition application . . . . . . . . . . . . . . . . . . . . . 27
3.5.1 State Representation . . . . . . . . . . . . . . . . . . . . . 28
1
3.6 Cross-shard transactions and receipts . . . . . . . . . . . . . . . . 29
3.6.1 Receipt transaction lifetime . . . . . . . . . . . . . . . . . 29
3.6.2 Handling too many receipts . . . . . . . . . . . . . . . . . 30
3.7 Chunks validation . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.7.1 Zero-knowledge proofs . . . . . . . . . . . . . . . . . . . . 33
3.8 State Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.9 Snapshots Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
4 Conclusion 34
2
Introduction
The past several years have seen significant progress in scaling blockchains. Back
in 2018, when the early team first started to build NEAR Protocol, Ethereum
was one of the only smart contract platforms available to builders and it suffered
from high transaction costs and low throughput. Today, Ethereum is scaling
through rollups (a.k.a. L2s1 ). Quite a few other layer 1 blockchains such as
Polkadot, Solana, Sui, and Aptos that promise different kinds of scaling have
launched in the past few years.
In general, most layer-one blockchains use one of two scaling approaches:
vertical scaling and horizontal scaling. For vertical scaling, a few approaches
are explored, including each validator operating more expensive hardware, par-
allelizing transaction execution, separating consensus from execution, etc. Hor-
izontal scaling has two categories: heterogeneous and homogeneous. In the
heterogeneous approach, there are different chains or execution environments
that operate independently and there is usually a main chain that orchestrates
everything and provides security guarantees. In the homogeneous approach, a
blockchain is divided into multiple parallel shards that have the same execution
environment and the protocol dictates how different shards communicate with
each other. Each shard maintains its own state, which allows the blockchain to
scale linearly by adding more shards.
NEAR Protocol chooses a homogeneous sharding approach to build a highly
scalable blockchain. This document outlines the general approach to blockchain
sharding as it exists today, the major problems that need to be overcome (in-
cluding state validity and data availability problems), and presents Nightshade,
the solution NEAR Protocol is built upon that addresses those issues. As of
this writing of the revised version in February 2024, NEAR has been live for
three and half years. There have been a total of 750M transactions and 57M
accounts on chain since the mainnet launch in October 2020.
Since the original publication of the Nightshade whitepaper in July 2019,
there have been a lot of new developments in blockchain protocol research, most
notably zero-knowledge proofs. In the case of NEAR, during the implementation
of Nightshade across different phases over three-plus years, we gained a better
understanding of blockchain scalability and also came to realize some limitations
of the original design. In short, we underestimated the engineering complexity
of fraud proofs in a sharded blockchain and started to question whether that
was still the most promising direction for the protocol. Recent advancements in
stateless validation research provided an alternative path, which not only elim-
inates the critical dependency on fraud proofs, but also significantly increases
per-shard throughput by enabling nodes to hold state in memory. As a result,
we decided to update the Nightshade design to incorporate stateless validation.
This is a revised edition of the Nightshade paper, version 2.0, reflecting this
change in the Nightshade roadmap. We will also elaborate on the power of zero-
knowledge proofs, and how they may be combined with stateless validation to
1 Technically a rollup is a type of L2, but the success of rollups has made the two terms
3
produce a future-proof sharding design that is both highly scalable and highly
decentralized. New or heavily modified text is included in section 2.4, section
2.5, section 3.5, section 3.7, and section 3.8.
1 Sharding Basics
Let’s start with the simplest approach to sharding. In this approach, instead
of running one blockchain we will run multiple, and call each such blockchain a
“shard.” Each shard will have its own set of validators. Here, and below, we use
a generic term “validator” to refer to participants that verify transactions and
produce blocks — either by mining, such as in Proof of Work, or via a voting-
based mechanism. For now, let’s assume that the shards never communicate
with each other.
This design, though simple, is sufficient to outline some of the initial major
challenges in sharding.
which brings us to the second point: who chooses the validators for each shard?
Controlling 5.1% of validators is only damaging if all those 5.1% of validators
are in the same shard. If validators can’t choose which shard they get to validate
4
in, a participant controlling 5.1% of the validators is highly unlikely to get all
their validators in the same shard, heavily reducing their ability to compromise
the system.
Almost all sharding designs today rely on some source of randomness to
assign validators to shards. Randomness on a blockchain is in itself a very chal-
lenging topic and is out of scope for this document. For now let’s assume there’s
some source of randomness we can use. We will cover validator assignment in
more detail in section 2.1.
Both randomness and validator assignment require computation that is not
specific to any particular shard. For that computation, practically all existing
designs have a separate blockchain that is tasked with performing operations
necessary for the maintenance of the entire network. Besides generating random
numbers and assigning validators to the shards, these operations often also
include receiving updates from shards and taking snapshots of them, processing
stakes and slashing in Proof-of-Stake systems, and rebalancing shards (when
that feature is supported). Such chain is called a Beacon chain in Ethereum, a
Relay chain in Polkadot, and the Cosmos Hub in Cosmos.
Throughout this document we will refer to such a chain as a Beacon chain.
The existence of the Beacon chain brings us to the next interesting topic:
quadratic sharding.
5
1.3 State sharding
Until now, we haven’t defined clearly what exactly is and is not separated when
a network is divided into shards. Specifically, nodes in a blockchain perform
three important tasks: not only do they 1) process transactions, they also 2)
relay validated transactions and completed blocks to other nodes and 3) store
the state and the history of the entire network ledger. Each of these three tasks
imposes a growing requirement on the nodes operating the network:
1. The task of processing transactions requires more compute power with the
increasing number of transactions being processed;
2. The task of relaying transactions and blocks requires more network band-
width with the increasing number of transactions being relayed;
3. The task of storing data requires more storage as the state grows. Im-
portantly, unlike the needs for processing power and network bandwidth,
the storage requirement grows even if the transaction rate (number of
transactions processed per second) remains constant.
From the above list it might appear that the storage requirement would be
the most pressing, since it is the only one that is being increased over time
even if the number of transactions per second doesn’t change, but in practice
the most pressing requirement today is the compute power. The entire state
of Ethereum as of this writing is 300GB — easily manageable by most of the
nodes. But the number of transactions Ethereum can process per second is
around 20 - orders of magnitude less than what is needed for many practical
use cases.
Zilliqa is the most well-known project that shards processing but not storage.
Sharding of processing is an easier problem because each node keeps the entire
state, meaning that contracts can freely invoke other contracts and read any data
from the blockchain. Some careful engineering is needed to make sure updates
from multiple shards updating the same parts of the state do not conflict. In
those regards Zilliqa takes a relatively simplistic approach2 .
While sharding of storage without sharding of processing was proposed, it is
extremely uncommon. Thus in practice sharding of storage, or State Sharding,
almost always implies sharding of processing and sharding of network band-
width.
Practically, under State Sharding, the nodes in each shard are building their
own blockchain that contains transactions affecting only the local part of the
global state that is assigned to that shard. Therefore, the validators in each
shard only need to store their local part of the global state and only execute
(and as such only relay) transactions that affect their part of the state. This
partition linearly reduces the requirement on all compute power, storage, and
network bandwidth, but introduces new problems, such as data availability and
cross-shard transactions, both of which we will cover below.
2 Our analysis of their approach can be found here: https://medium.com/nearprotocol/
8f9efae0ce3b
6
1.4 Cross-shard transactions
The sharding model we have described so far is not very useful, because if in-
dividual shards cannot communicate with each other, they are no better than
multiple independent blockchains that can’t efficiently communicate. The pro-
liferation of bridges in today’s multichain ecosystem is a clear signal that cross-
chain (or cross-shard) communication is crucial.
For now let’s only consider simple payment transactions, where each partici-
pant has an account on exactly one shard. If someone wishes to transfer money
from one account to another within the same shard, the transaction can be
processed entirely by the validators in that shard. If, however, Alice resides on
shard #1, and wants to send money to Bob who resides on shard #2, neither the
validators on shard #1 (who can’t credit Bob’s account) nor the validators on
shard #2 (who can’t debit Alice’s account) can process the entire transaction.
There are two families of approaches to cross-shard transactions:
7
Figure 2: Asynchronous cross-shard transactions
straightforward since the block structure and consensus are the same across
shards, and there’s a beacon chain that can be used for coordination. However,
in a sharded blockchain, all the shard chains are the same, while in the global
blockchain ecosystem there are lots of different blockchains, with different target
use cases, levels of decentralization, and privacy guarantees.
Building a system in which a set of chains have different properties but use
sufficiently similar consensus and block structure and have a common beacon
chain could enable an ecosystem of heterogeneous blockchains with a working
interoperability subsystem. Such a system is unlikely to feature validator rota-
tion, so some extra measures need to be taken to ensure security. Both Cosmos
and Polkadot are effectively these types of systems.4
8
will dive deeper into these probabilities in section 2.1). As discussed in section
1.4, cross-shard transactions involve certain state changes in multiple shards,
and the corresponding blocks in such shards that apply such state changes must
either all be finalized (i.e. appear in the selected chains on their corresponding
shards), or all be orphaned (i.e. not appear in the selected chains on their cor-
responding shards). Since in general the probability of shards being corrupted
is non-negligible, we can’t assume that forks won’t happen, even if byzantine
consensus had been reached among the shard validators, or if many blocks were
produced on top of the block with the state change.
This problem has multiple potential solutions, the most common being oc-
casional cross-linking of the latest shard chain block to the beacon chain. The
fork choice rule in the shard chains is then changed to always prefer the chain
that is cross-linked, and only apply the shard-specific fork-choice rule for blocks
published since the last cross-link.
9
the chain without it), every participant using the blockchain for any purpose
validates all the blocks, and will discard all blocks built on top of the invalid
block.
In figure 4 there are five validators, three of whom are malicious. They
created an invalid block A’, and then continued building new blocks on top
of it. Two honest validators have discarded A’ as invalid and built on top
of the last valid block known to them, creating a fork. Since there are fewer
validators in the honest fork, their chain is shorter. However, in a classic non-
sharded blockchain, every participant using the blockchain for any purpose is
responsible for validating all the blocks they receive and recomputing the state.
Thus any person with any interest in the blockchain would observe that A’ is
invalid, and thus also immediately discard B’, C’ and D’, taking the chain A-B
as the current longest valid chain.
In a sharded blockchain, however, no participant can validate all the trans-
actions on all the shards, so they need to have some way to confirm that at no
point in the history of any shard of the blockchain was an invalid block included.
Note that unlike with forks, cross-linking to the beacon chain is not a suf-
ficient solution for proving that the blockchain’s history contains no invalid
blocks, since the beacon chain doesn’t have the capacity to validate the blocks.
It can only validate that a sufficient number of validators in that shard signed
the block (and as such have attested to its correctness).
We will discuss solutions to this problem in section 2.2 below.
10
any participant needs to interact with a particular shard, they generally cannot
download and validate the entire history of the shard.
The partitioning aspect of sharding, however, raises a significant potential
problem: without downloading and validating the entire history of a particular
shard, a participant cannot be certain that the state they interact with is the
result of some valid sequence of blocks and that this sequence of blocks is indeed
the canonical chain in the shard. This is a problem that doesn’t exist in non-
sharded blockchains.
We will first present a simple solution to this problem that has been previ-
ously proposed by many protocols, and then analyze the way this solution can
break and the attempts that have been made to address it.
11
assumption built on top of the block which was the head of the canonical chain
before that. By induction the entire chain is valid, and since no set of validators
at any point produced forks, the naive assumption is also that the current chain
is the only chain in the shard. See figure 6 for a visualization.
This simple solution doesn’t work if we assume that validators can be cor-
rupted adaptively, which is not an unreasonable assumption.5 Adaptively cor-
rupting a single shard in a system with 1000 shards is significantly cheaper than
corrupting the entire system. Therefore, the security of the protocol decreases
linearly with the number of shards. To be certain about the validity of a block,
we must know that at any point in history no shard in the system has had a
colluding validator majority; taking into consideration adaptive adversaries, we
no longer have such certainty. As we discussed in section 1.5, colluding valida-
tors can produce two basic malicious behaviors: creating forks, and producing
invalid blocks.
Malicious forks can be addressed by blocks being cross-linked to the beacon
chain, which is generally designed to have significantly higher security than
the shard chains. Producing invalid blocks, however, is a significantly more
challenging problem to tackle.
12
block C (in a sense that the transactions in C are applied correctly) on top of B,
obfuscating the invalid block B, and initiates a cross-shard transaction to Shard
#2 that transfers those 1000 tokens to Bob’s account. From this moment, the
improperly created tokens reside on an otherwise completely valid blockchain in
Shard #2.
13
Figure 8: An invalid cross-shard transaction in a Chainweb-like system that
will get detected
Shard #2 is not only validating its own blockchain, but also the blockchains
of all the neighbors, including Shard #1. So if a malicious actor on Shard #1
attempts to create an invalid block B, then build block C on top of it and initiate
a cross-shard transaction, such a cross-shard transaction will not go through
since Shard #2 will have validated the entire history of Shard #1 which will
have caused it to identify invalid block B.
While corrupting a single shard is no longer a viable attack, corrupting a
few shards remains a potential problem. On figure 9 an adversary corrupting
both Shard #1 and Shard #2 successfully executes a cross-shard transaction to
Shard #3 with funds from an invalid block B by corrupting both shard #1 and
shard #2.
Shard #3 validates all the blocks in Shard #2, but not in Shard #1, and
has no way to detect the malicious block.
There are a few directions towards properly solving state validity: fraud
proofs, stateless validation, and cryptographic computation proofs.
2.3 Fishermen
The idea behind the first approach is the following: whenever a block header
is communicated between chains for any purpose (such as cross-linking to the
beacon chain, or a cross-shard transaction), there is a period of time during
which any honest validator can provide a proof that the block is invalid. There
are various constructions that enable very succinct proofs that the blocks are
invalid, so the communication overhead for the receiving nodes is much smaller
than that of receiving a full block.
With this approach, as long as there is at least one honest validator in the
shard, the system is secure.
14
Figure 9: An invalid cross-shard transaction in a Chainweb-like system that
will not get detected
1. The challenge period needs to be sufficiently long for the honest validator
to recognize a block was produced, download it, fully verify it, and prepare
the challenge (if the block is in fact invalid). Introducing such a period
would significantly slow down cross-shard transactions.
2. The existence of the challenge protocol creates a new vector of attack as
malicious nodes could spam the network with invalid challenges. An obvi-
15
ous solution to this problem is to make challengers deposit some amount
of tokens that are returned if the challenge is valid. This is only a par-
tial solution, as it might still be beneficial for the adversary to spam the
system with invalid challenges (and burn their deposits) — for example,
to prevent a valid challenge from an honest validator from going through.
These attacks are called Grieving Attacks.
3. The implementation is quite complex and difficult to test. The complexity
comes from two aspects: 1) making sure that a challenge can be properly
processed by validators who do not have the state of the shard and 2)
rolling back the state of the blockchain after a challenge is successfully
processed and slashing the offending validator. The first one is not easy,
as it involves building a lot of the machinery described in section 2.4. The
second one is even more difficult, especially in a sharded blockchain. When
an invalid state transition is found, we cannot just roll back the state of the
affected shard because it is possible for an invalid state transition to mint,
say, 1 billion native or fungible tokens, and send them to other shards.
As a result, the state of all shards needs to be rolled back simultaneously,
which leads to a multitude of problems, especially in regards to consensus
and validator assignment.
Testing challenges is even more daunting. This is a mechanism that is
never expected to trigger in practice yet is crucial to the design – if some-
one finds a vulnerability in the implementation, it is very likely that the
exploit could cause someone to lose money. This is likely the reason why
after quite a few years of the idea being proposed, there is still no full (i.e.,
permissionless) implementation of challenges in any blockchain at the time
of writing.7
submit them.
16
state witness to validate a block. State Witness refers to the state touched
during the execution of a chunk alonside with proof that they belong to the
state as of before the chunk. More specifically, assuming that a validator needs
to validate block h and it knows the prev state root sprev , i.e, state root as of
before block h, then a state witness is the state touched during the execution of
h and the proof that they indeed belong to the state specified by sprev . Using
the state witness, a validator can execute and verify blocks without maintaining
the state of that shard locally.
In order for stateless validation to work, some entity needs to be able to
provide state witness. Usually they are part of the overall validator set so that
they have an incentive to maintain the full state. It is worth noting that the state
witness provider, while necessary for the network to function, could not single-
handedly corrupt the network. This is because the state witness is validated
against the state root and even though a single node may be responsible for
producing the state witness, it has no way to produce an invalid state witness
without getting caught.
This property enables a design where most validators are lightweight and
only a few validators need to operate more expensive hardware. This is beneficial
for the decentralization of a blockchain network as it lowers the barrier to entry
to become a validator.
17
2. “Toxic waste” — zkSNARKs depend on a trusted setup in which a group
of people performs some computation and then discards the intermediate
values of that computation. If all the participants of the procedure collude
and keep the intermediate values, fake proofs can be created. zkSTARKs,
however, do not rely on the same underlying cryptographic primitives such
as KZG commitments and therefore do not suffer from the same problem;
3. Extra complexity introduced into the system design. While there are a
few zkEVM rollups in production today, they rely on a single sequencer
and a single prover, which greatly simplifies the design. How exactly ZK
proofs should be integrated into the design of a scalable blockchain is still
mostly a research topic at this point;
18
Figure 11: Merkle Tree
the money to you). Confident that the transaction is finalized, you provide the
service.
However, the validators never distributed the full content of block B to
anyone. As such, the honest validators of block C can’t retrieve the block, and
are either forced to stall the system or to build on top of A — and depriving
you, as a merchant, of money.
When we apply the same scenario to sharding, the definitions of full and
light node generally apply per shard: validators in each shard download every
block in that shard and validate every transaction in that shard, but other
nodes in the system, including those that snapshot shard chains’ state into the
19
beacon chain, only download the headers. Thus the validators in the shard are
effectively full nodes for that shard, while other participants in the system,
including the beacon chain, operate as light nodes.
For the fisherman approach we discussed above to work, honest validators
need to be able to download blocks that are cross-linked to the beacon chain.
If malicious validators cross-linked the header of an invalid block (or used it
to initiate a cross-shard transaction), but never distributed the block, honest
validators have no way to craft a challenge.
We will cover three complementary approaches to address this problem.
Figure 13: Validators need to download state and thus cannot be rotated
frequently
The problem with this naive approach is that it is impossible to prove later
whether the Notary was or was not able to download the block, so a Notary
can choose to always attest that they were able to download the block without
even attempting to retrieve it. One solution to this is for Notaries to provide
8 Here we assume stateless validation is not used, so validators all have to download the
state of a shard.
20
some evidence or to stake some amount of tokens attesting that the block was
downloaded. One such solution is discussed here: https://ethresear.ch/t/
1-bit-aggregation-friendly-custody-bonds/2236.
Both Polkadot and Ethereum Serenity have designs around this idea that
provide a way for light nodes to be reasonably confident that blocks are available.
The Ethereum Serenity approach has a detailed description in [2].
21
sign a relay chain block if they have their part of each parachain block that is
snapshotted to the relay chain block. Thus, if a relay chain block has signatures
from 2f + 1 validators, and as long as no more than f of them have violated
the protocol, each parachain block can be reconstructed by fetching the relevant
parts from the validators that followed the protocol. See figure 15.
22
3 Nightshade
3.1 From shard chains to shard chunks
The sharding model with shard chains and a beacon chain is very powerful but
comes with certain complexities. In particular, the fork choice rule needs to be
executed separately in each chain, so therefore the fork choice rule in the shard
chains and the beacon chain must be built differently and tested separately.
In Nightshade we model the system as a single blockchain, in which each
block logically contains all the transactions for all the shards, and changes the
whole state of all the shards. Physically, however, no participant downloads the
full state or the full logical block. Instead, each participant of the network either
maintains the state that corresponds to the shards that they validate transac-
tions for, or relies on others to provide them with state to validate transactions.
The list of all the transactions in the block is split into physical chunks (one
chunk per shard).
Under ideal conditions each block contains exactly one chunk per shard per
block, which roughly corresponds to the model with shard chains in which the
shard chains produce blocks with the same speed as the beacon chain. However,
some chunks might be missing due to network delays, so in practice each block
contains either one or zero chunks per shard. See section 3.3 for details on how
blocks are produced.
Figure 16: A model with shard chains on the left and with one chain with
blocks split into chunks on the right
3.2 Consensus
The two dominant approaches to consensus in blockchains today are longest (or
heaviest) chain, in which the chain that has the most work or stake used to
23
build it is considered canonical, and BFT, in which some set of validators reach
BFT consensus for each block.
In the recently proposed protocols, the latter is the dominant approach, since
it provides immediate finality. In the longest chain approach, more blocks need
to be built on top of a given block to ensure finality. For meaningful security,
the time it takes for a sufficient number of blocks to be built is often on the
order of hours.
Using BFT consensus on each block also has disadvantages, such as:
A hybrid model, in which the consensus used is some sort of heaviest chain
approach, but some blocks are periodically finalized using a BFT finality gadget,
maintains the advantages of both models. Such BFT finality gadgets are Casper
FFG [6], used in Ethereum 2.09 , Casper CBC, and GRANDPA used in Polkadot.
Nightshade uses heaviest-chain consensus. Specifically, when a block pro-
ducer produces a block (see section 3.3), they can collect signatures from other
block producers and validators, attesting to the previous block. The weight
of a block is then the cumulative stake of all the signers whose signatures are
included in the block. The weight of a chain is the sum of the block weights.
On top of heaviest-chain consensus, Nightshade uses a finality gadget that
uses attestations to finalize the blocks. To reduce the complexity of the system,
we use a finality gadget that doesn’t influence the fork-choice rule in any way.
Instead the gadget only introduces extra slashing conditions such that once a
9 Also see the whiteboard session with Justin Drake for an in-depth overview of Casper
24
block is finalized by the finality gadget, a fork is impossible, unless a very large
percentage of the total stake is slashed. The full details of NEAR’s consensus
algorithm can be found in the Doomslug paper.
25
this distinction is made to help provide more accurate context when we discuss
block production or chunk production.
Within an epoch, the block and chunk production schedule is determined by
a randomness seed generated at the beginning of the epoch and for each block
height, there is an assigned block producer. Similarly, for each height, there is
an assigned chunk producer for each shard.
Since chunk production, unlike block production, requires maintaining the
state, and for each shard only sw w/n block producers maintain the state per
shard, correspondingly only those sw w/n block producers are responsible for
producing chunks and associated state witnesses.
26
Figure 17: Each block contains one or zero chunks per shard, and each chunk
is erasure coded. Each part of the erasure coded chunk is sent to a designated
block producer via a special onepart message
producer since it is impossible to prove later that the block producer didn’t have
the onepart message.
To address the lazy block producer problem, each chunk producer when
creating a chunk must choose a color (red or blue) for each part of the future
encoded chunk, as well as store the bitmask of assigned color in the chunk
before it is encoded. Each onepart message then contains the color assigned to
the part, and the color is used when computing the Merkle root of the encoded
parts. If the chunk producer deviates from the protocol, it can be easily, since
either the Merkle root will not correspond to onepart messages, or the colors
in the onepart messages that correspond to the Merkle root will not match the
mask in the chunk.
When a block producer signs on a block, they include a bitmask of all the
red parts they received for the chunks included in the block. Publishing an
incorrect bitmask is a slashable behavior. If a block producer hasn’t received a
onepart message, they have no way of knowing the color of the message, and
thus have a 50% chance of being slashed if they attempt to blindly sign the
block.
27
of the Merkleized state after the chunk is applied.10 The state witness is then
shared with validators assigned to this shard for the next block so that they
could validate the chunk.
A validator only processes a block if:
3. For each chunk, it receives the full chunk if it is assigned to the corre-
sponding shard
Once the block is being processed, a validator applies the state transition
of the shard they are assigned to and checks whether the resulting state root
matches what is posted in the chunk header. If they match, the validator sends
an chunk endorsement to the next block producer. Block producers would
only include a chunk in a block if they have more than 2/3 endorsements from
validators assigned to the corresponding shard.
vious version where the previous state root instead of the post state root is stored in the
chunk headers, the implementation is changed to accommodate that. We describe the design
conceptually here to make it easier for readers to understand.
11 This is what Ethereum and many other blockchains use as of this writing.
28
3.6 Cross-shard transactions and receipts
If a transaction needs to affect more than one shard, it needs to be consecutively
executed in each shard separately. The full transaction is sent to the first shard
affected, and once the transaction is included in the chunk for such shard and
is applied after the chunk is included in a block, it generates a so-called receipt
transaction, which is routed to the next shard in which the transaction need to
be executed. If more steps are required, the execution of the receipt transaction
generates a new receipt transaction, and so on.
29
ceipts that they have collected for the shard in the onepart messages, as well as
all the transactions included in the chunk itself.
30
Figure 19: If all the receipts target the same shard, the shard may not have
the capacity to process them
Consider figure 20. By block B the shard 4 cannot process all the receipts,
so it only processes receipts originating from up to shard 3 in block A, and
records this. In block C the receipts up to shard 5 in block B are included, and
then by block D, the shard catches up, processing all the remaining receipts in
block B and all the receipts from block C.
31
3.7 Chunks validation
As mentioned in section 3.5, a chunk is validated by a set of stateless validators
assigned to the shard. In this section, we provide a more detailed analysis of the
security of the design. Again, we assume the existence of a on-chain randomness
beacon that can be used to generate random seeds with each block, which then
can be used to shuffle validators.
The core problem here can be formulated as follows: let’s assume a total
of n validators (with equal stake) where at most 1/3 of them are malicious.
There are s shards and k validators are assigned to each shard at every block,
so n = sk. What is the probability of a random assignment of validators to a
shard that results in a shard getting corrupted?
First, it is worth noting that a shard being corrupted means that more than
2/3 of validators assigned to that specific shard are malicious and can perform
an invalid state transition. If less than 2/3 of validators are malicious, it is
still possible for the chunk to be skipped due to insufficient endorsement, but
no invalid state transition is possible. In addition, since validators rotate every
single block, it is not possible for a shard to get indefinitely stalled due to a bad
assignment.
Now let’s consider the probability of a single shard getting corrupted. Let
p denote the probability of a randomly chosen validator being malicious (so
p ≤ 1/3). For a single shard to have l malicious validators, the probability is
hypergeometric distribution:
k n−k
l k−l
P (X = l) = n
k
Therefore, the probability of having more than 2/3 malicious validators for a
single shard is
k
P (X ≥ 2k/3) ≤ e−D(p+1/3||p)k = e− 3
by Chernoff bound.
For s shards, the probability that at least one shard is corrupted is
s
n
X
Pbad ≤ P (one shard is corrupted) ≤ se− 3s
i=1
When n and k are large, i.e, there are many validators assigned to the
n
same shard, the last term se− 3s can be made negligbly small. Our numeric
calculation based on multivariate hypergeometric distribution shows that with
800 validators and 4 shards, the probability of the networking getting corrupted
is roughtly 10−29 , which means that in expectation it takes 1029 blocks for the
system to fail. Assuming one block per second, that translates to 3 × 1021 years.
Therefore, while the security of the system is probabilistic, it is safe enough in
practice.
32
3.7.1 Zero-knowledge proofs
One problem that is often ignored in blockchain design is what happens when the
BFT assumption is violated, i.e, if there are more than 1/3 malicious validators
in a blockchain. Obviously the consensus could be broken and the network may
fail to produce blocks. More importantly, the malicious validators could poten-
tially attempt to create an invalid state transition. In a non-sharded blockchain,
doing this would not only require a higher total stake (2/3 of validators being
malicious), but also it is also very likely to be immediately noticed and a social
consensus may ensue to resolve the attack. This is because the likelihood of
every single validator being corrupted in a non-sharded blockchain is extremely
low; there may also be others running RPC nodes who would notice the invalid
state transition, as well.
In a sharded blockchain, however, things are a bit different. It is more likely
that the validators assigned to a shard could all be malicious and it is less likely
that many people would run nodes that track every shard. This gives rise to the
theoretical possibility of an invalid state transition attack occurring with few
or any participants noticing. The result would be disastrous: a billion tokens
could be minted out of thin air and moved to other shards and potentially even
be bridged to other blockchains.
This is where zero-knowledge proofs (ZKPs) can come in. Since ZKPs are
very cheap to verify, if there is a proof generated for the state transition of each
shard, then a very large number of people can independently verify the state
transition without having to rely on the signatures of validators to trust the
validity of a state transition. More specifically, every wallet could run a ZK full
client that verifies the state transition of the entire blockchain and be assured
that no invalid state transition has happened in the history of the blockchain.
ZKPs also enable a stateless validation design that is simpler and more
powerful than the one described in section 3.7. Instead of validators executing
the chunk using state witness and verifying the state transition, they could
verify one ZKP instead – which is much cheaper. The problem, however, is that
generating a ZKP takes a long time. So if we naively swap out state witness
with a ZKP, it won’t work due to the proof generation latency.
Recent advancements in separating consensus and execution [8] provide a
possible solution: blocks and chunks can be produced and optimistically ex-
ecuted while ZK proofs are being generated. Once a ZKP is generated, it is
submitted into a block for all validators to verify. Because ZKPs can be small
in size and quite cheap to verify13 , they can be included in the blocks directly
and all validators can validate the state transition of all shards14 . In this design,
the validators would be much more lightweight, given that they only need to
verify some ZKP. This means that the cost of operating a validator is going to
be much cheaper and that the validator set could be much larger, which further
improves the decentralization of the design.
13 While STARK proofs can be relatively large (a few hundred kilobytes), they can be
compressed again using a SNARK which would result in a proof of a few hundred bytes.
14 Alternatively, proofs from different shards can be aggregated into one ZKP for a block.
33
3.8 State Size
Sharding allows us to divide the state of the entire blockchain into those of
individual shards, which provides a solution to the state growth problem that
is becoming more and more imminent today. The stateless validation approach
makes the blockchain even more performant: there are relatively few chunk
producers and they can afford to operate more expensive hardware, which means
that for each individual shard, the state of the shard can be held in memory
when chunk producers produce and process chunks. Validators, on the other
hand, receive state witness, which is small in size, when they need to apply a
chunk and can also have the required state in memory. As a result, the state
reads and writes are very fast.
The cost of operating a chunk producer is not going to be exorbitantly high
either. If we limit the size of each shard to 50GB, then a chunk producer could
be operated with a 64GB RAM machine, which is fairly commonplace today.
This asymmetry between chunk producer and validator also works well when
state witness is replaced by ZKPs. Chunk producer could also operate a prover,
which requires more hardware resources and the validators can run on even
cheaper hardware due to the low cost of verifying ZKPs.
4 Conclusion
In this document, we discussed approaches to building sharded blockchains and
covered two major challenges that come with existing approaches, namely state
validity and data availability. We then presented Nightshade, a sharding design
that powers NEAR Protocol.
The design is a work in progress. If you have comments, questions, or
feedback on this document, please go to https://github.com/near/neps.
34
References
[1] Monica Quaintance Will Martino and Stuart Popejoy. Chainweb: A proof-
of-work parallel-chain architecture for massive throughput. 2018.
[2] Mustafa Al-Bassam, Alberto Sonnino, and Vitalik Buterin. Fraud proofs:
Maximising light client security and scaling blockchains with dishonest ma-
jorities. CoRR, abs/1809.09044, 2018.
[3] Songze Li, Mingchao Yu, Salman Avestimehr, Sreeram Kannan, and Pramod
Viswanath. Polyshard: Coded sharding achieves linearly scaling efficiency
and security simultaneously. CoRR, abs/1809.10361, 2018.
[4] Ittai Abraham, Guy Gueta, and Dahlia Malkhi. Hot-stuff the linear, optimal-
resilience, one-message BFT devil. CoRR, abs/1803.05069, 2018.
[5] Yossi Gilad, Rotem Hemo, Silvio Micali, Georgios Vlachos, and Nickolai
Zeldovich. Algorand: Scaling byzantine agreements for cryptocurrencies. In
Proceedings of the 26th Symposium on Operating Systems Principles, SOSP
’17, pages 51–68, New York, NY, USA, 2017. ACM.
[6] Vitalik Buterin and Virgil Griffith. Casper the friendly finality gadget.
CoRR, abs/1710.09437, 2017.
[7] Aniket Kate, Gregory Zaverucha, and Ian Goldberg. Constant-size commit-
ments to polynomials and their applications. pages 177–194, 12 2010.
[8] George Danezis, Eleftherios Kokoris-Kogias, Alberto Sonnino, and Alexan-
der Spiegelman. Narwhal and tusk: A dag-based mempool and efficient BFT
consensus. CoRR, abs/2105.11827, 2021.
35