Build and install Go-Ethereum

Go language development environment

First make sure the Go development environment is installed on your machine:

$ go versionGo version go1.15 Windows/amd64Copy the code

Download the go-Ethereum source code

git clone [email protected]:ethereum/go-ethereum.git
Copy the code

Switch to stable branch:

Git checkout v1.10.2Copy the code

Compile the installation

Go to the code directory CD go-Ethereum and execute the compile:

$ make all env GO111MODULE=on go run build/ci.go install ... .Copy the code

For Windows, you can install mingw32-make at mingw-w64.org/ and compile it:

$ mingw32-make all env GO111MODULE=on go run build/ci.go install ... .Copy the code

Or run the Go installation script directly:

go run build/ci.go install
Copy the code

Tips: This article is done in a Windows 10 environment

After compiling, you can view the executable file in the build/bin directory of the source code:

$ cd build/bin/ && ls

abidump.exe  
abigen.exe  
bootnode.exe  
checkpoint-admin.exe  
clef.exe  
devp2p.exe  
ethkey.exe  
evm.exe  
faucet.exe  
geth.exe  
p2psim.exe  
puppeth.exe  
rlpdump.exe
...
Copy the code

View geTH version information:

$. / geth. Exe version geth version: 1.10.2 - stable Git Commit: 97 d11b0187b4695ccf44e3b71b54155fe405a36f Architecture: Amd64 Go Version: Go1.15 Operating System: Windows...Copy the code

A: congratulations! We have completed the installation of Go-Ethereum

Use Clef to manage Ethereum accounts

In the previous part, we have compiled and installed clef, a go-Ethereum helper for managing accounts, creating accounts and completing signatures:

$ ./clef.exe h NAME: Clef - Manage Ethereum account operations Copyright 2013-2021 The go-ethereum Authors USAGE: clef.exe [options] command [command options] [arguments...]  COMMANDS: init Initialize the signer, generate secret storage attest Attest that a js-file is to be used setpw Store a credential for a keystore file delpw Remove a credential for a keystore file newaccount Create a new account gendoc Generate documentation about json-rpc format help, h Shows a list of commands or help for one command FLAGS OPTIONS: --loglevel value ... .Copy the code

Initialize the Clef

The initialization script creates a master key that requires a password of 10 or more characters to encrypt:

$ ./clef.exe init

...
Enter 'ok' to proceed:
> ok
...

Copy the code

Create account

When creating an account, you also need to provide a password of 10 or more characters for encryption:

$ ./clef.exe newaccount

...
Enter 'ok' to proceed:
> ok
...

## New account password

Please enter a password for the new account to be created (attempt 0 of 3)
...
Generated account 0x46e7328724A7...
Copy the code

After creating the account, you can view the account file in the default keystore directory. The following uses Windows as an example:

$ls - la ~ / AppData/Local/Ethereum/keystore UTC T07-2021-05-07-02-47.142238200 - Z - 46 e7328724a7...Copy the code

File content example:

{ "address": "46e7328724a7..." , "crypto": { ... }, "id": "..." , "version": 3 }Copy the code

Tips: You can use the same method to create multiple accounts, and you can pre-allocate the balance to these accounts when configuring the Genesis block

Ethereum node

For the sake of demonstration, we’ll launch a local private Ethereum network with only one node

The foundation block

The genesis block is configured with genesis.json, in which the alloc configuration item can set the pre-allocated account balance. It is recommended to use the accounts created by part Clef. After the node is started, there will be 10080 ETH balance on these accounts, which is convenient for development and debugging:

