Enterprise Ethereum Besu introductory course [original Pantheon]

Keywords: Blockchain network JSON curl Docker

Besu is the enterprise Ethereum product in Hyperledger, and its biggest advantage is that it is compatible with the Ethereum main network. This tutorial describes how to use Hyperledger Besu to quickly start an enterprise Ethernet network and use JSON RPC for data query and transaction submission, and how to use Truffle to develop enterprise Ethernet DApp and use built-in tools for data debugging and operation and maintenance monitoring.

1. Start enterprise Ethereum network

Ethereum tutorial recommendations: Introduction to Dapp | Dapp practice of e-commerce | Token actual combat | Php docking | Java docking | Python docking | C# docking | Dart docking

First, clone the source code of Besu's quickstart warehouse:

git clone https://github.com/PegaSysEng/besu-quickstart.git

Then enter the besu QuickStart directory and execute the following command to build the docker image of besu:

./run.sh

The above command will build a docker image and start four containers to simulate an enterprise Ethernet network with six besu nodes. When the script is finished, you can see the following output information:

*************************************
Besu Quickstart <version>
*************************************
List endpoints and services
----------------------------------
              Name                            Command               State               Ports
---------------------------------------------------------------------------------------------------------
besu-quickstart_bootnode_1     /opt/besu/bootnode_sta ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_explorer_1     nginx -g daemon off;             Up      0.0.0.0:32768->80/tcp
besu-quickstart_grafana_1      /run.sh                          Up      3000/tcp
besu-quickstart_minernode_1    /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_node_1         /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_node_2         /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_node_3         /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_node_4         /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
besu-quickstart_prometheus_1   /bin/prometheus --config.f ...   Up      9090/tcp
besu-quickstart_rpcnode_1      /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp

And the list of access end nodes:

****************************************************************
JSON-RPC HTTP service endpoint      : http://localhost:32768/jsonrpc
JSON-RPC WebSocket service endpoint : ws://localhost:32768/jsonws
GraphQL HTTP service endpoint       : http://localhost:32768/graphql
Web block explorer address          : http://localhost:32768
Prometheus address                  : http://localhost:32768/prometheus/graph
Grafana address                     : http://localhost:32768/grafana-dashboard                                                                        
****************************************************************
  • JSON-RPC HTTP service for DApp or Metamask wallet access
  • JSON-RPC WebSocket service is used by DApp to access nodes through websocket
  • GraphQL HTTP service GraphQL service for access nodes of DApp or Metamask wallets
  • The Web block browsing service is used to browse blocks. Enter the address in your browser
  • The Prometheus service is used to provide metrics data for the Prometheus dashboard
  • The Grafana service is used to provide data for the Grafana dashboard

To display the access end node again, use the following command:

./list.sh

2. Using enterprise Ethereum block browser

In this tutorial we use the Alethio lightweight Ethereum browser, which you can also use EthScan.

Open the address listed in the web block explorer endpoint mentioned above in your browser to view the block data in the enterprise Ethernet network.

You can see that there are six besu nodes in the block browser: four ordinary nodes, one outbound node and one boot node.

Click the block number on the right side of the Best Block to display the detailed data of the block:

Click the magnifying glass in the upper left corner to search the block, transaction hash, or Ethereum address:

3. Monitoring the health of Besu nodes

The operation and maintenance monitoring tools Prometheus and Grafana can be used to visualize the health status and operation of nodes. You can access these tools directly in your browser by referring to the access end nodes given above. For example, using Grafana:

4. Using JSON-RPC to access Besu nodes

Besu supports the standard Ethernet JSON-RPC API interface. For example, call with curl web3_clientVersion Command to view the version of a node:

curl -X POST --data '{
	"jsonrpc":"2.0",
	"method":"web3_clientVersion",
	"params":[],
	"id":1
}' <http-rpc-endpoint>

Where < HTTP RPC endpoint > indicates the address of the access end node listed above. You need to replace it according to your actual situation, such as http://localhost:32768/jsonrpc. The return result of the above command is similar to the following:

{
   "jsonrpc" : "2.0",
   "id" : 1,
   "result" : "besu/<version number>"
}

Or use net_peerCount Command to view the number of peers connected to a node:

curl -X POST --data '{
	"jsonrpc":"2.0",
	"method":"net_peerCount",
	"params":[],
	"id":1
}' <http-rpc-endpoint>

The results are as follows:

