Documentation
¶
Index ¶
- Constants
- Variables
- type Block
- func (b *Block) ByteStream() []byte
- func (b *Block) ConvertFromBlockHeaderPb(pbBlock *iproto.BlockPb)
- func (b *Block) ConvertFromBlockPb(pbBlock *iproto.BlockPb)
- func (b *Block) ConvertToBlockHeaderPb() *iproto.BlockHeaderPb
- func (b *Block) ConvertToBlockPb() *iproto.BlockPb
- func (b *Block) Deserialize(buf []byte) error
- func (b *Block) HashBlock() cp.Hash32B
- func (b *Block) Height() uint32
- func (b *Block) MerkleRoot() cp.Hash32B
- func (b *Block) PrevHash() cp.Hash32B
- func (b *Block) Serialize() ([]byte, error)
- func (b *Block) TranxsSize() uint32
- type BlockHeader
- type Blockchain
- func (bc *Blockchain) AddBlockCommit(blk *Block) error
- func (bc *Blockchain) AddBlockSync(blk *Block) error
- func (bc *Blockchain) BalanceOf(address string) uint64
- func (bc *Blockchain) Close() error
- func (bc *Blockchain) CreateRawTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx
- func (bc *Blockchain) CreateTransaction(from iotxaddress.Address, amount uint64, to []*Payee) *Tx
- func (bc *Blockchain) GetBlockByHash(hash cp.Hash32B) (*Block, error)
- func (bc *Blockchain) GetBlockByHeight(height uint32) (*Block, error)
- func (bc *Blockchain) GetHashByHeight(height uint32) (cp.Hash32B, error)
- func (bc *Blockchain) GetHeightByHash(hash cp.Hash32B) (uint32, error)
- func (bc *Blockchain) Init() error
- func (bc *Blockchain) MintNewBlock(txs []*Tx, toaddr, data string) *Block
- func (bc *Blockchain) ReadBlock(height uint32) *Block
- func (bc *Blockchain) Reset()
- func (bc *Blockchain) StoreBlock(start, end uint32) error
- func (bc *Blockchain) TipHash() cp.Hash32B
- func (bc *Blockchain) TipHeight() uint32
- func (bc *Blockchain) UtxoPool() map[cp.Hash32B][]*TxOutput
- func (bc *Blockchain) ValidateBlock(blk *Block) error
- type IBlockchain
- type Payee
- type Tx
- func (tx *Tx) ByteStream() []byte
- func (tx *Tx) ConvertFromTxPb(pbTx *iproto.TxPb)
- func (tx *Tx) ConvertToTxPb() *iproto.TxPb
- func (tx *Tx) ConvertToUtxoPb() *iproto.UtxoMapPb
- func (tx *Tx) Deserialize(buf []byte) error
- func (tx *Tx) Hash() cp.Hash32B
- func (tx *Tx) IsCoinbase() bool
- func (tx *Tx) Serialize() ([]byte, error)
- func (tx *Tx) TotalSize() uint32
- type TxInput
- type TxOutput
- type UtxoEntry
- type UtxoTracker
- func (tk *UtxoTracker) AddTx(tx *Tx, height uint32)
- func (tk *UtxoTracker) CreateTxInputUtxo(hash cp.Hash32B, index int32, unlockScript []byte) *TxInput
- func (tk *UtxoTracker) CreateTxOutputUtxo(address string, amount uint64) *TxOutput
- func (tk *UtxoTracker) Deserialize(buf []byte) error
- func (tk *UtxoTracker) GetPool() map[cp.Hash32B][]*TxOutput
- func (tk *UtxoTracker) Reset()
- func (tk *UtxoTracker) Serialize() ([]byte, error)
- func (tk *UtxoTracker) UpdateUtxoPool(blk *Block) error
- func (tk *UtxoTracker) UtxoEntries(address string, reqamount uint64) ([]*UtxoEntry, uint64)
- func (tk *UtxoTracker) ValidateTxInputUtxo(txIn *TxInput) uint64
- func (tk *UtxoTracker) ValidateUtxo(blk *Block) error
Constants ¶
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 )
const (
// GenesisCoinbaseData is the text in genesis block
GenesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
)
const (
// Version of blockchain protocol
Version = 1
)
Variables ¶
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 (*Block) ByteStream ¶
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 ¶
Deserialize parse the byte stream into Block
func (*Block) MerkleRoot ¶
MerkleRoot returns the Merkle root of this block.
func (*Block) TranxsSize ¶
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) 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) 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) 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 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 ¶
NewCoinbaseTx creates the coinbase transaction - a special type of transaction that does not require previously outputs.
func (*Tx) ByteStream ¶
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 ¶
Deserialize parse the byte stream into the Tx
func (*Tx) IsCoinbase ¶
IsCoinbase checks if it is a coinbase transaction by checking if Vin is empty
type TxInput ¶
type TxInput = iproto.TxInputPb
TxInput defines the transaction input protocol buffer
type TxOutput ¶
type TxOutput struct { *iproto.TxOutputPb // embedded // contains filtered or unexported fields }
TxOutput defines the transaction output protocol buffer
func CreateTxOutput ¶
CreateTxOutput creates a new transaction output
func NewTxOutput ¶
NewTxOutput returns a TxOutput instance
func (*TxOutput) ByteStream ¶
ByteStream returns a raw byte stream of transaction output
func (*TxOutput) IsLockedWithKey ¶
IsLockedWithKey checks if the UTXO in output is locked with script
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) 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