blockchain

package
v0.0.0-...-450b360 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GENESIS_BLOCK_NUMBER    = 0
	GENESIS_BLOCK_PREV_HASH = "0"
	GENESIS_BLOCK_PAYLOAD   = "genesis"
	// pre-calculated genesis block hash
	GENESIS_BLOCK_HASH = "e2a1f4f34d6d2ade6eaf221683ee2bcf1c579ca640310954c2e0f0e3457783c3"
)

magic hardcoded genesis block values

View Source
const (
	UnknownError = -1
	// block errors
	WrongBlockNumberError = iota
	NegativeBlockNumberError
	ExcitingBlockNumberError
	WrongPrevBlockHashError
	WrongBlockHashError
	EmptyBlockHashError
	BlockValidationError
	ChainValidationError
	EmptyChainError
	MineBlockError
	PeerSyncError
	AddBlockError
)

Variables

View Source
var ErrorMessages = map[int]string{
	UnknownError:             "unkown error",
	WrongBlockNumberError:    "wrong block number",
	NegativeBlockNumberError: "negative block number",
	ExcitingBlockNumberError: "block number exceeds chain lenght",
	WrongPrevBlockHashError:  "wrong previous block hash",
	WrongBlockHashError:      "wrong block hash",
	EmptyBlockHashError:      "block hash is empty",
	BlockValidationError:     "block validation error",
	ChainValidationError:     "chain validation error",
	EmptyChainError:          "chain is empty",
	MineBlockError:           "cannot mine block",
	PeerSyncError:            "cannot synchronize to chain",
	AddBlockError:            "cannot add block to chain",
}

Functions

func NewBlockchainChainError

func NewBlockchainChainError(errorCode int, err error) error

Types

type Block

type Block struct {
	Number        int
	PrevBlockHash string
	Payload       string
	Hash          string
}

func GetGenesisBlock

func GetGenesisBlock() Block

func (*Block) CalcHash

func (b *Block) CalcHash() string

func (*Block) Mine

func (b *Block) Mine() string

type Chain

type Chain struct {
	ID     uint8
	Blocks []Block
}

func NewChain

func NewChain(ID uint8) *Chain

NewChain creates a new chain with pre-calculated genesis block

func (*Chain) AddBlock

func (c *Chain) AddBlock(block Block) error

func (*Chain) GetBlock

func (c *Chain) GetBlock(blockNum int) Block

func (*Chain) MineBlock

func (c *Chain) MineBlock(payload string)

func (*Chain) Print

func (c *Chain) Print()

func (*Chain) ValidateBlock

func (c *Chain) ValidateBlock(blockNum int) error

func (*Chain) ValidateChain

func (c *Chain) ValidateChain() (int, error)

func (*Chain) ValidateNewBlock

func (c *Chain) ValidateNewBlock(block Block) error

type Peer

type Peer struct {
	ID    uint8
	Peers map[uint8]*Peer
	Chain *Chain
}

func NewPeer

func NewPeer(peerID, chainID uint8) *Peer

func (*Peer) AddBlock

func (p *Peer) AddBlock(block Block) error

func (*Peer) AddPeer

func (p *Peer) AddPeer(peer *Peer) error

func (*Peer) BroadcastBlock

func (p *Peer) BroadcastBlock(block Block)

func (*Peer) DeletePeerByID

func (p *Peer) DeletePeerByID(id uint8)

func (*Peer) GetBlock

func (p *Peer) GetBlock(blockNum int) Block

func (*Peer) GetChain

func (p *Peer) GetChain() *Chain

func (*Peer) GetChainLen

func (p *Peer) GetChainLen() int

func (*Peer) MineBlock

func (p *Peer) MineBlock(payload string)

func (*Peer) Sync

func (p *Peer) Sync() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL