A simplified blockchain implementation in Golang

Owner
Ivan Kuznetsov
Clojure, DevOps
Ivan Kuznetsov
Comments
  • ./main.go:4:9: undefined: CLI

    ./main.go:4:9: undefined: CLI

    Dear Jeiwan, I try to build your code with GoLand IDE (Go 1.9.2) and got the error `# command-line-arguments ./main.go:4:9: undefined: CLI

    Process finished with exit code 2` How to fix it?

  • Double spending ?

    Double spending ?

    ============ Block 03336ca7d60ba5344265de897b055fc2472681524a1751679c6ec17dc734ae70 ============
    Height: 2
    Prev. block: 09d26e7629fb8f5918faf0623351ba1f5740406fe29e6b433522b7441b34958e
    PoW: true
    
    --- Transaction e734d0dd763d10f1e4bb175bad312de1de51a6f08e7ddbc18300412f003f4654:
         Input 0:
           TXID:      598b4e12b0bd16e87f7cdd108a4361c27c147e943fe7742a807dd3d8d0a7f2db
           Out:       0
           Signature: 9802715ce2c6be6b5893c0ad6e77e13b9d6cf1a798b85e7597665edb297cf1e5f2af34a5c43235276bfd3215e6f3d1bf7e063538cec33d07e0b4e1dfce03e335
           PubKey:    7f3310fd561c9d414b9869c31431b649b304cf0177c743c9c3932584e9421102069a563840f3d076070a47c36479f56aa0453451e29e67b3e113c8d86ffa64a8
         Output 0:
           Value:  2
           Script: cf4459389be8f77df9ac2d41bb29e80f5f4120d0
         Output 1:
           Value:  8
           Script: 12facdbb0eb291427b4b9a4745b2553c5e659dc2
    --- Transaction 3bb3cd1345d276e8e76345c8aa109089c9f06d37f471f0603e97fd38bdb88905:
         Input 0:
           TXID:      598b4e12b0bd16e87f7cdd108a4361c27c147e943fe7742a807dd3d8d0a7f2db
           Out:       0
           Signature: 0dae57ad9c2385a0977a758c4b0fb184a44cdf0558d768653b4b8669a47814c9c0df39a64fe913a8cab653b90a3cbbae2c56c92520da66892d522863e11f31b6
           PubKey:    7f3310fd561c9d414b9869c31431b649b304cf0177c743c9c3932584e9421102069a563840f3d076070a47c36479f56aa0453451e29e67b3e113c8d86ffa64a8
         Output 0:
           Value:  1
           Script: c9f24de063058507e8ada37a93b02e8f02218ebf
         Output 1:
           Value:  9
           Script: 12facdbb0eb291427b4b9a4745b2553c5e659dc2
    --- Transaction e9e68fb36ddb0fb40f6dd7f24f2a867f33eb692c435765398257f8cc8e46cd55:
         Input 0:
           TXID:
           Out:       -1
           Signature:
           PubKey:    64353138386533393361346434363432306636316136333861636634386463656562343236643430
         Output 0:
           Value:  10
           Script: 85d3eb79bdacda199724477c03807f64de9db9ce
    
    $ ./blockchain_go getbalance -address 12jMcbWjGr9nPKj4QvKGyjgDyLeBqwLjeG
    Balance of '12jMcbWjGr9nPKj4QvKGyjgDyLeBqwLjeG': 17
    

    As you can see, transaction input (598b4e12b0bd16e87f7cdd108a4361c27c147e943fe7742a807dd3d8d0a7f2db) was used 2 times. Assume I have 3 wallets: A(10 coins), B(0 coins), C(0 coins). then i'm trying to send from A -> B(1 coins) and A -> C(2 coins). first transaction: A -> B (1 coins) and A -> A (9 coins) second transaction: A -> C (2 coins) and A -> A (8 coins) that will result A will have 17 coins instead of having only 7 coins left. A(17 coins), B(1 coin), C(2 coins) ?? P.S. both transactions happened in NODE_ID=3001 P.S.2 NODE_ID=3002 was miner node.

  • Sometimes createwallet generates invalid address.

    Sometimes createwallet generates invalid address.

    Step to reproduce

    1. Write some shell script that createwallet
    2. Use that address to with createblockchain command with flag -address

    loop until error occurs 'panic: ERROR: Address is not valid' that wont take too long.

  • Just a thank you

    Just a thank you

    I was looking for something like this for a while, thanks for taking the time to create this useful resource to learn more about what makes the blockchain work. Accept BTC tips? Cheers

  • Loop w/o end point

    Loop w/o end point

    If you run the project in the part_2 there is no end, and keeps printing the hash forever.

    Mining the block containing "Genesis Block" 3f22aecfa055d42189abe136137514db35bd852762b9d1129fd8098bc0bc5c45

  • Unable to verify wallet address generated seconds before

    Unable to verify wallet address generated seconds before

    Hi, I tryed go install in both macOs and Debian, same problem.

    `./blockchain_go createblockchain -address 1PEiixXK7E8S7pqkjTiePUnP9jZFBonBR9 2018/01/25 18:08:00 ERROR: Address is not valid panic: ERROR: Address is not valid

    goroutine 1 [running]: log.Panic(0xc420055dc8, 0x1, 0x1) /usr/local/go/src/log/log.go:326 +0xc0 main.(*CLI).createBlockchain(0xc420055f70, 0x7fff5fbffc2a, 0x22, 0xc420016048, 0x5) /Users/simo/go/src/blockchain_go/cli_createblockchain.go:10 +0x169 main.(*CLI).Run(0xc420055f70) /Users/simo/go/src/blockchain_go/cli.go:119 +0x615 main.main() /Users/simo/go/src/blockchain_go/main.go:5 +0x2b`

  • ValidateAddress Throws a Panic

    ValidateAddress Throws a Panic

    When creating a new blockchain, the current implementation of ValidateAddress panics:

    $ blockchain_go createblockchain -address $NODE_ID
    panic: runtime error: slice bounds out of range
    
    goroutine 1 [running]:
    main.ValidateAddress(0x7ffeefbff78c, 0x4, 0xc0000a55b0)
    	/Users/go-crypto/wallet.go:56 +0x202
    main.(*CLI).createBlockchain(0xc0000c5f88, 0x7ffeefbff78c, 0x4, 0xc00001a048, 0x4)
    	/Users/go-crypto/cli_createblockchain.go:9 +0x3f
    main.(*CLI).Run(0xc0000c5f88)
    	/Users/go-crypto/cli.go:117 +0x5ce
    main.main()
    	/Users/go-crypto/main.go:5 +0x2b
    

    where

    $ echo $NODE_ID
    3000
    

    Is there a potential quick fix here by changing:

    // wallet.go
    const addressChecksumLen = 4
    

    ?

  • Startnode not working

    Startnode not working

    go run main.go startnode -miner 16yKFXXWEGh1mN5fjfNHNrWS3j5oPvuoni Starting node maguayo Mining is on. Address to receive rewards: 16yKFXXWEGh1mN5fjfNHNrWS3j5oPvuoni 2018/01/15 09:53:49 listen tcp: lookup tcp/maguayo: nodename nor servname provided, or not known panic: listen tcp: lookup tcp/maguayo: nodename nor servname provided, or not known

    goroutine 1 [running]: log.Panic(0xc420049ce0, 0x1, 0x1) /usr/local/Cellar/go/1.8.3/libexec/src/log/log.go:322 +0xc0 ...

  • Include Package Import Names in Write-Up

    Include Package Import Names in Write-Up

    Thanks for the tutorial. I'm working my way through it now.

    One suggestion though. Could you reference which new packages you're importing at each stage. I'm following along with the tutorial and building bit-by-bit and I have to keep running the code, after each new section is introduced, waiting for the undefined: XXX errors and then looking up the Golang docs to find which package I need to import to include XXX

  • wrong base58 implentation

    wrong base58 implentation

    Hi. There should be something wrong with your base58 implentation after this commit.

    It will cause wallet address decode error like this.

    ./blockchain_go createblockchain -address 1DU9fP5ZtqjR3MJvHvV7vvWsuZcBFZ2PeG 2017/11/22 02:00:46 ERROR: Address is not valid panic: ERROR: Address is not valid

  • something in base58.go confused me

    something in base58.go confused me

    for b := range input {
           if b == 0x00 {
    		result = append([]byte{b58Alphabet[0]}, result...)
    	} else {
    		break
    	}
    }
    

    in this cycle, b is the index of input, not the value, so i think it may be wrong here. it should be

    for _, b := range input {
             ...
    }
    

    both in two functions this file(line26 & line42).

  • How to make transaction id unique?

    How to make transaction id unique?

    I'm reading part 4, and I think if txid is not unique for this tutorial. Txid should be unique for blockchain. Otherwise, you can't lookup each transaction by txid.

    The logic to generate txid. It generates the same id for newly mined coin:

    https://github.com/Jeiwan/blockchain_go/blob/e17722eb18caa7618dee298da677a9f5667a0513/transaction.go#L64-L72

    the FindUnspentTransactions would go wrong:

    https://github.com/Jeiwan/blockchain_go/blob/e17722eb18caa7618dee298da677a9f5667a0513/blockchain.go#L78-L82

    for example, could you mine 2 new coins with duplicated txid by calling NewCoinbaseTX("same_miner", "")

    Then one coin could be used and the other is not. The above FindUnspentTransactions logic can't distinguish those 2 coins.

    Even if you add rand in part 6, it still has a chance of colliding.

    https://github.com/Jeiwan/blockchain_go/blob/0538111918aad7d18f2b71b19f8ad0dec186e6db/transaction.go#L174-L188

    Any suggestion to make sure that the txid is unique?

  • Might not be an issue, have a question (Part 4)

    Might not be an issue, have a question (Part 4)

    Noticed something in the method Part 4: FindUnspentTransactions. Maybe I'm missing something obvious but can someone please correct me if I'm wrong.

    In the block of code below, shouldn't we get the spent outputs before we loop through tx.Vout? Otherwise spentTXOs[txID] will always be nil?

    for _, tx := range block.Transactions {
    			txID := hex.EncodeToString(tx.ID)
    
    		Outputs:
    			for outIdx, out := range tx.Vout {
    				// Was the output spent?
    				if spentTXOs[txID] != nil {
    					for _, spentOut := range spentTXOs[txID] {
    						if spentOut == outIdx {
    							continue Outputs
    						}
    					}
    				}
    
    				if out.CanBeUnlockedWith(address) {
    					unspentTXs = append(unspentTXs, *tx)
    				}
    			}
    
    			if tx.IsCoinbase() == false {
    				for _, in := range tx.Vin {
    					if in.CanUnlockOutputWith(address) {
    						inTxID := hex.EncodeToString(in.Txid)
    						spentTXOs[inTxID] = append(spentTXOs[inTxID], in.Vout)
    					}
    				}
    			}
    		}
    
  • Rename utility function IntToHex to IntToBytes

    Rename utility function IntToHex to IntToBytes

    Rename utility function IntToHex to IntToBytes because function convert Int64 into []byte. Often I see comments of people who read that tutorial that they are confused because of IntToHex function.

  • Update blockchain.go

    Update blockchain.go

    Edit the function NewBlockchain: 1.delete the parameter of the function,the parameter is not used. 2.fix the description of the function,the description is not right.

