Check bitcoin balance blockchain
Please keep in mind this is voluntarily simplified, if you wish to know more check the protocol documentation. One of the quirks of the protocol, is that the amount of the inputs and outputs in the transaction must match in reality, the output can be less than the input, and the remainder then constitutes the optional transaction fee. That rule greatly simplifies the validation of transactions, since there is no need to extract the entire history of transactions to figure out how much funds are spent or unspent for a given transaction: The drawback of this solution arises when you need to spend only a fraction of the amount received in a previous transaction.
In that case, the wallet software automatically creates two outputs to the transaction: Alice previously received 1 BTC from Chip and 0. The new transaction she makes has to reference both previous unspent transactions as inputs, since neither of these transactions taken individually have enough funds. One of the outputs of the transaction must be the 1. But Alice also need to add a 0. In the future, if she could use these 0.
As its core, a blockchain is a distributed database that allows direct transactions between two parties without the need of a central authority. This simple yet powerful concept has great implications for various institutions such as banks, governments and marketplaces, just to name a few.
Any business or organization that relies on a centralized database as a core competitive advantage can potentially be disrupted by blockchain technology. Putting aside all the hype around the price of Bitcoin and other cryptocurrencies, the goal of this blog post is to give you a practical introduction to blockchain technology. Sections 1 and 2 cover some core concepts behind blockchain, while section 3 shows how to implement a blockchain using Python.
We will also implement 2 web applications to make it easy for end users to interact with our blockchain. Please note that I'm using Bitcoin here as a medium for explaning the more general technology of "Blockchain", and most of the concepts described in this post are applicable to other blockchain use cases and crypto-currencies.
It all started with a white paper released in by an unknown person or entity using the name Satoshi Nakamoto. In the original Bitcoin white paper, Satoshi described how to build a peer-to-peer electronic cash system that allows online payments to be sent directly from one party to another without going through a centralized institution. This system solves an important problem in digital money called double-spending. If Alice and Bob use digital money, then the problem gets more complicated.
Digital money is in digital form and can be easily duplicated. This problem is called double-spending. One way of solving the double-spending problem is to have a trusted third party a bank for example between Alice, Bob and all other participants in the network. This third party is responsible for managing a centralized ledger that keeps track of and validates all the transactions in the network.
The drawback of this solution is that for the system to function, it requires trust in a centralized third party. To solve the double-spending problem, Satoshi proposed a public ledger, i. The goal of this section is to go deeper into the technical building blocks that power the blockchain. We will cover public key cryptography, hashing functions, mining and security of the blockchain.
Public-key cryptography, or asymmetrical cryptography, is any cryptographic system that uses pairs of keys: This accomplishes two functions: I recommend this article , if you're interested in the complete technical details of Bitcoin wallets. To send or receive BTCs, a user starts by generating a wallet which contains a pair of private and public keys. She then sign the transaction using her private key. A computer on the blockchain uses Alice's public key to verify that the transaction is authentic and adds the transaction to a block that will be later added to the blockchain.
All Bitcoin transactions are grouped in files called blocks. Bitcoin adds a new block of transactions every 10 minutes. Once a new block is added to the blockchain, it becomes immutable and can't be deleted or modified.
A special group of participants in the network called miners computers connected to the blockchain are responsible for creating new blocks of transactions. A miner has to authenticate each transaction using the sender's public key, confirm that the sender has enough balance for the requested transaction, and add the transaction to the block. Miners are completely free to choose which transactions to include in the blocks, therefore the senders need to include a transaction fee to incentivise the miners to add their transactions to the blocks.
For a block to be accepted by the blockchain, it needs to be "mined". To mine a block, miners need to find an extremely rare solution to a cryptographic puzzle. If a mined block is accepted by the blockchain, the miner receive a reward in bitcoins which is an additional incentive to transaction fees. The mining process is also referred to as Proof of Work PoW , and it's the main mechanism that enables the blockchain to be trustless and secure more on blockchain security later.
To understand the blockchain's cryptographic puzzle, we need to start with hash functions. A hash function is any function that can be used to map data of arbitrary size to data of fixed size. The values returned by a hash function are called hashes. Hash functions are usually used to accelerate database lookup by detecting duplicated records, and they are also widely used in cryptography.
A cryptographic hash function allows one to easily verify that some input data maps to a given hash value, but if the input data is unknown, it is deliberately difficult to reconstruct it by knowing the stored hash value. Bitcoins uses a cryptographic hash function called SHA SHA is applied to a combination of the block's data bitcoin transactions and a number called nonce. By changing the block data or the nonce, we get completely different hashes.
For a block to be considered valid or "mined", the hash value of the block and the nonce needs to meet a certain condition. For example, the four leading digits of the hash needs to be equal to "". We can increase the mining complexity by making the condition more complex, for example we can increase the number of 0s that the hash value needs to start with. The cryptograhic puzzle that miners need to solve is to find a nonce value that makes the hash value satisfies the mining condition.
You can use the app below to simulate block mining. When you type in the "Data" text box or change the nonce value, you can notice the change in the hash value. When you click the "Mine" button, the app starts with a nonce equals to zero, computes the hash value and checks if the leading four digits of the hash value is equal to "".
If the leading four digits are not equal to "", it increments the nonce by one and repeats the whole process until it finds a nonce value that satisify the condition.