core

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2018 License: LGPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Copyright (C) 2018 go-dappley authors

This file is part of the go-dappley library.

the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU Gc eneral Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.

This file is part of the go-dappley library.

the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.

This file is part of the go-dappley library.

the go-dappley library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

the go-dappley library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with the go-dappley library. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

View Source
const BlockPoolMaxSize = 100
View Source
const TransactionPoolLimit = 5
View Source
const UtxoForkMapKey = "utxoFork"
View Source
const UtxoMapKey = "utxo"

Variables

View Source
var (
	ErrBlockDoesNotExist   = errors.New("ERROR: Block does not exist in blockchain")
	ErrTransactionNotFound = errors.New("ERROR: Transaction not found")
)
View Source
var (
	ErrInsufficientFund = errors.New("ERROR: The balance is insufficient")
	ErrInvalidAmount    = errors.New("ERROR: Amount is invalid (must be > 0)")
)

Functions

func CompareTransactionTips

func CompareTransactionTips(a interface{}, b interface{}) int

func DeserializeUTXO

func DeserializeUTXO(d []byte) *utxoIndex

func GetStoredUtxoMap

func GetStoredUtxoMap(db storage.Storage, mapkey string) utxoIndex

func HashAddress

func HashAddress(address []byte) []byte

func HashPubKey

func HashPubKey(pubKey []byte) ([]byte, error)

func IsParentBlock

func IsParentBlock(parentBlk, childBlk *Block) bool

func IsParentBlockHash

func IsParentBlockHash(parentBlk, childBlk *Block) bool

func IsParentBlockHeight

func IsParentBlockHeight(parentBlk, childBlk *Block) bool

Types

type Address

type Address struct {
	Address string
}

func NewAddress

func NewAddress(addressString string) Address

func (Address) ValidateAddress

func (a Address) ValidateAddress() bool

type Addresses

type Addresses struct {
	Addresses []Address
}

type Block

type Block struct {
	// contains filtered or unexported fields
}

func Deserialize

func Deserialize(d []byte) *Block

func FakeNewBlockWithTimestamp

func FakeNewBlockWithTimestamp(t int64, transactions []*Transaction, parent *Block) *Block

func GenerateMockBlock

func GenerateMockBlock() *Block

func GenerateMockFork

func GenerateMockFork(size int, parent *Block) []*Block

the first item is the tail of the fork

func GenerateMockForkWithInvalidTx

func GenerateMockForkWithInvalidTx(size int, parent *Block) []*Block

the first item is the tail of the fork

func GenerateMockForkWithValidTx

func GenerateMockForkWithValidTx(size int, parent *Block) []*Block

the first item is the tail of the fork

func NewBlock

func NewBlock(transactions []*Transaction, parent *Block) *Block

func NewGenesisBlock

func NewGenesisBlock(address string) *Block

func (Block) AddSpendableOutputsAfterNewBlock

func (blk Block) AddSpendableOutputsAfterNewBlock(mapkey string, db storage.Storage)

func (*Block) CalculateHash

func (b *Block) CalculateHash() Hash

func (*Block) CalculateHashWithNonce

func (b *Block) CalculateHashWithNonce(nonce int64) Hash

func (Block) ConsumeSpendableOutputsAfterNewBlock

func (blk Block) ConsumeSpendableOutputsAfterNewBlock(mapkey string, db storage.Storage)

func (*Block) FindTransactionById

func (b *Block) FindTransactionById(txid []byte) *Transaction

func (*Block) FromProto

func (b *Block) FromProto(pb proto.Message)

func (*Block) GetCoinbaseTransaction

func (b *Block) GetCoinbaseTransaction() *Transaction

func (*Block) GetHash

func (b *Block) GetHash() Hash

func (*Block) GetHeight

func (b *Block) GetHeight() uint64

func (*Block) GetNonce

func (b *Block) GetNonce() int64

func (*Block) GetPrevHash

func (b *Block) GetPrevHash() Hash

func (*Block) GetTimestamp

func (b *Block) GetTimestamp() int64

func (*Block) GetTransactions

func (b *Block) GetTransactions() []*Transaction

func (*Block) HashTransactions

func (b *Block) HashTransactions() []byte

func (*Block) Rollback

func (b *Block) Rollback(txPool *TransactionPool)

func (*Block) Serialize

func (b *Block) Serialize() []byte

func (*Block) SetHash

func (b *Block) SetHash(hash Hash)