{
  "jsonrpc" : "2.0",
  "id" : 1,
  "result" : "0x6"
}

Or use eth_blockNumber To view the latest block number:

curl -X POST --data '{
	"jsonrpc":"2.0",
	"method":"eth_blockNumber",
	"params":[],
	"id":1
}' <http-rpc-endpoint>

The results are as follows:

{
  "jsonrpc" : "2.0",
  "id" : 1,
  "result" : "0x8b8"
}

5. Creating enterprise Ethereum transactions with MetaMask

Before sending the enterprise Ethereum transaction, we need to create an account, or use several accounts declared in the creation configuration of this private network:

Account 1: also the address of currency base

Address: 0xfe3b557e8fb62b89f4916b721be55ceb828dbd73
Private key : 0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63
Initial balance : 0xad78ebc5ac6200000 (200000000000000000000 in decimal)

Account number 2:

Address: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
Private key : 0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
Initial balance : 0x90000000000000000000000 (2785365088392105618523029504 in decimal)

Account number 3:

Address: 0xf17f52151EbEF6C7334FAD080c5704D77216b732
Private key : 0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
Initial balance : 0x90000000000000000000000 (2785365088392105618523029504 in decimal

After logging in to MetaMask, link to a node of Besu network we established, and then you can create a transaction.

6. Demonstration of Truffle pet store based on Besu network

To run truffle's Pet Shop demonstration, first we need to install truffle and Pet Shop templates, and then we need to make some simple adjustments to Besu's enterprise Ethernet network.

First install the truffle:

npm install -g truffle

Then create the pet shop tutorial directory and enter it:

mkdir pet-shop-tutorial
cd pet-shop-tutorial

Then extract Truffle's pet shop box:

truffle unbox pet-shop

To install the truffle Wallet:

npm install --save @truffle/hdwallet-provider

Next, modify the truss-config.js file in the pet shop tutorial directory to add our wallet provider. Please refer to the following for modification:

const PrivateKeyProvider = require("truffle-hdwallet-provider");
const privateKey = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63";

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },
    quickstartWallet: {
      provider: () => new PrivateKeyProvider(privateKey, "<YOUR HTTP RPC NODE ENDPOINT>"),
      network_id: "*"
    },
  }
};

Replace < your HTTP RPC node endpoint > with your HTTP RPC client node, for example http://localhost:32770/jsonrpc .

Replace the privateKey with the previous account 1, i.e. the currency base address, which contains Ethernet currency.

Because we use the enterprise Ethernet network instead of the Ganache emulator, we need to specify the network when performing contract deployment:

truffle migrate --network quickstartWallet

The output is similar to the following:

sing network 'quickstartWallet'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xfc1dbc1eaa14fa283c2c4415364579da0d195b3f2f2fefd7e0edb600a6235bdb
  Migrations: 0x9a3dbca554e9f6b9257aaa24010da8377c57c17e
Saving successful migration to network...
  ... 0x77cc6e9966b886fb74268f118b3ff44cf973d32b616ed4f050b3eabf0a31a30e
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Adoption...
  ... 0x5035fe3ea7dab1d81482acc1259450b8bf8fefecfbe1749212aca86dc765660a
  Adoption: 0x2e1f232a9439c3d459fceca0beef13acc8259dd8
Saving successful migration to network...
  ... 0xa7b5a36e0ebc9c25445ce29ff1339a19082d0dda516e5b72c06ee6b99a901ec0
Saving artifacts...

You can view the contract address in the above output in the block browser.

Also, when performing the test, specify to use our enterprise Ethernet network:

truffle test --network quickstartWallet

The output results are as follows:

Using network 'quickstartWallet'.

Compiling ./contracts/Adoption.sol...
Compiling ./test/TestAdoption.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...


  TestAdoption
    ✓ testUserCanAdoptPet (2071ms)
    ✓ testGetAdopterAddressByPetId (6070ms)
    ✓ testGetAdopterAddressByPetIdInArray (6077ms)


  3 passing (37s)

7. Stop / restart / clean up of enterprise Ethereum network

Use the following script to stop the enterprise Ethernet network composed of Besu, but do not delete the dockers container:

./stop.sh

Restart the enterprise Ethereum network using the following command:

./resume.sh

If you want to stop the network and delete the corresponding docker container, use the following command:

./remove.sh

Original link: Hyperledger Besu enterprise Ethereum quick course huizhi.com

Posted by pessi on Thu, 16 Jan 2020 19:06:45 -0800