Coder Social home page Coder Social logo

node-blockchain's Introduction

Blockchain in Node.js

A simple blockchain and cryptocurrency wallet implemented in Node.js and TypeScript (for learning purposes).

Watch the Blockchain in 100 Seconds video.

Usage

git clone <this-repo>

npm install
npm start

node-blockchain's People

Contributors

codediodeio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-blockchain's Issues

Spend money you don't have

The idea for the genesis block is to get currency into the system by adding a transaction to some specified wallet. In this case satoshi (I see what you did there). So satoshi needs to first send transactions to the other wallets, so they have currency as well. That theoretically happens in line 125-127 BUT it does NOT!

What I mean by that, is that the amount they held is never taken into account. Also there would be no currency in the system since the transaction in the genesis block to satoshi should contain the public key of satoshi instead.

JSON.stringify order

First of all thanks for the content and sharing the code!

I have one question though. Class is a js object, where fields are not ordered. If you produce a string from the Transaction with JSON.stringify you cannot ensure consistent order, right? So it might be that same transaction produces different hashes. Would it make more sense to convert this to an array and then convert to string?

Shouldn't blocks on the chain have both previous & current hashes?

According to the Bitcoin whitepaper and many videos I see online on the topic each block has a header (current hash that was mined) and prevHash matching the header of the previous block. However, this is not provided here so the chain is not actually a chain, but just many non-connected blocks. Furthermore, it seems that when forming a new block, the prevHash is not considered, but rather a new hash is made using the getter from class Block. Is this approach correct?

A confused point about function "mine"

It's a good project for me who want to learn the block chain.But there's a confused point that I can't figure out,the function "mine" seems no meanings when we use the addBlock.It does not reflect the relationship between mining and block.Then I think the return value of "mine" should be nonce+solution,and assign the return value to the nonce of block.

mine(nonce: number) {
    ...
    return nonce+solution;
    ...
 }

addBlock(transaction: Transaction, senderPublicKey: string, signature: Buffer) {
    ...
    if (isValid) {
      const newBlock = new Block(this.lastBlock.hash, transaction);
      newBlock.nonce = this.mine(newBlock.nonce);
      this.chain.push(newBlock);
    }
    ....
}

Mining implies the block will be added?

When mining, shouldn't the solutionHash be compared to a targetHash and the block only be added if solutionHash <= targetHash?

Otherwise, what stops a miner from having a list of predefined hashes that meet the criteria of having 4 leading zeros and just submitting them.

On that note, it would also make more sense to have a variable number of leading zeros in the mining process' expected solution hash for each block that needs to be added to the chain.

I am currently working on a project similar to this to get a better understanding in the cryptocurrency space.
Feel free to have a look and let me know if the above is indeed correct and I will make a PR

Had troubles getting the code to run - see below

jim@blockchain:~/Desktop/dev/node-blockchain$ npm start

[email protected] start /home/jim/Desktop/dev/node-blockchain
tsc && node .

โ›๏ธ mining...

<--- Last few GCs --->

[3125:0x8299a0] 48929 ms: Mark-sweep 1373.9 (1425.5) -> 1373.8 (1425.5) MB, 2545.5 / 25.8 ms (average mu = 0.188, current mu = 0.234) allocation failure GC in old space requested
[3125:0x8299a0] 53388 ms: Mark-sweep 1374.5 (1425.5) -> 1374.3 (1426.0) MB, 3740.9 / 8.6 ms (average mu = 0.173, current mu = 0.161) allocation failure GC in old space requested

<--- JS stacktrace --->

==== JS stack trace =========================================

0: ExitFrame [pc: 0x1a042de5452b]
1: StubFrame [pc: 0x1a042dd9a1f0]
2: ConstructFrame [pc: 0x1a042dd99d43]

Security context: 0x36207fcaee11
3: Transform [0x805070824c9] [_stream_transform.js:~104] [pc=0x1a042dd84ad6](this=0x2f6cb16f6659 ,options=0x30cebff825d9 )
4: mine [0xf816fe09011] [/home/jim/Desktop/dev/node-blockchain/index.js:~64] [pc=0x1a042df10ebe](this=0x0f816f...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0x7fb5f35b546c node::Abort() [/lib/x86_64-linux-gnu/libnode.so.64]
2: 0x7fb5f35b54b5 [/lib/x86_64-linux-gnu/libnode.so.64]
3: 0x7fb5f37e1e6a v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
4: 0x7fb5f37e20e1 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/lib/x86_64-linux-gnu/libnode.so.64]
5: 0x7fb5f3b7cc66 [/lib/x86_64-linux-gnu/libnode.so.64]
6: 0x7fb5f3b8e043 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
7: 0x7fb5f3b8e930 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/lib/x86_64-linux-gnu/libnode.so.64]
8: 0x7fb5f3b9091d v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
9: 0x7fb5f3b90975 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/lib/x86_64-linux-gnu/libnode.so.64]
10: 0x7fb5f3b5cdda v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/lib/x86_64-linux-gnu/libnode.so.64]
11: 0x7fb5f3de831e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/lib/x86_64-linux-gnu/libnode.so.64]
12: 0x1a042de5452b
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! [email protected] start: tsc && node .
npm ERR! Exit status 134
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/jim/.npm/_logs/2021-03-03T01_14_52_067Z-debug.log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.