-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_mo.txt
102 lines (76 loc) · 7.18 KB
/
my_mo.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
Blockchain is a distributed and decentralized ledger that stores data such as transactions, and that is publicly shared across all the nodes of it's network.
Blockchain consist of a collection of blocks and eacch block acts as a storage unit. It is given a data field which is used to store information in the block itself. Every block has hash field, data field, input for the block's hash includes ..., lashhash field. And thanks to lasthashes we have a chain of blocks.
Blockchain as a ledger, a ledger is a record keeping book to keep record of all economic transactions of an organizations it has payments, contracts, movemment of assets, etc. As a ledger blockchain serves a purpose of storing transactional data.
Beyond that blockchain is adistributed ledger cause ledger itself is shared with everyone using the blockchain network. Everyone has a copy of blockchain ledger which gets updates everytime. Ledger is not controlled by a single entity.
Cryptocurrency leverages the blockchain it does so in order to keep a public database of transactions that everyone can access.
Mining : adding transactions to the block, see when people submit transactions to the cryptocurrency network the transacaion then joins the transaction pool transactions initially stays in an unconfirmed state, miners then take a group of unconfirmed transactions and use them as the data to be officially recorded within a new block in a chain. And to gain the right to add a new block the miner will have to solve a computational puzzle called the proof of work algorithm. Once a miner solves a proof of work algorithm they can submit a block to the network. And this proof of work needs to be verified by another miner. And as a reward of doing the task of mining the miner will receive some cryptocurrecny so miners are trying to add new block.
Fundamentally : blockchain is a list of linked blocks, each block in the chain contains a link to the last blovk that came before it. Chain of links and it's linking those blocks.
//very basic Hash function, consistently generates unique value with every data input
//generate unique value for every data that comes in
const lightningHash = (data) => {
return data + '*';
}
//blockchain class with an array that contains block instances first it generates blocks with all it's dummy values each lastHash matches the hash that came before.
//A very bais Blockchain implementation
//JS class
block
class Block {
//constructor method for the block class with data, hash, lastHash
constructor(data, hash, lastHash){
this.data = data;
this.hash = hash;
this.lastHash = lastHash; //cause when we create a new block, we'll specify a data, hash, and a last hash field
}
}
//instance of Block class
const fooBlock = new Block('foo-data', 'foo-hash', 'foo-last-Hash');
console.log(fooBlock);
class Blockchain {
constructor() {
//Genesis block or the very first block
const genesis = new Block('gen-data', 'gen-hash', 'gen-lastHash');
//local chain field for the blockchain instance
this.chain = [genesis]; //an arr, idea is that this blockchain is going to have a list of Block instances within this chain array itself every new block is gonna be based on a prior block.
}
//generic addBlock method
addBlock(data) { //so now the block has data specified now we need the last hash and hash itself
const lastHash = this.chain(this.chain.length - 1).hash;
const hash = lightningHash(data + lastHash);
//block, and then push the block in the chain
const block = new Block(data, hash, lastHash);
this.chain.push(block);
}
}
const fooBlockchain = new Blockchain();
fooBlockchain.addBlock('one');
fooBlockchain.addBlock('two');
fooBlockchain.addBlock('three');
console.log(fooBlockchain);
//Block is a storage unit, block is storage unit itself, therefore the datadield specifies what the block is storing itself. 2nd Hashfield is the value generated for the block by taking the data and cryptographically transforming it. LastHash field creates a link b/w blocks for the blockchain.
//node.js as backend JS engine
//Jest is a JS lib to perform test on JS
at the core block consist of 4 pieces of data,
1. timestamp : we can use JS data object
2. lastHash : hash of the block that came before it
3. data to store, int, str, or rntire JS obj
4. hash : hash itself for the block
//let's create our first class
//first block : genesis block origin of the blockchain hardcoded with dummy values
//Add a method to the block class called mine block, the mine block method will create a block based off a block input arguments that represents the last block for that block, as well as some data to store within the new block itself. So 2nd argument is called data.
//Creating a block requires some computational work for a blockchain to grow at a resonable pace.
So to add a new mine block functionality we are gonna add a new section to the block called '___'.
//Moving on to generating the unique hash value for the block
The unique hash value is generated for the block is genereated from all the data fields particular to that block, this includes timestamp, data, and most importantly unique hash value of the block that came before it.
Generated from the timestamp, given data, and the lastHash. Use SHA256-Secure hashing algorithm 256 represents teh sie if the 256 bits of the hash.
e.g. foo -> is 256 0's and 1's we see it in hexadecimal form in hexadecimal characters where every 4bits by 1hex digit in the hex digit can be from zero to nine and eight f. 256/2 = 64 characters to represent all 256 bits of the binary in hexadecimal foerm.
Also is a one way fun means we cannot decrypt the data.
API and network blockchain backend
Get multiple blockchian instances interactn with each other, goal is to form network of nodes running a blockchain application. Initial way we get these nodes interact with each other is thru a common API.
Then each node in the network is going to run the same API in order to expose common methods that can be used to both reach each other's info and send data to each other.
An API is a set of callable methods that allows external parties to call code in an existing system for the blockchain.
For the blockchain sys two main methods we add to the API are :
1. Read the blockchain data
2. Write to teh blokchain.
To create the API we'll use a popular node.js framework called express, Express allows to create a web server that listens for a request and connections on a port of a OS. Now in this case we'll configure the express server to accept various HTTP requests.
These http requiests will be made over the webdefined by the http e.g. a GET http request is associated with reading data. So we'll have a GET request to read data from the blockchain. On the other hand POST http request is associated with sending data to the webserver. POST request can carry data to add a new block to the chain.
In the network there are gonna be multiple instances of the blockchain application that are going to be running theor own respective servers instances of the blockchain application. But here using the API respective blockchain instances will be able to read each other's data and add new block to the chain if necessary.