I want to fork the solana mainnet to test my programs on it. Is it possible to fork solana mainnet like how ganache or hardhat lets you fork the ethereum mainnet?
if yes, how to?
One can fork software or repositories. Forking a network of nodes (such as Solana MainnetBeta or Ethereum Mainnet) is a concept I've never heard of.
You can start your own network of nodes anytime. E.g. using a "network" of one node using solana-test-validator
.
You can also create a network of nodes, each running a fork of the Solana validator software. See source code on github.
Find info about how to run a Solana validator in the Solana Docs. Just running solana-validator help
will also take you a long way.
This new network will - by definition - not be MainnetBeta. There is only one MainnetBeta, that's the whole point of a Mainnet.
You can create your own networks, either with an exact copy of the Solana validator software, or using a fork with any changes you like.
However, it won't be Solana Mainnet - you could call it Solana GajeshNet, though. Or Gajana Mainnet.
I think 'partial clone' is a better word than 'fork' in the context of Solana. The solana-test-validator
has a few interesting flags that help you create partial clones of mainnet:
--clone <address>
will fetch and load the corresponding account from the passed in cluster (with -u mainnet-beta
or -u <rpc url>
) in the test validator. It can be passed in multiple times. Consider --maybe-clone
also if the account may not exist.
--bpf-program <address>
will fetch and load the corresponding program from the provided cluster.
--account <filename>
will load from disk a particular account state, which could have been dumped using solana account
from mainnet or otherwise.
You can easily load a test validator with dozens or hundreds of accounts cloned from mainnet by using these flags. Some important things to note:;
If you don't have a private RPC node, you may be rate limited if you have too many accounts. Perhaps first 'dump' the accounts then load them from file using --account
?
It is generally impractical to load the full network state in a clone because of the sheer size of state. So yes some homework needed to use this method, eg identifying the underlying accounts that you need.
Because you can load any account in this state, if you use the --account <filename>
version, you can also modify the state of accounts before you load them. Perhaps you'd like to make yourself an authority over something in your cloned version? Easily done...
Anchor directly supports these test validator commands in its Anchor.toml / Test.toml files so your Anchor tests can include cloned state.