Code Monkey home page Code Monkey logo

ethereumjs-common's Introduction

SYNOPSIS

NPM Package Build Status Actions Status Coverage Status JavaScript Style Guide Gitter

ETHEREUMJS-COMMON

Resources common to all Ethereum implementations

Succeeds the old ethereum/common library.

INSTALL

npm install ethereumjs-common

USAGE

All parameters can be accessed through the Common class which can be required through the main package and instantiated either with just the chain (e.g. 'mainnet') or the chain together with a specific hardfork provided.

Here are some simple usage examples:

const Common = require('ethereumjs-common')

// Instantiate with only the chain
let c = new Common('ropsten')
c.param('gasPrices', 'ecAddGas', 'byzantium') // 500

// Chain and hardfork provided
c = new Common('ropsten', 'byzantium')
c.param('pow', 'minerReward') // 3000000000000000000

// Access genesis data for Ropsten network
c.genesis().hash // 0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d

// Get bootstrap nodes for chain/network
c.bootstrapNodes() // Array with current nodes

It is encouraged to also explicitly set the supportedHardforks if the initializing library only supports a certain range of hardforks:

let c = new Common('ropsten', null, ['byzantium', 'constantinople', 'petersburg'])

This will e.g. throw an error when a param is requested for an unsupported hardfork and like this prevents unpredicted behaviour.

API

See the API documentation for a full list of functions for accessing specific chain and depending hardfork parameters. There are also additional helper functions like paramByBlock (topic, name, blockNumber) or hardforkIsActiveOnBlock (hardfork, blockNumber) to ease blockNumber based access to parameters.

Hardfork Params

Active Hardforks

There are currently parameter changes by the following past and future hardfork by the library supported:

  • chainstart
  • homestead
  • dao
  • tangerineWhistle
  • spuriousDragon
  • byzantium
  • constantinople
  • petersburg (aka constantinopleFix, apply together with constantinople)
  • istanbul
  • muirGlacier

Future Hardforks

The muirGlacier HF delaying the difficulty bomb and scheduled for January 2020 is supported by the library since v1.5.0.

Parameter Access

For hardfork-specific parameter access with the param() and paramByBlock() functions you can use the following topics:

  • gasConfig
  • gasPrices
  • vm
  • pow
  • casper
  • sharding

See one of the hardfork files like byzantium.json in the hardforks directory for an overview. For consistency, the chain start (chainstart) is considered an own hardfork.

The hardfork-specific json files only contain the deltas from chainstart and shouldn't be accessed directly until you have a specific reason for it.

Note: The list of gasPrices and gas price changes on hardforks is consistent but not complete, so there are currently gas price values missing (PRs welcome!).

Chain Params

Supported chains:

  • mainnet
  • ropsten
  • rinkeby
  • kovan
  • goerli (final configuration since v1.1.0)
  • Private/custom chain parameters

The following chain-specific parameters are provided:

  • name
  • chainId
  • networkId
  • genesis block header values
  • hardforks block numbers
  • bootstrapNodes list

To get an overview of the different parameters have a look at one of the chain-specifc files like mainnet.json in the chains directory, or to the Chain type in ./src/types.ts.

Working with private/custom chains

There are two ways to set up a common instance with parameters for a private/custom chain:

  1. You can pass a dictionary - conforming to the parameter format described above - with your custom values in the constructor or the setChain() method for the chain parameter.

  2. You can base your custom chain's config in a standard one, using the Common.forCustomChain method.

Bootstrap Nodes

There is no separate config file for bootstrap nodes like in the old ethereum-common library. Instead use the common.bootstrapNodes() function to get nodes for a specific chain/network.

Genesis States

Network-specific genesis files are located in the genesisStates folder.

Due to the large file sizes genesis states are not directly included in the index.js file but have to be accessed directly, e.g.:

const mainnetGenesisState = require('ethereumjs-common/dist/genesisStates/mainnet')

Or by accessing dynamically:

const genesisStates = require('ethereumjs-common/dist/genesisStates')
const mainnetGenesisState = genesisStates.genesisStateByName('mainnet')
const mainnetGenesisState = genesisStates.genesisStateById(1) // alternative via network Id

EthereumJS

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.

LICENSE

MIT

ethereumjs-common's People

Contributors

alcuadrado avatar alextsg avatar bit-warrior avatar evertonfraga avatar holgerd77 avatar ryanio avatar s1na avatar vpulim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ethereumjs-common's Issues

Updated pre-/post reentrency Constantinople support

As far as I can read out from both the notes from core developers meeting 53 as well as some covering media articles like this one on blockonomi.com there will now technically be two Constantinople hardforks, the current already happened on test nets including EIP-1283 and a second one again removing the feature. On mainnet there will only be the last one applied.

Technically this would mean two Constantinople files here in the src/hardforks directory. For naming I would prefer to keep constantinople for the latter one - since this will be the lasting one staying relevant in mid- and long term and choose something like constantinopleTmp for the testnet fixing one. In constantinopleTmp all parameters would have to be set to the values currently collected in the current constantinople file. The new constantinople file would then re-set the SSTORE values to its pre constantinopleTmp state.

For the different src/chains files the two (!) hardforks have to be added. There has to be special care regarding upstream libraries that this is done (and applied on upstream libraries like the VM) in a way that on mainnet on Constantinople switch all the values from constantinopleTmp are taken and then the value resetting (overwriting) in constantinople is also taking place. This can probably be achieved by adding both hardforks with the same block number, constantinopleTmp coming first. This has to be carefully tested if it really works on upstream though.

