Day 16: Become a self-taught blockchain developer with zero knowledge in 365 days

PREVIOUS: DAY 15

order 8: the fundamentals of Javascrpit (Technical aspect, Computer Science, Phase 1: a normal technican)

Javascript has 8 data types, which are number, string, bigint, string, boolean, null, undefined, object, and symbol. What is worth mentioning is object. I haven't learned object, but among the stuff I've learned so far, I can know that type obeject is the one for more complex data structure.

We can see that there is no "function" type, functions like alert is classified into object type.

1. We simplily ask for a space for variable, and assigned the function to the variable

create a function (type 1): function declaration
create a function (type 2): function expression
  1. The function itself is a value, and is classified into the object data type.Hence, when we try to print it out, it shows the whole value.
shows the whole funciton, for it's value

2. Variable, on the other hand, is not a value. It is merely the space with a name waiting for some value to be assigned to the space. Therefore, when we try to print out the variable, it will print out it's value(e.g. the function of the variable).

only print out the value part(without the let part)
  1. it is possible to copy the funciton to the variable
  2. the original function won't be changed off course.
  1. ask a yes/no quesiton, and have a feedback for each condition, which is also called callback function
  2. two methods: function decalration, function expression
function declaration
function expression (shorter)
  1. first difference: function expression is delcare under other syntax structure.
  2. second difference: subtle difference. The function of the function expression is created the moment the execution flow reaches it, and it is usable fom that moment, so the order of calling it out matters. The function of the function declaration, on the otehr hand, is visible in the whole script(e.g., global function declaration) and can be called earlier, so the order of calling doesn't matter.
  3. third difference: There are times when we want the commands in the funtion to be visible outside the function. The function declaration can't do the job, for the things inside its function is only visible inside its funciton, we can't call it from outside. It would be an error. Nevertheless, the function expression can so the job. We can assign the thing to the variable and will have the complete visibility.
  1. usually we use function declaration, for it offers more freedom to organixe, call it before the execution reaches it, more readable.
  2. if it's necessary, then use the other.
  1. simple, often useful than function expression, not that readable
  2. looks like an arrow: let func = (arg1, arg2, …, argN) => expression
  3. the same as
  1. more than one argement: e.g., let sum = (a, b) => a + b;
  2. one argument: the parantheses can be omitted, e.g., let double = n => n * 2;
  3. no argements: the parantheses would be empty but should be present, e.g., let sayHi = () => alert(“Hello!”);
  1. for multilime expressions, we should them in the {}
  2. We should use return to call the value back
  3. To conclude, for the kind of arrow function with no curly braces, the simple expression is right to the arrow, and the function evaluates it and return the result. And the other kind with curly braces, it is allowed to write multiple expressions, but requires a return to remind the function to return the value.

2.18 JavaScript specials (gerneral conclusion)

  1. the next line would also do the work, but the use of semicolon is more recommeded.
  2. semicolons are not required after the code block{}, or loops, e.g. , for(){}.
  1. To fully enable all features of Javascript
  2. should put "use strict " in the beginning

