什么是区块链?
区块链是一种去中心化账本,由多台相互独立的计算机(称为节点)共同维护。区块链网络不是依赖单一权威来追踪交易并维护状态,而是将这项职责分散到许多节点上。每个节点都保存一份自己的账本副本,并与其他节点协作,就哪些交易有效以及应按什么顺序执行达成一致。 你可以把区块链或去中心化账本看作是一份由几十个人分别独立维护的共享电子表格。每个人都有自己的副本,并且都遵循同样的更新规则。当有人想要做出更改时,整个群体会就该更改是否有效以及应按什么顺序发生达成一致。如果每个人都正确遵守规则,所有副本最终都会保持一致。如果有人试图在不遵循共识规则的情况下修改自己的副本,其他节点会拒绝其版本,因为它与网络达成的一致结果不匹配。这使区块链具备抗篡改能力:你需要控制网络中的多数力量,才能强行推动一次无效更改。为什么需要区块链?
传统数字系统通常依赖一个中心化权威来维护准确记录。比如,银行会维护账户余额的权威记录。用户信任银行能够正确处理交易,并防止诸如同一笔钱被花费两次之类的问题(也称为双花问题)。 区块链要解决的是一个更困难的挑战:在不依赖单一中心化权威的情况下,维护准确且可信的记录。在去中心化网络中,没有任何单一实体拥有最终决定权。相反,彼此并不信任的独立节点必须就账本状态达成一致。这要求同时解决几个问题:- 通过共识达成一致:节点如何就哪些交易被纳入,以及它们按什么顺序执行达成一致?
- 通过可验证篡改的密码学实现安全性:网络如何防止恶意节点创建欺诈交易或篡改历史?
- 通过确定性执行实现一致性:即使存在网络延迟和潜在故障,所有节点如何仍能维护完全相同的账本副本?
状态机:区块链的基础
从本质上讲,区块链是可复制的、确定性的状态机。什么是状态机?
在计算机科学中,**状态(State)**表示系统在某一特定时刻的全部当前数据。例如,在银行应用中,状态包括所有账户余额。在区块链或去中心化账本的语境中,状态包括所有账户余额、智能合约数据以及链上追踪的其他信息。 **状态机(state machine)**是一种通过应用交易,从一个状态迁移到另一个状态的系统。每笔交易都描述了一个应当改变状态的动作。 下面是一个使用银行账户的简单状态机示例:为什么说“确定性”?
**确定性(Deterministic)**意味着:同一笔交易作用于同一个状态时,总会产生相同的结果。这个性质对区块链和去中心化账本至关重要。 继续使用银行的例子:如果用户 A 起始有 $100,并向用户 B 发送 $30,那么其余额最终一定会变成 $70。无论由谁处理这笔交易、何时处理,或者从初始状态重复计算多少次,结果都始终相同。 在区块链中,确定性保证了所有节点都能独立得出相同的最终状态。如果逻辑不是确定性的,不同节点就会得到不同版本的账本,网络也就会失效。 在实践中,区块链应用必须避免引入非确定性来源,例如本地时间、浮点运算或外部网络调用。为什么说“可复制”?
**可复制(Replicated)**指的是:许多相互独立的节点各自运行着同一个状态机的副本。维护状态的不是单一中心服务器,而是多个独立节点分别维护自己完整的副本。 当一个新区块被加入区块链时,每个节点都会:- 接收包含有序交易列表的区块
- 通过本地状态机独立执行每一笔交易
- 得到相同的新状态(这要归功于确定性)。
区块链如何工作
理解了状态机之后,下一步就是看看区块链如何利用状态机,在许多相互独立的节点之间维护共享账本。节点
**节点(node)**是参与区块链网络的一台计算机。每个节点都存储着区块链状态的完整副本,接收并验证新交易,参与共识以就新区块达成一致,并执行交易以更新本地状态。一些节点称为验证者,它们通过提议区块和对区块投票直接参与共识;另一些节点则只是复制并验证整条链。 在公开、无许可的区块链中,通常任何人都可以运行一个节点,这使网络具备去中心化特性:没有任何单一实体控制账本。交易
交易(transaction,tx)是对变更区块链状态的请求。在 Cosmos SDK 区块链中,交易包含一个或多个消息(messages),这些消息表示需要执行的具体动作。消息可以表示许多不同的操作:- 将代币从一个账户转移到另一个账户
- 创建或更新智能合约
- 质押代币以成为验证者
- 对治理提案进行投票
区块
出于效率考虑,交易会被分组打包成区块(blocks)。区块是一批由网络一起处理的交易。每个区块都会通过密码学方式链接到前一个区块,从而形成一条区块链(chain of blocks)。这种链式结构会生成一段永久且可检测篡改的历史:如果有人试图修改过去的一笔交易,就会破坏它与之后所有区块之间的密码学链接,使网络能够明显发现篡改行为。从交易到区块
区块链不会逐笔处理交易,而是为了效率将它们分组为区块。流程如下:- 交易池(Mempool):节点将有效交易收集到一个称为内存池的等待区域中
- 区块提议:指定节点(称为验证者或区块提议者)从内存池中选择交易,并将其提议为下一个区块
- 共识:节点运行共识算法,就接受哪个候选区块以及顺序为何达成一致
- 区块提交:一旦达成共识,该区块即被最终确认并添加到区块链中
- 状态迁移:每个节点将新区块中的交易应用到本地状态机上,从而更新自己的状态副本
共识
**共识(Consensus)**是节点在彼此独立运行的情况下,仍能就单一权威版本的区块链达成一致的机制。在上面的第 3 步中,节点必须就接下来添加哪个区块以及添加顺序达成共识。 交易排序至关重要。考虑两笔交易:“用户 A 向用户 B 发送 100 个代币”和“用户 A 向用户 C 发送 100 个代币”。如果用户 A 只有 100 个代币,那么顺序就很关键,只有第一笔交易能够成功。不同节点接收到这些交易的顺序可能不同,因此需要通过共识来建立一个所有节点都遵循的、唯一且规范的排序。这可以防止双花问题,并确保确定性执行在每个节点上都产生相同结果。 共识算法保证:- 所有诚实节点都对同一序列的区块达成一致
- 即使部分节点离线或作恶,网络仍可继续运行
- 交易在所有节点之间保持一致的排序
区块如何链接
每个区块都包含一个带有区块元数据的区块头(block header)。关键在于,每个区块头都包含前一个区块头的密码学哈希值。 **哈希(hash)**就像数字指纹:它接收任意大小的数据,并输出一个唯一的、固定长度的字符串。例如,对文本 “Hello World” 进行哈希,可能会得到类似 “a591a6d4…” 的结果。其关键属性是,即使输入只发生极小变化(比如把 “Hello World” 改成 “Hello World!”),也会产生完全不同的哈希值。哈希函数是单向的,这意味着你无法从哈希值反推出原始数据。哈希函数还具备抗碰撞性:不会有两个不同输入产生相同的哈希值。 Cosmos 区块链使用 SHA-256 作为区块头和其他密码学操作中的哈希函数,该算法由 NSA 创建,用于将区块安全地链接在一起。这提供了密码学安全性:找到一个不同输入却产生相同哈希输出,在计算上几乎不可行,因此几乎不可能在不被发现的情况下篡改区块数据。 区块头还包含对区块交易和状态进行承诺的默克尔根,使节点和轻客户端能够高效验证数据。 这种哈希机制构成了一条可检测篡改的链。你将在下一节的演示中看到它如何发挥作用。区块链演示:不可篡改性
下面的演示展示了一条包含三个区块的区块链。你可以看到,每个区块都通过区块头中的哈希与前一个区块相连。尝试修改某个区块中的数据,看看这会如何改变该区块的哈希,并使其后所有区块失效。你还可以点击“Add Block”按钮向链中添加新区块。这是一个简化演示。实际的 Cosmos SDK 区块还包含额外的安全特性,例如验证者签名、时间戳、共识信息,以及用于交易验证的 Merkle 根。这里展示的密码学链接只是区块链安全性的其中一部分。
接下来是什么?
现在你已经理解了区块链的基础概念(状态机、确定性执行、复制以及密码学链接),下一步就是了解 Cosmos SDK 实际上如何实现这些概念。 在 区块链架构 中,你将了解:- CometBFT 如何处理共识与网络,以维护复制状态机
- 连接共识与应用逻辑的应用区块链接口(ABCI)
- Cosmos SDK 如何实现状态机层
- Cosmos 区块链应用的完整架构
What Is a Blockchain?
A blockchain is a decentralized ledger that multiple independent computers (called nodes) maintain together. Instead of relying on a single authority to track transactions and maintain state, blockchain networks distribute this responsibility across many nodes. Each node keeps its own copy of the ledger and works with other nodes to agree on what transactions are valid and in what order they should be applied. You can think of a blockchain or decentralized ledger as a shared spreadsheet that dozens of people maintain independently. Everyone has their own copy, and they all follow the same rules for updating it. When someone wants to make a change, the group agrees on whether that change is valid and what order it should happen in. If everyone follows the rules correctly, all copies end up identical. If someone tries to modify their copy without following the consensus rules, the other nodes will reject their version because it doesn’t match what the network agreed upon. This makes blockchains resistant to tampering: you’d need to control a majority of the network to force through an invalid change.Why Blockchains?
Traditional digital systems usually rely on a central authority to maintain accurate records. A bank, for example, maintains the definitive record of account balances. Users trust the bank to process transactions correctly and prevent problems like spending the same money twice (also known as the double-spend problem). Blockchains solve a more difficult challenge: maintaining accurate, trustworthy records without relying on a singular, central authority. In a decentralized network, no single entity has the final say. Instead, independent nodes must agree on the state of the ledger even though they don’t trust each other. This requires solving several problems simultaneously:- Agreement through consensus: How do nodes agree on which transactions are included and in what order they’re applied?
- Security through tamper-evident cryptography: How can the network prevent malicious nodes from creating fraudulent transactions or rewriting history?
- Consistency through deterministic execution: How do all nodes maintain identical copies of the ledger despite network delays and potential failures?
State Machines: The Foundation of Blockchains
At their core, blockchains are replicated, deterministic state machines.What Is a State Machine?
In computer science, State represents all the current data in a system at a specific point in time. For example, in a bank application, the state includes all account balances. In the context of a blockchain or decentralized ledger, the state includes all account balances, smart contract data, and other information the chain tracks. A state machine is a system that moves from one state to another by applying transactions. Each transaction describes an action that should change the state. Here’s a simple example of a state machine using a bank account:Why “Deterministic”?
Deterministic means that the same transaction applied to the same state will always produce the same result. This property is critical for blockchains and decentralized ledgers. Using the bank example: if User A starts with $100 and sends User B $30, their balance will always become $70. It doesn’t matter who processes this transaction, when they process it, or how many times they recalculate it from the initial state: the result will always be the same. In a blockchain, determinism ensures that all nodes independently arrive at the same final state. If the logic weren’t deterministic, different nodes would end up with different versions of the ledger, and the network would break down. In practice, blockchain applications must avoid sources of non-determinism such as local time, floating-point math, or external network calls.Why “Replicated”?
Replicated refers to the fact that many independent nodes each run their own copy of the same state machine. Instead of one central server maintaining the state, multiple independent nodes each maintain their own complete copy. When a new block is added to the blockchain, every node:- Receives the block with its ordered list of transactions
- Independently executes each transaction through their local state machine
- Arrives at the same new state (thanks to determinism).
How Blockchains Work
With an understanding of state machines, the next step is to see how blockchains use them to maintain a shared ledger across many independent nodes.Nodes
A node is a computer that participates in the blockchain network. Each node stores a complete copy of the blockchain’s state, receives and validates new transactions, participates in consensus to agree on new blocks, and executes transactions to update its local state. Some nodes, called validators, participate directly in consensus by proposing and voting on blocks, while other nodes simply replicate and verify the chain. In public, permissionless blockchains, anyone can typically run a node, which makes the network decentralized: no single entity controls the ledger.Transactions
A transaction (tx) is a request to change the blockchain’s state. In Cosmos SDK blockchains, transactions contain one or more messages that represent the specific actions to be executed. These messages can represent many different actions:- Transferring tokens from one account to another
- Creating or updating a smart contract
- Staking tokens to become a validator
- Voting on a governance proposal
Blocks
Transactions are grouped together into blocks for efficiency. A block is a batch of transactions that the network processes together. Each block is cryptographically linked to the previous block, forming a chain of blocks. This chain structure creates a permanent, tamper-evident history: if someone tries to alter a past transaction, it would break the cryptographic link to all subsequent blocks, making the tampering obvious to the network.From Transactions to Blocks
Rather than processing transactions one at a time, blockchains group them into blocks for efficiency. Here’s how it works:- Transaction pool (Mempool): Nodes collect valid transactions into a waiting area called the mempool
- Block proposal: A designated node (called a validator or block proposer) selects transactions from the mempool and proposes them as the next block
- Consensus: Nodes run a consensus algorithm to agree on which proposed block to accept and in what order
- Block commitment: Once consensus is reached, the block becomes final and is added to the blockchain
- State transition: Each node applies the transactions in the new block to their local state machine, updating their copy of the state
Consensus
Consensus is the mechanism by which nodes agree on a single, authoritative version of the blockchain despite operating independently. In step 3 above, nodes must reach consensus on which block to add next and in what order. Transaction ordering is critical. Consider two transactions: “User A sends 100 tokens to User B” and “User A sends 100 tokens to User C.” If User A only has 100 tokens, the order matters—only the first transaction can succeed. Different nodes might receive these transactions in different orders, so consensus is used to establish a single, canonical ordering that all nodes follow. This prevents the double-spend problem and ensures that deterministic execution produces identical results on every node. Consensus algorithms ensure that:- All honest nodes agree on the same sequence of blocks
- The network can continue operating even if some nodes are offline or malicious
- Transactions are ordered consistently across all nodes
How Blocks Are Linked
Each block contains a block header with metadata about the block. Critically, every block header includes a cryptographic hash of the previous block’s header. A hash is like a digital fingerprint: it takes data of any size and produces a unique, fixed-length string of characters. For example, hashing the text “Hello World” might produce something like “a591a6d4…”. The key property is that even a tiny change to the input (like changing “Hello World” to “Hello World!”) produces a completely different hash. Hash functions are one-way, which means you can’t reverse a hash back to the original data. Hash functions are also collision-resistant: no two different inputs produce the same hash. Cosmos blockchains use SHA-256 which was created by the NSA as the hash function for block headers and other cryptographic operations to securely link blocks together. This provides cryptographic security: finding a different input that produces the same hash output is computationally infeasible, making it virtually impossible to tamper with block data without detection. Block headers also include Merkle roots that commit to the block’s transactions and state, allowing nodes and light clients to verify data efficiently. This hashing mechanism creates a tamper-evident chain. You can see this in action in the demo in the next section.Blockchain Demo: Immutability
The demo below shows a blockchain with three blocks. You can see how each block is linked to the previous block by the hash in the block header. Try changing the data in a block to see how it changes the hash of that block and invalidates all subsequent blocks. You can add new blocks to the chain by clicking the “Add Block” button.This is a simplified demonstration. Actual Cosmos SDK blocks include additional security features like validator signatures, timestamps, consensus information, and Merkle roots for transaction verification. The cryptographic linking shown here is just one part of blockchain security.
What’s Next?
Now that you understand blockchain fundamentals (state machines, deterministic execution, replication, and cryptographic linking), the next step is to learn how Cosmos SDK actually implements these concepts. In Blockchain Architecture, you’ll explore:- How CometBFT handles consensus and networking to maintain the replicated state machine
- The Application Blockchain Interface (ABCI) that connects consensus to application logic
- How the Cosmos SDK implements the state machine layer
- The complete architecture of a Cosmos blockchain application