blockchain

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2018 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidTipHeight is the error returned when the block height is not valid
	ErrInvalidTipHeight = errors.New("invalid tip height")
	// ErrInvalidBlock is the error returned when the block is not valid
	ErrInvalidBlock = errors.New("failed to validate the block")
)
View Source
var Gen = &Genesis{
	ChainID:             uint32(1),
	TotalSupply:         uint64(10000000000),
	BlockReward:         uint64(5),
	Timestamp:           uint64(1524676419),
	ParentHash:          common.Hash32B{},
	GenesisCoinbaseData: "Connecting the physical world, block by block",
	CreatorAddr:         "io1qyqsyqcy222ggazmccgf7dsx9m9vfqtadw82ygwhjnxtmx",
	CreatorPubKey:       "d01164c3afe47406728d3e17861a3251dcff39e62bdc2b93ccb69a02785a175e195b5605517fd647eb7dd095b3d862dffb087f35eacf10c6859d04a100dbfb7358eeca9d5c37c904",
}

Gen hardcodes genesis default settings

Functions

This section is empty.

Types

type Block

type Block struct {
	Header    *BlockHeader
	Transfers []*action.Transfer
	Votes     []*action.Vote
}

Block defines the struct of block

func NewBlock

func NewBlock(chainID uint32, height uint64, prevBlockHash common.Hash32B,
	tsf []*action.Transfer, vote []*action.Vote) *Block

NewBlock returns a new block

func NewGenesisBlock

func NewGenesisBlock(cfg *config.Config) *Block

NewGenesisBlock creates a new genesis block

func (*Block) ByteStream

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

ByteStream returns a byte stream of the block

func (*Block) ByteStreamHeader

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

ByteStreamHeader returns a byte stream of the block header

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 parses the byte stream into a Block

func (*Block) HashBlock

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

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

func (*Block) Height

func (b *Block) Height() uint64

Height returns the height of this block

func (*Block) PrevHash

func (b *Block) PrevHash() common.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) SignBlock

func (b *Block) SignBlock(signer *iotxaddress.Address) error

SignBlock allows signer to sign the block b

func (*Block) TxRoot

func (b *Block) TxRoot() common.Hash32B

TxRoot returns the Merkle root of all txs and actions in this block.

type BlockHeader