Magic lies here — Statically vs Dynamically Typed Languages, Dynamic typing vs. static typing

  1. three way to declare one: let, const(can't be change, stiil classified in variables), var(old version)
  2. variable name: digits(but not in first letter), letters, non-latim alphabets, $, _
  3. dynamically typed language: the data type of the variable is checked during the run-time.If there is two types of variable in the same name declared, then it will just kick the first one off. Its oppisite is static typed, whose type checking is performed in the cimpile-time. It means that you can't just change the data type of the variable, for its type is already constrained during the complie-time, and the type absolutely needs to abide by its previous setting.
  4. 8 data type: string, number, bigint, boolean, null, undefined, object, symbol. (the typeof to test the type has two widely acepted mistake)
  1. modal: prompt, confirm, alert
  1. arithmetical operator: +, -, *, /, %, **
  2. assignment operator: =
  3. bitwise operator: and, or, xor, not, left shift, right shift, zero-fill right shift
  4. conditional operator(ternary operator): ?
  5. logical operator: &&, ||, !
  6. nullish coalescing operator: a??b
  7. comparions: ==, <, <=, >, >=
  8. strict eqality operator: ===
  9. other operator: string concatenation(+), numeric conversion(+), modify in place(e.g. +=, *=), increment(++)/decrement(--), comma operator(,).
  1. if(){}
  2. if(){}else{}
  3. if{}else if{}else{}
  1. while(){}
  2. do{}while()
  3. for(){}
  4. directive: break, continue
  5. label for break and continue
  1. replace multiple if
  2. switch(){case...;case...;default...;}
  1. function declaration: function(){.... return..;}
  2. funciton expression: let x=funciton(){.... return}
  3. arrow function

(1) let x =(,)=>(,)

(2) let x = (,)=>{..... return..}

Code quality

Debugging is necessary because we want to find out and fix the problem. Therefore, we have to learn the method and technique for debugging. And chrome(browser) is the common runtime environment for Javascript. Although I haven't learned running Javascript in the browser yet, the major concepts and the techniques for debugging might be similiar. This part would be pretty useful and significant.

The basic concept for debugging. Although it takes chrome for an example, but it's mostly the fundamental concept for debugging.

  1. three parts: file navigator, code editor, javascript debugging
  2. fourth part: console (Esc)
  1. The concept of breakpoints: it is the part that divides codes into different execution parts. By clicking it, during the execution, the code will pause, which is convenient for debugging.
  2. It's useful for having various of breakpoints.
  3. We can even create a conditional breakpoint, which triggers when the given expressions is truthy.
  1. debugger; // ← the debugger stops here
  2. no need to look up to the script in the developer tool in order to set the breakpoint.
  1. watch: shows the current value of the expressions
  2. call stack: shows the nested call chain
  3. scope: local and global variables

1. Third way of debugging: pause in case of an error

you can just jump to here without setting the breakpoint

3.2 Coding Style

A good code style is the clean code which is pretty readable.

  1. curly braces
  2. line length
  3. indents: horizontal, vertical
  4. semicolons
  5. nesting levels: avoid levels, it is better for only one level
  6. funciton placement: code first, then the function. It would be clear that what is does.
  7. style guides: many existing guides
  8. automated linters: tools to check the style of the code. e.g., npm install -g eslint

3.3 Comments

  1. the amount of comments should be minimal
  2. To the root problem, the code should be easily understand without the funciton. e.g. changing the complicated code with the function. Funcitons themselves tell what's going on. There is even no need for the comment.
unclean one
clean one
  1. simpliy describe the architecture: overview of components, how they interact, the control flow, e.g., /* Returns x raised to the n-th power.
  2. (JSDoc)document the function: usage, parameters, returned value, e.g.,
  • @param {number} x The number to raise.
  • * @param {number} n The power, must be a natural number.
  • * @return {number} x raised to the n-th power.

=> others are able to understand the purpose of the function without looking at the code

=> tools

3. why it is solved this way: the codes can't tell you why it is solved this way. Commenting it is a good way to continue the developments in the future or by others.

4. the subtle features: anything subtle and counter-intuitive is worth commenting.

order (2): The detailed research of Bitcion(technical aspect, Blockchain technology, Bitcion)

Mining

All miners would have a mechanism to select one transaction in the mining pool, such as the one with the highest transaction fee. Then the miners would have the competition, competing who would be the fastest to meet the required standard of the hash. The first finding one and publish to the network would receive the transaction fees and new coin.

HOW DO BLOCKCHAIN NETWORKS VALIDATE DATA?, How are transactions validated?

After successfully finding the required ourcome, the miner will send it the the connected node, and the node recieving this would verify there is no mistakes. After validating it, it will continue to propagate this to the next connected node. Repeating this until the whole network has an up-to-date ledger.

proof-of-work

Transaction verification vs block mining

In order to generate a successful hash, the miner is the role to validates the transaction by spending enough physical compuational power and time.

The block

Bitcoin mining How do you create a block?

The transactions is one of the two components in the block. It is connected together as a merkle tree. The structure of the merkle tree is simple. The lowest level are all the transactions. And the combination of the two leaf transactions are hashed again and become the parent of these leafs. And two hashed parents got their parents in the same way. At last, we got the root of the merkle tree. Which is an unchagable summary of the whole tree.

security problems

Basically, there are four propoties of the hash function. The first is the deterministic, meaning the with the identical input, the output will always br the same. The second is fast. As complicated as the function, it costs few computational power. The third is unique. There is no two inputs generating the same output.The last is irreversible, meaning that there is no way to reason what input is by the output.

The birthday attack overturn the third rule above. It is also called collision, which the attacker find the input generating the same output as the other input. The best prevention is to hash the hash, it is also called double hashing

Are blockchains that safe? How to attack and prevent attacks, What’s a Sybil attack?, 新手知識|區塊鏈到底在幹嘛〈下〉區塊鏈安全嗎?共識機制?智能合約如何運作?

The sybil attack means the one adversary is able to control multiple nodes. It is unknown that if it's true because the nodes are all anonymous. If they are able to control so many nodes, they could collectively change the ledger. And proof-of-work is the machanism to prevent this, for it requires the block creater to contribute the compuational power, and the chance to create one is allocated proportionally, meaning that even sybil is able to control multiple nodes, it doesn't have many opportunities to create the block, which is the prerequisities for tamper with the ledger.

Are blockchains that safe? How to attack and prevent attacks

51% attack is often confused with the sybil attack. The former is the case that one entity is able to control the 51% of the computational power of the network. With the power to generate more than half hash rate, one is encouraged for double-spending attack. However, gaining this kind of computaitonal power is unrealistic, requiring an unbelievable resources. Under the proof-of-work machanism, the 51% attack of Bitcoin is nearly impossible.

For adversaties willing to attack, there is few incentives. First, if they acquire the power to attack, the cost of the preparation is uncountable. Second, if they got the power to attack in the long term, the price of the coin would become empty, which will stop them from attacking.

#

NEXT: DAY 19

Taiwanese. Male. Ximena is my daughter(future). Capable of self-taught anything (a bit overconfidence). Face the ordeal of listening to the sage on the stage.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store