{" config ": {" chainId" : 1337, / / generally agreed local private network chianid = 1337 "homesteadBlock" : 0, / / homesteadBlock block height, private network only! For example: 0 "eip150Block": 0, // Ditto "eip155Block": 0, // ditto "eip158Block": 0, // ditto "byzantiumBlock": 0, // Ditto "byzantiumBlock": 0, // ditto "constantinopleBlock": 0, // ditto "petersburgBlock": 0, // ditto "istanbulBlock":0, // ditto "muirGlacierBlock":0, // ditto "berlinBlock":0 // Ditto // Note: Invalid sender}, "alloc": invalid sender}, "alloc": invalid sender}, "alloc": invalid sender}, "alloc": { "248672bac919..." :{"balance":"10080000000000000000000"}, // Note unit 1 eth = 10^18 wei "46e7328724a7..." :{"balance":"10080000000000000000000"} }, "nonce": "0x0000000000000042", "difficulty": "0x020000", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa", "gasLimit": "0x4c4b40" }Copy the code

Initialize the genesis block:

$. / geth. Exe init ~ / AppData/Local/Ethereum/genesis json INFO [05-07 | 15:39:59.. 549] Maximum peer count ETH = 50 LES = 0 Total = 50 INFO [05-07 | 15:39:59.. 574] Set global gas cap cap = 25000000... The INFO [05-07 | 15:39:59. 623] Successfully demonstrate genesis state database = lightchaindata hash = "dcc595... 7102aa"Copy the code

Start node

Open a new terminal window:

$. / geth. Exe - nodiscover INFO [05-07 | 15:45:11.. 014] Starting geth on Ethereum mainnet... . The INFO [05-07 | 15:45:11. 401] Started P2P networking self = "enode: / / eda1a8e2d4d... @ 127.0.0.1:30303? discpo rt=0" ...Copy the code

Option description:

— noDiscover // Disable p2p network node discovery to ensure that no other network is joined

Connect the nodes

The ipc inter-process communication channel is enabled by default when the node is started, and the RPC remote call service that supports HTTP or WebSocket can also be actively enabled

Through the IPC service or RPC interface, nodes can execute management, query, and transaction commands provided by external users

Here we first use IPC to connect to the node that has been started

To open a new terminal, go to the Geth JavaScript console:

$ ./geth.exe attach \\.\pipe\geth.ipc Welcome to the Geth JavaScript console! Instance: Geth d11b01 / Windows/v1.10.2 - stable - 97 - amd64 / go1.15 coinbase: 0 x61d55d8015aa... . modules: Admin :1.0 Debug :1.0 ETH :1.0 ethash:1.0 Miner :1.0 NET :1.0 Personal :1.0 RPC :1.0 txpool:1.0 web3:1.0 To exit, press ctrl-d >Copy the code

Unlock the account

Before each transaction is sent, the sender needs to complete the signature with a key

To facilitate testing, you can use the password to unlock the transaction sender’s account in advance, and geTH will automatically use the key pair to sign the transaction data if the transaction is sent later

> personal.unlockAccount("46e7328724a7..." , "012..." ,36000) // Parameters: Account address, account password, and unlock validity period (in seconds) // Unlocked successfully trueCopy the code

Transfer transactions

Next we’ll execute the trading-related instructions to see how Transactions are handled by Ethereum

Query balance

Enter the query balance instruction:

> eth.getBalance("46e7328724a..." Wei (1 eth = 10^18 wei) = 1.008e+22Copy the code

Using the genesis block pre-allocated account as the query parameter, you can see that the balance is indeed the pre-allocated amount

Can also be converted to ether units:

> web3.fromWei(eth.getBalance("46e7328724a..." ),"ether") // The unit of the query result is eth 10080Copy the code

transfer

Enter the transfer instruction:

// Transfer 10 eth > eth. SendTransaction ({from:"46e7328724a7..." ,to:"248672bac9196446..." , the value: web3. ToWei (10, "Mr")}) / / return the result: trading hash "0 x25d0909d7fa61be3449c9c9f6c..."Copy the code

Note:

Although the transaction was successfully submitted, the node did not start mining, so the transaction will not be packaged to produce new blocks

Trades are still in the trading pool waiting to be packaged

Check the trading pool status:

> txpool.status
{
  pending: 1,
  queued: 0
}
Copy the code

Start the dig

> miner. Start (1)Copy the code

You can see the block output log on the GEth terminal:

The INFO [05-07 | 17:45:18. 367] Updated mining, threads threads = 1 INFO [05-07 | 17:45:18. 367] Transaction pool price threshold The updated price = 1000000000 INFO [05-07 | 17:56:05. 031] Etherbase automatically configured Address = 0 x61d55d8015aa018ea87f2d4f2a1ee8af1912a887 INFO [05-07 | 17:56:05.. 032] Commit new mining, the work number = 1 Sealhash = "a29c2e... TXS a33157 "uncles = 0 = 0 gas = 0 feeds = 0 elapsed = 0 s INFO [05-07 | 17:56:05.. 033] Commit new mining, the work number = 1 Sealhash = "a068c9... E980b4 "uncles = 0 = 21000 feeds = 2.1 TXS = 1 gas e-05 elapsed = 1.009 ms INFO [05-07 | 17:56:12.. 566] Successfully sealed new block Number = 1 sealhash = "a068c9... E980b4 "hash =" cf056d... 8 cf707 "elapsed = 7.533 s INFO [05-07 | 17:56:12.. 566] 🔨 mined potential block number = 1 hash =" cf056d... 8cf707" ...Copy the code

Suspension of mining

> miner.stop() // Check account balance: > eth.getBalance("248672bac91964465...") "+ 10 eth > eth. GetBalance ("46e7328724a7b9...") // sender: -10 eth - transfer fee (gas * gasPrice) > eth. GetBalance ("0x61d55D8015Aa01...") // miner: + reward * blockNum + gas * gasPriceCopy the code

Check your account balance to confirm that the transfer was successful

Intelligent contract

Contracts are created and executed on Ethereum by sending transactions, but the transactions carry more data, such as bytecode to the contract or the calling method

Write a contract

Smart contracts can be written in any high-level language, as long as they are compiled at deployment time into bytecode that the Ethereum VIRTUAL machine EVM can execute

Example of smart contract written in Solidity (soliditylang.org/) language:

/ / SPDX - License - Identifier: CC - BY - SA - 4.0

// Version of Solidity compiler this program was written for
pragma solidity ^0.6. 0;

// Our first contract is a faucet!
contract Faucet {
    // Accept any incoming amount
    receive () external payable {}
    
    // Give out ether to anyone who asks
    function withdraw(uint withdraw_amount) public {

        // Limit withdrawal amount
        require(withdraw_amount <= 100000000000000000);

        // Send the amount to the address that requested itmsg.sender.transfer(withdraw_amount); }}Copy the code

Tips: Use the online IDE remix.ethereum.org/ to facilitate development and debugging of smart contracts

Compiling contracts into bytecode in remix:

{ "linkReferences": {}, "object": "608060405234801561001057600080fd5b5060f48061001f6000396000f3fe608060405260043610601f5760003560e01c80632e1a7d4d14602a576 025565b36602557005b600080fd5b348015603557600080fd5b50605f60048036036020811015604a57600080fd5b810190808035906020019092919 05050506061565b005b67016345785d8a0000811115607557600080fd5b3373ffffffffffffffffffffffffffffffffffffffff166108fc829081150 290604051600060405180830381858888f1935050505015801560ba573d6000803e3d6000fd5b505056fea26469706673582212207a2c9ed47daa046 769d1020e307564fedff522d6340096ab14fa5829bd5f230764736f6c634300060c0033", "opcodes": "PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0xF4 DUP1 PUSH2 0x1F PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x4 CALLDATASIZE LT PUSH1 0x1F  JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0x2E1A7D4D EQ PUSH1 0x2A JUMPI PUSH1 0x25 JUMP JUMPDEST CALLDATASIZE PUSH1 0x25 JUMPI STOP JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST CALLVALUE DUP1 ISZERO PUSH1 0x35 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x5F PUSH1 0x4 DUP1 CALLDATASIZE SUB PUSH1 0x20 DUP2 LT ISZERO PUSH1 0x4A JUMPI PUSH1  0x0 DUP1 REVERT JUMPDEST DUP2 ADD SWAP1 DUP1 DUP1 CALLDATALOAD SWAP1 PUSH1 0x20 ADD SWAP1 SWAP3 SWAP2 SWAP1 POP POP POP  PUSH1 0x61 JUMP JUMPDEST STOP JUMPDEST PUSH8 0x16345785D8A0000 DUP2 GT ISZERO PUSH1 0x75 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND PUSH2 0x8FC DUP3 SWAP1 DUP2 ISZERO MUL SWAP1 PUSH1  0x40 MLOAD PUSH1 0x0 PUSH1 0x40 MLOAD DUP1 DUP4 SUB DUP2 DUP6 DUP9 DUP9 CALL SWAP4 POP POP POP POP ISZERO DUP1 ISZERO PUSH1 0xBA JUMPI RETURNDATASIZE PUSH1 0x0 DUP1 RETURNDATACOPY RETURNDATASIZE PUSH1 0x0 REVERT JUMPDEST POP POP JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 PUSH27 0x2C9ED47DAA046769D1020E307564FEDFF522D6340096AB14FA5829 0xBD 0x5F 0x23 SMOD PUSH5 0x736F6C6343 STOP MOD 0xC STOP CALLER ", "sourceMap": "169:405:0:-:0;;;;;;;;;;;;;;;;;;;" }Copy the code

The Object field is the bytecode data carried by the transaction

Deployment of contract

The deployment contract is the same as the transfer transaction, but the parameters only need to be from and data

> eth.sendTransaction({from:"46e7328724a7..." ,data:"0x608060405234801561001057600080fd5b5060f48061001f6000396000f3fe608060405260043610601f5760003560e01c80632e1a7d4d1 4602a576025565b36602557005b600080fd5b348015603557600080fd5b50605f60048036036020811015604a57600080fd5b8101908080359060200 1909291905050506061565b005b67016345785d8a0000811115607557600080fd5b3373ffffffffffffffffffffffffffffffffffffffff166108fc8 29081150290604051600060405180830381858888f1935050505015801560ba573d6000803e3d6000fd5b505056fea26469706673582212207a2c9ed 47 daa046769d1020e307564fedff522d6340096ab14fa5829bd5f230764736f6c634300060c0033 "}) / / transaction hash "0 x5a90c06d0af12b921fa62efddfe3376b07c1ab0b820b3626016c5ead959d42a1" / / parameters: // From the account address of the sender of the transaction // the bytecode generated by the part compilation on the data. Note: the prefix 0x is added to indicate the hexadecimal dataCopy the code

Call the contract

Calling a contract is the same as the transfer transaction method, but the arguments are only from to and data

> eth.sendTransaction({from:"46e7328724a..." ,to:"37F5A36e1..." Data: "0 x2e1a7d4d00000000000000000000000000000000000000000000000000000000000186a0"}) / / parameters: // From the account address of the sender of the transaction // to the contract address // data The ABI encoding of the contract call information, note that the prefix 0x is appendixed to indicate the hexadecimal dataCopy the code

You can use abidump to decode abi information:

$ ./abidump.exe 0x2e1a7d4d00000000000000000000000000000000000000000000000000000000000186a0

Info: Transaction invokes the following method: "withdraw(uint256: 100000)"
Copy the code

summary

Through the above steps, we completed the setup of the local development environment for Ethereum and understood the basic process of creating an account, transferring transactions, deploying and invoking contracts

Let’s dive into the code details and see how these instructions actually work

Refer to the content

  • eth.wiki/
  • geth.ethereum.org/docs/
  • docs.soliditylang.org/en/v0.8.4/
  • Web3js. Readthedocs. IO/en/v1.2.11 /…
  • Documenter.getpostman.com/view/411725…

(End of article)