From 1ed026041447302476389bd34df0bfbbe0f7a3bc Mon Sep 17 00:00:00 2001 From: "Andreas M. Antonopoulos" Date: Wed, 18 Jun 2014 20:05:23 -0400 Subject: [PATCH] ch5 more --- ch05.asciidoc | 72 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/ch05.asciidoc b/ch05.asciidoc index e6436c4d..9637ad6c 100644 --- a/ch05.asciidoc +++ b/ch05.asciidoc @@ -18,35 +18,35 @@ A transaction's lifecycle starts with the transaction's creation, also known as [[tx_origination]] ==== Creating Transactions -In some ways it helps to think of a transaction in the same way as a paper cheque. Like a cheque, a transaction is an instrument that expresses the intent to transfer money and is not visible to the financial system until it is submitted for execution. Like a cheque, the originator of the transaction does not have to be the one signing the transaction. Transactions can be created online or offline by anyone, even if they are not able to sign for the funds spent by the check. While a cheque references a specific account as the source of the funds, a transaction references a specific previous transaction as its source (also known as an "input"), rather than an account. +In some ways it helps to think of a transaction in the same way as a paper cheque. Like a cheque, a transaction is an instrument that expresses the intent to transfer money and is not visible to the financial system until it is submitted for execution. Like a cheque, the originator of the transaction does not have to be the one signing the transaction. Transactions can be created online or offline by anyone, even if the person creating the transaction is not an authorized signer on the account. For example an accounts payable clerk might process payable cheques for signature by the CEO. Similarly, an accounts payable clerk can create bitcoin transactions and then have the CEO apply digital signatures to make them valid. While a cheque references a specific account as the source of the funds, a bitcoin transaction references a specific previous transaction as its source, rather than an account. -Once a transaction has been created, it is signed by the owner (or owners) of the source funds. If it was properly formed and signed, the signed transaction is now valid and contains all the information needed to execute the transfer of funds. Now, the valid transaction has to reach the bitcoin network so that it can be propagated until it reaches a miner for inclusion in the pubic ledger, the blockchain. +Once a transaction has been created, it is signed by the owner (or owners) of the source funds. If it was properly formed and signed, the signed transaction is now valid and contains all the information needed to execute the transfer of funds. Finally, the valid transaction has to reach the bitcoin network so that it can be propagated until it reaches a miner for inclusion in the pubic ledger, the blockchain. [[tx_bcast]] ==== Broadcasting Transactions to the Bitcoin Network -{Start with BTC is money into data structure - making it impossible to stop. Then mention the size of a typical transaction - then methods to transmit. Typical transmission method. The extraordinary transmission options. No stopping } Next, a transaction needs to be delivered to the bitcoin network so that it can be propagated and be included in the blockchain. In essence, a bitcoin transaction is just 300-400 bytes of data and has to reach any one of tens of thousands of bitcoin nodes. The sender does not need to trust the nodes they use to broadcast the transaction, as long as they use more than one to ensure that it propagates. The nodes don't need to trust the sender or establish the sender's "identity". Since the transaction is signed and contains no confidential information, private keys or credentials, it can be publicly broadcast using any underlying network transport that is convenient. Unlike credit card transactions, for example, which contain sensitive information and can only be transmitted on encrypted networks, a bitcoin transaction can be sent over any network. As long as the transaction can reach a bitcoin node that will propagate it into the bitcoin network, it doesn't matter how it is transported to the first node. Bitcoin transactions can therefore be transmitted to the bitcoin network over insecure networks such as Wifi, Bluetooth, NFC, Chirp, barcodes or by copy and paste in a web form. In extreme cases, a bitcoin transaction could be transmitted over packet radio, satellite relay or shortwave using burst transmission, spread spectrum or frequency hoping to evade detection and jamming. A bitcoin transaction could even be encoded as smileys (emoticons) and posted in a public forum or sent as a text message or Skype chat message. Bitcoin has turned money into a data structure making it virtually impossible to stop anyone from creating and executing a bitcoin transaction. [[tx_propagation]] ==== Propagating Transactions on the Bitcoin Network -Once a bitcoin transaction is sent to any node connected to the bitcoin network, the transaction will be validated by that node and then propagated to all other nodes that it is connected to. The bitcoin network is a peer-to-peer network, meaning that it each bitcoin node is connected to a few other bitcoin nodes discovered through the peer-to-peer protocol. The entire network forms a loosely connected mesh without a fixed topology or any structure, making all nodes equal peers. Messages, including transactions and blocks are propagated from each node to the peers it is connected to. A new transaction injected into any node on the network will be sent to 3-4 of the neighboring nodes, each of which will send it to 3-4 more nodes and so on. In this way, within a few seconds a valid transaction will propagate in an exponentially expanding ripple across the network until all connected nodes have received it. The bitcoin network is designed to propagate transactions and blocks to all nodes in an efficient and resilient manner that is resistant to attacks. To prevent spamming, denial of service attacks or other nuisance attacks agains the bitcoin system, every node will independently validate every transaction before propagating it further. A malformed transaction will not get beyond one node. The rules by which transactions are validated are explained in more detail in <> +Once a bitcoin transaction is sent to any node connected to the bitcoin network, the transaction will be validated by that node. If valid, that node will propagate it to the other nodes it is connected to and a success message will be returned synchronously to the originator. If the transaction is invalid, the node will reject it and synchronously return a rejection message to the originator. The bitcoin network is a peer-to-peer network, meaning that each bitcoin node is connected to a few other bitcoin nodes which it discovers during startup through the peer-to-peer protocol. The entire network forms a loosely connected mesh without a fixed topology or any structure, making all nodes equal peers. Messages, including transactions and blocks are propagated from each node to the peers it is connected to. A new validated transaction injected into any node on the network will be sent to 3-4 of the neighboring nodes, each of which will send it to 3-4 more nodes and so on. In this way, within a few seconds a valid transaction will propagate in an exponentially expanding ripple across the network until all connected nodes have received it. The bitcoin network is designed to propagate transactions and blocks to all nodes in an efficient and resilient manner that is resistant to attacks. To prevent spamming, denial of service attacks or other nuisance attacks agains the bitcoin system, every node will independently validate every transaction before propagating it further. A malformed transaction will not get beyond one node. The rules by which transactions are validated are explained in more detail in <> [[tx_mining]] ==== Mining Transactions into Blocks -Transactions are grouped together into blocks, which are in turn linked in a long chain called the blockchain. This forms the authoritative ledger of all transactions since bitcoin's beginning in 2009. The blockchain is the subject of the next chapter, where we will examine the formation of the authoritative record through the competitive process of proof-of-work, also known as mining. +Some of the nodes in the bitcoin network participate in "mining". Mining is the process creating new blocks of transactions that will become part of the blockchain. Miners collect transactions and group them into blocks, they then attempt to prove each block with the proof-of-work algorithm. Blocks with a valid proof of work are added to and extend the linked chain of blocks called the blockchain. Once a transaction is added to the blockchain, the new owner of the funds can reference it in a new transaction and spend the funds. -Some of the nodes in the bitcoin network will be participating in "mining", trying to create new blocks of transactions. These mining nodes will collect all the transactions received since the last block and will include them in a new block. If a mining node is able to find the proof-of-work solution to a new block it will add that block to the blockchain and propagate the new block so that all other participants in the network can also add it to the blockchain. Once a transaction is added to the blockchain, the new owner of the funds can reference it in a new transaction and spend the funds. +The blockchain forms the authoritative ledger of all transactions since bitcoin's beginning in 2009. The blockchain is the subject of the next chapter, where we will examine the formation of the authoritative record through the competitive process of proof-of-work, also known as mining. [[tx_structure]] === Transaction Structure -A transaction is a data structure that encodes a transfer of value from a source of funds, called an "input", to a destination, called an "output". Transaction inputs and outputs are not related to accounts or identities. Instead you should think of them as bitcoin amounts, chunks of bitcoin, being locked with a specific secret which only the owner can unlock. +A transaction is a data structure that encodes a transfer of value from a source of funds, called an "input", to a destination, called an "output". Transaction inputs and outputs are not related to accounts or identities. Instead you should think of them as bitcoin amounts, chunks of bitcoin, being locked with a specific secret which only the owner, or person know knows the secret, can unlock. A transaction contains a number of fields, in addition to the inputs and outputs, as follows: + [[tx_data_structure]] .The structure of a transaction [options="header"] @@ -62,37 +62,64 @@ A transaction contains a number of fields, in addition to the inputs and outputs [[tx_inputs_outputs]] === Transaction Outputs and Inputs -Transactions move money from inputs to outputs, with each transaction's output becoming available as an input for subsequent transactions. When you send money to a friend, you are create "outputs" that your friend can spend. When your friend spends that money, their transaction uses as inputs the unspent outputs from your transaction and creates new outputs for whoever they are paying next. This way, transactions form a chain, with each transaction spending outputs from a previous transaction and creating new outputs. If you follow a transaction chain far enough backwards, you will arrive at a coinbase transaction, which is a transaction creating new bitcoins from mining. That's the only type of transaction that doesn't consume previous outputs but instead creates outputs from nothing. - -In this section we start by looking at transaction outputs, because you can't understand inputs without first understanding the outputs of a transaction. +The fundamental building block of a bitcoin transaction is an _unspent transaction output_ or UTXO, a block of currency. UTXO are indivisible chunks of bitcoin currency locked to a specific owner, recorded on the blockchain, and recognized as currency units by the entire network. The bitcoin network tracks the set of all available (unspent) UTXO, currently numbering in the millions. As bitcoin users receive bitcoin these are recorded as UTXO on the blockchain and may be scattered amongst hundreds of transactions and hundreds of blocks. In effect, there is no such thing as a balance of a bitcoin address or account. There are only scattered UTXO, locked to specific owners. The concept of a user's bitcoin balance is artificial and created by the wallet application, by scanning the blockchain and aggregating all UTXO that belongs to addresses controlled by that user. Like loose coins and bank notes of different denominations in a purse, an end-user's bitcoin wallet application tracks all the UTXO owned by that user's keys and presents the sum of all the various UTXO values as the user's current balance. [TIP] ==== -What comes first? Inputs or outputs, the chicken or the egg? Strictly speaking, outputs come first because coinbase transactions generate coins without inputs and create outputs from nothing. +The are no accounts or balances in bitcoin, there are only _unspent transaction outputs_ (UTXO) scattered in the blockchain. +==== + +Unlike cash which exists in specific denominations, one dollar, five dollars, ten dollars, etc., a UTXO can have any arbitrary value denominated as a multiple of satoshis (the smallest bitcoin unit equal to 100 millionth of a bitcoin). While UTXO can be any arbitrary value, once created it is indivisible just like a coin that cannot be cut in half. If a UTXO is larger than the desired value of a transaction, it must still be consumed in its entirety and change must be generated in the transaction. In other words, if you have a 20 bitcoin UTXO and want to pay 1 bitcoin, your transaction must consume the entire 20 bitcoin UTXO and produce two outputs: one paying 1 bitcoin to your desired recipient and another paying 19 bitcoin in change back to your wallet. As a result, bitcoin transactions must occasionally generate change to account for consuming UTXO in excess of the desired value. + +In simple terms, transactions consume from a users available UTXO and create new UTXO locked to a new owner, the destination address of the transaction. Imagine a shopper buying a beverage reaching into their wallet and trying to find a combination of coins and bank notes that will cover the price of the beverage. The shopper will choose exact change if available, or a combination of smaller denominations, or if necessary, a larger unit such as a bank note. If they hand too much money to the shop owner they will expect change, which they will return to their wallet and have available for future transactions. Similarly, a bitcoin transaction must be created from a users UTXO in whatever denominations that user has available. They cannot cut a UTXO in half anymore than they can cut a dollar bill in half and use it as currency. The user's wallet application will typically select from the users available UTXO various units to compose an amount greater than or equal to the desired transaction amount. As with real life, the bitcoin application can use several strategies to satisfy the purchase amount: combining several smaller units, finding exact change, or using a single unit larger than the transaction value and making change. + +The UTXO consumed by a transaction are called transaction inputs, while the UTXO created by a transaction are called transaction outputs. This way, chunks of bitcoin value move forward from owner to owner in a chain of transactions consuming and creating UTXO. Transactions consume UTXO unlocking it with the signature of the current owner and create UTXO locking it to the bitcoin address of the new owner. + +The exception to this is a special type of transaction called the _coinbase_ transaction, which is the first transaction in each block. This transaction is placed there by the "winning" miner and creates brand-new bitcoin payable to that miner as a reward for mining. This is how bitcoin's money supply is created during the mining process as we will see in <> + + +[TIP] +==== +What comes first? Inputs or outputs, the chicken or the egg? Strictly speaking, outputs come first because coinbase transactions, which generate new bitcoin, have no inputs and create outputs from nothing. ==== [[tx_outs]] ==== Transaction Outputs -Transaction outputs are, in a sense, the currency of the bitcoin system. They are the fundamental unit that is spent in a transaction and the fundamental building block of any transaction. Unspent Transaction Outputs, also known as UTXO are tracked by every full node bitcoin client in a database held in memory, called the UTXO set or UTXO pool. Any new transaction seen on the network must consume (spend) one or more of these outputs from the UTXO pool. +The destination of a bitcoin transaction is called an _output_. Every bitcoin transaction creates outputs, which are recorded on the bitcoin ledger. Almost all of these outputs, with one exception (see <>) create spendable chunks of bitcoin called _unspent transaction outputs_ or UTXO, which are then recognized by the whole network and available for future transactions to spend. Giving someone bitcoin is giving them an unspent transaction output (UTXO) registered to their address and available for them to spend. + +UTXO are tracked by every full node bitcoin client in a database held in memory, called the _UTXO set_ or _UTXO pool_. Any new transaction will consume (spend) one or more of these outputs from the UTXO set. + +Transaction outputs consist of two parts: + +* An amount of bitcoin, denominated in _satoshis_, the smallest bitcoin unit +* A _locking script_, also known as an "encumbrance" that "locks" this amount by specifying the conditions that must be met to spend the output + +The transaction scripting language that is used to define the locking script used in transaction outputs and corresponding unlocking script used in transaction inputs is discussed in detail in <> ===== Spending Conditions (Encumbrances) -Transactions create outputs by assigning a specific amount (in satoshis) to a specific _encumbrance_ or locking-script. In most cases the locking script will lock the output so that it can only be spent by the owner of a specific bitcoin address, thereby transferring ownership of that amount to the new owner. When Alice pays Bob's Cafe for a cup of coffee, her transaction is creating a coffee-amount output that is _encumbered_ or locked to Bob Cafe's bitcoin address. That output will be recorded on the blockchain and be part of the Unspent Transaction Output set corresponding to Bob's Cafe, meaning it will show in Bob's wallet as a balance until it is spent. When Bob chooses to spend that amount, his transaction releases the encumbrance, unlocking the output with a signature from Bob's private key. - -===== Unspent Transaction Outputs (UTXO) - -From a technical perspective bitcoin doesn't have accounts or balances, it only has Unspent Transaction Outputs, referred to as UTXO. Balances and accounts are higher-level constructs that are created by summing all the UTXO available for spending under a specific address or more accurately by a specific private key. You wallet balance is nothing more than the sum of all UTXO available to spend with the keys in your wallet. - -[TIP] -==== -The are no accounts or balances in bitcoin, only UTXO. -==== +Transactions create outputs by assigning a specific amount (in satoshis) to a specific _encumbrance_ or locking-script. In most cases the locking script will lock the output so that it can only be spent by the owner of a specific bitcoin address, thereby transferring ownership of that amount to the new owner. When Alice pays Bob's Cafe for a cup of coffee, her transaction is creating a coffee-amount output that is _encumbered_ or locked to Bob Cafe's bitcoin address. That output will be recorded on the blockchain and be part of the Unspent Transaction Output set corresponding to Bob's Cafe, meaning it will show in Bob's wallet as a balance until it is spent. When Bob chooses to spend that amount, his transaction releases the encumbrance, unlocking the output by providing an unlocking script containing a signature from Bob's private key. +[[tx_out_structure]] +.The structure of a transaction output +[options="header"] +|======= +|Size| Field | Description +| 8 bytes | Amount | Bitcoin Value in Satoshis (10^^-8 bitcoin) +| 1-9 bytes (VarInt) | Locking-Script Size | Locking-script length in bytes, to follow +| Variable | Locking-Script | A script defining the conditions needed to spend the output +|======= [[tx_inputs]] ==== Transaction Inputs + +Transaction inputs are pointer references to UTXO, by reference to the transaction hash and sequence number where they are recorded in the blockchain. To spend UTXO, a transaction input also includes unlocking-scripts (usually signatures) for each referenced UTXO in order to prove ownership of those funds. + + + ===== Spending the Transaction Outputs + ===== Unlocking the Conditions (Encumbrance Satisfaction) [[tx_fees]] @@ -109,6 +136,7 @@ The are no accounts or balances in bitcoin, only UTXO. === Non-Standard Transactions +[[tx_script]] === Transaction Scripts and Script Language ==== Scripting Language