Blockchain-go - A repository that houses a blockchain implemented in Go

blockchain-go This is a repository that houses a blockchain implemented in Go. F

May 1, 2022
Implementation of blockchain using golang

go-blockchain Basic implementation of blockchain using golang. Initialize Blockc

Feb 18, 2022
Mini Blockchain Implementation In Golang Inspired by Go-Ethereum🚀

JP Blockchain ?? ?? Mini Blockchain Implementation In Golang Inspired by Go Ethereum & BlockChain Bar by Lukas (Web3Coach) Features Working Core Compo

Aug 8, 2022
A basic implementation of Blockchain

GoChain A basic implementation of blockchain in go. Building $ cd cmd $ go build -o gochain Usage Starting a node You can start as many nodes as you

Nov 26, 2022
A basic blockchain implementation written in Go

Blockchain Having fun implementing a blockchain using Golang. Using Minimum Viable Blockchain Keys The Blockchain uses ECDSA (224 bits) keys. When a u

Dec 29, 2022
Blockchain implementation with Go

Ultra Basic Blockchain with Go Ultra-basic blockchain implementation with Go. Ru

Feb 17, 2022
Kava - Reference implementation of Kava, a blockchain for cross-chain DeFi. Built using the cosmos-sdk

DeFi for Crypto. Telegram | Medium | Discord Reference implementation of Kava, a

Apr 4, 2022
chia-blockchain some function implement in golang

gochia chia-blockchain some function implement in golang Package bls-signatures implement blspy Usage? Now we can use it to generate plot memo and id,

May 27, 2022
Implementing blockchain using Golang ✔️
 Implementing blockchain using Golang ✔️

Implementing blockchain using Golang ✔️ Keys The Blockchain uses ECDSA (224 bits) keys.

May 24, 2022
Use golang to reproduce the basic blockchain

Blockchain_with_Go Use golang to reproduce the basic blockchain Update Panel V0.1 No transactions but noly blocks are allowed. V0.5 Transactions are n

Dec 30, 2022
Stackledger: a new blockchain written in golang

StackLedger Stackledger is a new blockchain. It is written in golang and uses a novel networking stack built from two primitives: channels and extensi

Feb 21, 2022
goBlockChain is a version of the classic blockChain with POW algorithms written in golang.

goBlockChain is a version of the classic blockChain with POW algorithms written in golang. May come in handy when learning about this technology.

Oct 31, 2021
The Cosmos-SDK is a framework for building blockchain applications in Golang.
The Cosmos-SDK is a framework for building blockchain applications in Golang.

The Cosmos-SDK is a framework for building blockchain applications in Golang. It is being used to build Gaia, the first implementation of the Cosmos Hub.

