The speaker share his vision on the prospects of employing the technology for practical tasks. He presented basics of the blockchain architecture with case studies of JavaScript blockchain implementation using Node.js.
This presentation by Valerii Radchenko (Senior Software Engineer, Consultant, GlobalLogic, Kharkiv) was delivered at GlobalLogic Kharkiv JS TechTalk #2 on August 17, 2018.
3. The Blockchain
Wikipedia says:
A blockchain, originally block chain, is a growing list of records, called
blocks, which are linked using cryptography.
4. The Blockchain
Wikipedia says:
A blockchain, originally block chain, is a growing list of records, called
blocks, which are linked using cryptography.
The blockchain is a way of storing data for free access and making changes
that are safe, confirmed by most users in an open distributed network.
5. Examples and tools
• As an example, we will look at very simplified cryptocurrency
implementation.
• Tools and modules:
- Node.js
- Typescript
- Crypto (Node.js core module)
- Elliptic (npm package)
- Base58, big-integer(npm package)
- levelUp, levelDown (npm packages)
6. Agenda
1. Basic Concept
2. Persistence
3. Proof Of Work
4. Addresses
5. Transactions
6. Mining
7. More
8. Conclusion
8. Blocks
• In blockchain, its blocks store valuable information.
• Besides this, a block contains some technical information, like:
- Version
- Current timestamp
- Its hash
- Hash of the previous block
10. Block Hash
• One of the important thing in a blockchain is calculating hashes.
- It makes blockchain secure.
- Calculating hashes requires some time even on powerful computers.
- This is an intentional architectural design, which makes adding new blocks
difficult, thus preventing their modification after they’re added.
12. Blockchain
• Blockchain is a database structure: it’s an ordered, back-linked list.
• Which means that blocks are stored in the insertion order and that each
block is linked to the previous one. This structure allows to quickly get the
latest block in a chain and to get a block by its hash.
14. Genesis block
• In any blockchain, there must be at least one block.
• The first block in the chain is called the genesis block and creating a
blockchain must start by creating such block.
17. What kind of database fit our needs?
• We need a simple serverless database to store our blocks one after another
- NoSQL
- Serverless
- Key/value or documented
- Simple and fast
19. BitcoinCore Database Structure
• 'l' -> 4-byte file number: the last block file number used
• 'c' + 32-byte transaction hash -> unspent transaction output record for that
transaction
• 32-byte block hash -> block record
• The full database structure description
22. Proof Of Work
• A proof of work is a piece of data which is difficult (costly, time-consuming)
to produce but easy for others to verify and which satisfies certain
requirements.
• Producing a proof of work can be a random process with low probability so
that a lot of trial and error is required on average before a valid proof of
work is generated.
27. Bitcoin Address
• 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa This is the very first Bitcoin
address
• Addresses are not something that identifies you as the owner of a “wallet”
• Address is a public key
• In Bitcoin, your identity is a pair of private and public keys stored on your
computer
28.
29. Public-key Cryptography
• Digital Signatures guarantee:
- Data wasn’t modified while being transferred from a sender to a recipient
- Data was created by a certain sender
- The sender cannot deny sending the data
• In order to sign data we need:
- Data to sign
- Private key
• In order to verify a signature:
- Data that was signed
- The signature
- Public key
38. Miners
• Miners are nodes are run on powerful or specialized hardware (like ASIC),
and their only goal is to mine new blocks as fast as possible. Miners are
only possible in blockchains that use Proof-of-Work
39. The Coinbase Transaction
• In Bitcoin, outputs come before inputs.
• When a miner starts mining a block, it adds a coinbase transaction to it. A
coinbase transaction is a special type of transactions, which doesn’t require
previously existing outputs. It creates outputs out of nowhere.
40.
41. • Every block must store at least one
transaction and it’s no more possible to mine
blocks without transactions.
Storing Transactions in Blockchain
42. Change
• Each output might be used only once. This means that once an input got a
reference to an output the output cannot be used again even if its value is
higher then needed to create a transaction. In this case, we create
additional output in which value we place a difference between transaction
amount and referenced output value. It is a change.
43. Unspent Transaction Outputs (Balance)
• Unspent means that these outputs weren’t referenced in any inputs
- tx0, output 1
- tx1, output 0
- tx3, output 0
- tx4, output 0
• When we check balance, we need only those that can be unlocked by the
key we own.
44.
45. Sending Coins
• We want to send some coins to someone else. For this, we need to create
a new transaction, put it in a block, and mine the block.
• Before creating new outputs, we first have to find all unspent outputs and
ensure that they store enough value.
46. The UTXO Set
• Cause, we could have tons of blocks, we do not want to iterate through all
of them. And UTXO Set can help us avoid this.
47.
48.
49.
50.
51. Merkle Tree
• The full Bitcoin database (i.e., blockchain) takes more than 282 Gb of disk
space
• Because of the decentralized nature of Bitcoin, every node in the network
must be independent and store a full copy of the blockchain
52. Merkle Tree And Simplified Payment Verification (SPV)
• SPV is a light Bitcoin node that doesn’t download the whole blockchain and
doesn’t verify blocks and transactions. Instead, it finds transactions in
blocks (to verify payments) and is linked to a full node to retrieve just
necessary data. This mechanism allows having multiple light wallet nodes
with running just one full node.
60. Conclusion
• Node.js is a great platform for creating P2P networks, easy coding your
blockchain applications and a big amount of various crypto functions.
• But there are some cons:
- Very slow mining performance
- There is no ability to use GPU without workarounds