blockchain

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 27 Imported by: 30

Documentation

Index

Constants

View Source
const (
	SigP256k1  = "secp256k1"
	SigP256sm2 = "p256sm2"
)

const

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")
	// 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 (
	// DefaultConfig is the default config of chain
	DefaultConfig = Config{
		ChainDBPath:                "/var/data/chain.db",
		TrieDBPatchFile:            "/var/data/trie.db.patch",
		TrieDBPath:                 "/var/data/trie.db",
		StakingPatchDir:            "/var/data",
		IndexDBPath:                "/var/data/index.db",
		BloomfilterIndexDBPath:     "/var/data/bloomfilter.index.db",
		CandidateIndexDBPath:       "/var/data/candidate.index.db",
		StakingIndexDBPath:         "/var/data/staking.index.db",
		SGDIndexDBPath:             "/var/data/sgd.index.db",
		ContractStakingIndexDBPath: "/var/data/contractstaking.index.db",
		ID:                         1,
		EVMNetworkID:               4689,
		Address:                    "",
		ProducerPrivKey:            generateRandomKey(SigP256k1),
		SignatureScheme:            []string{SigP256k1},
		EmptyGenesis:               false,
		GravityChainDB:             db.Config{DbPath: "/var/data/poll.db", NumRetries: 10},
		Committee: committee.Config{
			GravityChainAPIs: []string{},
		},
		EnableTrielessStateDB:         true,
		EnableStateDBCaching:          false,
		EnableArchiveMode:             false,
		EnableAsyncIndexWrite:         true,
		EnableSystemLogIndexer:        false,
		EnableStakingProtocol:         true,
		EnableStakingIndexer:          false,
		AllowedBlockGasResidue:        10000,
		MaxCacheSize:                  0,
		PollInitialCandidatesInterval: 10 * time.Second,
		StateDBCacheSize:              1000,
		WorkingSetCacheSize:           20,
		StreamingBlockBufferSize:      200,
		PersistStakingPatchBlock:      19778037,
	}

	// ErrConfig config error
	ErrConfig = errors.New("config error")
)
View Source
var ErrVault = errors.New("vault error")

ErrVault vault error

Functions

func Productivity added in v1.0.0

func Productivity(bc Blockchain, startHeight uint64, endHeight uint64) (map[string]uint64, error)

Productivity returns the map of the number of blocks produced per delegate in given epoch

Types

type BlockBuilderFactory added in v0.11.0

type BlockBuilderFactory interface {
	// NewBlockBuilder creates block builder
	NewBlockBuilder(context.Context, func(action.Envelope) (action.SealedEnvelope, error)) (*block.Builder, error)
}

BlockBuilderFactory is the factory interface of block builder

type BlockCreationSubscriber added in v0.4.4

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

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

type Blockchain

