# References

https://blog.csdn.net/wo541075754/article/details/98847308

https://zhuanlan.zhihu.com/p/33865441

https://learnblockchain.cn/2019/04/08/genesis

# Installation

I’d like to install Ethereum from source, because it’s more easy and I can get the latest version.

According to the README.md, you may need to install Go (version 1.14 or later) and a C compiler first.

The output will be in go-ethereum/build/bin/ by default.

If you want to install in a different way, please refer to this.

# Build Private Chain by Geth

We can modify the configuration of our own chain so that it will be more easy for us to debug the smart contract deployed and to trace each transaction.

We need to create a new directory to be the main workspace, and we need to create genesis.json in this directory before building the first block.

genesis.json

Run the command below to initialize.

# How to Play with it

First we should notice that if we run the command like:

There will be error when we trying to unlock an account:

This is because in the new version of geth, to keep secure, the HTTP channel unlocking account is disabled by default. We can refer this issue from github.

If we just want to debug on our private chain, this may work (simply adding --allow-insecure-unlock):

## Send Transaction

We can try to create 2 accounts to make transaction.

Let’s presume that 0x5d31e74f0e52fe3fd58bd8079f84bc601a3f6d89 has some ethereum, so if he want to send his ethereum to another account, we should unlock first.

Now we can try to send trasaction.

Seems like it works! Let’s check their balance again:

It’s weird! Both balance don’t change at all!

Actually, the transaction do have been commited to the blockchain, but has not been handled:

To make the transaction been processed, it must be mined. So we start mining, and then wait for a block to be mined, then stop mining.

Done!

## Deploy a Contract

Sometimes we need to deploy a smart contract on our own chain in order to debug, and we need to check a certain transaction detail after the transacton is done. It’s convenient for us to use the APIs from web3py to do them all.

Installing web3py:

In the entire Ethereum network, the service we start through geth can be called a node, and rpc (which we have already included in the starting command) protocol can be used to send RPC operations to the node, and to receive data from the nodes. Using web3, it can be simply like this:

In this case only, we use http://127.0.0.1:7050 to connect to our node (just as the parameters --rpc --rpcaddr 127.0.0.1 --rpcport 7050 stated). And if the output is true, we have successfully connected to our node.

There are more RPC methods to connect, according to the circumstances, you can choose one the most suitable for yourself:

• IPCProvider
• HTTPProvider
• Auto check

And how can we deploy the contract on the node? Some APIs from web3py also work. First you need to compile your contract on Remix to get its bytecode and ABI. Using command solc in console to get them is also OK.

For instance, If we want to call a function from a contract, the python code should be like this (The solidity source code is from BalsnCTF 2019 Bank):

However if we try to run it, the program seems to be suspended. Remember what we talk about in the previous chapter? Only if there is at least one node minning, the transaction would be handled. So all we need is just to mine one block like this:

At last the execution result will be like this:

We can see that after the execution of BalsnCTFContract.constructor(), a contractAddress will be returned for users to locate the contract, and further more, we can use the address to call a function from the contract without deploying the contract again.