func (*Block) SetNonce

func (b *Block) SetNonce(nonce int64)

func (*Block) ToProto

func (b *Block) ToProto() proto.Message

func (Block) UpdateUtxoIndexAfterNewBlock

func (blk Block) UpdateUtxoIndexAfterNewBlock(mapkey string, db storage.Storage)

func (*Block) VerifyHash

func (b *Block) VerifyHash() bool

func (*Block) VerifyTransactions

func (b *Block) VerifyTransactions(utxo utxoIndex) bool

type BlockHeader

type BlockHeader struct {
	// contains filtered or unexported fields
}

func (*BlockHeader) FromProto

func (bh *BlockHeader) FromProto(pb proto.Message)

func (*BlockHeader) ToProto

func (bh *BlockHeader) ToProto() proto.Message

type BlockHeaderStream

type BlockHeaderStream struct {
	Hash      Hash
	PrevHash  Hash
	Nonce     int64
	Timestamp int64
}

type BlockPool

type BlockPool struct {
	// contains filtered or unexported fields
}

func NewBlockPool

func NewBlockPool(size int) *BlockPool

func (*BlockPool) BlockRequestCh

func (pool *BlockPool) BlockRequestCh() chan BlockRequestPars

func (*BlockPool) GetBlockchain

func (pool *BlockPool) GetBlockchain() *Blockchain

func (*BlockPool) GetForkPoolHeadBlk

func (pool *BlockPool) GetForkPoolHeadBlk() *Block

func (*BlockPool) GetForkPoolTailBlk

func (pool *BlockPool) GetForkPoolTailBlk() *Block

func (*BlockPool) IsHigherThanFork

func (pool *BlockPool) IsHigherThanFork(block *Block) bool

func (*BlockPool) IsParentOfFork

func (pool *BlockPool) IsParentOfFork(blk *Block) bool

func (*BlockPool) IsTailOfFork

func (pool *BlockPool) IsTailOfFork(blk *Block) bool

func (*BlockPool) Push

func (pool *BlockPool) Push(block *Block, pid peer.ID)

func (*BlockPool) ReInitializeForkPool

func (pool *BlockPool) ReInitializeForkPool(blk *Block)

func (*BlockPool) ResetForkPool

func (pool *BlockPool) ResetForkPool()

func (*BlockPool) SetBlockchain

func (pool *BlockPool) SetBlockchain(bc *Blockchain)

func (*BlockPool) VerifyTransactions

func (pool *BlockPool) VerifyTransactions(utxo utxoIndex) bool

Verify all transactions in a fork

type BlockRequestPars

type BlockRequestPars struct {
	BlockHash Hash
	Pid       peer.ID
}

type BlockStream

type BlockStream struct {
	Header       *BlockHeaderStream
	Transactions []*Transaction
	Height       uint64
}

type Blockchain

type Blockchain struct {
	// contains filtered or unexported fields
}

func CreateBlockchain

func CreateBlockchain(address Address, db storage.Storage, consensus Consensus) *Blockchain

CreateBlockchain creates a new blockchain db

func GenerateMockBlockchain

func GenerateMockBlockchain(size int) *Blockchain

func GenerateMockBlockchainWithCoinbaseTxOnly

func GenerateMockBlockchainWithCoinbaseTxOnly(size int) *Blockchain

func GenerateMockBlockchainWithCoinbaseTxOnlyWithConsensus

func GenerateMockBlockchainWithCoinbaseTxOnlyWithConsensus(size int, consensus Consensus) *Blockchain

func GetBlockchain

func GetBlockchain(db storage.Storage, consensus Consensus) (*Blockchain, error)

func (*Blockchain) AddBlockToDb added in v0.1.1

func (bc *Blockchain) AddBlockToDb(block *Block)

record the new block in the database

func (*Blockchain) AddBlockToTail added in v0.1.1

func (bc *Blockchain) AddBlockToTail(block *Block)

func (*Blockchain) FindTransaction

func (bc *Blockchain) FindTransaction(ID []byte) (Transaction, error)

TODO: optimize performance

func (*Blockchain) FindTransactionFromIndexBlock

func (bc *Blockchain) FindTransactionFromIndexBlock(txID []byte, blockId []byte) (Transaction, error)

func (*Blockchain) FindUTXO

func (bc *Blockchain) FindUTXO(pubKeyHash []byte) ([]TXOutput, error)

func (*Blockchain) FindUnspentTransactions

