Ledger Snapshot in Hyperledger Fabric v2.3

Introduction

Ledger snapshot is one of the two features introduced in Fabric v2.3, which was released November last year. The snapshot gives us an alternative way to join new peers into a fabric network. In this article we will give highlights on the ledger snapshot, and walk through a demonstration on how things work.

Overview

For those who are familiarized with the process of adding new peers in a running fabric network, either in existing peer organization or in a new organization, we use the peer channel join with the channel genesis block file, and the new peer will then obtain the whole ledger from other peers in the channel. After going through all the transactions in the block, the world state gets updated. It would take time and effort before the full ledger is ready for use.

Image for post
Image for post
Join new peer with channel genesis block (prior to v2.3)
Image for post
Image for post
Join new peer by snapshot (v2.3)

Demonstration Overview

Our demonstration is to use ledger snapshot from a peer of a live fabric network to join a new peer. After that we will show the peer works normally, and make some observations on this new peer.

Demonstration

Step 1: Bring Up Test Network

We first bring up the Test Network with -ca option. We also create mychannel and let both peers join the channel.

cd test-network
./network.sh up createChannel -ca
Image for post
Image for post
Image for post
Image for post

Step 2: Deploy SACC and Invoking Chaincode Function

We deploy SACC for testing.

./network.sh deployCC -ccn mycc -ccp ../chaincode/sacc -ccl go
Image for post
Image for post
Image for post
Image for post
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C mychannel -n mycc --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Peter"]}'
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Step 3: Bring Up New Peer

We follow the same process of bringing up peer1.org1.example.com. Details can be found here.

export FABRIC_CA_CLIENT_HOME=${PWD}/organizations/peerOrganizations/org1.example.com/fabric-ca-client register --caname ca-org1 --id.name peer1 --id.secret peer1pw --id.type peer --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pemmkdir -p organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.comfabric-ca-client enroll -u https://peer1:peer1pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp --csr.hosts peer1.org1.example.com --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pemcp ${PWD}/organizations/peerOrganizations/org1.example.com/msp/config.yaml ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp/config.yamlfabric-ca-client enroll -u https://peer1:peer1pw@localhost:7054 --caname ca-org1 -M ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls --enrollment.profile tls --csr.hosts peer1.org1.example.com --csr.hosts localhost --tls.certfiles ${PWD}/organizations/fabric-ca/org1/tls-cert.pemcp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/tlscacerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crtcp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/signcerts/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.crtcp ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/keystore/* ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/server.key
docker-compose -f docker/docker-compose-peer1org1.yaml up -d
# peer0.org1
peer channel list
# peer1.org1
CORE_PEER_ADDRESS=localhost:8051 peer channel list
Image for post
Image for post

Step 4: Join New Peer with Ledger Snapshot

We first take a snapshot in peer0.org1.example.com. Note here we specify -b 0, which means the snapshot is taken immediately after this command. You can schedule a snapshot on a future block. See documentation for more detail.

peer snapshot submitrequest -c mychannel -b 0 --peerAddress localhost:7051 --tlsRootCertFile ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
Image for post
Image for post
Content of a snapshot
docker cp peer0.org1.example.com:/var/hyperledger/production/snapshots/completed/mychannel/6/ snapshotdocker cp snapshot/ peer1.org1.example.com:/opt/gopath/src/github.com/hyperledger/fabric/peer
CORE_PEER_ADDRESS=localhost:8051 peer channel joinbysnapshot --snapshotpath /opt/gopath/src/github.com/hyperledger/fabric/peer/snapshot/
Image for post
Image for post
CORE_PEER_ADDRESS=localhost:8051 peer channel listCORE_PEER_ADDRESS=localhost:8051 peer channel getinfo -c mychannel
Image for post
Image for post
Image for post
Image for post

Step 5: Testing Chaincode in New Peer

We first install the chaincode package to peer1.org1.example.com. This brings up the chaincode container for peer1.org1.example.com to handle chaincode operation.

CORE_PEER_ADDRESS=localhost:8051 peer lifecycle chaincode install mycc.tar.gz
CORE_PEER_ADDRESS=localhost:8051 peer chaincode query -C mychannel -n mycc -c '{"Args":["get","name"]}'
Image for post
Image for post
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile $ORDERER_CA -C mychannel -n mycc --peerAddresses localhost:8051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["set","name","Mary"]}'
Image for post
Image for post
Image for post
Image for post

Step 6: Fetching Blocks

As we know, peer1.org1.example.com does not have those blocks before the snapshot. Here we can see the result.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Step 7: Clean Up

To tear down the network

./network.sh down
docker volume prune

Summary

We have seen the new feature ledger snapshot in Fabric v2.3, and see how we can use the snapshot to bring up new peers joining a fabric network. Note the limitation shown in the documentation and see whether snapshot is something you can consider in future.

Written by

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store