This repository guides the users
- Create a single-node Ethereum test network with no connections to any external peers.
- Convert a local Geth into a testnet and deploy a simple smart contract written using the Remix online integrated development environment (IDE).
Note : Starting Geth in developer mode does the following:
- Initializes the data directory with a testing genesis block
- Sets max peers to 0 (meaning Geth does not search for peers)
- Turns off discovery by other nodes (meaning the node is invisible to other nodes)
- Sets the gas price to 0 (no cost to send transactions)
- Uses the Clique proof-of-authority consensus engine which allows blocks to be mined as-needed without excessive CPU and memory consumption
- Uses on-demand block generation, producing blocks when transactions are waiting to be mined
Note : To setup Geth - Refer the steps
On the Terminal type :
geth --dev --http --http.api eth,web3,net --http.corsdomain "http://remix.ethereum.org"
- The terminal will display the following logs, confirming Geth has started successfully in developer mode:
- Open a new terminal, and create a folder for the new node :
mkdir geth_node
clef newaccount --keystore geth_node
Note :
- When prompted type the password.
- For quick reference, note it. Ex. gethnode123
- It generates the Public key and stores the secret key in the folder, geth_node
geth attach /tmp/geth.ipc
1. To display the existing accounts
eth.accounts
Note:
- This returns an array containing a single address, eventhough no accounts having yet been explicitly created.
- This is the coinbase account.
- The coinbase address is the recipient of the total amount of ether created at the local network genesis.
eth.coinbase==eth.accounts[0]
2. Querying the ether balance of the coinbase account
eth.getBalance(eth.coinbase)/1e18
eth.getBalance(eth.coinbase)
web3.fromWei(eth.getBalance(eth.coinbase))
- The return value is in units of Wei, which is divided by 118 to give units of ether.
- This can be done explicitly or by calling the web3.FromWei() function
3. Query the ehther balance of the new account created via clef in step 2
eth.getBalance("0x9Ec0a126c014C31456384746d83071F19925aAA2")
4. Send funds from coinbase to the new account created via clef
eth.sendTransaction({from: eth.coinbase, to: "0x9Ec0a126c014C31456384746d83071F19925aAA2", value: web3.toWei(50, "ether")})
- If successful, transaction hash is generated as a repsonse.
- After the funds transfer check the balance of coinbase and the receipient account.
5. To check the transaction details
eth.getTransaction("0x19ec8c7da780b5492c1cca65408f24f1ad8b5a38114a0a5c862e47e875f6b6b8")
- Here, 0x19ec8c7da780b5492c1cca65408f24f1ad8b5a38114a0a5c862e47e875f6b6b8 is the hash generated when the transaction was submitted.
- Now go to the first terminal, where our geth is running in dev mode.
- Here, we can see that the first block is mined.
Tutorial - 2 : Convert a local Geth into a testnet and deploy a simple smart contract written using the Remix
- Create a folder named, geth_node for this tutorial
geth --http --http.corsdomain="https://remix.ethereum.org" --http.api web3,eth,debug,personal,net --vmdebug --datadir geth_node --dev console
- The terminal will display the following logs, confirming Geth has started successfully in developer mode.
- The node is waiting for the transaction to mine the block
1. Open the Storage.sol file available at Remix IDE
2. Compile and deploy using Remix IDE
- Click the green play above the contract file
- Click the Deploy menu in Remix IDE.
- In the drop-down menu labelled ENVIRONMENT, select Custom - External Http Provider.
- This will open an information pop-up with instructions for configuring Geth
- Click OK.
- The ACCOUNT field should automatically populate with the address of the account created earlier using the Geth Javascript console.
- This account is same as the one displayed on the Javascript Console
- To deploy Storage.sol, click DEPLOY.
- A transaction occurs and block is mined
- The following logs in the Geth terminal confirm that the contract was successfully deployed.
- To fetch the transaction details from the terminal
3. Interact with contract using Remix
- The contract is now deployed on a local testnet version of the Ethereum blockchain.
- This means there is a contract address that contains executable bytecode that can be invoked by sending transactions with instructions
- After deployment, the Deployed Contracts tab in the sidebar automatically populates with the public functions exposed by Storage.sol.
- To send a value to the contract storage, type a number in the field adjacent to the store button, then click the button. Transaction is submitted and block is mined
Note :
- The from address is the account that sent the transaction,
- the to address is the deployment address of the contract.
- The value entered into Remix is now in storage at that contract address.
- Check the transaction mined and added to the block from the JavaScript Console
- To retrive value, click the button - retrieve
- Alternatively, it can be retrieved using web3.getStorageAt using the Geth Javascript console.
web3.eth.getStorageAt("0xe5c5aaa604c12273b92d8e48d133158abbf44a5f", 0)
- The following command returns the value in the contract storage
- Here, 0xe5c5aaa604c12273b92d8e48d133158abbf44a5f (replace the given address of the receipient in the Geth logs).
- Its the storage address
- Here, 0x000000000000000000000000000000000000000000000000000000000000002d is the hexadecimal value for 45 with which the number was reinitialized.
Note : This tutorial used an ephemeral blockchain that is completely destroyed and started afresh during each dev-mode session.