github.com/kaituanwang/hyperledger@v2.0.1+incompatible/docs/source/ledger/ledger.md (about)

     1  # Ledger
     2  
     3  **Audience**: Architects, Application and smart contract developers,
     4  administrators
     5  
     6  A **ledger** is a key concept in Hyperledger Fabric; it stores important factual
     7  information about business objects; both the current value of the attributes of
     8  the objects, and the history of transactions that resulted in these current
     9  values.
    10  
    11  In this topic, we're going to cover:
    12  
    13  * [What is a Ledger?](#what-is-a-ledger?)
    14  * [Storing facts about business objects](#ledgers-facts-and-states)
    15  * [A blockchain ledger](#a-blockchain-ledger)
    16  * [The world state](#world-state)
    17  * [The blockchain data structure](#blockchain)
    18  * [How blocks are stored in a blockchain](#blocks)
    19  * [Transactions](#transactions)
    20  * [World state database options](#world-state-database-options)
    21  * [The **Fabcar** example ledger](#example-ledger-fabcar)
    22  * [Ledgers and namespaces](#namespaces)
    23  * [Ledgers and channels](#channels)
    24  
    25  ## What is a Ledger?
    26  
    27  A ledger contains the current state of a business as a journal of transactions.
    28  The earliest European and Chinese ledgers date from almost 1000 years ago, and
    29  the Sumerians had [stone
    30  ledgers](http://www.sciencephoto.com/media/686227/view/accounting-ledger-sumerian-cuneiform)
    31  4000 years ago -- but let's start with a more up-to-date example!
    32  
    33  You're probably used to looking at your bank account. What's most important to
    34  you is the available balance -- it's what you're able to spend at the current
    35  moment in time. If you want to see how your balance was derived, then you can
    36  look through the transaction credits and debits that determined it. This is a
    37  real life example of a ledger -- a state (your bank balance), and a set of
    38  ordered transactions (credits and debits) that determine it. Hyperledger Fabric
    39  is motivated by these same two concerns -- to present the current value of a set
    40  of ledger states, and to capture the history of the transactions that determined
    41  these states.
    42  
    43  ## Ledgers, Facts and States
    44  
    45  A ledger doesn't literally store business objects -- instead it stores **facts**
    46  about those objects. When we say "we store a business object in a ledger" what
    47  we really mean is that we're recording the facts about the current state of an
    48  object, and the facts about the history of transactions that led to the current
    49  state. In an increasingly digital world, it can feel like we're looking at an
    50  object, rather than facts about an object. In the case of a digital object, it's
    51  likely that it lives in an external datastore; the facts we store in the ledger
    52  allow us to identify its location along with other key information about it.
    53  
    54  While the facts about the current state of a business object may change, the
    55  history of facts about it is **immutable**, it can be added to, but it cannot be
    56  retrospectively changed. We're going to see how thinking of a blockchain as an
    57  immutable history of facts about business objects is a simple yet powerful way
    58  to understand it.
    59  
    60  Let's now take a closer look at the Hyperledger Fabric ledger structure!
    61  
    62  
    63  ## The Ledger
    64  
    65  In Hyperledger Fabric, a ledger consists of two distinct, though related, parts
    66  -- a world state and a blockchain. Each of these represents a set of facts about
    67  a set of business objects.
    68  
    69  Firstly, there's a **world state** -- a database that holds **current values**
    70  of a set of ledger states. The world state makes it easy for a program to directly
    71  access the current value of a state rather than having to calculate it by traversing
    72  the entire transaction log. Ledger states are, by default, expressed as **key-value** pairs,
    73  and we'll see later how Hyperledger Fabric provides flexibility in this regard.
    74  The world state can change frequently, as states can be created, updated and deleted.
    75  
    76  Secondly, there's a **blockchain** -- a transaction log that records all the
    77  changes that have resulted in the current the world state. Transactions are
    78  collected inside blocks that are appended to the blockchain -- enabling you to
    79  understand the history of changes that have resulted in the current world state.
    80  The blockchain data structure is very different to the world state because once
    81  written, it cannot be modified; it is **immutable**.
    82  
    83  ![ledger.ledger](./ledger.diagram.1.png) *A Ledger L comprises blockchain B and
    84  world state W, where blockchain B determines world state W. We can also say that
    85  world state W is derived from blockchain B.*
    86  
    87  It's helpful to think of there being one **logical** ledger in a Hyperledger
    88  Fabric network. In reality, the network maintains multiple copies of a ledger --
    89  which are kept consistent with every other copy through a process called
    90  **consensus**. The term **Distributed Ledger Technology** (**DLT**) is often
    91  associated with this kind of ledger -- one that is logically singular, but has
    92  many consistent copies distributed throughout a network.
    93  
    94  Let's now examine the world state and blockchain data structures in more detail.
    95  
    96  ## World State
    97  
    98  The world state holds the current value of the attributes of a business object
    99  as a unique ledger state. That's useful because programs usually require the
   100  current value of an object; it would be cumbersome to traverse the entire
   101  blockchain to calculate an object's current value -- you just get it directly
   102  from the world state.
   103  
   104  ![ledger.worldstate](./ledger.diagram.3.png) *A ledger world state containing
   105  two states. The first state is: key=CAR1 and value=Audi. The second state has a
   106  more complex value: key=CAR2 and value={model:BMW, color=red, owner=Jane}. Both
   107  states are at version 0.*
   108  
   109  A ledger state records a set of facts about a particular business object. Our
   110  example shows ledger states for two cars, CAR1 and CAR2, each having a key and a
   111  value. An application program can invoke a smart contract which uses simple
   112  ledger APIs to **get**, **put** and **delete** states. Notice how a state value
   113  can be simple (Audi...) or compound (type:BMW...). The world state is often
   114  queried to retrieve objects with certain attributes, for example to find all red
   115  BMWs.
   116  
   117  The world state is implemented as a database. This makes a lot of sense because
   118  a database provides a rich set of operators for the efficient storage and
   119  retrieval of states.  We'll see later that Hyperledger Fabric can be configured
   120  to use different world state databases to address the needs of different types
   121  of state values and the access patterns required by applications, for example in
   122  complex queries.
   123  
   124  Applications submit transactions which capture changes to the world state, and
   125  these transactions end up being committed to the ledger blockchain. Applications
   126  are insulated from the details of this [consensus](../txflow.html) mechanism by
   127  the Hyperledger Fabric SDK; they merely invoke a smart contract, and are
   128  notified when the transaction has been included in the blockchain (whether valid
   129  or invalid). The key design point is that only transactions that are **signed**
   130  by the required set of **endorsing organizations** will result in an update to
   131  the world state. If a transaction is not signed by sufficient endorsers, it will
   132  not result in a change of world state. You can read more about how applications
   133  use [smart contracts](../smartcontract/smartcontract.html), and how to [develop
   134  applications](../developapps/developing_applications.html).
   135  
   136  You'll also notice that a state has a version number, and in the diagram above,
   137  states CAR1 and CAR2 are at their starting versions, 0. The version number is for
   138  internal use by Hyperledger Fabric, and is incremented every time the state
   139  changes. The version is checked whenever the state is updated to make sure the
   140  current states matches the version at the time of endorsement. This ensures that
   141  the world state is changing as expected; that there has not been a concurrent
   142  update.
   143  
   144  Finally, when a ledger is first created, the world state is empty. Because any
   145  transaction which represents a valid change to world state is recorded on the
   146  blockchain, it means that the world state can be re-generated from the
   147  blockchain at any time. This can be very convenient -- for example, the world
   148  state is automatically generated when a peer is created. Moreover, if a peer
   149  fails abnormally, the world state can be regenerated on peer restart, before
   150  transactions are accepted.
   151  
   152  ## Blockchain
   153  
   154  Let's now turn our attention from the world state to the blockchain. Whereas the
   155  world state contains a set of facts relating to the current state of a set of
   156  business objects, the blockchain is an historical record of the facts about how
   157  these objects arrived at their current states. The blockchain has recorded every
   158  previous version of each ledger state and how it has been changed.
   159  
   160  The blockchain is structured as sequential log of interlinked blocks, where each
   161  block contains a sequence of transactions, each transaction representing a query
   162  or update to the world state. The exact mechanism by which transactions are
   163  ordered is discussed [elsewhere](../peers/peers.html#peers-and-orderers);
   164  what's important is that block sequencing, as well as transaction sequencing
   165  within blocks, is established when blocks are first created by a Hyperledger
   166  Fabric component called the **ordering service**.
   167  
   168  Each block's header includes a hash of the block's transactions, as well a hash
   169  of the prior block's header. In this way, all transactions on the ledger are sequenced
   170  and cryptographically linked together. This hashing and linking makes the ledger data
   171  very secure. Even if one node hosting the ledger was tampered with, it would not be able to
   172  convince all the other nodes that it has the 'correct' blockchain because the ledger is
   173  distributed throughout a network of independent nodes.
   174  
   175  The blockchain is always implemented as a file, in contrast to the world state,
   176  which uses a database. This is a sensible design choice as the blockchain data
   177  structure is heavily biased towards a very small set of simple operations.
   178  Appending to the end of the blockchain is the primary operation, and query is
   179  currently a relatively infrequent operation.
   180  
   181  Let's have a look at the structure of a blockchain in a little more detail.
   182  
   183  ![ledger.blockchain](./ledger.diagram.2.png) *A blockchain B containing blocks
   184  B0, B1, B2, B3. B0 is the first block in the blockchain, the genesis block.*
   185  
   186  In the above diagram, we can see that **block** B2 has a **block data** D2 which
   187  contains all its transactions: T5, T6, T7.
   188  
   189  Most importantly, B2 has a **block header** H2, which contains a cryptographic
   190  **hash** of all the transactions in D2 as well as a hash of H1. In this way,
   191  blocks are inextricably and immutably linked to each other, which the term **blockchain**
   192  so neatly captures!
   193  
   194  Finally, as you can see in the diagram, the first block in the blockchain is
   195  called the **genesis block**.  It's the starting point for the ledger, though it
   196  does not contain any user transactions. Instead, it contains a configuration
   197  transaction containing the initial state of the network channel (not shown). We
   198  discuss the genesis block in more detail when we discuss the blockchain network
   199  and [channels](../channels.html) in the documentation.
   200  
   201  ## Blocks
   202  
   203  Let's have a closer look at the structure of a block. It consists of three
   204  sections
   205  
   206  * **Block Header**
   207  
   208    This section comprises three fields, written when a block is created.
   209  
   210    * **Block number**: An integer starting at 0 (the genesis block), and
   211    increased by 1 for every new block appended to the blockchain.
   212  
   213    * **Current Block Hash**: The hash of all the transactions contained in the
   214    current block.
   215  
   216    * **Previous Block Header Hash**: The hash from the previous block header.
   217  
   218    These fields are internally derived by cryptographically hashing the block
   219    data. They ensure that each and every block is inextricably linked to its
   220    neighbour, leading to an immutable ledger.
   221  
   222    ![ledger.blocks](./ledger.diagram.4.png) *Block header details. The header H2
   223    of block B2 consists of block number 2, the hash CH2 of the current block data
   224    D2, and the hash of the prior block header H1.*
   225  
   226  
   227  * **Block Data**
   228  
   229    This section contains a list of transactions arranged in order. It is written
   230    when the block is created by the ordering service. These transactions have a
   231    rich but straightforward structure, which we describe [later](#Transactions)
   232    in this topic.
   233  
   234  
   235  * **Block Metadata**
   236  
   237    This section contains the certificate and signature of the block creator which is used to verify
   238    the block by network nodes.
   239    Subsequently, the block committer adds a valid/invalid indicator for every transaction into
   240    a bitmap that also resides in the block metadata, as well as a hash of the cumulative state updates
   241    up until and including that block, in order to detect a state fork.
   242    Unlike the block data  and header fields, this section is not an input to the block hash computation.
   243  
   244  
   245  ## Transactions
   246  
   247  As we've seen, a transaction captures changes to the world state. Let's have a
   248  look at the detailed **blockdata** structure which contains the transactions in
   249  a block.
   250  
   251  ![ledger.transaction](./ledger.diagram.5.png) *Transaction details. Transaction
   252  T4 in blockdata D1 of block B1 consists of transaction header, H4, a transaction
   253  signature, S4, a transaction proposal P4, a transaction response, R4, and a list
   254  of endorsements, E4.*
   255  
   256  In the above example, we can see the following fields:
   257  
   258  
   259  * **Header**
   260  
   261    This section, illustrated by H4, captures some essential metadata about the
   262    transaction -- for example, the name of the relevant chaincode, and its
   263    version.
   264  
   265  
   266  * **Signature**
   267  
   268    This section, illustrated by S4, contains a cryptographic signature, created
   269    by the client application. This field is used to check that the transaction
   270    details have not been tampered with, as it requires the application's private
   271    key to generate it.
   272  
   273  
   274  * **Proposal**
   275  
   276    This field, illustrated by P4, encodes the input parameters supplied by an
   277    application to the smart contract which creates the proposed ledger update.
   278    When the smart contract runs, this proposal provides a set of input
   279    parameters, which, in combination with the current world state, determines the
   280    new world state.
   281  
   282  
   283  * **Response**
   284  
   285    This section, illustrated by R4, captures the before and after values of the
   286    world state, as a **Read Write set** (RW-set). It's the output of a smart
   287    contract, and if the transaction is successfully validated, it will be applied
   288    to the ledger to update the world state.
   289  
   290  
   291  * **Endorsements**
   292  
   293    As shown in E4, this is a list of signed transaction responses from each
   294    required organization sufficient to satisfy the endorsement policy. You'll
   295    notice that, whereas only one transaction response is included in the
   296    transaction, there are multiple endorsements. That's because each endorsement
   297    effectively encodes its organization's particular transaction response --
   298    meaning that there's no need to include any transaction response that doesn't
   299    match sufficient endorsements as it will be rejected as invalid, and not
   300    update the world state.
   301  
   302  That concludes the major fields of the transaction -- there are others, but
   303  these are the essential ones that you need to understand to have a solid
   304  understanding of the ledger data structure.
   305  
   306  ## World State database options
   307  
   308  The world state is physically implemented as a database, to provide simple and
   309  efficient storage and retrieval of ledger states. As we've seen, ledger states
   310  can have simple or compound values, and to accommodate this, the world state
   311  database implementation can vary, allowing these values to be efficiently
   312  implemented. Options for the world state database currently include LevelDB and
   313  CouchDB.
   314  
   315  LevelDB is the default and is particularly appropriate when ledger states are
   316  simple key-value pairs. A LevelDB database is co-located with the peer
   317  node -- it is embedded within the same operating system process.
   318  
   319  CouchDB is a particularly appropriate choice when ledger states are structured
   320  as JSON documents because CouchDB supports the rich queries and update of richer
   321  data types often found in business transactions. Implementation-wise, CouchDB
   322  runs in a separate operating system process, but there is still a 1:1 relation
   323  between a peer node and a CouchDB instance. All of this is invisible to a smart
   324  contract. See [CouchDB as the StateDatabase](../couchdb_as_state_database.html)
   325  for more information on CouchDB.
   326  
   327  In LevelDB and CouchDB, we see an important aspect of Hyperledger Fabric -- it
   328  is *pluggable*. The world state database could be a relational data store, or a
   329  graph store, or a temporal database.  This provides great flexibility in the
   330  types of ledger states that can be efficiently accessed, allowing Hyperledger
   331  Fabric to address many different types of problems.
   332  
   333  ## Example Ledger: fabcar
   334  
   335  As we end this topic on the ledger, let's have a look at a sample ledger. If
   336  you've run the [fabcar sample application](../write_first_app.html), then you've
   337  created this ledger.
   338  
   339  The fabcar sample app creates a set of 10 cars each with a unique identity; a
   340  different color, make, model and owner. Here's what the ledger looks like after
   341  the first four cars have been created.
   342  
   343  ![ledger.transaction](./ledger.diagram.6.png) *The ledger, L, comprises a world
   344  state, W and a blockchain, B. W contains four states with keys: CAR0, CAR1, CAR2
   345  and CAR3. B contains two blocks, 0 and 1. Block 1 contains four transactions:
   346  T1, T2, T3, T4.*
   347  
   348  We can see that the world state contains states that correspond to CAR0, CAR1,
   349  CAR2 and CAR3. CAR0 has a value which indicates that it is a blue Toyota Prius,
   350  currently owned by Tomoko, and we can see similar states and values for the
   351  other cars. Moreover, we can see that all car states are at version number 0,
   352  indicating that this is their starting version number -- they have not been
   353  updated since they were created.
   354  
   355  We can also see that the blockchain contains two blocks.  Block 0 is the genesis
   356  block, though it does not contain any transactions that relate to cars. Block 1
   357  however, contains transactions T1, T2, T3, T4 and these correspond to
   358  transactions that created the initial states for CAR0 to CAR3 in the world
   359  state. We can see that block 1 is linked to block 0.
   360  
   361  We have not shown the other fields in the blocks or transactions, specifically
   362  headers and hashes.  If you're interested in the precise details of these, you
   363  will find a dedicated reference topic elsewhere in the documentation. It gives
   364  you a fully worked example of an entire block with its transactions in glorious
   365  detail -- but for now, you have achieved a solid conceptual understanding of a
   366  Hyperledger Fabric ledger. Well done!
   367  
   368  ## Namespaces
   369  
   370  Even though we have presented the ledger as though it were a single world state
   371  and single blockchain, that's a little bit of an over-simplification. In
   372  reality, each chaincode has its own world state that is separate from all other
   373  chaincodes. World states are in a namespace so that only smart contracts within
   374  the same chaincode can access a given namespace.
   375  
   376  A blockchain is not namespaced. It contains transactions from many different
   377  smart contract namespaces. You can read more about chaincode namespaces in this
   378  [topic](./developapps/chaincodenamespace.html).
   379  
   380  Let's now look at how the concept of a namespace is applied within a Hyperledger
   381  Fabric channel.
   382  
   383  ## Channels
   384  
   385  In Hyperledger Fabric, each [channel](../channels.html) has a completely
   386  separate ledger. This means a completely separate blockchain, and completely
   387  separate world states, including namespaces. It is possible for applications and
   388  smart contracts to communicate between channels so that ledger information can
   389  be accessed between them.
   390  
   391  You can read more about how ledgers work with channels in this
   392  [topic](./developapps/chaincodenamespace.html#channel).
   393  
   394  
   395  ## More information
   396  
   397  See the [Transaction Flow](../txflow.html),
   398  [Read-Write set semantics](../readwrite.html) and
   399  [CouchDB as the StateDatabase](../couchdb_as_state_database.html) topics for a
   400  deeper dive on transaction flow, concurrency control, and the world state
   401  database.
   402  
   403  <!--- Licensed under Creative Commons Attribution 4.0 International License
   404  https://creativecommons.org/licenses/by/4.0/ -->