bob icon indicating copy to clipboard operation
bob copied to clipboard

Hardhat/anvil for Bitcoin local development

Open nud3l opened this issue 2 years ago • 5 comments

Is your feature request related to a problem? Please describe. Local development/regtest is difficult and often impractical, especially when a local Bitcoin node is required. It requires good knowledge of Bitcoin core (which is good to have) but:

  1. There's an overwhelming number of options to start a Bitcoin node
  2. It's not possible to fork Bitcoin mainnet or testnet into a local devnet for testing
  3. Interacting with regtest is not very ergonomical via bitcoin-cli

Describe the solution you'd like We need an easy-to-use tool similar to bitcoind -regtest with additional functionality. It does not need to provide consensus compatibility, but should make it easier to replicate Bitcoin "quirks" for testing. For example with regtest we cannot emulate chain forks to test light client (SPV) logic, it is also cumbersome to auto-mine blocks and bitcoind does not allow us to "fork" from some pre-existing state. The tool should be similar to anvil from the Foundry suite and should provide Bitcoin RPC compatibility so we can re-use existing tools such as Electrs.

Describe alternatives you've considered We have been using regtest for quite some time, right now nothing exists which satisfies these requirements.

Additional context We should use rust-bitcoin and existing Bitcoin libraries as much as possible. We may need to provide additional tooling to support forking since we need to load state over HTTP(S), it may be possible with Electrs but requires further experimentation.

nud3l avatar Oct 23 '23 08:10 nud3l

Here are some of the advantages of working with Anvil on Ethereum, I've extracted those which could also make sense for Bitcoin.

The toolkit is quite configurable and has several options for startup:

  • Test accounts generated and pre-funded
  • Configurable dev account balances
  • Can enable and configure interval mining (otherwise on demand)
  • Change mempool sorting logic
  • Set timestamp of genesis block
  • Use a specific hard fork or load state from a remote URL (and block number)
  • Configure gas limit, price, etc.

It also provides a compatible ETH RPC interface and some additional methods:

  • Impersonate accounts (force send transactions from an externally owned account)
  • Drop transactions from the mempool
  • Set balances and gas prices
  • Dump or load chain state
  • Revert to an earlier height (or reset completely)

The docs here list many more configurations and methods.

gregdhill avatar Oct 24 '23 12:10 gregdhill

The Bitcoin Dev Kit (BDK) provides a variety of tooling, for instance we can integrate the wallet functionality - signing and coin selection.

gregdhill avatar Oct 25 '23 10:10 gregdhill

Instead of writing our own tool from scratch we want to:

  1. Use https://github.com/vulpemventures/nigiri/ for bitcoin regtest setup
  2. Fork electrs to add ordinals and brc20 functions, see #55
  3. Add our electrs fork to nigiri
  4. Create a hackathon/simple starter template that consists of nigiri and foundry as toolchains and includes an automatic deployment of BOB smart contracts to be used in local test environments

nud3l avatar Oct 26 '23 01:10 nud3l

@gregdhill do you think we should close this issue and create a new one?

nud3l avatar Oct 26 '23 07:10 nud3l

I would be inclined to leave this open for now and get feedback from teams working directly with regtest so we can get a better idea of the pain points to determine if a more grandiose tool is required. We can open a separate issue to track the setup tooling you described above.

gregdhill avatar Oct 31 '23 07:10 gregdhill