Skip to content

Local Node

eoliphan edited this page Mar 14, 2017 · 13 revisions

Each Ethereum client can be used easily as a single node private blockchain (run on localhost). This is very useful for testing or demonstrating decentralized applications and smart contracts.

Local Node on Private Blockchain

To set up a private blockchain (single machine) on your localhost you will need an Ethereum client installed; for this case we will use the Go-Ethereum client geth (installation instructions). From the command line or terminal prompt type:

Step 1: Genesis Block

If you are building a private blockchain you will need to define a genesis block. A genesis block sets the initial parameters and token distribution your private blockchain. This is fully customizable, but for simplicty please download pre-generated custom genesis block: http://ledgerlabs.io/files/customgenesis.json. Save this to the same directory where you will run the main geth commands.

Step 2: Deploy Private network

When you are ready to deploy your own custom blockchain, run the following command from the same directory as your custom genesis file (or specify the directory manually).

(A) Create a new account; only needs to be done once:

geth --datadir="~/test/" account new, and type in a new password.

(B) Instantiate and run the new network; the genesis command --genesis customgenesis.json only needs to run once for your private network:

geth --genesis customgenesis.json --datadir="~/test/" --networkid 23422 --rpc --rpccorsdomain="*" --rpcport="8545" --minerthreads="1" --mine --nodiscover --maxpeers=0 console

There are a few things to note about the options here.

  • --mine The mine function processes transactions and progagates smart contracts through the network. Since our network is a just a local computer, this function "mines" local blocks and adds transactions to its own blockchain. For testing purposes this is perfectly fine and desirable. Note: If you haven't run the --mine option yet, the system will have to create a DAG file. This will become evident when you run the command and the message reads "Generating DAG: X%"; this should only take a couple minutes, depending on your processing speed. Once you have the DAG file generated, it is stored on disk and does not need to be generated again when you restart geth.

  • --datadir="~/test/" can be set to any directory on your system. If you don't specify this option, or run geth without it, the system will use the default directory; if you are using your machine to sync the main ethereum network, then we highly recommend using a non-default directory for your test or private blockchain. If, however, your system is used only for testing, then the --datadir option can be omitted entirely.

  • --networkid This gives your private blockchain a unique numerical ID. You can make this number any integer you want. We recommend using a 5 digit random number. Note that network ID's 1 and 2 are reserved for the ethereum main-net and testnet respcetively.

  • --rpc enables the "remote procedure call" function. If you are running a local node and want your web interface to connect to it via the json rpc (web3) API, then this must be enabled.

  • --unlock 0 This unlocks the first account on the system. Important if the contract you deploy on this node needs to send funds from its main account.

  • --console This enables the javascript console.

###Smart Contracts / Decentralized Applications While the geth instance is running in a separate terminal window, you can call your private blockchain instance from your online compiler or deployed application. For compiling, you can use Cosmo.to or Realtime Solidity Compiler. When your application is deployed, you can point your web3.js instance to localhost:8545 directly. For an example, please download and deploy Meteor-Bolilerplate-dapp.

You can’t perform that action at this time.