⛓️
Blockchain
  • Start!
  • Go_lang
    • Tutorial
    • 1. Banking
    • 2. Dictionary
    • 3. URL Checker
    • Useful Methods - Slice
    • Useful data structure
  • RUST
    • Start
    • Basic
    • Basic Programming Concepts
      • Variables and Mutability
  • Bitcoin
    • Start
    • Introduction
    • Transactions
  • GO-BITCOIN
    • Start
    • 1. Blocks & Blockchain
    • 2. Proof of work
    • 3. BadgerDB
    • 4. Transactions
    • 5. Wallet
    • 6. Adding Digital Signatures
  • COSMOS
    • 코스모스 SDK
    • 코스모스 SDK 실습 - nameservice
    • 코스모스 허브는 어떻게 사용하는가?
    • 코스모스 허브, 금융의 역사를 다시 쓰다
    • Tendermint
      • ABCI
      • Messages
  • Cosmos Tutorial
    • Tutorials
    • 1. Blog
    • Nameservice
    • [Starport] Escrow Account: Scavenge
    • [Starport] Inter-Blockchain Communication: Basics
    • Create an IBC Interchain Exchange module
      • Introduction
      • App Design
      • Initialize the Blockchain
      • Create the Order Book
  • Ethereum
    • Start
    • Gas
    • Oracle Problem
  • consensus
    • DPoS
    • PBFT
    • Network model
  • cryptosystem
    • 대칭키 암호
    • IPFS
  • Social token
    • Rally
    • DeSo
      • Bitclout
      • Deso: The Decentralized Social Network
      • Setting Up Your Dev Environment
      • Deso Code Walkthrough
      • Web3 Will Not Be Built on Smart Contracts
  • 재윤TV
    • Start
    • 유니스왑에 대해서 아라보자
      • Concept
      • V2 백서 분석
      • V2 코드 분석
Powered by GitBook
On this page
  • 구현 내용
  • Block
  • Blockchain
  • main.go
  • Code
  • Reference

Was this helpful?

  1. GO-BITCOIN

1. Blocks & Blockchain

간단한 블록과 블록체인을 만들어보자.

구현 내용

  • 블록과 블록들을 연결하는 블록체인 구조를 설계

  • 블록을 생성, 블록체인에 추가

Block

  • Hash, Data, PrevHash를 가지는 간단한 블록을 설계.

  • 이전 해쉬를 이용해 해쉬를 생성.

  • Data와 이전 해쉬값을 이용한 현재 해쉬값 생성 메소드 작성.

Blockchain

  • 블록들을 이어주는 블록 포인터 배열을 생성.

  • AddBlock 메소드 작성.

main.go

package main

import (
	"bytes"
	"crypto/sha256"
	"fmt"
)

// BlockChain structure
type BlockChain struct {
	blocks []*Block
}

// Block structure
type Block struct {
	Hash     []byte
	Data     []byte
	PrevHash []byte
}

// DeriveHash : Data와 PrevHash를 이용해서 Hash를 생성한다.
func (b *Block) DeriveHash() {
	info := bytes.Join([][]byte{b.Data, b.PrevHash}, []byte{})
	hash := sha256.Sum256(info)
	b.Hash = hash[:]
}

// CreateBlock : data와 prevHash를 받아서 새로운 Hash를 생성한 블록을 생성한다.
func CreateBlock(data string, prevHash []byte) *Block {
	block := &Block{[]byte{}, []byte(data), prevHash}
	block.DeriveHash()
	return block
}

// AddBlock : data의 값을 가지는 블록을 추가한다.
func (chain *BlockChain) AddBlock(data string) {
	prevBlock := chain.blocks[len(chain.blocks)-1]
	new := CreateBlock(data, prevBlock.Hash)
	chain.blocks = append(chain.blocks, new)
}

// Genesis : 체인의 맨 처음 블록이다. prevHash 값이 비어있다.
func Genesis() *Block {
	return CreateBlock("Genesis", []byte{})
}

// InitBlockChain : Genesis 블록을 시작으로 하는 블록체인을 생성한다.
func InitBlockChain() *BlockChain {
	return &BlockChain{[]*Block{Genesis()}}
}

func main() {
	chain := InitBlockChain()

	chain.AddBlock("First Block after Genesis")
	chain.AddBlock("Second Block after Genesis")
	chain.AddBlock("Third Block after Genesis")

	for _, block := range chain.blocks {
		fmt.Printf("Previous Hash: %x\n", block.PrevHash)
		fmt.Printf("Data in Block: %s\n", block.Data)
		fmt.Printf("Hash: %x\n", block.Hash)
		fmt.Println()
	}
}

Code

Reference

PreviousStartNext2. Proof of work

Last updated 3 years ago

Was this helpful?

GitHub - HTaeha/Blockchain-in-Golang at part_1GitHub
Logo