blockchain

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// ValueSizeInBytes defines the size of value in byte units
	ValueSizeInBytes = 8
	// LockScriptSizeInBytes defines the size of lock script in byte units
	LockScriptSizeInBytes = 4

	// VersionSizeInBytes defines the size of version in byte units
	VersionSizeInBytes = 4
	//NumTxInSizeInBytes defines the size of number of transaction inputs in byte units
	NumTxInSizeInBytes = 4
	//NumTxOutSizeInBytes defines the size of number of transaction outputs in byte units
	NumTxOutSizeInBytes = 4
	//LockTimeSizeInBytes defines the size of lock time in byte units
	LockTimeSizeInBytes = 4
)
View Source
const (
	// GenesisCoinbaseData is the text in genesis block
	GenesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
)
View Source
const (
	// Version of blockchain protocol
	Version = 1
)

Variables

View Source
var (
	// ErrInvalidBlock is the error returned when the block is not vliad
	ErrInvalidBlock = errors.New("failed to validate the block")
)

Functions

This section is empty.

Types

type Block

type Block struct {
	Header *BlockHeader
	Tranxs []*Tx
}

Block defines the struct of block make sure the variable type and order of this struct is same as "type Block" in blockchain.pb.go

func NewBlock

func NewBlock(chainID uint32, height uint32, prevBlockHash cp.Hash32B, transactions []*Tx) *Block

NewBlock returns a new block

func (*Block) ByteStream

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

ByteStream returns a byte stream of the block used to calculate the block hash

func (*Block) ConvertFromBlockHeaderPb

func (b *Block) ConvertFromBlockHeaderPb(pbBlock *iproto.BlockPb)

ConvertFromBlockHeaderPb converts BlockHeaderPb to BlockHeader

func (*Block) ConvertFromBlockPb

func (b *Block) ConvertFromBlockPb(pbBlock *iproto.BlockPb)

ConvertFromBlockPb converts BlockPb to Block

func (*Block) ConvertToBlockHeaderPb

func (b *Block) ConvertToBlockHeaderPb() *iproto.BlockHeaderPb

ConvertToBlockHeaderPb converts BlockHeader to BlockHeaderPb

func (*Block) ConvertToBlockPb

func (b *Block) ConvertToBlockPb() *iproto.BlockPb

ConvertToBlockPb converts Block to BlockPb

func (*Block) Deserialize

func (b *Block) Deserialize(buf []byte) error

Deserialize parse the byte stream into Block

func (*Block) HashBlock

func (b *Block) HashBlock() cp.Hash32B

HashBlock return the hash of this block (actually hash of block header)

func (*Block) Height

func (b *Block) Height() uint32

Height returns the height of this block

func (*Block) MerkleRoot

func (b *Block) MerkleRoot() cp.Hash32B

MerkleRoot returns the Merkle root of this block.

func (*Block) PrevHash

func (b *Block) PrevHash() cp.Hash32B

PrevHash returns the hash of prev block

func (*Block) Serialize

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

Serialize returns the serialized byte stream of the block

func (*Block) TranxsSize

func (b *Block) TranxsSize() uint32

TranxsSize returns the size of transactions in this block

type BlockHeader

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

BlockHeader defines the struct of block header make sure the variable type and order of this struct is same as "BlockHeaderPb" in blockchain.pb.go

type Blockchain

type Blockchain struct {
	Utk *UtxoTracker // tracks the current UTXO pool
	// contains filtered or unexported fields
}

Blockchain implements the IBlockchain interface

func CreateBlockchain

func CreateBlockchain(address string, cfg *config.Config) *Blockchain

CreateBlockchain creates a new blockchain and DB instance

func NewBlockchain

func NewBlockchain(db *blockdb.BlockDB, cfg *config.Config) *Blockchain

NewBlockchain creates a new blockchain instance

func (*Blockchain) AddBlockCommit

func (bc *Blockchain) AddBlockCommit(blk *Block) error

AddBlockCommit adds a new block into blockchain

func (*Blockchain) AddBlockSync

func (bc *Blockchain) AddBlockSync(blk *Block) error

AddBlockSync adds a past block into blockchain used by block syncer when the chain in out-of-sync

func (*Blockchain) BalanceOf

func (bc *Blockchain) BalanceOf(address string) uint64

BalanceOf returns the balance of an address

func (*Blockchain) Close

func (bc *Blockchain) Close() error

Close closes the Db connection

func (*Blockchain) CreateRawTransaction

func (bc *Blockchain) CreateRawTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx

CreateRawTransaction creates a unsigned transaction paying 'amount' from 'from' to 'to'

func (*Blockchain) CreateTransaction

func (bc *Blockchain) CreateTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx

CreateTransaction creates a signed transaction paying 'amount' from 'from' to 'to'

func (*Blockchain) GetBlockByHash

func (bc *Blockchain) GetBlockByHash(hash cp.Hash32B) (*Block, error)

GetBlockByHash returns block from the blockchain hash by hash

func (*Blockchain) GetBlockByHeight

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

GetBlockByHeight returns block from the blockchain hash by height

func (*Blockchain) GetHashByHeight

func (bc *Blockchain) GetHashByHeight(height uint32) (cp.Hash32B, error)

GetHashByHeight returns block's hash by height

func (*Blockchain) GetHeightByHash

func (bc *Blockchain) GetHeightByHash(hash cp.Hash32B) (uint32, error)

GetHeightByHash returns block's height by hash

func (*Blockchain) Init

func (bc *Blockchain) Init() error

Init initializes the blockchain

func (*Blockchain) MintNewBlock

func (bc *Blockchain) MintNewBlock(txs []*Tx, toaddr, data string) *Block

MintNewBlock creates a new block with given transactions. Note: the coinbase transaction will be added to the given transactions when minting a new block.

func (*Blockchain) ReadBlock

func (bc *Blockchain) ReadBlock(height uint32) *Block

ReadBlock read the block from file on disk

func (*Blockchain) Reset

func (bc *Blockchain) Reset()

Reset reset for next block

func (*Blockchain) StoreBlock

func (bc *Blockchain) StoreBlock(start, end uint32) error

StoreBlock persists the blocks in the range to file on disk

func (*Blockchain) TipHash

func (bc *Blockchain) TipHash() cp.Hash32B

TipHash returns tip block's hash

func (*Blockchain) TipHeight

func (bc *Blockchain) TipHeight() uint32

TipHeight returns tip block's height

func (*Blockchain) UtxoPool

func (bc *Blockchain) UtxoPool() map[cp.Hash32B][]*TxOutput

UtxoPool returns the UTXO pool of current blockchain

func (*Blockchain) ValidateBlock

func (bc *Blockchain) ValidateBlock(blk *Block) error

ValidateBlock validates a new block before adding it to the blockchain

type IBlockchain

type IBlockchain interface {
	// Init initializes the blockchain
	Init() error
	// Close closes the Db connection
	Close() error
	// GetHeightByHash returns block's height by hash
	GetHeightByHash(hash cp.Hash32B) (uint32, error)
	// GetHashByHeight returns block's hash by height
	GetHashByHeight(height uint32) (cp.Hash32B, error)
	// GetBlockByHeight returns block from the blockchain hash by height
	GetBlockByHeight(height uint32) (*Block, error)
	// GetBlockByHash returns block from the blockchain hash by hash
	GetBlockByHash(hash cp.Hash32B) (*Block, error)
	// TipHash returns tip block's hash
	TipHash() cp.Hash32B
	// TipHeight returns tip block's height
	TipHeight() uint32
	// Reset reset for next block
	Reset()
	// ValidateBlock validates a new block before adding it to the blockchain
	ValidateBlock(blk *Block) error
	// MintNewBlock creates a new block with given transactions.
	// Note: the coinbase transaction will be added to the given transactions
	// when minting a new block.
	MintNewBlock([]*Tx, string, string) *Block
	// AddBlockCommit adds a new block into blockchain
	AddBlockCommit(blk *Block) error
	// AddBlockSync adds a past block into blockchain
	// used by block syncer when the chain in out-of-sync
	AddBlockSync(blk *Block) error
	// BalanceOf returns the balance of a given address
	BalanceOf(string) uint64
	// UtxoPool returns the UTXO pool of current blockchain
	UtxoPool() map[cp.Hash32B][]*TxOutput
	// CreateTransaction creates a signed transaction paying 'amount' from 'from' to 'to'
	CreateTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx
	// CreateRawTransaction creates a signed transaction paying 'amount' from 'from' to 'to'
	CreateRawTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx
}

IBlockchain defines the interface of blockchain

type Payee

type Payee struct {
	Address string
	Amount  uint64
}

Payee defines the struct of payee

func NewPayee

func NewPayee(address string, amount uint64) *Payee

NewPayee returns a Payee instance

type Tx

type Tx struct {
	Version  uint32
	NumTxIn  uint32 // number of transaction input
	TxIn     []*TxInput
	NumTxOut uint32 // number of transaction output
	TxOut    []*TxOutput
	LockTime uint32 // UTXO to be locked until this time
}

Tx defines the struct of transaction make sure the variable type and order of this struct is same as "type Tx" in blockchain.pb.go

func NewCoinbaseTx

func NewCoinbaseTx(toaddr string, amount uint64, data string) *Tx

NewCoinbaseTx creates the coinbase transaction - a special type of transaction that does not require previously outputs.

func NewTx

func NewTx(version uint32, in []*TxInput, out []*TxOutput, lockTime uint32) *Tx

NewTx returns a Tx instance

func (*Tx) ByteStream

func (tx *Tx) ByteStream() []byte

ByteStream returns a raw byte stream of trnx data

func (*Tx) ConvertFromTxPb

func (tx *Tx) ConvertFromTxPb(pbTx *iproto.TxPb)

