blockchain

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2019 License: Apache-2.0 Imports: 38 Imported by: 30

Documentation

Index

Constants

View Source
const (
	// Rau is the smallest non-fungible token unit
	Rau int64 = 1
	// KRau is 1000 Rau
	KRau = Rau * 1000
	// MRau is 1000 KRau
	MRau = KRau * 1000
	// GRau is 1000 MRau
	GRau = MRau * 1000
	// Qev is 1000 GRau
	Qev = GRau * 1000
	// Jin is 1000 Qev
	Jin = Qev * 1000
	// Iotx is 1000 Jin, which should be fit into int64
	Iotx = Jin * 1000
)
View Source
const RecoveryHeightKey key = "recoveryHeight"

RecoveryHeightKey indicates the recovery height key used by context

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")
	// ErrActionNonce is the error when the nonce of the action is wrong
	ErrActionNonce = errors.New("invalid action nonce")
	// ErrGasHigherThanLimit indicates the error of gas value
	ErrGasHigherThanLimit = errors.New("invalid gas for action")
	// ErrInsufficientGas indicates the error of insufficient gas value for data storage
	ErrInsufficientGas = errors.New("insufficient intrinsic gas value")
	// ErrBalance indicates the error of balance
	ErrBalance = errors.New("invalid balance")
)
View Source
var Gen = &Genesis{
	TotalSupply:         ConvertIotxToRau(10000000000),
	Timestamp:           1524676419,
	ParentHash:          hash.Hash32B{},
	GenesisCoinbaseData: "Connecting the physical world, block by block",
}

Gen hardcodes genesis default settings

Functions

func ConvertIotxToRau added in v0.4.0

func ConvertIotxToRau(iotx int64) *big.Int

ConvertIotxToRau converts an Iotx to Rau

func NewGenesisActions added in v0.4.4

func NewGenesisActions(chainCfg config.Chain, ws factory.WorkingSet) []action.SealedEnvelope

NewGenesisActions creates a new genesis block

func PickAction added in v0.4.4

func PickAction(gasLimit uint64, actionIterator actioniterator.ActionIterator) ([]action.SealedEnvelope, error)

PickAction returns picked action list

Types

type BlockCreationSubscriber added in v0.4.4

type BlockCreationSubscriber interface {
	HandleBlock(*block.Block) error
}

BlockCreationSubscriber is an interface which will get notified when a block is created

type Blockchain

