Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidChain = errors.New("Invalid chain is given to replace original chain")
ErrInvalidChain is used when trying to replace an invalid chain
var ErrInvalidTransactions = errors.New("Invalid transactions")
ErrInvalidTransactions is used when trying to replace chain with invalid transactions
var ErrMissingLastBlock = errors.New("Missing last block")
ErrMissingLastBlock is used when new block is not provided last block hash
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
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 ¶
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 ¶
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 ¶
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