Nov 26, 2021
Pasaje de código de JS a Golang. Ejemplo de funcionamiento de una blockchain simple.
Pasaje de código de JS a Golang. Ejemplo de funcionamiento de una blockchain simple.

blockchain-dummy-test Pasaje de código de JS a Golang. Ejemplo de funcionamiento de una blockchain simple. Fuente: https://medium.com/geekculture/bloc

Oct 3, 2022
Creating a blockchain in golang

blockchain-in-go Creating a blockchain in go Disclaimer : This code base is sour

Dec 26, 2021
DERO Homomorphic Encryption Blockchain Protocol
DERO Homomorphic Encryption Blockchain Protocol

Homomorphic encryption is a form of encryption allowing one to perform calculations on encrypted data without decrypting it first. The result of the computation is in an encrypted form, when decrypted the output is the same as if the operations had been performed on the unencrypted data.

Dec 27, 2022
DERO: Secure, Anonymous Blockchain with Smart Contracts. Subscribe to Dero announcements by sending mail to [email protected] with subject: subscribe announcements
DERO: Secure, Anonymous Blockchain with Smart Contracts.  Subscribe to Dero announcements by sending mail to lists@dero.io with subject: subscribe announcements

Welcome to the Dero Project DERO News Forum Wiki Explorer Source Twitter Discord Github Stats WebWallet Medium Table of Contents ABOUT DERO PROJECT DE

Dec 7, 2022
run ABI encoded data against the ethereum blockchain

Run EVM code against a database at a certain block height - Note You can't run this against a running geth node - because that would share the db and

Nov 11, 2021
Go module for the Cardano Blockchain

cardano-go cardano-go is both a library for creating go applicactions that interact with the Cardano Blockchain as well as a CLI to manage Cardano Wal

Dec 1, 2022