func (bc *Blockchain) FindUnspentTransactions(pubKeyHash []byte) ([]Transaction, error)

TODO: optimize performance

func (*Blockchain) GetBlockByHash

func (bc *Blockchain) GetBlockByHash(hash Hash) (*Block, error)

func (*Blockchain) GetBlockPool added in v0.1.1

func (bc *Blockchain) GetBlockPool() *BlockPool

func (*Blockchain) GetConsensus added in v0.1.1

func (bc *Blockchain) GetConsensus() Consensus

func (*Blockchain) GetDb added in v0.1.1

func (bc *Blockchain) GetDb() storage.Storage

func (*Blockchain) GetMaxHeight

func (bc *Blockchain) GetMaxHeight() uint64

func (*Blockchain) GetTailBlock

func (bc *Blockchain) GetTailBlock() (*Block, error)

func (*Blockchain) GetTailBlockHash added in v0.1.1

func (bc *Blockchain) GetTailBlockHash() Hash

func (*Blockchain) GetTxPool added in v0.1.1

func (bc *Blockchain) GetTxPool() *TransactionPool

func (Blockchain) GetUtxoStateAtBlockHash

func (bc Blockchain) GetUtxoStateAtBlockHash(db storage.Storage, hash []byte) (utxoIndex, error)

input db and block hash, output utxoindex state @block hash block

func (*Blockchain) IsHigherThanBlockchain added in v0.1.1

func (bc *Blockchain) IsHigherThanBlockchain(block *Block) bool

func (*Blockchain) IsInBlockchain

func (bc *Blockchain) IsInBlockchain(hash Hash) bool

func (*Blockchain) Iterator

func (bc *Blockchain) Iterator() *Blockchain

func (*Blockchain) MergeFork

func (bc *Blockchain) MergeFork()

func (*Blockchain) Next

func (bc *Blockchain) Next() (*Block, error)

func (*Blockchain) NextFromIndex

func (bc *Blockchain) NextFromIndex(indexHash []byte) (*Block, error)

func (*Blockchain) Rollback added in v0.1.1

func (bc *Blockchain) Rollback(targetHash Hash) bool

rollback the blockchain to a block with the targetHash

func (*Blockchain) SetConsensus added in v0.1.1

func (bc *Blockchain) SetConsensus(consensus Consensus)

func (*Blockchain) SetTailBlockHash added in v0.1.1

func (bc *Blockchain) SetTailBlockHash(tailBlockHash Hash)

func (*Blockchain) String

func (bc *Blockchain) String() string

type Consensus

type Consensus interface {
	Validate(block *Block) bool
	Start()
	Stop()
	StartNewBlockMinting()
	Setup(NetService, string)
	SetTargetBit(int)
}

type Hash

type Hash []byte

type KeyPair

type KeyPair struct {
	PrivateKey ecdsa.PrivateKey
	PublicKey  []byte
}

func NewKeyPair

func NewKeyPair() *KeyPair

func (KeyPair) GenerateAddress

func (w KeyPair) GenerateAddress() Address

type NetService

type NetService interface {
	SendBlock(block *Block) error
	GetPeerID() peer.ID
	GetBlockchain() *Blockchain
}

type RcvedBlock

type RcvedBlock struct {
	Block *Block
	Pid   peer.ID
}

type TXInput

type TXInput struct {
	Txid      []byte
	Vout      int
	Signature []byte
	PubKey    []byte
}

func MockTxInputs

func MockTxInputs() []TXInput

func (*TXInput) FromProto

func (in *TXInput) FromProto(pb proto.Message)

func (*TXInput) ToProto

func (in *TXInput) ToProto() proto.Message

func (*TXInput) UsesKey

func (in *TXInput) UsesKey(pubKeyHash []byte) bool

UsesKey checks whether the address initiated the transaction

type TXOutput

type TXOutput struct {
	Value      *common.Amount
	PubKeyHash []byte
}

func MockTxOutputs

func MockTxOutputs() []TXOutput

func NewTXOutput

func NewTXOutput(value *common.Amount, address string) *TXOutput

func (*TXOutput) FromProto

func (out *TXOutput) FromProto(pb proto.Message)

func (*TXOutput) IsLockedWithKey

func (out *TXOutput) IsLockedWithKey(pubKeyHash []byte) bool

func (*TXOutput) Lock

func (out *TXOutput) Lock(address []byte)

func (*TXOutput) ToProto

