mining

package
v0.0.0-...-e59846b Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidChain = errors.New("Invalid chain is given to replace original chain")

ErrInvalidChain is used when trying to replace an invalid chain

View Source
var ErrInvalidTransactions = errors.New("Invalid transactions")

ErrInvalidTransactions is used when trying to replace chain with invalid transactions

View Source
var ErrMissingLastBlock = errors.New("Missing last block")

ErrMissingLastBlock is used when new block is not provided last block hash

View Source
var ErrShorterChain = errors.New("Current chain is the longest, Incoming chain is no longer, No replacement")

ErrShorterChain is used when trying to replace a shorter chain

Functions

This section is empty.

Types

type Block

type Block struct {
	Timestamp  int64         `json:"timestamp"`
	LastHash   *string       `json:"lastHash"`
	Hash       *string       `json:"hash"`
	Data       []Transaction `json:"data"`
	Nonce      uint32        `json:"nonce"`
	Difficulty uint32        `json:"difficulty"`
}

Block represents a block in blockchain

func CreateGenesisBlock

func CreateGenesisBlock(genesisLastHash string, genesisHash string, genesisDifficulty uint32, genesisNonce uint32) (*Block, error)

CreateGenesisBlock returns the genesis block created from config

type Blockchain

type Blockchain struct {
	Chain []Block `json:"chain"`
}

Blockchain represents a blockchain

type Input

type Input struct {
	Timestamp int64  `json:"timestamp"`
	Amount    uint64 `json:"amount"`
	Address   string `json:"address"`
	Signature string `json:"sig"`
}

Input of transaction

type MockedListing

type MockedListing struct {
	mock.Mock
}

MockedListing is a mocked object that implememnts listing.Service

func (*MockedListing) GetBlockCount

func (m *MockedListing) GetBlockCount() uint32

GetBlockCount returns the latest block count in blockchain

func (*MockedListing) GetBlockchain

func (m *MockedListing) GetBlockchain() *listing.Blockchain

GetBlockchain returns a list of blocks from genesis block

func (*MockedListing) GetLastBlock

func (m *MockedListing) GetLastBlock() listing.Block

GetLastBlock adds mined block to blockchain

type MockedRepository

type MockedRepository struct {
	mock.Mock
}

MockedRepository is a mocked object that implememnts mining.Repository

func (*MockedRepository) AddBlock

func (m *MockedRepository) AddBlock(minedBlock *Block) error

AddBlock adds mined block to blockchain

func (*MockedRepository) ReplaceChain

func (m *MockedRepository) ReplaceChain(newChain *Blockchain) error

ReplaceChain replaces new valid longer chain with the original chain

type MockedValidating

type MockedValidating struct {
	mock.Mock
}

MockedValidating is a mocked object that implememnts validating.Service

func (*MockedValidating) ContainsValidTransactions

func (m *MockedValidating) ContainsValidTransactions(bc *validating.Blockchain) (bool, error)

ContainsValidTransactions returns true if blockchain contains valid transactions

func (*MockedValidating) IsValidChain

func (m *MockedValidating) IsValidChain(bc *validating.Blockchain) bool

IsValidChain returns true if list of blocks compose valid blockchain

type Repository

type Repository interface {
	// AddBlock adds a minedBlock into blockchain
	AddBlock(minedBlock *Block) error
	ReplaceChain(newChain *Blockchain) error
}

Repository provides access to in-memory blockchain

type Service

type Service interface {
	MineNewBlock(lastBlock *Block, data []Transaction) (*Block, error)
	AddBlock(minedBlock *Block) error
	ReplaceChain(newChain *Blockchain) error
}

Service provides block creating operations

func NewService

func NewService(r Repository, l listing.Service, v validating.Service, mineRate int64) Service

NewService creates a creating service with necessary dependencies

type Transaction

type Transaction struct {
	ID     string            `json:"id"`
	Input  Input             `json:"input"`
	Output map[string]uint64 `json:"output"`
}

Transaction in data

Jump to

Keyboard shortcuts

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