Skip to content

Commit

Permalink
Rebuild examples
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Aug 15, 2024
1 parent a7ce0f0 commit 8de1a5d
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 253 deletions.
76 changes: 39 additions & 37 deletions packages/block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ Instantiation Example:
```ts
// ./examples/simple.ts

import { BlockHeader } from '@ethereumjs/block'
import { createBlockHeader } from '@ethereumjs/block'
import { bytesToHex } from '@ethereumjs/util'

const headerData = {
import type { HeaderData } from '@ethereumjs/block'

const headerData: HeaderData = {
number: 15,
parentHash: '0x6bfee7294bf44572b7266358e627f3c35105e1c3851f3de09e6d646f955725a7',
gasLimit: 8000000,
timestamp: 1562422144,
}
const header = BlockHeader.fromHeaderData(headerData)
const header = createBlockHeader(headerData)
console.log(`Created block header with hash=${bytesToHex(header.hash())}`)
```

Expand Down Expand Up @@ -77,11 +79,11 @@ This library supports the creation of [EIP-1559](https://eips.ethereum.org/EIPS/
```ts
// ./examples/1559.ts

import { Block } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })

const block = Block.fromBlockData(
const block = createBlock(
{
header: {
baseFeePerGas: BigInt(10),
Expand All @@ -98,7 +100,7 @@ console.log(Number(block.header.calcNextBaseFee())) // 11

// So for creating a block with a matching base fee in a certain
// chain context you can do:
const blockWithMatchingBaseFee = Block.fromBlockData(
const blockWithMatchingBaseFee = createBlock(
{
header: {
baseFeePerGas: block.header.calcNextBaseFee(),
Expand All @@ -121,12 +123,13 @@ Starting with the `v4.1.0` release there is support for [EIP-4895](https://eips.
```ts
// ./examples/withdrawals.ts

import { Block } from '@ethereumjs/block'
import { Common, Chain } from '@ethereumjs/common'
import { createBlock } from '@ethereumjs/block'
import { Common, Mainnet } from '@ethereumjs/common'
import { Address, hexToBytes } from '@ethereumjs/util'

import type { WithdrawalData } from '@ethereumjs/util'

const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })

