Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic structure #1

Merged
merged 6 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions core/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core

// Block represents a block in the blockchain
type Block struct {
BlockNumber uint64
Hash []byte
PreviousHash []byte
TimeStamp uint64 // Unix timestamp (https://www.unixtimestamp.com/)
Data string // FIXME It will be changed to some struct contains txs.
}

// NewBlock creates and returns new block
func NewBlock(blockNumber uint64, previousHash []byte, data string) *Block {
// TODO
return &Block{blockNumber, []byte{}, previousHash, 1234567890, data}
}

// NewGenesisBlock creates and returns genesis block
func NewGenesisBlock() *Block {
return NewBlock(0, []byte{}, "Planus Genesis Block")
}

// Serialize serializes block to byte array
func (block *Block) Serialize() []byte {
// TODO
return []byte{}
}

// Deserialize deserializes byte array to block
func Deserialize(bytes []byte) *Block {
// TODO
return &Block{}
}
55 changes: 55 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package core

import (
"log"
)

// Blockchain represents a list of connected blocks
type Blockchain struct {
blocks []*Block
}

// NewBlockchain creates a blockchain with genesis block
func NewBlockchain() *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}

// AddBlock adds the block into blockchain
func (blockchain *Blockchain) AddBlock(block *Block) {
if (blockchain.isValidBlock(block)) {
blockchain.blocks = append(blockchain.blocks, block)
}
}

// CreateBlock creates a new block and returns it
func (blockchain *Blockchain) CreateBlock(data string) *Block {
return NewBlock(
blockchain.GetLatestBlockNumber() + 1,
blockchain.GetLatestBlock().PreviousHash, data)
}

// GetLatestBlock returns latest block in the blockchain
func (blockchain *Blockchain) GetLatestBlock() *Block {
return blockchain.blocks[blockchain.GetLatestBlockNumber()]
}

// GetLatestBlockNumber returns latest block number in the blockchain
func (blockchain *Blockchain) GetLatestBlockNumber() uint64 {
// FIXME Use other methods instead of len() that supports unit64
return (uint64)(len(blockchain.blocks) - 1)
}

// isValidBlock validates block and returns valid or not
func (blockchain *Blockchain) isValidBlock(block *Block) bool {
// TODO Validate block number with GetLatestBlockNumber()
// TODO Validate previous block hash with GetLatestBlock()
// TODO Validate new block hash
return true
}

// FOR TEST
func (blockchain *Blockchain) ShowBlockchainForDebug() {
for idx, block := range blockchain.blocks {
log.Println(idx, block.Data)
}
}
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"./core"
)

func main() {
blockchain := core.NewBlockchain()
blockchain.AddBlock(blockchain.CreateBlock("New Block 1"))
blockchain.AddBlock(blockchain.CreateBlock("New Block 2"))

// Test
blockchain.ShowBlockchainForDebug()
}