func (out *TXOutput) ToProto() proto.Message

type Transaction

type Transaction struct {
	ID   []byte
	Vin  []TXInput
	Vout []TXOutput
	Tip  uint64
}

func MockTransaction

func MockTransaction() *Transaction

func NewCoinbaseTX

func NewCoinbaseTX(to, data string, blockHeight uint64) Transaction

NewCoinbaseTX creates a new coinbase transaction

func NewUTXOTransaction

func NewUTXOTransaction(db storage.Storage, from, to Address, amount *common.Amount, senderKeyPair KeyPair, bc *Blockchain, tip uint64) (Transaction, error)

NewUTXOTransaction creates a new transaction

func NewUTXOTransactionforAddBalance

func NewUTXOTransactionforAddBalance(to Address, amount *common.Amount, keyPair KeyPair, bc *Blockchain) (Transaction, error)

for add balance

func (*Transaction) FindAllTxinsInUtxoPool

func (tx *Transaction) FindAllTxinsInUtxoPool(utxoPool utxoIndex) map[string]TXOutput

Find the transaction in a utxo pool. Returns true only if all Vins are found in the utxo pool

func (*Transaction) FromProto

func (tx *Transaction) FromProto(pb proto.Message)

func (*Transaction) GetPrevTransactions added in v0.1.1

func (tx *Transaction) GetPrevTransactions(bc *Blockchain) map[string]Transaction

func (*Transaction) Hash

func (tx *Transaction) Hash() []byte

Hash returns the hash of the Transaction

func (Transaction) IsCoinbase

func (tx Transaction) IsCoinbase() bool

func (Transaction) Serialize

func (tx Transaction) Serialize() []byte

Serialize returns a serialized Transaction

func (*Transaction) Sign

func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction)

Sign signs each input of a Transaction

func (Transaction) String

func (tx Transaction) String() string

String returns a human-readable representation of a transaction

func (*Transaction) ToProto

func (tx *Transaction) ToProto() proto.Message

func (*Transaction) TrimmedCopy

func (tx *Transaction) TrimmedCopy() Transaction

TrimmedCopy creates a trimmed copy of Transaction to be used in signing

func (*Transaction) Verify

func (tx *Transaction) Verify(utxo utxoIndex) bool

return true if the transaction is verified

func (*Transaction) VerifySignatures

func (tx *Transaction) VerifySignatures(prevTXs map[string]TXOutput) bool

Verify verifies signatures of Transaction inputs

type TransactionPool

type TransactionPool struct {
	Transactions sorted.Slice
	// contains filtered or unexported fields
}

func GenerateMockTransactionPool

func GenerateMockTransactionPool(numOfTxs int) *TransactionPool

func NewTransactionPool

func NewTransactionPool() *TransactionPool

func (*TransactionPool) ConditionalAdd

func (txPool *TransactionPool) ConditionalAdd(tx Transaction)

func (*TransactionPool) FilterAllTransactions

func (txPool *TransactionPool) FilterAllTransactions(utxoPool utxoIndex)

func (*TransactionPool) PopSortedTransactions added in v0.1.1

func (txPool *TransactionPool) PopSortedTransactions() []*Transaction

need to optimize

func (*TransactionPool) PushTransaction

func (txPool *TransactionPool) PushTransaction(msg string)

todo: will change the input from string to transaction

func (*TransactionPool) RemoveMultipleTransactions

func (txPool *TransactionPool) RemoveMultipleTransactions(txs []*Transaction)

func (*TransactionPool) Start

func (txPool *TransactionPool) Start()

func (*TransactionPool) Stop

func (txPool *TransactionPool) Stop()

func (*TransactionPool) StructDelete

func (txPool *TransactionPool) StructDelete(tx interface{})

func (*TransactionPool) StructPush

func (txPool *TransactionPool) StructPush(val interface{})

Push a new value into slice

func (*TransactionPool) Traverse

func (txPool *TransactionPool) Traverse(txHandler func(tx Transaction) bool)

function f should return true if the transaction needs to be pushed back to the pool

type TxIndex

type TxIndex struct {
	BlockId    []byte
	BlockIndex int
}

type UTXOutputStored

type UTXOutputStored struct {
	Value      *common.Amount
	PubKeyHash []byte
	Txid       []byte
	TxIndex    int
}

func GetAddressUTXOs

func GetAddressUTXOs(mapkey string, pubkey []byte, db storage.Storage) []UTXOutputStored

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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