Skip to content
You are reading Hyperledger Besu development version documentation and some displayed features may not be available in the stable release. You can switch to stable version using the version box at screen bottom.

Updated on August 13, 2020

Privacy-enabled private network example tutorial

The privacy-enabled private network example runs a private network of Hyperledger Besu and Orion nodes managed by Docker Compose. It’s an expanded version of the private network example.

You can use the Block Explorer, make JSON-RPC requests, and create transactions using Metamask as described in the private network example. This tutorial describes how to use the examples provided in the web3js-eea library to create and send private transactions.

Important

This tutorial runs a private network suitable for education or demonstration purposes and is not intended for running production networks.

Prerequisites

To run this tutorial, you must have the following installed:

Clone Besu sample networks source code

Clone the repository from the besu- repository.

git clone https://github.com/PegaSysEng/besu-sample-networks.git

Note

To use a specific version of Hyperledger Besu, set the BESU_VERSION environment variable.

Clone web3js-eea libraries

Clone the PegaSysEng/web3js-eea library:

git clone https://github.com/PegaSysEng/web3js-eea.git

In the web3js-eea directory:

npm install

Start the network

Important

If running in Windows, please run commands from the GitBash shell

In the besu-sample-networks directory, start the network with privacy enabled:

./run-privacy.sh

The script pulls the Docker images starts the network. Pulling the images takes a few minutes the first time. The network details display.

       Name                      Command               State                              Ports
--------------------------------------------------------------------------------------------------------------------------
privacy_bootnode_1    /opt/besu/bootnode_sta ...   Up      30303/tcp, 8545/tcp, 8546/tcp
privacy_explorer_1    nginx -g daemon off;             Up      0.0.0.0:25000->80/tcp
privacy_minernode_1   /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
privacy_node1_1       /opt/besu/node_start.s ...   Up      30303/tcp, 0.0.0.0:20000->8545/tcp, 0.0.0.0:20001->8546/tcp
privacy_node2_1       /opt/besu/node_start.s ...   Up      30303/tcp, 0.0.0.0:20002->8545/tcp, 0.0.0.0:20003->8546/tcp
privacy_node3_1       /opt/besu/node_start.s ...   Up      30303/tcp, 0.0.0.0:20004->8545/tcp, 0.0.0.0:20005->8546/tcp
privacy_orion1_1      /orion/bin/orion data/data ...   Up
privacy_orion2_1      /orion/bin/orion data/data ...   Up
privacy_orion3_1      /orion/bin/orion data/data ...   Up
privacy_rpcnode_1     /opt/besu/node_start.s ...   Up      30303/tcp, 8545/tcp, 8546/tcp
****************************************************************
JSON-RPC HTTP service endpoint      : http://localhost:8545   *
JSON-RPC WebSocket service endpoint : ws://localhost:8546   *
Web block explorer address          : http://localhost:25000   *
****************************************************************

Send private transactions and read values

The Event Emitter script deploys a contract with a privacy group of Node1 and Node2. That is, the other nodes cannot access the contract. After deploying the contract, Event Emitter stores a value.

In the web3js-eea directory, run eventEmitter.js:

node example/eventEmitter.js

Tip

The network takes a few minutes to get started. If you get a Error: socket hang up error, the network isn’t fully setup. Wait and then run the command again.

The Event Emitter logs display.

Transaction Hash  0xe0776de9a9d4e30be0025c1308eed8bc45502cba9fe22c504a56e2fd95343e6f
Waiting for transaction to be mined ...
Private Transaction Receipt
 { contractAddress: '0x2f351161a80d74047316899342eedc606b13f9f8',
  from: '0xfe3b557e8fb62b89f4916b721be55ceb828dbd73',
  to: null,
  output:
   '0x6080604052600436106100565763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633fa4f245811461005b5780636057361d1461008257806367e404ce146100ae575b600080fd5b34801561006757600080fd5b506100706100ec565b60408051918252519081900360200190f35b34801561008e57600080fd5b506100ac600480360360208110156100a557600080fd5b50356100f2565b005b3480156100ba57600080fd5b506100c3610151565b6040805173ffffffffffffffffffffffffffffffffffffffff9092168252519081900360200190f35b60025490565b604080513381526020810183905281517fc9db20adedc6cf2b5d25252b101ab03e124902a73fcb12b753f3d1aaa2d8f9f5929181900390910190a16002556001805473ffffffffffffffffffffffffffffffffffffffff191633179055565b60015473ffffffffffffffffffffffffffffffffffffffff169056fea165627a7a72305820c7f729cb24e05c221f5aa913700793994656f233fe2ce3b9fd9a505ea17e8d8a0029',
  logs: [] }
Waiting for transaction to be mined ...
Transaction Hash: 0xbf14d332fa4c8f50d90cb02d47e0f825b8b2ef987c975306f76a598f181f4698
Event Emited: 0x000000000000000000000000fe3b557e8fb62b89f4916b721be55ceb828dbd7300000000000000000000000000000000000000000000000000000000000003e8
Waiting for transaction to be mined ...
Get Value: 0x00000000000000000000000000000000000000000000000000000000000003e8
Waiting for transaction to be mined ...
Transaction Hash: 0x5b538c5690e3ead6e6f811ad23c853bc63b3bca91635b3b611e51d2797b5f073
Event Emited: 0x000000000000000000000000fe3b557e8fb62b89f4916b721be55ceb828dbd73000000000000000000000000000000000000000000000000000000000000002a
Waiting for transaction to be mined ...
Get Value: 0x000000000000000000000000000000000000000000000000000000000000002a

Call eth_getTransactionReceipt where:

  • <TransactionHash> is the transaction hash displayed in the Event Emitter logs.
  • <JSON-RPC Endpoint> is the JSON-RPC HTTP service endpoint displayed when starting the network.
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["<TransactionHash>"],"id":1}' <JSON-RPC Endpoint>
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xe0776de9a9d4e30be0025c1308eed8bc45502cba9fe22c504a56e2fd95343e6f"],"id":1}' http://localhost:8545

The transaction receipt for the privacy marker transaction displays with a contractAddress of null.

{
  "jsonrpc" : "2.0",
  "id" : 1,
  "result" : {
    "blockHash" : "0xfacdc805f274553fcb2a12d3ef524f465c25e58626c27101c3e6f677297cdae9",
    "blockNumber" : "0xa",
    "contractAddress" : null,
    "cumulativeGasUsed" : "0x5db8",
    "from" : "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
    "gasUsed" : "0x5db8",
    "logs" : [ ],
    "logsBloom" : "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "status" : "0x1",
    "to" : "0x000000000000000000000000000000000000007e",
    "transactionHash" : "0xe0776de9a9d4e30be0025c1308eed8bc45502cba9fe22c504a56e2fd95343e6f",
    "transactionIndex" : "0x0"
  }
}

Stop the network

Do one of the following to stop the network:

  • Stop the network:

    ./stop.sh
    
  • Stop the network and remove the containers and volumes:

    ./remove.sh
    
  • Stop the network and delete the Docker images:

    ./delete.sh
    
Questions or feedback? You can discuss issues and obtain free support on Hyperledger Besu chat channel.
For Hyperledger Besu community support, contact the mailing list besu@lists.hyperledger.org