type Blockchain interface {
	lifecycle.StartStopper

	// For exposing blockchain states
	// BlockHeaderByHeight return block header by height
	BlockHeaderByHeight(height uint64) (*block.Header, error)
	// BlockFooterByHeight return block footer by height
	BlockFooterByHeight(height uint64) (*block.Footer, error)
	// ChainID returns the chain ID
	ChainID() uint32
	// EvmNetworkID returns the evm network ID
	EvmNetworkID() uint32
	// ChainAddress returns chain address on parent chain, the root chain return empty.
	ChainAddress() string
	// TipHash returns tip block's hash
	TipHash() hash.Hash256
	// TipHeight returns tip block's height
	TipHeight() uint64
	// Genesis returns the genesis
	Genesis() genesis.Genesis
	// Context returns current context
	Context(context.Context) (context.Context, 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(timestamp time.Time) (*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

	// 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, g genesis.Genesis, dao blockdao.BlockDAO, bbf BlockBuilderFactory, opts ...Option) Blockchain

NewBlockchain creates a new blockchain and DB instance

type Config added in v1.8.4

type Config struct {
	ChainDBPath                string           `yaml:"chainDBPath"`
	TrieDBPatchFile            string           `yaml:"trieDBPatchFile"`
	TrieDBPath                 string           `yaml:"trieDBPath"`
	StakingPatchDir            string           `yaml:"stakingPatchDir"`
	IndexDBPath                string           `yaml:"indexDBPath"`
	BloomfilterIndexDBPath     string           `yaml:"bloomfilterIndexDBPath"`
	CandidateIndexDBPath       string           `yaml:"candidateIndexDBPath"`
	StakingIndexDBPath         string           `yaml:"stakingIndexDBPath"`
	SGDIndexDBPath             string           `yaml:"sgdIndexDBPath"`
	ContractStakingIndexDBPath string           `yaml:"contractStakingIndexDBPath"`
	ID                         uint32           `yaml:"id"`
	EVMNetworkID               uint32           `yaml:"evmNetworkID"`
	Address                    string           `yaml:"address"`
	ProducerPrivKey            string           `yaml:"producerPrivKey"`
	ProducerPrivKeySchema      string           `yaml:"producerPrivKeySchema"`
	SignatureScheme            []string         `yaml:"signatureScheme"`
	EmptyGenesis               bool             `yaml:"emptyGenesis"`
	GravityChainDB             db.Config        `yaml:"gravityChainDB"`
	Committee                  committee.Config `yaml:"committee"`

	EnableTrielessStateDB bool `yaml:"enableTrielessStateDB"`
	// EnableStateDBCaching enables cachedStateDBOption
	EnableStateDBCaching bool `yaml:"enableStateDBCaching"`
	// EnableArchiveMode is only meaningful when EnableTrielessStateDB is false
	EnableArchiveMode bool `yaml:"enableArchiveMode"`
	// EnableAsyncIndexWrite enables writing the block actions' and receipts' index asynchronously
	EnableAsyncIndexWrite bool `yaml:"enableAsyncIndexWrite"`
	// deprecated
	EnableSystemLogIndexer bool `yaml:"enableSystemLog"`
	// EnableStakingProtocol enables staking protocol
	EnableStakingProtocol bool `yaml:"enableStakingProtocol"`
	// EnableStakingIndexer enables staking indexer
	EnableStakingIndexer bool `yaml:"enableStakingIndexer"`
	// AllowedBlockGasResidue is the amount of gas remained when block producer could stop processing more actions
	AllowedBlockGasResidue uint64 `yaml:"allowedBlockGasResidue"`
	// MaxCacheSize is the max number of blocks that will be put into an LRU cache. 0 means disabled
	MaxCacheSize int `yaml:"maxCacheSize"`
	// PollInitialCandidatesInterval is the config for committee init db
	PollInitialCandidatesInterval time.Duration `yaml:"pollInitialCandidatesInterval"`
	// StateDBCacheSize is the max size of statedb LRU cache
	StateDBCacheSize int `yaml:"stateDBCacheSize"`
	// WorkingSetCacheSize is the max size of workingset cache in state factory
	WorkingSetCacheSize uint64 `yaml:"workingSetCacheSize"`
	// StreamingBlockBufferSize
	StreamingBlockBufferSize uint64 `yaml:"streamingBlockBufferSize"`
	// PersistStakingPatchBlock is the block to persist staking patch
	PersistStakingPatchBlock uint64 `yaml:"persistStakingPatchBlock"`
}

Config is the config struct for blockchain package

func (*Config) ProducerAddress added in v1.8.4

func (cfg *Config) ProducerAddress() address.Address

ProducerAddress returns the configured producer address derived from key

func (*Config) ProducerPrivateKey added in v1.8.4

func (cfg *Config) ProducerPrivateKey() crypto.PrivateKey

ProducerPrivateKey returns the configured private key

func (*Config) SetProducerPrivKey added in v1.8.4

func (cfg *Config) SetProducerPrivKey() error

SetProducerPrivKey set producer privKey by PrivKeyConfigFile info

type Option added in v0.3.0

type Option func(*blockchain) error

Option sets blockchain construction parameter

func BlockValidatorOption added in v0.11.0

func BlockValidatorOption(blockValidator block.Validator) Option

BlockValidatorOption sets block validator

func ClockOption added in v0.3.0

func ClockOption(clk clock.Clock) Option

ClockOption overrides the default clock

type PubSubManager added in v0.11.0

type PubSubManager interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
	AddBlockListener(BlockCreationSubscriber) error
	RemoveBlockListener(BlockCreationSubscriber) error
	SendBlockToSubscribers(*block.Block)
}

PubSubManager is an interface which handles multi-thread publisher and subscribers

func NewPubSub added in v0.11.0

func NewPubSub(bufferSize uint64) PubSubManager

NewPubSub creates new pubSub struct with buffersize for pendingBlock buffer channel

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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