@ethereumjs/trie v6.0.0-rc.1
Pre-releaseThis is the release candidate (RC1) for the upcoming breaking releases on the various EthereumJS libraries. The associated release notes below are the main source of information on the changeset, also for the upcoming final releases, where we'll just provide change addition summaries + references to these RC1 notes.
At time of the RC1 releases there is/was no plan for a second RC round and breaking releases following relatively shorty (2-3 weeks) after the RC1 round. Things may change though depending on the feedback we'll receive.
Introduction
This round of breaking releases brings the EthereumJS libraries to the browser. Finally! 🤩
While you could use our libraries in the browser libraries before, there had been caveats.
WE HAVE ELIMINATED ALL OF THEM.
The largest two undertakings: First: we have rewritten all (half) of our API and elimited the usage of Node.js specific Buffer
all over the place and have rewritten with using Uint8Array
byte objects. Second: we went throuh our whole stack, rewrote imports and exports, replaced and updated dependencies all over and are now able to provide a hybrid CommonJS/ESM build, for all libraries. Both of these things are huge.
Together with some few other modifications this now allows to run each (maybe adding an asterisk for client and devp2p) of our libraries directly in the browser - more or less without any modifications - see the examples/browser.html
file in each package folder for an easy to set up example.
This is generally a big thing for Ethereum cause this brings the full Ethereum Execution Layer (EL) protocol stack to the browser in an easy accessible way for developers, for the first time ever! 🎉
This will allow for easy-to-setup browser applications both around the existing as well as the upcoming Ethereum EL protocol stack in the future. 🏄🏾♂️ We are beyond excitement to see what you guys will be building with this for "Browser-Ethereum". 🤓
Browser is not the only thing though why this release round is exciting: default Shanghai hardfork, full Cancun support, significantly smaller bundle sizes for various libraries, new database abstractions, a simpler to use EVM, API clean-ups throughout the whole stack. These are just the most prominent additional things here to mention which will make the developer heart beat a bit faster hopefully when you are scanning to the vast release notes for every of the 15 (!) releases! 🧑🏽💻
So: jump right in and enjoy. We can't wait to hear your feedback and see if you agree that these releases are as good as we think they are. 🙂 ❤️
The EthereumJS Team
New Trie Node Cache
There is a new permanent trie node cache which can be leveraged to make Trie operations significantly faster, see PR #2667. Since this also increases base-memory usage of a trie instantiation, this new cache is mainly intended to be used in rather long-lived trie scenarios.
The new cache can be activated by setting a fitting cache size with the new cacheSize
option (default: 0
(deactivated)).
Hybrid CJS/ESM Build
We now provide both a CommonJS and an ESM build for all our libraries. 🥳 This transition was a huge undertaking and should make the usage of our libraries in the browser a lot more straight-forward, see PR #2685, #2783, #2786, #2764, #2804 and #2809 (and others). We rewrote the whole set of imports and exports within the libraries, updated or completely removed a lot of dependencies along the way and removed the usage of all native Node.js primitives (like https
or util
).
There are now two different build directories in our dist
folder, being dist/cjs
for the CommonJS and dist/esm
for the ESM
build. That means that direct imports (which you generally should try to avoid, rather open an issue on your import needs), need an update within your code (do a dist
or the like code search).
Both builds have respective separate entrypoints in the distributed package.json
file.
A CommonJS import of our libraries can then be done like this:
const { Chain, Common } = require('@ethereumjs/common')
const common = new Common({ chain: Chain.Mainnet })
And this is how an ESM import looks like:
import { Chain, Common } from '@ethereumjs/common'
const common = new Common({ chain: Chain.Mainnet })
Using ESM will give you additional advantages over CJS beyond browser usage like static code analysis / Tree Shaking which CJS can not provide.
Side note: along this transition we also rewrote our whole test suite (yes!!!) to now work with Vitest instead of Tape
.
Buffer -> Uint8Array
With these releases we remove all Node.js specific Buffer
usages from our libraries and replace these with Uint8Array representations, which are available both in Node.js and the browser (Buffer
is a subclass of Uint8Array
). While this is a big step towards interoperability and browser compatibility of our libraries, this is also one of the most invasive operations we have ever done, see the huge changeset from PR #2566 and #2607. 😋
We nevertheless think this is very much worth it and we tried to make transition work as easy as possible.
How to upgrade?
For this library you should check if you use one of the following constructors, methods, constants or types and do a search and update input and/or output values or general usages and add conversion methods if necessary:
Trie.create() / new Trie() // root constructor option
Trie.root(value?: Uint8Array | null): Uint8Array
Trie.checkRoot(root: Uint8Array): Promise<boolean>
Trie.get(key: Uint8Array, throwIfMissing = false): Promise<Uint8Array | null>
Trie.put(key: Uint8Array, value: Uint8Array): Promise<void>
Trie.del(key: Uint8Array): Promise<void>
Trie.findPath(key: Uint8Array, throwIfMissing = false): Promise<Path>
Trie.walkTrie(root: Uint8Array, onFound: FoundNodeFunction): Promise<void>
Trie.lookupNode(node: Uint8Array | Uint8Array[]): Promise<TrieNode | null>
Trie.createProof(key: Uint8Array): Promise<Proof>
Trie.verifyProof()
Trie.createReadStream()
Trie.hash(msg: Uint8Array): Uint8Array
So basically the whole API. Lol. 😋
We have converted existing Buffer conversion methods to Uint8Array conversion methods in the @ethereumjs/util bytes
module, see the respective README section for guidance.
Prefixed Hex Strings as Default
The mixed usage of prefixed and unprefixed hex strings is a constant source of errors in byte-handling code bases.
We have therefore decided to go "prefixed" by default, see PR #2830 and #2845.
The hexToBytes
and bytesToHex
methods, also similar methods like intToHex
, now take 0x
-prefixed hex strings as input and output prefixed strings. The corresponding unprefixed methods are marked as deprecated
and usage should be avoided.
Please therefore check you code base on updating and ensure that values you are passing to constructors and methods are prefixed with a 0x
.
Other Changes
- Support for
Node.js 16
has been removed (minimal version:Node.js 18
), PR #2859 - Breaking:
DB
interface andMapDB
implementation have been moved to @ethereumjs/util (for re-usage), PR #2669 - Breaking: The
copy()
method has been renamed toshallowCopy()
(same underlying state DB), PR #2826