Blockchain and Bitcoin(under the hood)

Pages: 12
Hi guys,

I'm watching this fascinating video about what's really happening under the hood of Bitcoin ( https://www.youtube.com/watch?v=bBC-nXj3Ng4 ).

Please correct me if any of my details are wrong they are probably a little murky, I'm just as of recent trying to understand the premise behind btc.

At the start(of the video) he explains how everyone keeps a ledger, this ledger contains every transaction. Such as Alice plays Bob 0.005 btc but how do we know this is a legitimate payment? Maybe Bob added this line to the ledger, A way around this is to add a digital signature to the end of the transaction. Each person has a private and public key pair. If Alice really does want to pay Bob 0.005 btc she will run the transaction and her private key through a function that will create a digital signature, this signature can be verified by putting the message,signature and Alice's public key into a function, if the function returns true then we know that Alice does really intend to pay Bob 0.005 btc. But what prevents Bob from just copying and pasting this line multiple times? Well each transaction will have a unique ID, so the signature will actually consist of the transaction ID, the transaction and Alice's private key (this obviously done by Alice). Each person on the network verifies this transaction with Alice's public key, the transaction ID, the transaction (message) and the signature. The transaction is added to the individual ledgers if the function that verifies the transaction returns true. No ledger can have the same digital signature twice.

Okay so that isn't exactly what happens, it's a precursor if anything. What's really happening is each participant on the bitcoin network has a thing called a blockchain, this blockchain is a chain of ledgers, each block obviously containing transactions that are confirmed. Blocks are created by miners, blocks are created containing a number of transactions, each block's hash/digest must begin with a certain amount of zeroes (it alternates but let's just pretend it's 30 for simplicity). By hashing a block of transactions with the hash of the previous block and also a special number it then looks for a hash that starts with 30 zeroes, the miner hashes the previous blocks hash and transactions with a number, if the hash starts with 30 consecutive zeroes that means we have found the correct number and the block gets broadcasted to every node on the network. The minor also gets rewarded.

The chances of finding that special number that gives a hash that starts with 30 zeroes is 1/(2^30), so each miner is basically playing a lottery to try solve that computation first( It probably does help to have high end GPUs etc). When the block is found it is broadcasted to all the participants on the bitcoin network who then add it to their respective chains.

So my questions that follow are.

How many transactions are in one block?

When a transaction is made does the person who is sending bitcoin to another participant broadcast the transaction to the miners? And how does the miner(s) verify the transaction that it/they received is legitimate? (I'm guessing it has something to do with the digital signatures I prefaced in the beginning)

From what I can tell the transaction fee will give the miner more incentive to include that transaction in a block, so if a person decided to pay a higher transaction fee that means they have more chances of being included in that block hence taking quicker for their transaction to be processes, right?

To expand on the above question, does each miner receive all the transactions? Is it possible one miner may not receive a certain transaction ( a high priority one ) and solve the computation not including that transaction in the block? and will that transaction be lost??

And to again expand above, how are transactions queued? I mean how does the blockchain ensure that no transaction gets lost?

So yeah this may be a long thread :)

Thanks
I do not know any of that.
But this is interesting to me ... I read about it long ago, if you control enough hardware you can spoof the transactions:
https://en.bitcoin.it/wiki/Weaknesses#Attacker_has_a_lot_of_computing_power
see how a hacker with a lot of computers can spend the same money multiple times example!

There is a lot of info out there on how it works... but the whole transaction history and majority agreement design seem fishy (read, someone, someday, is going to steal a lot of money).
Last edited on
That's very true, in the video they mention that a node/participant can actually forge a block. So they could add a transaction of one node sending them btc that( essentially stealing from them) and look for a hash that begins with say 30 zeroes, that malicious participant will only send that block to the desired node he/she intends to steal from. But this almost virtually impossible to succeed as not only does the malicious participant have to work out a correct hash ( in our example 1/(2^30)) but he/she has to do it before every single miner. In the off chance the participant is correct there is a concept called computational work, where the recipient (in this case the victim) waits for a couple of blocks before accepting the blocks as legit. So it would be only an astronomically low chance the malicious user would guess 6-7 blocks before all the miners do.

But with that being said I'm sure there has to be some flaws in the system that some day will be leveraged. But yeah that's really interesting, I'm sure if you got someone crazy enough or had enough money to purchase that much computing power then without a doubt that would be a serious concern.

Other things that confuse me is when a transaction occurs, does the miner(s) verify that the transaction is legit by using the senders public key and message? And if so I wonder how the blockchain ensures one person doesn't spend more money than they actually have?

Another puzzling thing, I use a wallet like most crypto owners, does each user have a blockchain? and if so where is it stored, or does each crytpo wallet provider or exchange keep one copy of the blockchain and use them for all it's hosts (the latter seems more plausible to me).


Another big question I have is do miners all put the same transactions in the same block? For example do all they miners choose the same transactions for respective block they are mining?? (I would imagine so but I could be wrong, the only problem I have with this is what if one if the miners does not get one of the transactions or what if one of the miners gets a transaction in the wrong order? )
Last edited on
if you got someone crazy enough or had enough money to purchase that much computing power then without a doubt that would be a serious concern.

there are large groups of hackers and you can also distribute a botnet malware. You can also rent a fair bit of computing from like AWS, for a short burst. It is a concern :)