type Blockchain interface {
	lifecycle.StartStopper

	// Balance returns balance of an account
	Balance(addr string) (*big.Int, error)
	// Nonce returns the nonce if the account exists
	Nonce(addr string) (uint64, error)
	// CreateState adds a new account with initial balance to the factory
	CreateState(addr string, init *big.Int) (*state.Account, error)
	// CandidatesByHeight returns the candidate list by a given height
	CandidatesByHeight(height uint64) ([]*state.Candidate, error)
	// For exposing blockchain states
	// GetHeightByHash returns Block's height by hash
	GetHeightByHash(h hash.Hash32B) (uint64, error)
	// GetHashByHeight returns Block's hash by height
	GetHashByHeight(height uint64) (hash.Hash32B, error)
	// GetBlockByHeight returns Block by height
	GetBlockByHeight(height uint64) (*block.Block, error)
	// GetBlockByHash returns Block by hash
	GetBlockByHash(h hash.Hash32B) (*block.Block, error)
	// GetTotalTransfers returns the total number of transfers
	GetTotalTransfers() (uint64, error)
	// GetTotalVotes returns the total number of votes
	GetTotalVotes() (uint64, error)
	// GetTotalExecutions returns the total number of executions
	GetTotalExecutions() (uint64, error)
	// GetTotalActions returns the total number of actions
	GetTotalActions() (uint64, error)
	// GetTransfersFromAddress returns transaction from address
	GetTransfersFromAddress(address string) ([]hash.Hash32B, error)
	// GetTransfersToAddress returns transaction to address
	GetTransfersToAddress(address string) ([]hash.Hash32B, error)
	// GetTransfersByTransferHash returns transfer by transfer hash
	GetTransferByTransferHash(h hash.Hash32B) (*action.Transfer, error)
	// GetBlockHashByTransferHash returns Block hash by transfer hash
	GetBlockHashByTransferHash(h hash.Hash32B) (hash.Hash32B, error)
	// GetVoteFromAddress returns vote from address
	GetVotesFromAddress(address string) ([]hash.Hash32B, error)
	// GetVoteToAddress returns vote to address
	GetVotesToAddress(address string) ([]hash.Hash32B, error)
	// GetVotesByVoteHash returns vote by vote hash
	GetVoteByVoteHash(h hash.Hash32B) (*action.Vote, error)
	// GetBlockHashByVoteHash returns Block hash by vote hash
	GetBlockHashByVoteHash(h hash.Hash32B) (hash.Hash32B, error)
	// GetExecutionsFromAddress returns executions from address
	GetExecutionsFromAddress(address string) ([]hash.Hash32B, error)
	// GetExecutionsToAddress returns executions to address
	GetExecutionsToAddress(address string) ([]hash.Hash32B, error)
	// GetExecutionByExecutionHash returns execution by execution hash
	GetExecutionByExecutionHash(h hash.Hash32B) (*action.Execution, error)
	// GetBlockHashByExecutionHash returns Block hash by execution hash
	GetBlockHashByExecutionHash(h hash.Hash32B) (hash.Hash32B, error)
	// GetReceiptByActionHash returns the receipt by action hash
	GetReceiptByActionHash(h hash.Hash32B) (*action.Receipt, error)
	// GetActionsFromAddress returns actions from address
	GetActionsFromAddress(address string) ([]hash.Hash32B, error)
	// GetActionsToAddress returns actions to address
	GetActionsToAddress(address string) ([]hash.Hash32B, error)
	// GetActionByActionHash returns action by action hash
	GetActionByActionHash(h hash.Hash32B) (action.SealedEnvelope, error)
	// GetBlockHashByActionHash returns Block hash by action hash
	GetBlockHashByActionHash(h hash.Hash32B) (hash.Hash32B, error)
	// GetFactory returns the state factory
	GetFactory() factory.Factory
	// GetChainID returns the chain ID
	ChainID() uint32
	// ChainAddress returns chain address on parent chain, the root chain return empty.
	ChainAddress() string
	// TipHash returns tip block's hash
	TipHash() hash.Hash32B
	// TipHeight returns tip block's height
	TipHeight() uint64
	// StateByAddr returns account of a given address
	StateByAddr(address string) (*state.Account, 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(
		actionMap map[string][]action.SealedEnvelope,
		producerPubKey keypair.PublicKey,
		producerPriKey keypair.PrivateKey,
		producerAddr string,
		timestamp int64,
	) (*block.Block, error)
	// CommitBlock validates and appends a block to the chain
	CommitBlock(blk *block.Block) error
	// ValidateBlock validates a new block before adding it to the blockchain
	ValidateBlock(blk *block.Block) error

	// For action operations
	// Validator returns the current validator object
	Validator() Validator
	// SetValidator sets the current validator object
	SetValidator(val Validator)

	// For smart contract operations
	// ExecuteContractRead runs a read-only smart contract operation, this is done off the network since it does not
	// cause any state change
	ExecuteContractRead(ex *action.Execution) (*action.Receipt, error)

	// AddSubscriber make you listen to every single produced block
	AddSubscriber(BlockCreationSubscriber) error

	// RemoveSubscriber make you listen to every single produced block
	RemoveSubscriber(BlockCreationSubscriber) error
}

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

func NewBlockchain

func NewBlockchain(cfg config.Config, opts ...Option) Blockchain

NewBlockchain creates a new blockchain and DB instance

type Creator added in v0.4.0

type Creator struct {
	PubKey string `yaml:"pubKey"`
	PriKey string `yaml:"priKey"`
}

Creator is the Creator of the genesis block

type Genesis added in v0.2.0

type Genesis struct {
	TotalSupply         *big.Int
	Timestamp           int64
	ParentHash          hash.Hash32B
	GenesisCoinbaseData string
	CreatorPubKey       string
	CreatorPrivKey      string
}

Genesis defines the Genesis default settings

func (*Genesis) CreatorAddr added in v0.2.0

func (g *Genesis) CreatorAddr() string

CreatorAddr returns the creator address on a particular chain

func (*Genesis) CreatorPKHash added in v0.4.0

func (g *Genesis) CreatorPKHash() hash.PKHash

CreatorPKHash returns the creator public key hash

type GenesisAction added in v0.2.0

type GenesisAction struct {
	Creation       Creator     `yaml:"creator"`
	SelfNominators []Nominator `yaml:"selfNominators"`
	Transfers      []Transfer  `yaml:"transfers"`
	SubChains      []SubChain  `yaml:"subChains"`
}

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

type IndexBuilder added in v0.4.4

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

IndexBuilder defines the index builder

func NewIndexBuilder added in v0.4.4

func NewIndexBuilder(chain Blockchain) (*IndexBuilder, error)

NewIndexBuilder instantiates an index builder

func (*IndexBuilder) HandleBlock added in v0.4.4

func (ib *IndexBuilder) HandleBlock(blk *block.Block) error

HandleBlock handles the block and create the indices for the actions and receipts in it

func (*IndexBuilder) Start added in v0.4.4

func (ib *IndexBuilder) Start(_ context.Context) error

Start starts the index builder

func (*IndexBuilder) Stop added in v0.4.4

func (ib *IndexBuilder) Stop(_ context.Context) error

Stop stops the index builder

type Nominator added in v0.2.0

type Nominator struct {
	PubKey string `yaml:"pubKey"`
	PriKey string `yaml:"priKey"`
}

Nominator is the Nominator struct for vote struct

type Option added in v0.3.0

type Option func(*blockchain, config.Config) error

Option sets blockchain construction parameter

func BoltDBDaoOption added in v0.3.0

func BoltDBDaoOption() Option

BoltDBDaoOption sets blockchain's dao with BoltDB from config.Chain.ChainDBPath

func ClockOption added in v0.3.0

func ClockOption(clk clock.Clock) Option

ClockOption overrides the default clock

func DefaultStateFactoryOption added in v0.3.0

func DefaultStateFactoryOption() Option

DefaultStateFactoryOption sets blockchain's sf from config

func InMemDaoOption added in v0.3.0

func InMemDaoOption() Option

InMemDaoOption sets blockchain's dao with MemKVStore

func InMemStateFactoryOption added in v0.3.0

func InMemStateFactoryOption() Option

InMemStateFactoryOption sets blockchain's factory.Factory as in memory sf

func PrecreatedDaoOption added in v0.3.0

func PrecreatedDaoOption(dao *blockDAO) Option

PrecreatedDaoOption sets blockchain's dao

func PrecreatedStateFactoryOption added in v0.3.0

func PrecreatedStateFactoryOption(sf factory.Factory) Option

PrecreatedStateFactoryOption sets blockchain's state.Factory to sf

type SubChain added in v0.4.0

type SubChain struct {
	ChainID            uint32 `yaml:"chainID"`
	SecurityDeposit    int64  `yaml:"securityDeposit"`
	OperationDeposit   int64  `yaml:"operationDeposit"`
	StartHeight        uint64 `yaml:"startHeight"`
	ParentHeightOffset uint64 `yaml:"parentHeightOffset"`
}

SubChain is the SubChain struct

type Transfer added in v0.2.0

type Transfer struct {
	Amount      int64  `yaml:"amount"`
	RecipientPK string `yaml:"recipientPK"`
}

Transfer is the Transfer struct

type Validator added in v0.2.0

type Validator interface {
	// Validate validates the given block's content
	Validate(block *block.Block, tipHeight uint64, tipHash hash.Hash32B) error
	// ValidateActionsOnly validates the actions only
	ValidateActionsOnly(
		actions []action.SealedEnvelope,
		pk keypair.PublicKey,
		chainID uint32,
		height uint64,
	) error
	// AddActionValidators add validators
	AddActionValidators(...protocol.ActionValidator)
	AddActionEnvelopeValidators(...protocol.ActionEnvelopeValidator)
}

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