Blockchain with Hyperledger Fabric - Second Edition

Book description

Learn to develop blockchain-based distributed ledgers and deploy a Hyperledger Fabric network with concrete exercises and examples

Key Features

  • Updated with the latest features and additions that come with Hyperledger Fabric 2
  • Write your own smart contracts and services using Java and JavaScript on a Hyperledger Fabric network
  • Dive into real-world blockchain challenges such as integration and scalability

Book Description

Blockchain with Hyperledger Fabric - Second Edition is a refreshed and extended version of the successful book on practical Hyperledger Fabric blockchain development. This edition includes many new chapters, alongside comprehensive updates and additions to the existing ones. Entirely reworked for Hyperledger Fabric version 2, this edition will bring you right up to date with the latest in blockchain. Using a real-world Trade Finance and Logistics example, with working code available on GitHub, you'll really understand both how and why Hyperledger Fabric can be used to maximum effect. This book is your comprehensive guide and reference to explore and build blockchain networks using Hyperledger Fabric version 2.

This edition of the book begins by outlining the evolution of blockchain, including an overview of relevant blockchain technologies. Starting from first principles, you'll learn how to design and operate a permissioned blockchain network based on Hyperledger Fabric version 2. You will learn how to configure the main architectural components of a permissioned blockchain network including Peers, Orderers, Certificate Authorities, Channels, and Policies. You'll then learn how to design, develop, package, and deploy smart contracts, and how they are subsequently used by applications. This edition also contains chapters on DevOps, blockchain governance, and security, making this your go-to book for Hyperledger Fabric version 2.

What you will learn

  • Discover why blockchain is a technology and business game changer
  • Set up blockchain networks using Hyperledger Fabric version 2
  • Understand how to create decentralized applications
  • Learn how to integrate blockchains with existing systems
  • Write smart contracts and services quickly with Hyperledger Fabric and Visual Studio Code
  • Design transaction models and smart contracts with Java, JavaScript, TypeScript, and Golang
  • Deploy REST gateways to access smart contracts and understand how wallets maintain user identities for access control
  • Maintain, monitor, and govern your blockchain solutions

Who this book is for