ConvertFromTxPb converts a protobuf's Tx back to type Tx

func (*Tx) ConvertToTxPb

func (tx *Tx) ConvertToTxPb() *iproto.TxPb

ConvertToTxPb creates a protobuf's Tx using type Tx

func (*Tx) ConvertToUtxoPb

func (tx *Tx) ConvertToUtxoPb() *iproto.UtxoMapPb

ConvertToUtxoPb creates a protobuf's UTXO

func (*Tx) Deserialize

func (tx *Tx) Deserialize(buf []byte) error

Deserialize parse the byte stream into the Tx

func (*Tx) Hash

func (tx *Tx) Hash() cp.Hash32B

Hash returns the hash of the Tx

func (*Tx) IsCoinbase

func (tx *Tx) IsCoinbase() bool

IsCoinbase checks if it is a coinbase transaction by checking if Vin is empty

func (*Tx) Serialize

func (tx *Tx) Serialize() ([]byte, error)

Serialize returns a serialized byte stream for the Tx

func (*Tx) TotalSize

func (tx *Tx) TotalSize() uint32

TotalSize returns the total size of this transaction

type TxInput

type TxInput = iproto.TxInputPb

TxInput defines the transaction input protocol buffer

func NewTxInput

func NewTxInput(hash cp.Hash32B, index int32, unlock []byte, seq uint32) *TxInput

NewTxInput returns a TxInput instance

type TxOutput

type TxOutput struct {
	*iproto.TxOutputPb // embedded
	// contains filtered or unexported fields
}

TxOutput defines the transaction output protocol buffer

func CreateTxOutput

func CreateTxOutput(toaddr string, value uint64) *TxOutput

CreateTxOutput creates a new transaction output

func NewTxOutput

func NewTxOutput(amount uint64, index int32) *TxOutput

NewTxOutput returns a TxOutput instance

func (*TxOutput) ByteStream

func (out *TxOutput) ByteStream() []byte

ByteStream returns a raw byte stream of transaction output

func (*TxOutput) IsLockedWithKey

func (out *TxOutput) IsLockedWithKey(lockScript []byte) bool

IsLockedWithKey checks if the UTXO in output is locked with script

func (*TxOutput) TotalSize

func (out *TxOutput) TotalSize() uint32

TotalSize returns the total size of transaction output

type UtxoEntry

type UtxoEntry struct {
	*iproto.TxOutputPb // embedded
	// contains filtered or unexported fields
}

UtxoEntry contain TxOutput + hash and index

type UtxoTracker

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

UtxoTracker tracks the active UTXO pool

func NewUtxoTracker

func NewUtxoTracker() *UtxoTracker

NewUtxoTracker returns a UTXO tracker instance

func (*UtxoTracker) AddTx

func (tk *UtxoTracker) AddTx(tx *Tx, height uint32)

AddTx is called by TxPool to add a transaction

func (*UtxoTracker) CreateTxInputUtxo

func (tk *UtxoTracker) CreateTxInputUtxo(hash cp.Hash32B, index int32, unlockScript []byte) *TxInput

CreateTxInputUtxo returns a UTXO transaction input

func (*UtxoTracker) CreateTxOutputUtxo

func (tk *UtxoTracker) CreateTxOutputUtxo(address string, amount uint64) *TxOutput

CreateTxOutputUtxo creates transaction to spend UTXO

func (*UtxoTracker) Deserialize

func (tk *UtxoTracker) Deserialize(buf []byte) error

Deserialize parse the byte stream into the Tx

func (*UtxoTracker) GetPool

func (tk *UtxoTracker) GetPool() map[cp.Hash32B][]*TxOutput

GetPool returns the UTXO pool

func (*UtxoTracker) Reset

func (tk *UtxoTracker) Reset()

Reset reset the out index

func (*UtxoTracker) Serialize

func (tk *UtxoTracker) Serialize() ([]byte, error)

Serialize returns a serialized byte stream for the Tx

func (*UtxoTracker) UpdateUtxoPool

func (tk *UtxoTracker) UpdateUtxoPool(blk *Block) error

UpdateUtxoPool updates the UTXO pool according to transactions in the block

func (*UtxoTracker) UtxoEntries

func (tk *UtxoTracker) UtxoEntries(address string, reqamount uint64) ([]*UtxoEntry, uint64)

UtxoEntries returns list of UTXO entries containing >= requested amount, and return (nil, addr's total balance) if cannot reach reqamount

func (*UtxoTracker) ValidateTxInputUtxo

func (tk *UtxoTracker) ValidateTxInputUtxo(txIn *TxInput) uint64

ValidateTxInputUtxo validates the UTXO in transaction input return amount of UTXO if pass, 0 otherwise

func (*UtxoTracker) ValidateUtxo

func (tk *UtxoTracker) ValidateUtxo(blk *Block) error

ValidateUtxo validates all UTXO in the block

Jump to

Keyboard shortcuts

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