Skip to content

Commit

Permalink
✨ Feat: Add deepCopy to BaseClient (#1312)
Browse files Browse the repository at this point in the history
## Description

_Concise description of proposed changes_

## Testing

Explain the quality checks that have been done on the code changes

## Additional Information

- [ ] I read the [contributing docs](../docs/contributing.md) (if this
is your first contribution)

Your ENS/address:



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added a `deepCopy` method to various classes, allowing users to create
deep copies of client states for enhanced flexibility and functionality.

- **Documentation**
- Expanded documentation to include details on deep copying clients,
forking networks with fork transport, and the required JSON-RPC methods
for Tevm functionality.
- Updated line number references and method signatures for better
clarity and accuracy in the documentation.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: William Cory <[email protected]>
  • Loading branch information
roninjin10 and William Cory authored Jul 13, 2024
1 parent 3698987 commit a8c810b
Show file tree
Hide file tree
Showing 44 changed files with 594 additions and 277 deletions.
7 changes: 7 additions & 0 deletions .changeset/slow-trainers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@tevm/receipt-manager": minor
"@tevm/base-client": minor
"@tevm/txpool": minor
---

Add deepCopy method to BaseClient ReceiptManager and TxPool
2 changes: 2 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"node_modules",
"package.json",
".vercel",
".tevm",
"**/.tevm",
"**/.vercel",
"**/package.json",
"**/coverage",
Expand Down
49 changes: 40 additions & 9 deletions docs/src/content/docs/learn/clients/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,38 @@ memoryClient
.then(console.log);
```

### Forking a Network
### Forking a Network with fork Transport

Viem clients themselves are EIP-1193 providers and can be used as the fork transport. These options are available for both `createMemoryClient` and `createTevmTransport`. Once you fork, the forked block is saved under block tag `fork`, and any blocks mined higher than the forked block will not be included in the forked chain.
Tevm can fork a network via being provided an [transport](https://viem.sh/docs/clients/transports/http) with an [EIP-1193 compatabile request function](https://eips.ethereum.org/EIPS/eip-1193).

```typescript
import { createMemoryClient, http } from "tevm";
import { optimism } from "tevm/common";

const client = createMemoryClient({
// fork optimism at blockNumber 420n
fork: { transport: http("https://mainnet.optimism.io")(), blockTag: 420n },
common: optimism,
});

console.log(await client.getBlockNumber()); // 420n
```

For most of Tevm functionality to work, the forked network must support the following JSON-RPC methods:

- eth_blockNumber
- eth_getStorageAt
- eth_getProof
- eth_getBlockByNumber
- eth_getCode

Nearly all nodes do support these methods so using Tevm is a good way to get access to other methods like eth_debugTraceTransaction if your RPC node otherwise doesn't support it. Just fork the RPC node with tevm and execute the RPC method locally.

### Forking another client

Any client with an EIP-1559 `request` function works so this includes all viem clients, and all tevm clients and more.

Forking your own clients could be useful depending on use case:

```typescript
import { createMemoryClient, http } from "tevm";
Expand All @@ -99,15 +128,17 @@ const forkedClient = createMemoryClient({
forkedClient.getBlockNumber().then(console.log);
```

For most of Tevm functionality to work, the forked network must support the following JSON-RPC methods:
### Deep Copying a client

- eth_blockNumber
- eth_getStorageAt
- eth_getProof
- eth_getBlockByNumber
- eth_getCode
For the vast majority of use cases you will want to fork rather than clone because forking is much more performant. But if your use case requires it such as needing to copy the mempool you can also `deepCopy()` clients.

Nearly all nodes do support these methods so using Tevm is a good way to get access to other methods like eth_debugTraceTransaction if your RPC node otherwise doesn't support it. Just fork the RPC node with tevm and execute the RPC method locally.
```typescript
import { createMemoryClient } from "tevm";

const client = createMemoryClient();

const copy = await client.deepCopy();
```

### Use Cases

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a8c810b

Please sign in to comment.