Before we continue on this I would like to have at least one confirmation that my analysis is correct.

forCustomChain is not a function

Hi,

I'm following this example https://github.com/ethereumjs/ethereumjs-tx/blob/9a6324f64f4da1cb550a3eec4eaef95da4ab441b/examples/custom-chain-tx.ts to try to get a tx for a private network.

My code is:
const Tx = require('ethereumjs-tx').Transaction; const Common = require('ethereumjs-common') const customCommon = Common.forCustomChain( 'mainnet', { name: 'my-network', networkId: 8995, chainId: 8995, }, 'petersburg', )

If I try a console.log(Common) forCustomChain seems to be a function but when trying to execute the js file it shows an error forCustomChain is not a function...

Any help is appreciated

Guillermo

[Istanbul] EIP-2200: Rebalance net-metered SSTORE gas cost with consideration of SLOAD gas cost change

Part of ethereumjs/ethereumjs-monorepo#501

Follow-up EIP to the failed Constantinople SSTORE EIP, see also #27 . There need to be some update to the price params respectively new params added, eventually named along the lines of PR #27 .

Should likely be addressed together with #61 in one combined PR (or at least released).

Update: Since EIP-2200 is still in heavy discussion it might actually be also make sense to handle and release separately.

TypeError: Common is not a constructor

const Common = require('ethereumjs-common');

// Instantiate with only the chain
let c = new Common('ropsten');

This keeps giving me this error:

let c = new Common('ropsten');
        ^

TypeError: Common is not a constructor

This library is too restrictive for testing networks

I'm planning to upgrade ethereumjs-tx in Buidler, and found that it may not be possible to do that if I want to enable EIP155.

The problem is that testing networks normally use non-standard chain ids (e.g. Ganache). If I want to sign a tx locally, I should do something like:

const { Transaction } = require("ethereumjs-tx");
new Transaction({ ... }, { chain: 123 });

But this results in this error:

Error: Chain with ID 123 not supported
    at Common.setChain (/private/tmp/tcommon/node_modules/ethereumjs-common/dist/index.js:35:23)
    at new Common (/private/tmp/tcommon/node_modules/ethereumjs-common/dist/index.js:16:34)
    at new Transaction (/private/tmp/tcommon/node_modules/ethereumjs-tx/dist/transaction.js:65:28)
    at Object.<anonymous> (/private/tmp/tcommon/index.js:2:1)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)

Taking a look at setChain I noticed that it's impossible to use a non-standard chain id.

The only workaround I see is by extending this class or creating a new one altogether, but it doesn't feel right for users to have to do this.

I'd LOVE to have an option to create a chain with the same params than a standard one, except for the genesis state and the chain id.

Support for new Goerli testnet

Goerli is a new - currently still experimental - cross-client PoA network, see https://github.com/goerli/testnet for information on network setup, genesis information and bootnodes.

Along with Goerli setup there is also some reactivation and new discussion here ethereum/EIPs#1085 around a common genesis file format. We should actively engage, integrate stuff here (minimally e.g. the terminology) which is settled and contribute with ideas where there is still need for discussion.

Integration here would be a separate issue, generally Goerli support and genesis file format integration is complementary.

Account addresses in genesisStates should be prefixed with '0x'

Many of the addresses in the alloc field in the genesisStates json files aren't prefixed with 0x. This causes state roots computed from these files to be incorrect when using ethereumjs/merkle-patricia-tree to load these states. The reason for this is that the Trie.prototype.put function https://github.com/ethereumjs/merkle-patricia-tree/blob/master/src/baseTrie.js#L87) calls ethUtil.toBuffer() on the account address which treats the address as a plain string instead of a hex string if it's not prefix with a 0x.

As a fix, we can either prefix all of the addresses with a 0x in the alloc field of each genesisStates file or modify genesisStates/index.js to programmatically add a '0x' prefix if its missing.

Add Kovan hardfork blocks

When I set up this library I left out the Kovan hardfork blocks - see here - simply because I didn't find them online on first sight.

These should be added, shouldn't be such a thing, eventually grabbable by some Parity Ethereum code search or something.

Remove hybridCasper hardfork draft

There is still some hybridCasper hardfork file along with some references both in code and docs as a left over from some time where there was some kind of a linear hardfork roadmap. ๐Ÿ˜„

This should be removed.

[Istanbul] EIP-1884: Trie-size-dependent Opcode Repricing > Price Parameters

Part of ethereumjs/ethereumjs-monorepo#501

EIP-1884 introduces price changes for trie-size-dependent opcode to better balance the network along growth, as well as a new opcode SELFBALANCE.

The new price parameters have to be added to the Common library for the Istanbul HF.

Should likely be addressed together with #62 in one combined PR (or at least released).

Update: Since EIP-2200 is still in heavy discussion it might actually be also make sense to handle and release separately.

Add support for private networks

In order to support private networks, we should allow an instance of Common to be created from a chain spec and/or a geth genesis file. Any of the following would be user-friendly ways to construct a Common instance from a spec file:

const common = Common.fromSpec('./genesis.json')
const common = Common.fromJson('./genesis.json')
const spec = require('./genesis.json')
const common = new Common(spec) // detect first argument is an Object instead of string

We could also consider migrating the json definition files in the chains/ folder to the chain spec format for consistency and future-proofing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.