type BlockHeader struct {
	Pubkey []byte // block miner's public key
	// 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 interface {
	service.Service

	// For exposing blockchain states
	// GetHeightByHash returns Block's height by hash
	GetHeightByHash(hash common.Hash32B) (uint64, error)
	// GetHashByHeight returns Block's hash by height
	GetHashByHeight(height uint64) (common.Hash32B, error)
	// GetBlockByHeight returns Block by height
	GetBlockByHeight(height uint64) (*Block, error)
	// GetBlockByHash returns Block by hash
	GetBlockByHash(hash common.Hash32B) (*Block, error)
	// GetTotalTransfers returns the total number of transfers
	GetTotalTransfers() (uint64, error)
	// GetTotalVotes returns the total number of votes
	GetTotalVotes() (uint64, error)
	// GetTransfersFromAddress returns transaction from address
	GetTransfersFromAddress(address string) ([]common.Hash32B, error)
	// GetTransfersToAddress returns transaction to address
	GetTransfersToAddress(address string) ([]common.Hash32B, error)
	// GetTransfersByTransferHash returns transfer by transfer hash
	GetTransferByTransferHash(hash common.Hash32B) (*action.Transfer, error)
	// GetBlockHashByTransferHash returns Block hash by transfer hash
	GetBlockHashByTransferHash(hash common.Hash32B) (common.Hash32B, error)
	// GetVoteFromAddress returns vote from address
	GetVotesFromAddress(address string) ([]common.Hash32B, error)
	// GetVoteToAddress returns vote to address
	GetVotesToAddress(address string) ([]common.Hash32B, error)
	// GetVotesByVoteHash returns vote by vote hash
	GetVoteByVoteHash(hash common.Hash32B) (*action.Vote, error)
	// GetBlockHashByVoteHash returns Block hash by vote hash
	GetBlockHashByVoteHash(hash common.Hash32B) (common.Hash32B, error)
	// TipHash returns tip block's hash
	TipHash() (common.Hash32B, error)
	// TipHeight returns tip block's height
	TipHeight() (uint64, error)
	// StateByAddr returns state of a given address
	StateByAddr(address string) (*state.State, error)

	// For block operations
	// MintNewBlock creates a new block with given actions
	// Note: the coinbase transfer will be added to the given transfers when minting a new block
	MintNewBlock(tsf []*action.Transfer, vote []*action.Vote, address *iotxaddress.Address, data string) (*Block, error)
	// MintNewDummyBlock creates a new dummy block with no transactions
	MintNewDummyBlock() (*Block, error)
	// CommitBlock validates and appends a block to the chain
	CommitBlock(blk *Block) error
	// ValidateBlock validates a new block before adding it to the blockchain
	ValidateBlock(blk *Block) error

	// For action operations
	// CreateTransfer creates a signed transfer paying 'amount' from 'from' to 'to'
	CreateTransfer(nonce uint64, from *iotxaddress.Address, amount *big.Int, to *iotxaddress.Address) (*action.Transfer, error)
	// CreateRawTransfer creates an unsigned transfer paying 'amount' from 'from' to 'to'
	CreateRawTransfer(nonce uint64, from *iotxaddress.Address, amount *big.Int, to *iotxaddress.Address) *action.Transfer
	// CreateVote creates a signed vote
	CreateVote(nonce uint64, selfPubKey []byte, votePubKey []byte) (*action.Vote, error)
	// CreateRawVote creates an unsigned vote
	CreateRawVote(nonce uint64, selfPubKey []byte, votePubKey []byte) *action.Vote

	// Validator returns the current validator object
	Validator() Validator
	// SetValidator sets the current validator object
	SetValidator(val Validator)
}

Blockchain represents the blockchain data structure and hosts the APIs to access it

func CreateBlockchain

func CreateBlockchain(cfg *config.Config, sf state.Factory) Blockchain

CreateBlockchain creates a new blockchain and DB instance

func NewBlockchain

func NewBlockchain(dao *blockDAO, cfg *config.Config, sf state.Factory) Blockchain

NewBlockchain creates a new blockchain instance

type Genesis

type Genesis struct {
	InitDelegatesPubKey []string
	ChainID             uint32
	TotalSupply         uint64
	BlockReward         uint64
	Timestamp           uint64
	ParentHash          common.Hash32B
	GenesisCoinbaseData string
	CreatorAddr         string
	CreatorPubKey       string
}

Genesis defines the Genesis default settings

type GenesisAction

type GenesisAction struct {
	SelfNominators SelfNominators `yaml:"selfNominators"`
	Transfers      []Transfer     `yaml:"transfers"`
}

GenesisAction is the root action struct, each package's action should be put as its sub struct

type Nominator

type Nominator struct {
	PubKey    string `yaml:"pubKey"`
	Address   string `yaml:"address"`
	Signature string `yaml:"signature"`
}

Nominator is the Nominator struct for vote struct

type Payee

type Payee struct {
	Address string
	Amount  uint64
}

Payee defines the struct of payee

type SelfNominators

type SelfNominators struct {
	Nominators []Nominator `yaml:"nominator"`
}

SelfNominators is the config struct for SelfNominator struct

type Transfer

type Transfer struct {
	Amount    int64  `yaml:"amount"`
	Recipient string `yaml:"recipient"`
	Signature string `yaml:"signature"`
}

Transfer is the Transfer struct

type Validator

type Validator interface {
	// Validate validates the given block's content
	Validate(block *Block, tipHeight uint64, tipHash common.Hash32B) error
}

Validator is the interface of validator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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