This book is designed in such a way that professionals from different areas including business leaders, technology leaders, blockchain application developers, and even beginners can benefit from it.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. Downloading the example code
      1. Conventions used
    4. Get in touch
      1. Reviews
  2. Blockchain – An Enterprise and Industry Perspective
    1. Our focus for the new edition
    2. Defining the terms – what is blockchain?
    3. Design considerations for blockchain solutions
      1. Four core building blocks
      2. Additional capabilities to consider
    4. Fundamentals of the secure transaction processing protocol
    5. Where blockchain technology has been and where it's going
      1. The great divide
      2. An economic model for blockchain delivery
      3. Learning as we go
      4. The promise of trust and accountability
    6. Blockchain in the enterprise
      1. What applications are a good fit?
      2. Enterprise blockchain business evaluation considerations
        1. A few thoughts on blockchain business models
        2. Business growth and innovation
        3. How do growth and innovation relate to a blockchain-powered network?
        4. Considerations for evaluating the economic value of blockchain entities
        5. Blockchain investment rubric
      3. How does the enterprise view blockchain?
      4. Integrating a blockchain infrastructure for the whole enterprise
    7. Enterprise design principles
      1. Business drivers and evolution
      2. Ensuring the sustainability of blockchain-based business networks
      3. Design principles that drive blockchain adoption
    8. Business considerations for choosing a blockchain framework
    9. Technology considerations for choosing a blockchain framework
      1. Identity management
      2. Scalability
      3. Enterprise security
      4. Development tooling
      5. Crypto-economic models
      6. Decentralization with systemic governance
      7. Enterprise support
      8. Use case-driven pluggability choices
        1. Shared ledger technology
        2. Consensus
        3. Crypto algorithms and encryption technology
    10. Enterprise integration and designing for extensibility
    11. Other considerations
      1. Consensus, ACID properties, and CAP
      2. Attestation – SSCs are signed and encrypted
      3. Use of HSMs
    12. Summary
    13. References
  3. Exploring Hyperledger Fabric
    1. Building on the foundations of open computing
    2. Fundamentals of the Hyperledger project
      1. The Linux Foundation
      2. Hyperledger
      3. Open source and open standards
    3. Hyperledger frameworks, tools, and building blocks
      1. Hyperledger distributed ledger frameworks
      2. Hyperledger libraries
      3. Hyperledger tools
      4. The building blocks of blockchain solutions
    4. Hyperledger Fabric component design
      1. Principles of Hyperledger design
      2. Hyperledger Fabric reference architecture
      3. Hyperledger Fabric runtime architecture
      4. Strengths and advantages of a componentized design
    5. Hyperledger Fabric – the journey of a sample transaction
    6. Actors and components in a Hyperledger Fabric network
      1. Actors in a blockchain network
      2. Components in a blockchain network
      3. Developer interaction
    7. CAP theorem
    8. New features covered in this book
    9. Summary
  4. Business Networks
    1. A busy world of purposeful activity
    2. Why a language for business networks?
    3. Defining a business network
    4. Introducing participants
      1. Types of participant
        1. Individual participants
        2. Organizational participants
        3. System or device participants
      2. A participant is an agent
      3. Participant identity
    5. Introducing assets
      1. Assets flow between participants
      2. Tangible and intangible assets
      3. The structure of assets
      4. Ownership
        1. A special kind of relationship
        2. Ownership and asset tokenization
      5. Asset life cycles
      6. Describing a life cycle with transactions
    6. Introducing transactions
      1. Change and transactions
      2. Transaction definition and instance
      3. Implicit and explicit transactions
      4. The importance of contracts
      5. Signatures
      6. Smart contract multi-party transactions
      7. Digital transaction processing
      8. Initiating transactions
      9. Transaction history
      10. Transaction streams
      11. A network of networks
      12. Current value and transaction history
      13. A business network as a history of transactions
      14. Regulators
    7. Introducing events
      1. A universal concept
      2. Event notifications
      3. An event example
      4. Events and transactions
      5. External and explicit events
      6. Loosely coupled design
      7. Events are useful!
    8. Business network technology
      1. More dematerialization
      2. The blockchain benefit
      3. Interacting with a blockchain
        1. Organizational structure
        2. Technical componentry
        3. Participants
        4. APIs
        5. Application tier
        6. Smart contract
        7. Ledger
        8. Peer
        9. Ordering service
        10. Network channel
        11. Identity
    9. Summary
  5. Setting the Stage with a Business Scenario
    1. Trading and letters of credit
      1. The importance of trust in facilitating trade
      2. The letter of credit process today
    2. Business scenario and use case
      1. Overview
      2. Real-world processes
      3. Simplified and modified processes
      4. Terms used in trade finance and logistics
      5. Shared process workflow
      6. Shared assets and data
      7. Participants' roles and capabilities
      8. Advantages of blockchain applications over current real-world processes
    3. Designing and configuring a Hyperledger Fabric trade network
      1. Designing a network
      2. Installing prerequisites
      3. Setting up the development/test environment
        1. Testing the IBP extension
      4. Forking and cloning the trade-network repository
      5. Creating and running a network configuration
        1. Preparing the network
        2. Generating network cryptographic material
        3. Generating channel artifacts
        4. Composing a sample trade network
        5. Network components' configuration files
        6. Configuring peer databases
    4. Launching a sample trade network
    5. Configuring our development environment for network operations
      1. Creating Fabric identity wallets
      2. Creating a Fabric environment
        1. Creating a node file
        2. Creating and connecting to a Fabric environment in VS Code
    6. Summary
  6. Designing Smart Contract Transactions and Ledger Data Structures
    1. Architecture of the trade solution
    2. Starting the contract development
      1. Opening and packaging a contract
      2. Deploying a contract in the testing environment
        1. Invoking and debugging a contract
    3. Creating a contract
      1. The contract interface
      2. Implementing the contract
    4. Access control
      1. ABAC
        1. Registering a user
        2. Enrolling a user
        3. Access control in the contract
    5. Implementing contract functions
      1. Scenario contracts' functions
      2. Defining contract assets
      3. Coding contract functions
        1. Creating an asset
        2. Reading and modifying an asset
    6. Testing a contract
      1. Creating a test suite file
      2. Defining a test suite
      3. Unit tests
      4. Running tests in the terminal
      5. Running tests in VS Code
    7. Advanced contract design topics
      1. Cross-contract and cross-ledger invocation
      2. Composite keys
      3. Range and composite key queries
      4. State queries and CouchDB
      5. Indexes
      6. History queries
    8. Transaction mechanisms
      1. The ReadSet and WriteSet
      2. Multiversion concurrency control
    9. Logging output
      1. Configuration
      2. Logging API
      3. Standard output and error
      4. Additional API functions
    10. Summary
  7. Developing Smart Contracts
    1. Business networks
    2. Solution application components
      1. The transaction ledger and multi-party transactions
      2. Smart contracts
      3. Applications
      4. Network channels
    3. The multi-party transaction
      1. Transaction type
      2. Transaction identifier
      3. Proposal
      4. Response
        1. Valid and invalid transactions
    4. The ledger
      1. State database
        1. State
        2. State collections
      2. Blockchain
        1. Immutable blocks and transactions
        2. Immutability
      3. Primacy of the blockchain
    5. Smart contracts
      1. A central role in the network
      2. Smart contracts and consensus
      3. Writing a smart contract
        1. Smart contract structure
        2. Contract class and transaction methods
        3. Elaborating a smart contract
        4. Writing smart contract transaction methods
        5. Marshalling the transaction proposal input
        6. Accessing the ledger to generate a transaction response
        7. Accessing the state database
        8. Returning a signed transaction response
        9. Checking that business objects exist
        10. Transaction handlers
        11. Other functionality available in fabric-contract-api
        12. Annotations
      4. Packaging smart contracts
        1. A word on terminology
        2. Smart contract package definition agreement
    6. Programming language
      1. Choice of programming language
        1. Using a type system
        2. Modularity – a big idea in Hyperledger Fabric
    7. Endorsement policy
      1. The structure of an endorsement policy
        1. Network agreement
        2. Why endorsement?
        3. Endorsement policy – a separate concern
        4. State-based endorsement
        5. Collection endorsement policy
    8. Summary
  8. Developing Applications
    1. Applications
      1. The three basic application operations
      2. The application SDK
        1. Declarative APIs and the fabric-network package
        2. Separating the physical from the logical
      3. Querying the ledger
      4. Submitting a new transaction
        1. The process
        2. How the SDK makes consensus easy
      5. Requesting ledger notification
    2. Wallets and identity
      1. Using an identity
      2. Using a wallet
    3. Gateways
      1. Why gateways are useful
      2. Discovery
      3. A network view for an application
    4. Accessing networks and smart contracts
      1. Single network gateway
      2. Multi-network gateway
    5. Querying the ledger
      1. Smart contract packaging and namespaces
      2. Using EvaluateTransaction to query the ledger
      3. Separating the logical from the physical
      4. Synchronous and asynchronous queries
      5. Query in a more complex topology
    6. Submitting a new transaction
      1. The other organizations in the network
      2. What it means to evaluate and submit a transaction
      3. The network topology doesn't matter!
      4. Atomic transactions within and across networks
      5. Combining transactions in a single unit of work
    7. Events and notifications
      1. Transactions, events, and smart contracts
        1. Transactions and events
        2. Smart contracts and events
      2. Event listening and notification
        1. Listening – how it works
        2. Asynchronous responses
    8. Summary
  9. Advanced Topics for Developing Smart Contracts and Applications
    1. Customizing SDK behavior
      1. Connection options to the rescue
      2. Event handler functions
        1. Checkpoint handler
    2. Transient data
      1. Explicitly recording transaction input
      2. Hiding the transaction input
    3. Private data
      1. Using private data collections
        1. Smart contract exploitation
        2. Private data with transient data
      2. Understanding private data collections
      3. Private data consensus and off-chain data
      4. Private data collections and verifiability
      5. Recapping on styles
        1. The verify-style transaction
        2. Applications, organizations, and private data
      6. Private data consensus
      7. The insert-style transaction
        1. The most important trade
        2. Insert-style smart contract
        3. Insert-style application transaction submission
      8. Read your own writes
    4. Collection and state endorsements policy
      1. Levels of endorsement policy
      2. A note on collection policies
      3. The transfer-style transaction
      4. The createInfo transaction
        1. The transferInfo transaction
        2. The updateInfo transaction
      5. The transfer-style application transaction submission
      6. Summarizing smart contracts, transactions, and applications
    5. Advanced smart contract packaging
      1. Multi-definition packages
      2. Functional packaging
    6. Summary
  10. Network Operation and Distributed Application Building
    1. Stages in a Fabric network's life cycle
    2. Fabric application model and architecture
      1. Fabric application development overview
      2. Architecture of a Fabric application for trade
    3. Operations – network setup and bootstrap
      1. Operations overview – channel and contract setup
      2. Prerequisites – creating all channel artifacts
        1. Creating all channel artifacts
        2. Launching the network
      3. Overview of Fabric tools and commands
      4. Creating channels
        1. Using the trade.sh script as a shortcut
        2. Verifying block creation
      5. Joining organization peers to channels
      6. Setting organization anchor peers on channels
      7. Running all channel operations in one go
      8. Installing and initializing contracts
        1. Using CLI tools to install and initialize contracts
        2. Installing contracts using the VS Code IBP extension
    4. Developing service-layer applications
      1. Application runtime life cycle
      2. An application for the importer's organization
        1. Importer application structure
        2. Project dependencies
        3. Setting application properties and generating a connection profile
        4. Designing a service API for the importer application
        5. User registration, login, and session management
        6. User roles and access control
        7. Fabric registration, enrolment, and identity wallets
        8. Contract invocations through gateways
        9. Launching and testing the application
        10. Event management
    5. Exercising the application through a presentation layer
      1. Launching applications
      2. An end-to-end scenario – trade request to final payment
        1. Users and their credentials
        2. Registering users
        3. Logging in users
        4. Requesting a trade as an importer
        5. Accepting a trade as an exporter
        6. Requesting a letter of credit as an importer
        7. Issuing a letter of credit as an importer's bank
        8. Accepting a letter of credit as an exporter's bank
        9. Requesting an export license as an exporter
        10. Issuing an export license as a regulator
        11. Preparing a shipment as an exporter
        12. Accepting a shipment and issuing a bill of lading as a carrier
        13. Requesting a partial payment as an exporter's bank
        14. Making a partial payment as an importer's bank
        15. Delivering a shipment as a carrier
        16. Requesting the balance payment as an exporter's bank
        17. Making the balance payment as an importer's bank
        18. Viewing the list of active or processed trades as an importer
    6. Using a production-grade ordering service
    7. Summary of key steps
    8. Summary
  11. Enterprise Design Patterns and Considerations
    1. Design considerations
      1. Managing heterogeneity
      2. Process alignment
      3. Message affinity
      4. Service discovery
      5. Identity mapping
    2. Integration design patterns
      1. Integrating with an existing system of record
      2. Integrating with an operational data store for blockchain analytics
      3. Microservice and event-driven architecture
    3. Resiliency and fault tolerance
      1. Reliability and availability
      2. Serviceability
    4. Summary
  12. Agility in a Blockchain Network
    1. Defining the promotion process
      1. Aligning the promotion process to components
      2. Smart contract considerations
      3. Integration layer considerations
    2. Continuous integration
      1. Promotion process overview
      2. Configuring a CI pipeline
        1. Customizing the pipeline process
        2. Publishing our smart contract package
      3. Configuring your GitHub repository
        1. Setting the code owners of our smart contract
        2. Protecting the master branch
        3. Configuring Git for commit signing and validation
        4. Configuring GPG on your local workstation
    3. Testing considerations for the trade network
      1. Unit testing
        1. Defining our user story and business scenarios
        2. Test fixture
        3. Mapping scenarios to Mocha
        4. Implementing unit tests
      2. Integration testing
        1. Setting up the integration tests
        2. Testing the trade business process
        3. Running the integration tests
      3. Exercising the end-to-end process
        1. Creating a new transaction
        2. Adding the mergeTrade unit test
        3. Submitting a pull request with a signed commit
        4. Releasing the new version
    4. Continuous delivery
      1. Kubernetes as a platform for Hyperledger Fabric
      2. Infrastructure as Code and Ansible
      3. Deployment process overview
        1. Centralization versus decentralization
        2. Notifying the consortium
      4. Applying these concepts to our network
        1. Setting up Ansible
        2. Starting the network using Ansible
        3. Pulling a new chaincode release
        4. Upgrading the network
    5. Summary
  13. Governance – A Necessary Evil of Regulated Industries
    1. Decentralization and governance
    2. Exploring business models
      1. Blockchain benefits
        1. Supply chain management
        2. Healthcare
        3. Letters of credit
      2. From benefits to profits
      3. B2B and B2C considerations
      4. Network business models
        1. Founder-led networks
        2. Consortium-based networks
        3. Community-based networks
        4. Hybrid models
    3. Funding models
      1. Token-based models
      2. Traditional models
      3. Membership considerations
    4. Governance of a business network
    5. Mapping business roles to blockchain roles
    6. Governance structure
      1. Centralized governance
        1. Exploring the levels of governance
        2. Mapping governance to organizational structure
      2. Decentralized governance
    7. Business domains and processes
      1. Membership life cycle
      2. Funding and fees
      3. Regulation
      4. Education
      5. Service life cycle
      6. Disputes
    8. Blockchain as a governance tool
      1. Managing network policies
      2. Types of policies
        1. Organization level versus network level
        2. Consortiums
        3. Channels
        4. Endorsements
    9. Summary
  14. Life in a Blockchain Network
    1. Modifying or upgrading a Hyperledger Fabric application
      1. Changing requirements and update modes
      2. Fabric blockchain and application life cycle
      3. Network organization resource updates
        1. Adding a peer to an organization
        2. Installing a smart contract on a new peer
        3. Removing a peer from an organization
      4. Channel configuration updates
        1. Adding a new organization to a live network
        2. Prerequisites for adding a new organization to the network
        3. Generating cryptographic material for the new organization
        4. Generating channel artifacts for the new organization
        5. Generating the cryptographic and channel artifacts in one operation
        6. Docker containers for the new organization
        7. Launching network components for the new organization
        8. Updating the channel configuration
        9. Joining the new organization's peer to the channels
        10. Setting the anchor peer for the new organization's peer on the channels
        11. Installing contracts on peers in the new organization
      5. Smart contract and policy updates
        1. Overview of contract update procedures
        2. Modification in contract business logic
        3. Endorsement policy updates
        4. Upgrading contract code and endorsement policies on the channels
        5. Augmenting the distributed application
      6. Platform upgrades
        1. Upgrading a live four-organization trade network
    2. System monitoring and performance
      1. Measurement and analytics
      2. Fabric system measurement considerations
      3. Metrics for performance analysis
      4. Fabric application performance measurement and data collection
        1. Measuring system health indicators
        2. Fabric support for component monitoring
        3. Profiling containers and applications
        4. Measuring application performance
      5. Fabric engineering guidelines for performance
        1. Platform performance characteristics
        2. System bottlenecks
        3. Configuration and tuning
        4. Ledger data availability and caching
        5. Fabric performance measurement and benchmarking
    3. Summary
  15. Hyperledger Fabric Security
    1. Hyperledger Fabric design goals impacting security
    2. Hyperledger Fabric architecture
      1. Fabric CA or membership service provider
      2. Peer
      3. Smart contracts
      4. Ledger
      5. Private data
      6. Ordering node
        1. History of the ordering service in Hyperledger Fabric
    3. Network bootstrap and data governance: the first step toward security
      1. Bootstrapping the network with known members
      2. Defining the process for sharing data
      3. Defining the data model of the shared data
      4. Mapping data sharing needs Hyperledger-based mechanisms for operational agility
      5. Adding new members to the network (or channel)
      6. Deploying, instantiating, and upgrading smart contracts on peers in the network
    4. Strong identities: the key to the security of the Hyperledger Fabric network
      1. Bootstrapping Fabric CA
        1. Register
        2. Enroll
      2. Revoking identities
      3. Practical considerations in managing users in Fabric CA
    5. Smart contract security
      1. How is a smart contract shared with other endorsing peers?
      2. Who can install smart contracts?
      3. Smart contract encryption
      4. Attribute-based access control
    6. Common threats and how Hyperledger Fabric mitigates them
    7. Hyperledger Fabric and quantum computing
    8. GDPR considerations
    9. Summary
  16. Blockchain's Future, Protocol Commercialization, and Challenges Ahead
    1. Summary of key Hyperledger projects
      1. Hyperledger frameworks: business blockchain technology
      2. Distributed ledger frameworks
      3. Hyperledger libraries
      4. Hyperledger tools
    2. Blockchain's future and the challenges ahead
      1. Addressing the divide: the enterprise blockchain and cryptoasset-driven ecosystem
      2. Interoperability: understanding business service integration
      3. Blockchain protocol commercialization: a "BizTech" agenda
        1. Layer 1 – Foundational trust and transaction layer
        2. Layer 2 – Protocol optimization layer
        3. Layer 3 – Blockchain business service layer
        4. Layer 4 – Industry/business application layer
        5. Adjunct business layer
        6. Devising modularity to avoid lock-in linkages
      4. Scalability and economic viability of blockchain solutions
    3. How can you help and stay engaged?
    4. Summary
  17. Another Book You May Enjoy
  18. Index

Product information

  • Title: Blockchain with Hyperledger Fabric - Second Edition
  • Author(s): Nitin Gaur, Anthony O'Dowd, Petr Novotny, Luc Desrosiers, Venkatraman Ramakrishna, Salman A. Baset
  • Release date: November 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839218750