Transferred from: http://blog.csdn.net/bluecloudmatrix/article/details/51859333
Fabric originated from IBM, the original intention in order to serve industrial production, IBM will open 44,000 lines of code, is a great contribution, so that we can have the opportunity to explore the principle of the blockchain so close, but after all, IBM is from its own interests and the interests of customers, not without purpose to do this public welfare cause, While we are looking at fabric, there is a prudent thought: the blockchain does not have to be this way, it differs from the most essential non-technical of Bitcoin. Let's take a closer look at the key terminology of fabric (because some words are more accurate in English, I don't translate it hard).1. TerminologyTransaction it a request to execute a function on the ledger, which is implemented with Chaincode Transactor emitting Transaction entities, such as it may be a client application Ledger legder can be understood as a string of encrypted block chains, each containing information such as transactions and the current world state, which is a set of variables, Contains transactions execution results Chaincode This is an application-level code (also known as smart Contract, smart contracts), which is stored on the ledger as part of the transaction. That is, Chaincode to run transaction, and then run the results may modify one of the world State Validating Peer participants, which is a computer node responsible for enforcing conformance protocols, confirming transactions, and maintaining ledgers on the network. Nonvalidating Peer it is equivalent to a proxy node used to connect Transactor and adjacent VP (Validating Peer) nodes. A NVP node does not perform transactions but goes back and validates them. It will also assume the role of the event stream server and provide rest services permissioned Ledger This is a blockchain network that requires every entity and node to be a member of the network, and all anonymous nodes are not allowed to connect Privacy Used to protect and conceal the identity of chain Transactors, when a network member checks a transaction, if there is no privilege, it is unable to track the transaction transactor confidentiality This feature makes the transaction not visible to all people, only open to stakeholders Auditability use of blockchain for commercial purposes requires compliance with rules to facilitate the investigation of transactions by regulators2. Architecture
The architecture core Logic has three: membership, blockchain, and Chaincode.
2.1 Membership Services
This service is used to manage node identities, privacy, confidentiality, and auditability. In a non-permissioned blockchain network, participants do not require authorization, and all nodes are treated as if they were to submit a transaction to save the transactions in chunks (blocks). The membership service is to turn a non-permissioned blockchain network into a permissioned blockchain network with public Key Infrastructure (PKI), de-center, and consistency. 2.2 Blockchain Services
Blockchain Services manages distributed ledgers using a peer protocol built on the HTTP/2. Provides the most efficient hashing algorithm to maintain a copy of the World State. Adopt pluggable ways to set consensus agreements based on specific requirements, such as PBFT,RAFT,POW, POS, and so on. 2.3 Chaincode Services
Chaincode Services provides a secure and lightweight sandbox run mode that performs Chaincode logic on the VP node. Using the container environment, the base image is a signature-verified security image, including the OS layer and Development Chaincode language, runtime, and SDK layers, which currently support the go, Jave, and Nodejs development languages. 2.4 Events
In the Blockchain network, the VP nodes and Chaincode send events to trigger some listening actions. For example, Chaincode is a user code that can generate user events. 2.5 API and CLI
Provides a rest API that allows you to register users, query blockchain, and send transactions. Some APIs for Chaincode, which can be used to perform transactions and query trade results. For developers, the CLI can be used to quickly test chaincode, or to query the status of transactions. 3. Topology
The topological structure of distributed network is very worthy of study. In this world, there are many participants, different roles, different interests, various situations dealing with the rules and laws of distributed networks, and the rule-inadequate surrounding area. In the blockchain network, there are membership service, VP node, NVP node, one or more applications, they form a chain, then there will be multiple chain, each chain have their own security requirements and operational requirements. 3.1 Single VP node network
The simplest network consists of only one VP node, thus eliminating the consensus part.
more than 3.2 VP node networks
Multiple VP and NVP participating networks are valuable and practical. The NVP node shares the working pressure of the VP node and assumes the task of handling API requests and events.
For VP nodes, a mesh network is formed between the VP nodes to propagate the information. A NVP node can be connected to a neighboring VP node if allowed. The NVP node can be omitted if the application can communicate directly with the VP node. 3.3 Multichain
There will be more than one chain in the network, the intentions of each chain are different. 4. Protocol
Fabric is a two-way streaming message that uses GRPC to communicate with peers. Use Protocol buffer to serialize the data structure to be passed. 4.1 Message
The message is divided into four types: Discovery,transaction,synchronization and Consensus. Each message contains more sub-information, which is indicated by payload.
Payload is an opaque byte array that contains objects such as Transaction or Response. For example, if type is chain_transaction, then payload is a TRANSACTION object.
message message {enum Type {UNDEFINED = 0;
Disc_hello = 1;
Disc_disconnect = 2;
Disc_get_peers = 3;
Disc_peers = 4;
Disc_newmsg = 5;
Chain_status = 6;
Chain_transaction = 7;
Chain_get_transactions = 8;
Chain_query = 9;
Sync_get_blocks = 11;
Sync_blocks = 12;
sync_block_added = 13;
Sync_state_get_snapshot = 14;
Sync_state_snapshot = 15;
Sync_state_get_deltas = 16;
Sync_state_deltas = 17;
RESPONSE = 20;
CONSENSUS = 21;
} Type type = 1;
Bytes payload = 2;
Google.protobuf.Timestamp Timestamp = 3; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
4.1.1 Discovery Messages
A newly started node that, if Core_peer_discovery_rootnode (ROOTNODE refers to the IP of any other node in the network) is specified, it will start to run the DISCOVERY protocol. RootNode, as the first discovery node, then discovers all nodes in the entire network through the RootNode node. The Discovery protocol information is Disc_hello, and its payload is a HelloMessage object that contains information about the sending node:
Message HelloMessage {
Peerendpoint peerendpoint = 1;
UInt64 blocknumber = 2;
}
Message peerendpoint {
Peerid ID = 1;
string address = 2;
Enum Type {
UNDEFINED = 0;
VALIDATOR = 1;
Non_validator = 2;
}
Type type = 3;
Bytes pkiid = 4;
}
Message Peerid {
string name = 1;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Properties |
meaning |
| Peerid |
The name of the node defined at the beginning of the launch or defined in the configuration file |
| Peerendpoint |
Describes the node and determines whether it is a NVP and VP node |
| Pkiid |
The encryption ID of the node |
| Address |
Ip:port |
| Blocknumber |
The node currently owns the height of the blockchain |
If a node receives the Disc_hello information and finds that the block height is higher than its current block height, it will immediately send a synchronization protocol to synchronize its status with the entire network (mark: But it does not seem to be the logic of synchronization at the source level).
After this first join node completes Disc_hello this round of message delivery, the next periodic send Disc_get_peers to discover the other nodes joined the network. In order to reply to Disc_get_peers, a node sends disc_peers. 4.1.2 Synchronization Messages
The synchronization protocol starts with the above-mentioned discovery protocol, which triggers synchronization when a node discovers that its block state is inconsistent with the other nodes. The node broadcasts (broadcast) three kinds of information: sync_get_blocks, Sync_state_get_snapshot, or
Sync_state_get_deltas, at the same time the corresponding received three kinds of information: sync_blocks, Sync_state_snapshot or Sync_state_deltas.
Currently, the consensus algorithm for fabric embedding is pbft.
Sync_get_blocks will request a series of contiguous blocks, and payload will be a Syncblockrange object in the data structure being sent.
Message Syncblockrange {
UInt64 correlationid = 1;
UInt64 start = 2;
UInt64 end = 3;
}
1 2 3 4 5 1 2 3 4 5
The received node will reply to Sync_blocks, whose payload is a Syncblocks object:
Message Syncblocks {
Syncblockrange range = 1;
Repeated Block blocks = 2;
}
1 2 3 4 1 2 3 4
Start and end represent the starting and ending block. For example, Start=3, End=5, represents the block 3,4,5;start=5, End=3, representing the block 5,4,3.
Sync_state_get_snapshot will request a SNAPSHOT of the current world state, and the payload of that information is a Syncstatesnapshotrequest object:
Message syncstatesnapshotrequest {
UInt64 correlationid = 1;
}
1 2 3 1 2 3
Co