core

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: LGPL-3.0 Imports: 28 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 pubKeyHash 3 of the License, or (at your option) any later pubKeyHash.

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 pubKeyHash 3 of the License, or (at your option) any later pubKeyHash.

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 pubKeyHash 3 of the License, or (at your option) any later pubKeyHash.

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 BlockCacheLRUCacheLimit = 1024
View Source
const BlockPoolMaxSize = 100
View Source
const ContractTxouputIndex = 0
View Source
const ForkCacheLRUCacheLimit = 128
View Source
const LengthForBlockToBeConsideredHistory = 100

Variables

View Source
var (
	ErrBlockDoesNotExist         = errors.New("ERROR: Block does not exist in blockchain")
	ErrNotAbleToGetLastBlockHash = errors.New("ERROR: Not able to get last block hash in blockchain")
	ErrTransactionNotFound       = errors.New("ERROR: Transaction not found")
	ErrDuplicatedBlock           = errors.New("ERROR: Block already exists in blockchain")
)
View Source
var (
	ErrIncorrectPublicKey = errors.New("Public key is not correct")
	ErrEmptyPublicKeyHash = errors.New("Empty public key hash")
)
View Source
var (
	ErrInsufficientFund  = errors.New("transaction: the balance is insufficient")
	ErrInvalidAmount     = errors.New("transaction: amount is invalid (must be > 0)")
	ErrTXInputNotFound   = errors.New("transaction: transaction input not found")
	ErrNewUserPubKeyHash = errors.New("transaction: create pubkeyhash error")
)
View Source
var (
	ErrInvalidAddress = errors.New("Invalid Address")
)
View Source
var (
	ErrInvalidPubKeyHashVersion = errors.New("Invalid Public Key Hash Version ")
)
View Source
var (
	// tx metrics
	MetricsTxDoubleSpend = dapmetrics.NewCounter("dap.txpool.doublespend")
)

Metrics for core

Functions

func Checksum added in v0.1.2

func Checksum(payload []byte) []byte

Checksum finds the checksum of a public key hash

func GetAddressPayloadLength added in v0.1.2

func GetAddressPayloadLength() int

GetAddressPayloadLength get the payload length

func HashAddress

func HashAddress(address string) []byte

func IsHashEqual added in v0.1.2

func IsHashEqual(h1 Hash, h2 Hash) bool

func IsParentBlockHash

func IsParentBlockHash(parentBlk, childBlk *Block) bool

func IsParentBlockHeight

func IsParentBlockHeight(parentBlk, childBlk *Block) bool

func WaitDoneOrTimeout added in v0.1.2

func WaitDoneOrTimeout(done Done, timeOut int)

Types

type Address

type Address struct {
	Address string
}

func NewAddress

func NewAddress(addressString string) Address

func (Address) GetPubKeyHash added in v0.1.2

func (a Address) GetPubKeyHash() ([]byte, bool)

GetPubKeyHash decodes the address to the original public key hash. If unsuccessful, return false

func (Address) IsContract added in v0.1.2

func (a Address) IsContract() (bool, error)

isContract checks if an address is a Contract address

func (Address) String added in v0.1.2

func (a Address) String() string

String returns the address in string type

func (Address) ValidateAddress

func (a Address) ValidateAddress() bool

ValidateAddress checks if an address is valid

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 GenerateBlockWithCbtx added in v0.1.2

func GenerateBlockWithCbtx(addr Address, lastblock *Block) *Block

func GenerateMockBlock

func GenerateMockBlock() *Block

func NewBlock

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

func NewGenesisBlock

func NewGenesisBlock(address string) *Block

func (*Block) CalculateHash

func (b *Block) CalculateHash() Hash

func (*Block) CalculateHashWithNonce

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

func (*Block) CalculateHashWithoutNonce added in v0.1.2

func (b *Block) CalculateHashWithoutNonce() Hash

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) GetHeader added in v0.1.2

func (b *Block) GetHeader() *BlockHeader

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) GetSign added in v0.1.2

func (b *Block) GetSign() 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) IsParentBlock added in v0.1.2

func (parent *Block) IsParentBlock(child *Block) bool

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) SignBlock added in v0.1.2

func (b *Block) SignBlock(key string, data []byte) bool

func (*Block) ToProto

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

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
	Sign      Hash
	Height    uint64
}

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) GetSyncState added in v0.1.2

func (pool *BlockPool) GetSyncState() bool

func (*BlockPool) Push

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

func (*BlockPool) SetBlockchain

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

func (*BlockPool) SetSyncState added in v0.1.2

func (pool *BlockPool) SetSyncState(sync bool)

func (*BlockPool) VerifyTransactions

func (pool *BlockPool) VerifyTransactions(utxo UTXOIndex, forkBlks []*Block) bool

Verify all transactions in a fork

type BlockPoolInterface added in v0.1.2

type BlockPoolInterface interface {
	SetBlockchain(bc *Blockchain)
	BlockRequestCh() chan BlockRequestPars
	GetBlockchain() *Blockchain
	GetSyncState() bool
	SetSyncState(bool)
	VerifyTransactions(utxo UTXOIndex, forkBlks []*Block) bool
	Push(block *Block, pid peer.ID)
}

type BlockRequestPars

type BlockRequestPars struct {
	BlockHash Hash
	Pid       peer.ID
}

type BlockStream

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

type Blockchain

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

func CreateBlockchain

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

CreateBlockchain creates a new blockchain db

func GenerateMockBlockchain

func GenerateMockBlockchain(size int) *Blockchain

func GenerateMockBlockchainWithCoinbaseTxOnly

func GenerateMockBlockchainWithCoinbaseTxOnly(size int) *Blockchain

func GetBlockchain

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

func (*Blockchain) AddBlockToDb added in v0.1.1

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

AddBlockToDb record the new block in the database

func (*Blockchain) AddBlockToTail added in v0.1.1

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

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) GetBlockByHash

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

func (*Blockchain) GetBlockByHeight added in v0.1.2

func (bc *Blockchain) GetBlockByHeight(height uint64) (*Block, error)

func (*Blockchain) GetBlockPool added in v0.1.1

func (bc *Blockchain) GetBlockPool() BlockPoolInterface

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) 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(forkBlks []*Block, forkParentHash Hash)

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) SetBlockPool added in v0.1.2

func (bc *Blockchain) SetBlockPool(blockPool BlockPoolInterface)

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

	Setup(NetService, string)
	SetKey(string)

	// Start runs the consensus algorithm and begins to produce blocks
	Start()

	// Stop ceases the consensus algorithm and block production
	Stop()

	// IsProducingBlock returns true if this node itself is currently producing a block
	IsProducingBlock() bool

	// TODO: Should separate the concept of producers from PoW
	AddProducer(string) error
	GetProducers() []string
}

type Done added in v0.1.2

type Done func() bool

type Hash

type Hash []byte

type KeyPair

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

func GetKeyPairByString added in v0.1.2

func GetKeyPairByString(privateKey string) *KeyPair

func NewKeyPair

func NewKeyPair() *KeyPair

func (KeyPair) GenerateAddress

func (w KeyPair) GenerateAddress(isContract bool) Address

type NetService

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

type PubKeyHash added in v0.1.2

type PubKeyHash struct {
	PubKeyHash []byte
}

func NewContractPubKeyHash added in v0.1.2

func NewContractPubKeyHash() PubKeyHash

NewContractPubKeyHash generates a smart Contract public key hash

func NewUserPubKeyHash added in v0.1.2

func NewUserPubKeyHash(pubKey []byte) (PubKeyHash, error)

NewUserPubKeyHash hashes a public key and returns a user type public key hash

func (PubKeyHash) GenerateAddress added in v0.1.2

func (pkh PubKeyHash) GenerateAddress() Address

GenerateAddress generates an address from a public key hash

func (PubKeyHash) GetPubKeyHash added in v0.1.2

func (pkh PubKeyHash) GetPubKeyHash() []byte

GetPubKeyHash gets the public key hash

func (PubKeyHash) IsContract added in v0.1.2

func (pkh PubKeyHash) IsContract() (bool, error)

GenerateAddress generates an address from a public key hash

type RcvedBlock

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

type ScEngine added in v0.1.2

type ScEngine interface {
	ImportSourceCode(source string)
	Execute()
}

type TXInput

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

func MockTxInputs

func MockTxInputs() []TXInput

func MockTxInputsWithPubkey added in v0.1.2

func MockTxInputsWithPubkey(pubkey []byte) []TXInput

func (*TXInput) FromProto

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

func (*TXInput) ToProto

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

type TXOutput

type TXOutput struct {
	Value      *common.Amount
	PubKeyHash PubKeyHash
	Contract   string
}

func MockTxOutputs

func MockTxOutputs() []TXOutput

func NewContractTXOutput added in v0.1.2

func NewContractTXOutput(contract string) *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 string)

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, tip *common.Amount) Transaction

NewCoinbaseTX creates a new coinbase transaction

func NewUTXOTransaction

func NewUTXOTransaction(utxos []*UTXO, from, to Address, amount *common.Amount, senderKeyPair KeyPair,
	tip *common.Amount, contract string) (Transaction, error)

NewUTXOTransaction creates a new transaction

func (*Transaction) Execute added in v0.1.2

func (tx *Transaction) Execute(index UTXOIndex, sc ScEngine)

Execute executes the smart contract the transaction points to. it doesnt do anything if is a normal transaction

func (*Transaction) FindAllTxinsInUtxoPool

func (tx *Transaction) FindAllTxinsInUtxoPool(utxoPool UTXOIndex) ([]*UTXO, error)

FindAllTxinsInUtxoPool 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) GetContract added in v0.1.2

func (tx *Transaction) GetContract() string

GetContract returns the smart contract code in a transaction

func (*Transaction) GetContractAddress added in v0.1.2

func (tx *Transaction) GetContractAddress() Address

GetContractAddress gets the smart contract's address if a transaction deploys a smart contract

func (*Transaction) GetToHashBytes added in v0.1.2

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

GetToHashBytes Get bytes for hash

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, prevUtxos []*UTXO) error

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, blockHeight uint64) bool

Verify ensures signature of transactions is correct or verifies against blockHeight if it's a coinbase transactions

type TransactionPool

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

func NewTransactionPool

func NewTransactionPool(limit uint32) *TransactionPool

func (*TransactionPool) Push added in v0.1.2

func (txPool *TransactionPool) Push(tx Transaction)

func (*TransactionPool) RemoveMultipleTransactions

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

func (*TransactionPool) ValidTxns added in v0.1.2

func (txPool *TransactionPool) ValidTxns(filter filterTx) []*Transaction

type TxIndex

type TxIndex struct {
	BlockId    []byte
	BlockIndex int
}

type UTXO added in v0.1.2

type UTXO struct {
	TXOutput
	Txid    []byte
	TxIndex int
}

UTXO contains the meta info of an unspent TXOutput.

type UTXOIndex added in v0.1.2

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

UTXOIndex holds all unspent TXOutputs indexed by public key hash.

func GetUTXOIndexAtBlockHash added in v0.1.2

func GetUTXOIndexAtBlockHash(db storage.Storage, bc *Blockchain, hash Hash) (UTXOIndex, error)

GetUTXOIndexAtBlockHash returns the previous snapshot of UTXOIndex when the block of given hash was the tail block.

func LoadUTXOIndex added in v0.1.2

func LoadUTXOIndex(db storage.Storage) UTXOIndex

LoadUTXOIndex returns the UTXOIndex fetched from db.

func NewUTXOIndex added in v0.1.2

func NewUTXOIndex() UTXOIndex

NewUTXOIndex initializes an UTXOIndex instance

func (*UTXOIndex) ApplyTransaction added in v0.1.2

func (utxos *UTXOIndex) ApplyTransaction(tx *Transaction) error

func (UTXOIndex) DeepCopy added in v0.1.2

func (utxos UTXOIndex) DeepCopy() UTXOIndex

creates a deepcopy of the receiver object

func (UTXOIndex) FindUTXO added in v0.1.2

func (utxos UTXOIndex) FindUTXO(txid []byte, vout int) *UTXO

FindUTXO returns the UTXO instance of the corresponding TXOutput in the transaction (identified by txid and vout) if the TXOutput is unspent. Otherwise, it returns nil.

func (UTXOIndex) FindUTXOByVin added in v0.1.2

func (utxos UTXOIndex) FindUTXOByVin(pubkeyHash []byte, txid []byte, vout int) *UTXO

FindUTXOByVin returns the UTXO instance identified by pubkeyHash, txid and vout

func (UTXOIndex) GetAllUTXOsByPubKeyHash added in v0.1.2

func (utxos UTXOIndex) GetAllUTXOsByPubKeyHash(pubkeyHash []byte) []*UTXO

GetAllUTXOsByPubKeyHash returns all current UTXOs identified by pubkey.

func (UTXOIndex) GetUTXOsByAmount added in v0.1.2

func (utxos UTXOIndex) GetUTXOsByAmount(pubkeyHash []byte, amount *common.Amount) ([]*UTXO, error)

GetUTXOsByAmount returns a number of UTXOs that has a sum more than or equal to the amount

func (UTXOIndex) Save added in v0.1.2

func (utxos UTXOIndex) Save(mapkey string, db storage.Storage) error

Save stores the index to db

func (*UTXOIndex) UpdateUtxoState added in v0.1.2

func (utxos *UTXOIndex) UpdateUtxoState(txs []*Transaction, db storage.Storage) ([]*Transaction, error)

Update removes the UTXOs spent in the transactions in newBlk from the index and adds UTXOs generated in the transactions to the index. The index will be saved to db as a result. If saving failed, index won't be updated.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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