Implements schema and functions related to Ethereum's block. |
---|
Note: this README
reflects the state of the library from v3.0.0
onwards. See README
from the standalone repository for an introduction on the last preceeding release.
npm install @ethereumjs/block
There are three static factories to instantiate a Block
:
Block.fromBlockData(blockData: BlockData = {}, opts?: BlockOptions)
Block.fromRLPSerializedBlock(serialized: Buffer, opts?: BlockOptions)
Block.fromValuesArray(values: BlockBuffer, opts?: BlockOptions)
For BlockHeader
instantiation analogue factory methods exists, see API docs linked below.
Instantiation Example:
import { BlockHeader } from '@ethereumjs/block'
const headerData = {
number: 15,
parentHash: '0x6bfee7294bf44572b7266358e627f3c35105e1c3851f3de09e6d646f955725a7',
difficulty: 131072,
gasLimit: 8000000,
timestamp: 1562422144,
}
const header = BlockHeader.fromHeaderData(headerData)
Properties of a Block
or BlockHeader
object are frozen with Object.freeze()
which gives you enhanced security and consistency properties when working with the instantiated object. This behavior can be modified using the freeze
option in the constructor if needed.
API Usage Example:
try {
await block.validate(blockchain)
// Block validation has passed
} catch (err) {
// handle errors appropriately
}
Tests in the tests
directory are partly outdated and testing is primarily done by running the BlockchainTests
from within the ethereumjs-vm package.
To avoid bloating this repository with ethereum/tests JSON files, we usually copy specific JSON files and wrap them with some metadata (source, date, commit hash). There's a helper to aid in that process and can be found at wrap-ethereum-test.sh.
See our organizational documentation for an introduction to EthereumJS
as well as information on current standards and best practices.
If you want to join for work or do improvements on the libraries have a look at our contribution guidelines.