forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildBlock.ts
42 lines (36 loc) · 1.45 KB
/
buildBlock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { createBlock } from '@ethereumjs/block'
import { Common, Mainnet } from '@ethereumjs/common'
import { createLegacyTx } from '@ethereumjs/tx'
import { Account, bytesToHex, createAddressFromPrivateKey, hexToBytes } from '@ethereumjs/util'
import { buildBlock, createVM } from '@ethereumjs/vm'
const main = async () => {
const common = new Common({ chain: Mainnet })
const vm = await createVM({ common })
const parentBlock = createBlock(
{ header: { number: 1n } },
{ skipConsensusFormatValidation: true },
)
const headerData = {
number: 2n,
}
const blockBuilder = await buildBlock(vm, {
parentBlock, // the parent @ethereumjs/block Block
headerData, // header values for the new block
blockOpts: {
calcDifficultyFromHeader: parentBlock.header,
freeze: false,
skipConsensusFormatValidation: true,
putBlockIntoBlockchain: false,
},
})
const pk = hexToBytes('0x26f81cbcffd3d23eace0bb4eac5274bb2f576d310ee85318b5428bf9a71fc89a')
const address = createAddressFromPrivateKey(pk)
const account = new Account(0n, 0xfffffffffn)
await vm.stateManager.putAccount(address, account) // create a sending account and give it a big balance
const tx = createLegacyTx({ gasLimit: 0xffffff, gasPrice: 75n }).sign(pk)
await blockBuilder.addTransaction(tx)
// Add more transactions
const block = await blockBuilder.build()
console.log(`Built a block with hash ${bytesToHex(block.hash())}`)
}
void main()