const withdrawal = <WithdrawalData>{
index: BigInt(0),
Expand All @@ -135,7 +138,7 @@ const withdrawal = <WithdrawalData>{
amount: BigInt(1000),
}

const block = Block.fromBlockData(
const block = createBlock(
{
header: {
withdrawalsRoot: hexToBytes(
Expand Down Expand Up @@ -165,29 +168,29 @@ To create blocks which include blob transactions you have to active EIP-4844 in
```ts
// ./examples/4844.ts

import { Common, Chain, Hardfork } from '@ethereumjs/common'
import { Block } from '@ethereumjs/block'
import { BlobEIP4844Transaction } from '@ethereumjs/tx'
import { Address } from '@ethereumjs/util'
import { loadKZG } from 'kzg-wasm'
import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { create4844BlobTx } from '@ethereumjs/tx'
import { createAddressFromPrivateKey } from '@ethereumjs/util'
import { randomBytes } from 'crypto'
import { loadKZG } from 'kzg-wasm'

const main = async () => {
const kzg = await loadKZG()

const common = new Common({
chain: Chain.Mainnet,
chain: Mainnet,
hardfork: Hardfork.Cancun,
customCrypto: {
kzg,
},
})
const blobTx = BlobEIP4844Transaction.fromTxData(
{ blobsData: ['myFirstBlob'], to: Address.fromPrivateKey(randomBytes(32)) },
const blobTx = create4844BlobTx(
{ blobsData: ['myFirstBlob'], to: createAddressFromPrivateKey(randomBytes(32)) },
{ common },
)

const block = Block.fromBlockData(
const block = createBlock(
{
header: {
excessBlobGas: 0n,
Expand All @@ -207,7 +210,7 @@ const main = async () => {
)
}

main()
void main()
```

**Note:** Working with blob transactions needs a manual KZG library installation and global initialization, see [KZG Setup](https://github.com/ethereumjs/ethereumjs-monorepo/tree/master/packages/tx/README.md#kzg-setup) for instructions.
Expand Down Expand Up @@ -236,8 +239,7 @@ import {
const main = async () => {
const common = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.Cancun,
eips: [7685, 4788],
hardfork: Hardfork.Prague,
})

const depositRequestData = {
Expand All @@ -261,7 +263,7 @@ const main = async () => {
console.log(
`Instantiated block with ${
block.requests?.length
} request, requestTrieValid=${await block.requestsTrieIsValid()}`,
} deposit request, requestTrieValid=${await block.requestsTrieIsValid()}`,
)
}

Expand Down Expand Up @@ -389,15 +391,15 @@ An Ethash/PoW block can be instantiated as follows:
```ts
// ./examples/pow.ts

import { Block } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { createBlock } from '@ethereumjs/block'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'pow'
console.log(common.consensusAlgorithm()) // 'ethash'

Block.fromBlockData({}, { common })
createBlock({}, { common })
console.log(`Old Proof-of-Work block created`)
```

Expand All @@ -410,15 +412,15 @@ A clique block can be instantiated as follows:
```ts
// ./examples/clique.ts

import { Block } from '@ethereumjs/block'
import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { createBlock } from '@ethereumjs/block'
import { Common, Goerli, Hardfork } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Goerli, hardfork: Hardfork.Chainstart })
const common = new Common({ chain: Goerli, hardfork: Hardfork.Chainstart })

console.log(common.consensusType()) // 'poa'
console.log(common.consensusAlgorithm()) // 'clique'

Block.fromBlockData({ header: { extraData: new Uint8Array(97) } }, { common })
createBlock({ header: { extraData: new Uint8Array(97) } }, { common })
console.log(`Old Clique Proof-of-Authority block created`)
```

Expand Down Expand Up @@ -450,12 +452,12 @@ You can instantiate a Merge/PoS block like this:
```ts
// ./examples/pos.ts

import { Block } from '@ethereumjs/block'
import { Chain, Common } from '@ethereumjs/common'
import { createBlock } from '@ethereumjs/block'
import { Common, Mainnet } from '@ethereumjs/common'

const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })

const block = Block.fromBlockData(
const block = createBlock(
{
// Provide your block data here or use default values
},
Expand Down
27 changes: 14 additions & 13 deletions packages/blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ The following is an example to instantiate a simple Blockchain object, put block
```ts
// ./examples/simple.ts

import { Block } from '@ethereumjs/block'
import { Blockchain } from '@ethereumjs/blockchain'
import { Common, Hardfork } from '@ethereumjs/common'
import { createBlock } from '@ethereumjs/block'
import { createBlockchain } from '@ethereumjs/blockchain'
import { Common, Hardfork, Mainnet } from '@ethereumjs/common'
import { bytesToHex } from '@ethereumjs/util'

const main = async () => {
const common = new Common({ chain: 'mainnet', hardfork: Hardfork.London })
const common = new Common({ chain: Mainnet, hardfork: Hardfork.London })
// Use the safe static constructor which awaits the init method
const blockchain = await Blockchain.create({
const blockchain = await createBlockchain({
validateBlocks: false, // Skipping validation so we can make a simple chain without having to provide complete blocks
validateConsensus: false,
common,
})

// We use minimal data to provide a sequence of blocks (increasing number, difficulty, and then setting parent hash to previous block)
const block = Block.fromBlockData(
const block = createBlock(
{
header: {
number: 1n,
Expand All @@ -63,7 +63,7 @@ const main = async () => {
},
{ common, setHardfork: true },
)
const block2 = Block.fromBlockData(
const block2 = createBlock(
{
header: {
number: 2n,
Expand All @@ -87,7 +87,7 @@ const main = async () => {
// Block 1: 0xa1a061528d74ba81f560e1ebc4f29d6b58171fc13b72b876cdffe6e43b01bdc5
// Block 2: 0x5583be91cf9fb14f5dbeb03ad56e8cef19d1728f267c35a25ba5a355a528f602
}
main()
void main()
```

### Database Abstraction / Removed LevelDB Dependency
Expand Down Expand Up @@ -141,16 +141,17 @@ For many custom chains we might come across a genesis configuration, which can b
```ts
// ./examples/gethGenesis.ts

import { Blockchain } from '@ethereumjs/blockchain'
import { Common, parseGethGenesis } from '@ethereumjs/common'
import { createBlockchain } from '@ethereumjs/blockchain'
import { createCommonFromGethGenesis } from '@ethereumjs/common'
import { bytesToHex, parseGethGenesisState } from '@ethereumjs/util'

import gethGenesisJson from './genesisData/post-merge.json'

const main = async () => {
// Load geth genesis json file into lets say `gethGenesisJson`
const common = Common.fromGethGenesis(gethGenesisJson, { chain: 'customChain' })
const common = createCommonFromGethGenesis(gethGenesisJson, { chain: 'customChain' })
const genesisState = parseGethGenesisState(gethGenesisJson)
const blockchain = await Blockchain.create({
const blockchain = await createBlockchain({
genesisState,
common,
})
Expand All @@ -161,7 +162,7 @@ const main = async () => {
)
}

main()
void main()
```

The genesis block from the initialized `Blockchain` can be retrieved via the `Blockchain.genesisBlock` getter. For creating a genesis block from the params in `@ethereumjs/common`, the `createGenesisBlock(stateRoot: Buffer): Block` method can be used.
Expand Down
2 changes: 0 additions & 2 deletions packages/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ use the `chain` option to activate one of the custom chains passed or activate a
(e.g. `mainnet`) and switch to other chains - including the custom ones - by using `Common.setChain()`.

```ts
// ./examples/customChains.ts

import { Common } from '@ethereumjs/common'
import myCustomChain1 from './genesisData/testnet.json'
import myCustomChain2 from './genesisData/testnet2.json'
Expand Down
26 changes: 13 additions & 13 deletions packages/devp2p/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Create your peer table:
// examples/dpt.ts

import { DPT } from '@ethereumjs/devp2p'
import { bytesToHex, hexToBytes, randomBytes } from '@ethereumjs/util'
import { bytesToHex, hexToBytes } from '@ethereumjs/util'

const PRIVATE_KEY = hexToBytes('0xed6df2d4b7e82d105538e4a1279925a16a84e772243e80a561e1b201f2e78220')
const main = async () => {
Expand All @@ -77,10 +77,10 @@ const main = async () => {
})
console.log(`DPT is active and has id - ${bytesToHex(dpt.id!)}`)
// Should log the DPT's hex ID - 0xcd80bb7a768432302d267729c15da61d172373ea036...
await dpt.destroy()
dpt.destroy()
}

main()
void main()
```

Add some bootstrap nodes (or some custom nodes with `dpt.addPeer()`):
Expand Down Expand Up @@ -165,25 +165,25 @@ instance with the network you want to connect to and then create an `RLPx` objec
```ts
// ./examples/rlpx.ts

import { Chain, Common } from '@ethereumjs/common'
import { RLPx, ETH } from '@ethereumjs/devp2p'
import { Common, Mainnet } from '@ethereumjs/common'
import { ETH, RLPx } from '@ethereumjs/devp2p'
import { hexToBytes } from '@ethereumjs/util'

const main = async () => {
const common = new Common({ chain: Chain.Mainnet })
const common = new Common({ chain: Mainnet })
const PRIVATE_KEY = hexToBytes(
'0xed6df2d4b7e82d105538e4a1279925a16a84e772243e80a561e1b201f2e78220'
'0xed6df2d4b7e82d105538e4a1279925a16a84e772243e80a561e1b201f2e78220',
)
const rlpx = new RLPx(PRIVATE_KEY, {
maxPeers: 25,
capabilities: [ETH.eth65, ETH.eth64],
common,
})
console.log(`RLPx is active - ${rlpx._isAlive()}`)
await rlpx.destroy()
rlpx.destroy()
}

main()
void main()
```
### API
Expand Down Expand Up @@ -264,8 +264,8 @@ Wait for follow-up messages to arrive, send your responses.

eth.events.on('message', async (code: ETH.MESSAGE_CODES, payload: any) => {
// We keep track of how many of each message type are received
if (code in ETH.MESSAGE_CODES) {
requests.msgTypes[code] = code + 1
if (code in requests.msgTypes) {
requests.msgTypes[code]++
```
See the `peer-communication.ts` example for a more detailed use case.
Expand Down Expand Up @@ -333,7 +333,7 @@ les.sendStatus({
forkID: [hexToBytes('0x3b8e0691'), intToBytes(1)],
})

les.events.once('status', (status: LES.Status) => {
les.events.once('status', (status: devp2p.LES.Status) => {
const msg = [
Uint8Array.from([]),
[
Expand All @@ -351,7 +351,7 @@ Wait for follow-up messages to arrive, send your responses.
```ts
// ./examples/peer-communication-les.ts#L103-L105

les.events.on('message', async (code: LES.MESSAGE_CODES, payload: any) => {
les.events.on('message', async (code: devp2p.LES.MESSAGE_CODES, payload: any) => {
switch (code) {
case devp2p.LES.MESSAGE_CODES.BLOCK_HEADERS: {
```
Expand Down
Loading

0 comments on commit 8de1a5d

Please sign in to comment.