Peer-to-Peer Protocols and Networks
At the foundation of DeFi, the technology stack begins with a peer-to-peer (P2P) protocol and network. A protocol is the set of rules and standards that govern direct communication between different peers in a network, whereas the network itself consists of independent people or businesses that operate the hardware and software needed to participate in the network. Specifically, in a P2P network, there are two or more independent participants who operate computers (nodes) and share authority and storage of the data via the internet. There is no need for a central server in a P2P network, and therefore, no single entity has control over it. This differs from the more popular client-server model, where users request and receive services from a centralized server that stores, manages, and protects the data; for example, a user’s device interacts with Facebook by sending a request to Facebook’s servers, which then retrieves requested data—posts, likes, etc.—and runs the application. Essentially, Facebook’s parent company, Meta, has complete control over who can or cannot access their data and applications.
A mechanism for storing data and communications for a P2P network is known as a public blockchain, which is a type of distributed ledger technology. Essentially, each node in the P2P network runs a software application that enables it to communicate with other nodes in the network, validate new transactions and blocks according to the network’s rules, maintain a copy of the blockchain, and have the option to participate in the creation of blocks.
Hashing
Information stored in a block is identified by a hash value and includes the hash value of the previous block to link two blocks together, creating a chain—hence, the term “blockchain.” A hash value is generated through cryptographic hashing, which is a mathematical process of inputting data into a hash function to output a unique string of alphanumeric characters used to identify blocks in a blockchain and link them together. Essentially, a hash function is an algorithm that takes the transactions in a block, the hash value of the previous block in the chain, and other relevant block data as input, and generates a bit-string that serves as a representation of that data—i.e., the hash value. Importantly, if that data were altered in the slightest way, the hash function would generate a completely different hash value.
In short, a hash value represents a block’s data such that any alterations to the underlying data are readily identifiable. This plays a crucial role in maintaining the immutability of blockchain transactions because once a transaction is recorded, it cannot be altered or deleted without also altering the block’s hash value and disrupting the chain of connecting hashes. Once a block is created, it is verified by a consensus mechanism, which is the process by which the network’s nodes agree on the validity of transactions and the current state of the blockchain.
Consensus Mechanisms
Before a transaction reaches consensus (explained in the next section), it undergoes initial verification by the network’s nodes for completion and correctness (e.g., signature validity, balance sufficiency, etc.). Once verified, the transaction is placed in a memory pool, or mempool—a pool of unconfirmed transactions—where it awaits inclusion in a block by a miner or validator. When a block is proposed, each node receives said block, independently validates its authenticity, and adds it to their copy of the blockchain. Through this process, the network reaches consensus on what is the correct chain of transactions—also known as network synchronization.
Network synchronization is an ongoing process by which all nodes in a network update their copies of the blockchain to ensure they all hold the same, most current version of the blockchain. When a new block is created or verified by a node, the node then broadcasts it to neighboring nodes in the network and the process continues as such. When a node receives a new block that is attached to a part of the blockchain that it doesn’t have, it will compare this chain to its own. The node adopts the chain based on criteria for chain selection that varies depending on the consensus mechanism. The most popular forms of consensus mechanisms for blockchain networks are Proof-of-Work (PoW) and Proof-of-Stake (PoS).
Proof-of-Work
PoW is most notably used in the Bitcoin network and requires nodes, known as miners, to compete to solve a cryptographic puzzle by finding a specific value known as a nonce. Miners combine this nonce with the block’s data (e.g., previous block hash, timestamp, etc.) through a hash function, which then creates the hash value. The goal is to find a hash value that meets a specific criterion set by the network. Miners essentially input different nonces through the hash function until one succeeds. Then the network checks that the hash value and the block’s transactions are correct. If everything is correct, the miner is rewarded with a newly minted network token, such as a bitcoin.
Mining requires computing power and energy, which is used as an incentive system and security mechanism. A bad actor attempting to introduce a fraudulent block is disincentivized by the high energy cost required to solve for the hash value that would be lost when the network does not validate their block. Essentially, the actor would incur a significant energy cost for nothing in return. In order for a bad actor to successfully implement their desired block, they would need to control over 51% of the network’s computational power to validate their block. This would take a tremendous amount of energy and would cost them more than they would profit, especially as networks like Bitcoin are continuously expanding.
Nodes in a PoW network adopt the chain with the largest cumulative difficulty – i.e., the greatest amount of computational work from cryptographic hashing – as a consensus for maintaining network synchronization. This computational work signals agreement among miners, and is therefore considered to contain the most valid and trusted blocks.
Proof-of-Stake
PoS, notably adopted by the Ethereum network among many others, uses a different consensus approach. PoS may divide block production into time intervals known as slots. For each slot, the blockchain protocol randomly selects a validator to propose a new block and broadcast it to the larger set of validators (i.e., attesters), so they can then attest (vote on) the validity, or correctness, of the block and add it to the chain once it receives a threshold of attestations. Meanwhile, almost immediately, the next slot begins, and the process starts anew. As a result, the network uses less energy than in PoW, because nodes no longer need to expend computational power to compete to solve a cryptographic puzzle.
To prevent bad actors from manipulating the information stored on a network, staking requires providing collateral to the network in order to become a validator. Successful validators and attesters are rewarded with a newly minted network token, such as an ether on the Ethereum network. Staking also disincentivizes malicious behavior through punitive measures. If a validator acts dishonestly or negligently, their staked tokens are slashed, meaning the blockchain’s underlying software automatically reduces the validator’s staked tokens once the network detects the behavior. Thus, while the selection process of validators is random, the probability of being selected increases with the amount staked, because the validator has more to lose if they behave maliciously.
Unlike in a PoW network, where nodes adopt a chain based on the computational work done, nodes in a PoS network adopt a chain based on the amount of stake-weighted attestation votes backing it. When the group of validator nodes stake their tokens, they do so to participate in the validation process. And even if they are not chosen as the validator for a specific block, they attest blocks and their staked tokens remain active and could be used in future block validations. Therefore, following the most attestation votes best reflects the consensus of the network, as it represents the greatest economic commitment from network participants.
Public-Key Cryptography
A novel aspect of cryptocurrency transactions is that they are done in a P2P manner—i.e., without a third-party intermediary. This is securely done through a form of asymmetric cryptography—also known as public key cryptography—so that a user is not required to trust an intermediary or another user to transact.
A user can generate a private key by using cryptographic algorithms that produce a random string of characters. The private key is then the basis for mathematically generating the corresponding public key. Importantly, while public key generation is easily computed, it is nearly impossible to reverse-engineer the private key from the public key—hence, making it a secure cryptographic process.
Asymmetric cryptography is used in authenticating the sender’s identity and the transaction’s information by producing a digital signature. This process begins with the automatic generation of a cryptographic hash of the transaction—much like the hash generated for a block, this hash serves as an identifier and consists of a long string of characters. The sender then uses their private key to sign the transaction’s hash, producing a digital signature. Upon receiving the transaction, the network uses the sender’s public key to verify the digital signature and recover the original hash. Also upon receipt, a new hash is generated in the same manner as the original hash, and because it is generated using the same transaction data, the two hashes are identical. This allows the network to compare the hashes and verify that the transaction has not been altered in transit and confirm its authenticity. Overall, this process not only authenticates the sender’s identity but also ensures the integrity of the transaction.
Lastly, to make sending cryptocurrency more user-friendly, a blockchain address is mathematically generated from a public key as a shorter string of characters. This serves as a more practical representation used for securely sending and receiving transactions. With a better understanding of asymmetric cryptography, it is evident that this mechanism provides a variety of benefits such as: securing transactions and user information without needing an intermediary, enabling non-repudiation, and eliminating the need to trust other users.