oh, on getting it out of order, I *think* there is a scheme where the most agreeing wins. So if 10 guys put data in block 5 and one puts it in block 6, it goes in 5. This is why having the big computing power is a risk, you can overwhelm everyone else and define what is 'correct' in a bad way.
Last edited on
This is actually kind of what I do for a living.

How many transactions are in one block?
Transactions have a variable length. In principle you could pay a huge fee to have a gigantic transaction that used up every last available byte of the maximum block length. In Bitcoin I believe a block will contain at most a few thousand transactions.

When a transaction is made does the person who is sending bitcoin to another participant broadcast the transaction to the miners? And how does the miner(s) verify the transaction that it/they received is legitimate? (I'm guessing it has something to do with the digital signatures I prefaced in the beginning)
A Bitcoin transaction consists of "inputs" and "outputs". An output can be thought of broadly as the sending of an amount to someone, and an input as the receiving. Each input of a transaction must "spend" an unspent output of a previous transaction; that is, it must include a message signed with the private key corresponding to the public key that was included in the unspent output. The miner doesn't need any external information to verify an incoming transaction.

Note that Bitcoin includes as part of its inputs and outputs a non-Turing-complete scripting system that allows for more sophisticated signatures schemes that the one I mentioned above, at the cost of making the transaction larger. This is just the most common one.

From what I can tell the transaction fee will give the miner more incentive to include that transaction in a block, so if a person decided to pay a higher transaction fee that means they have more chances of being included in that block hence taking quicker for their transaction to be processes, right?
Yes.

To expand on the above question, does each miner receive all the transactions?
Not necessarily.

Is it possible one miner may not receive a certain transaction ( a high priority one ) and solve the computation not including that transaction in the block? and will that transaction be lost??
The protocol does not guarantee that miners will be working to include blocks with the exact same content. I.e. The same transactions in the same order. In fact, it's very likely that the opposite will happen. A transaction may very well be delayed to the next block, or in fact indefinitely. That's one problem with Bitcoin when you send a transaction with insufficient fee. The transaction is left sort of in limbo and it's difficult to decide how to proceed.

And to again expand above, how are transactions queued? I mean how does the blockchain ensure that no transaction gets lost?
It doesn't. Transaction broadcasting is part of the P2P networking protocol, not part of the consensus protocol. The network does not guarantee inclusion even of valid transactions even after infinite time. It could happen, for example, that miners collude to not include any more thansactions from a particular sender, and the network will continue operating within spec.

And if so I wonder how the blockchain ensures one person doesn't spend more money than they actually have?
A transaction's output can only be spent once. In other words, only a single input in a single transaction can reference that particular TXO. Additionally, the sum of the amounts of a transaction's outputs cannot be greater than a transaction's inputs. Thus you can follow a chain (or rather a tree) of transactions that trace the origin of those satoshis.
The exception to the above is miner transactions, which generate BTC from nothing (as block reward) as well as deposit the total block fees into a UTXO for themselves.

Hope this answers your questions.
Last edited on
whoooosh...over my head lol

@helios,
So I understood some of that, but what I gather is that bitcoin is actually pretty secure? Other than the fact that its value goes up and down like a butterfly on meth, and it doesn't actually physically exist ;)

Something I can't understand is, how did this even become a thing? Money that's "mined" with computers, like something out of Mine Craft or whatever...It's just kind of crazy when you think about it.
Something I can't understand is, how did this even become a thing?

My understanding is that Bitcoin was designed to protect its users by cutting out central banks and monetary policies as potential SPOFs.

(China is currently cracking down on Bitcoin miners under the pretense of environmental concern, but the PRC's real problem with it is that it is outside the reach of the government.)

More or less like Bitcoin, the USD (since 1971) is fiat money; the USD is just paper. There's no major difference between paper that the US government says is valuable and bits of data that the network says is valuable. Except of course that the network cannot make unilateral decisions.
Last edited on
Actually the original idea of Bitcoin was ostensibly to serve as cash for the Internet. The idea was to cut out middlemen like banks and PayPal for transactions between Internet users. It turned out to be woefully inadequate for this purpose once the network began to see actual usage:
* Pitiful transaction throughput. Something in the order of <5 tx/s. For a system meant to serve the entire Internet.
* Long wait times for transaction inclusion in a block.
* Huge fees.
However, when it became clear that Bitcoin was a great vehicle for speculation, stakeholders began pushing different narratives about the network's goal. First it was store of value (also terrible at it, but for non-technical reasons), and it kept kept changing with the times. Who knows what they now say Bitcoin is supposed to do.

Since Bitcoin cannot serve the purpose it was built for, it's clear why BTC's price is so volatile while USD's isn't. The actual utility of the network is not zero, but it's very, very low. You can send pseudonymous transactions very slowly by having the network spend not insignificant amounts of compute power. Whoop-de-doo. How much is that really worth? The answer: much, much less than BTC's current market cap. BTC's price is primarily driven not by its utility as a tool for trade or by its supply like USD, but rather by how much people think it's worth. Of course it's going to be volatile if the price is entirely divorced from reality.

Bitcoin is really just a game of hot potato where you really don't want to be the one holding the potato at the end. The sad part is that it's the poor saps who are only entering the game now who are most likely to lose in the end.
@mbozzi,
Fiat money, yep. That's why I have so much invested in gold and silver, it's a lot less prone to drastic changes in value.

@helios,
This is sounding less and less like something I would ever want to use. I mean, if you have money to spend on this kind of thing, then it would be less of a risk; but I'm not a millionaire lol.
Great input Helios,

I understand most of the concepts that you explained but one that is still not completely sinking in is in how the blockchain prevents someone from overspending and how it keeps track of how much an individual has in his/her wallet.

So as you explained every bitcoin transaction has an input and output, the output being the recipient's btc wallet address and the input being your wallet address. Every transaction has an input and output and this is stored on the blockchain. But I'm still failing to connect the dot how this would prevent an individual from spending btc they don't have?

Also let's say I have 2 btc, an attacker forges a transaction and says I have actually sent them 2 btc in fact I didn't. So the transaction was not signed with my private key thus the transaction is not valid, who verifies that this transaction isn't legit? Is the transaction verified before it is broadcasted to miners?

Also if let's say someone got a hold of my private key could they then make fraudulent transactions?

On a side note what is a TXO? (I'm guessing stands for transaction output? )

Last edited on
I believe TXO is "transaction output."
That's why I have so much invested in gold and silver, it's a lot less prone to drastic changes in value.
Strictly speaking, the price of gold is also bubbled. The price of gold is what it is primarily because people think that's what it is, and that's mostly for historical reasons. Imagine if a country dug underground and found that under its entire territory there was a 100 m-thick layer of pure gold. Would the country become immensely rich, or would the price of gold sink? It would do the latter, right? That means gold is really not very different from a currency; it's a token that stands for something else.
In contrast, oil is different. Oil has inherent utility because it's an energy source (as long as you have the technology to extract that energy). Food is similar in this respect, although faster and more direct. You capture sunlight by photosynthesis in hydrogen-carbon bonds and after a few transformation steps you have an energy source that's consumable by humans or animals.

From this we can infer that the absolute safest investment is to own parts of a variety of food production and processing companies.

how the blockchain prevents someone from overspending and how it keeps track of how much an individual has in his/her wallet.
Bitcoin has no concept of wallet or account. All the network knows is that there's a certain number of transaction outputs (TXOs) that are unspent (UTXO). It's theoretically impossible to correlate these to any number of individuals or even any number of root private keys, for the reason in the next paragraph. The best that can be said is that for certain TXOs (>99%) you can determine the address that the sender entered into their software.

Wallet software is supposed to keep track of which public keys and addresses a user has funds in. It does this by deriving new private keys from the root private key using a deterministic tree-walking algorithm. Using this information, the software can find UTXOs for a given address and either display a balance for that address or generate a new transaction that spends those UTXOs.

The way the network prevents overspending is by not allowing an STXO to be spent again. It's really quite simple; if a TXO is referenced by any TXI then it's spent, otherwise it's unspent.

So as you explained every bitcoin transaction has an input and output
Every Bitcoin transaction has some number of inputs and some number of outputs.

the output being the recipient's btc wallet address and the input being your wallet address.
Not exactly, but let's go with that as a helpful abstraction.

who verifies that this transaction isn't legit?
Miners won't bother including in a block transactions that break the network's rules (e.g. transactions that double-spend, transactions that spend TXOs they can't verify, etc.). If a miner tried to do that, all other nodes in the network (both miners and non-miners) would treat the block as invalid and ignore it, thus the miner would have wasted compute power hashing a useless block.

Is the transaction verified before it is broadcasted to miners?
I'm unsure. It's possible it's done as a flood control measure. You wouldn't want nodes in the network being able to relay arbitrary messages. It's Bitcon, not IRC.

Also if let's say someone got a hold of my private key could they then make fraudulent transactions?
Define "fraudulent". All the network cares about is that the signatures in the transaction's inputs match the keys in the UTXOs referenced in those inputs. It doesn't (and couldn't) care about which person is sending the transaction.
In other words, if someone somehow got ahold of your private key, they could relay a valid transaction that moves all your funds into an address you don't control and you'd have no recourse.
Starting to click now,

I'll just use the abstraction that the input is the senders wallet and output is the recipients wallet for now, I'll also use the abstraction of just a single input and output being created for every transaction for now.

So what you're saying is every transaction has an input and output,for example let's say I wanted to send 1 btc to a friend, the input of that transaction will be my wallet and the output of the transaction will be my friends wallet. Every unspent transaction will not have an output for example, when my friend receives the btc , he decides not to spend it so the input is his wallet but there is no output yet because the btc is unspent, but when my friend wants to spend that btc I sent to him, the input then becomes his wallet and the output becomes the wallet where he is sending that btc, so basically this allows that bitcoin to be traced back all the way to the beginning, right?

A part that confuses me is as follows, each transaction is recorded on a block on the blockchain but how can the transaction be verified if the whole block itself uses a cryptographic hash such as sha256? If we were to trace back the transactions to the beginning how would this be done as the output of each block would pretty much be gibberish right(a hash/digest )? Or are the transactions(in each block) not hashed?
Last edited on
So what you're saying is every transaction has an input and output,for example let's say I wanted to send 1 btc to a friend, the input of that transaction will be my wallet and the output of the transaction will be my friends wallet. Every unspent transaction will not have an output for example, when my friend receives the btc , he decides not to spend it so the input is his wallet but there is no output yet because the btc is unspent, but when my friend wants to spend that btc I sent to him, the input then becomes his wallet and the output becomes the wallet where he is sending that btc, so basically this allows that bitcoin to be traced back all the way to the beginning, right?
No. You create the transaction with its inputs and outputs. Your friend can't rewrite your transaction to have outputs you didn't add originally.

The transaction structure looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Output{
    uint32 amount;
    byte[] public_key;
};

class Input{
    Output *output_spent;
    byte[] signature;
};

class Transaction{
    Input[] inputs;
    Output[] outputs;
};
Suppose you have one UTXO with 10 mBTC and you want to send 1 mBTC to your friend. You would construct a transaction like this:
1
2
3
4
5
6
    Transaction tx;
    tx.inputs.add(Input(utxo, unique_random_message + sign_message(unique_random_message, your_private_key)));
    //1 mBTC == 100,000 satoshis
    uint32 amount = 100'000;
    tx.outputs.add(Output(amount, your_friends_public_key));
    tx.outputs.add(Output(utxo.amount - amount - fee, your_public_key)); 
Most transactions have a single input and two outputs because, since a TXO can only be spent once, if you don't create a second TXO to send the unspent funds back to yourself you lose them as transaction fees.

each transaction is recorded on a block on the blockchain but how can the transaction be verified if the whole block itself uses a cryptographic hash such as sha256?
The block doesn't consist of just the 32 bytes of the SHA-256 digest. If it did, the blockchain wouldn't be a chain, it would just be a meaningless string of cryptographically secure pseudo-random numbers. What would be the point of that? At the very least, a blockchain's[1] block must reference the hash of the previous block, so that a DAG may be assembled.
The block consists of some metadata, all the transactions in the block as they were relayed to the network, and the PoW block digest, which should hash the entire block up to that point.



[1] Not just Bitcoin but any blockchain. E.g. a Git repository.
Last edited on
If you want an abstraction for transactions you can think of them like this:

Imagine a transaction as if it was a conference room in a bank where you're going to shuffle bank notes around. The inputs are requests to bring in bags of money addressed to you, and the outputs are bags of money you address to other people (or yourself).

So for example, you want to send $100,000 to your grandma and $100,000 to your friend. You have ten bags of money in the bank, each containing $30,000.

Before entering the room, you write in a paper a request to bring in some number of bags. You need to know the bags numbers ahead of time. The bank is going to check that the bags you write down are addressed to you, and won't let you enter the room if there's any irregularities.
You request to bring in 7 of your bags (totalling $210,000). After you enter the room, a bank representative brings in the bags you requested. You cut the bags open and you spill their contents on the large table in the room.
First of all you set aside $1,000 to pay the bank for their services. You're not going to make any bags for that, you're just going to leave a small pile of notes in the room when you leave, and someone from the bank will later come along to collect that money.
Then you make a bag addressed your grandma containing $100,000 and you give it to the bank representative. The bank is going to hold on to it until your granma comes in wanting to spend that money.
You do the same for your friend.
When you're done, you notice there's still $9,000 on the table. For security reasons the bank doesn't let you have any cash on your person when you leave the room, so you gather it all up and put it in a bag address to yourself, which you give to the representative.

That's more or less how Bitcoin transactions work.
Oh okay I think I'm "kind of" seeing the picture clearer now.

I'll try reference the code as best as I can. I'm guessing UTXO will be the amount of btc you have in your account/balance. Each transaction will have one input (I'm sure it probably could consist of more than one input but for this example I'll stick with one). The input is your balance(utxo), I'm not too sure what the unique random message is but the second argument will be the digital signature( transaction signed with private key).

Each transaction also normally consists of more than one output, one output will be the amount you are sending to your friend. The second will be the change from the transaction which will obviously be sent back to your account.

But how are these transactions tracked back? So for example let's say I decide to send Alice one btc from my account of three btc. Three weeks later Alice sends that exact btc to Bob. How can that very bitcoin that Bob now has be tracked back to me?


And oh that makes sense, So the blocks aren't encrypted. Each block contains a header which is the previous blocks hash/digest (As long as it's not the genesis block). That block also contains some transactions which are not encrypted, the block also contains it's 256 bit hash. That said hash is then the header to the next block effectively linking them. If someone tries to change a transaction the hash of that block will change and will subsequently change the following blocks hashes also. So if anybody decided to pick an arbitrary block on the blockchain they could actually view the transactions, correct?
But how are these transactions tracked back?
Since the blockchain works as a publicly viewable ledger, anyone can see that you sent funds to Alice and that Alice sent funds to Bob. When a series of transactions all have a single input it's trivial to determine the path of funds, since every time you go to the previous transaction there's only a single transaction being referenced.

As an aside, it's not always possible to do that traceback, though. Since a) transactions can have multiple inputs and multiple outputs, and b) a single person can control multiple addresses that other people cannot prove are related without additional information, it's not too difficult to construct a program that can receive funds from multiple users and structure transactions so that the funds are totally anonymous and untraceable when they leave the program. Such programs are known as Bitcoin tumblers.

And oh that makes sense, So the blocks aren't encrypted. Each block contains a header which is the previous blocks hash/digest (As long as it's not the genesis block). That block also contains some transactions which are not encrypted, the block also contains it's 256 bit hash. That said hash is then the header to the next block effectively linking them. If someone tries to change a transaction the hash of that block will change and will subsequently change the following blocks hashes also. So if anybody decided to pick an arbitrary block on the blockchain they could actually view the transactions, correct?
Correct.
I think I have heard about Bitcoin mixers/tumblers before, quite interesting. From what I have read Monero seems to provide complete anonymity as opposed to most other coins.
Another question that has sprung up in my mind is how does a miner get rewarded/ when is a block created.

In the video I shared at the start of the thread which explains the fundamentals of bitcoin and blockchain technology, the author mentions that in order to find a block, the block will have to be hashed with a random number( a nonce) if the nonce + the block gives a hash that results in the target number of consecutive zeroes than the block is created and the miner is rewarded.

But I'm now reading that in order for a block to be created the block has to be hashed with a nonce, if the value of the hash is <= a target number then the block is accepted, so which one is correct?
The miner rewards himself by creating a transaction with no inputs and one output that only he can spend.

Both descriptions are equivalent. The block digest is an integer. E.g. 0xFFFFFFFF. If you require that the integer be less than or equal to, say, 0xFFFF, that's equivalent to requiring that the full expansion have at least four zeroes in front.
Pages: 12