ledger

package
v0.0.0-...-d9e9996 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2019 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RootBlockVersion for version 1
	RootBlockVersion = 0
	// BlockVersion for version 1
	BlockVersion = 1
	// BlockCacheSize block counts in lru cache
	BlockCacheSize = 100 //block counts in lru cache
)

Variables

View Source
var (
	// ErrBlockNotExist is returned when a block to query not exist in specific chain
	ErrBlockNotExist = errors.New("block not exist in this chain")
	// ErrTxNotFound is returned when a transaction to query not exist in confirmed table
	ErrTxNotFound = errors.New("transaction not found")
	// ErrTxDuplicated ...
	ErrTxDuplicated = errors.New("transaction duplicated in different blocks")
	// ErrRootBlockAlreadyExist is returned when two genesis block is checked in the process of confirming block
	ErrRootBlockAlreadyExist = errors.New("this ledger already has genesis block")
	// NumCPU returns the number of CPU cores for the current system
	NumCPU = runtime.NumCPU()
)
View Source
var (
	// MemCacheSize baseDB memory level max size
	MemCacheSize = 128 //MB
	// FileHandlersCacheSize baseDB memory file handler cache max size
	FileHandlersCacheSize = 1024 //how many opened files-handlers cached
	// DisableTxDedup ...
	DisableTxDedup = false //whether disable dedup tx beform confirm
	// TxSizePercent max percent of txs' size in one block
	TxSizePercent = 0.8
)

Functions

func IsProofed

func IsProofed(blockID []byte, targetBits int32) bool

IsProofed check workload proof

func MakeBlockID

func MakeBlockID(block *pb.InternalBlock) ([]byte, error)

MakeBlockID generate BlockID

func MakeMerkleTree

func MakeMerkleTree(txList []*pb.Transaction) [][]byte

MakeMerkleTree generate merkele-tree

Types

type ConfirmStatus

type ConfirmStatus struct {
	Succ        bool  // 区块是否提交成功
	Split       bool  // 提交后是否发生了分叉
	Orphan      bool  // 是否是个孤儿节点
	TrunkSwitch bool  // 是否导致了主干分支切换
	Error       error //错误消息
}

ConfirmStatus block status

type GenesisBlock

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

GenesisBlock genesis block data structure

func NewGenesisBlock

func NewGenesisBlock(ib *pb.InternalBlock) (*GenesisBlock, error)

NewGenesisBlock new a genesis block

func (*GenesisBlock) CalcAward

func (gb *GenesisBlock) CalcAward(blockHeight int64) *big.Int

CalcAward calc system award by block height

func (*GenesisBlock) GetConfig

func (gb *GenesisBlock) GetConfig() *RootConfig

GetConfig get config of genesis block

func (*GenesisBlock) GetInternalBlock

func (gb *GenesisBlock) GetInternalBlock() *pb.InternalBlock

GetInternalBlock returns internal block of genesis block

type InvokeRequest

type InvokeRequest struct {
	ModuleName   string            `json:"module_name"`
	ContractName string            `json:"contract_name"`
	MethodName   string            `json:"method_name"`
	Args         map[string]string `json:"args"`
}

InvokeRequest define genesis reserved_contracts configure

type Ledger

type Ledger struct {
	GenesisBlock *GenesisBlock //创始块
	// contains filtered or unexported fields
}

Ledger define data structure of Ledger

func NewLedger

func NewLedger(storePath string, xlog log.Logger, otherPaths []string, kvEngineType string, cryptoType string) (*Ledger, error)

NewLedger create an empty ledger, if it already exists, open it directly

func (*Ledger) CheckBlock

func (l *Ledger) CheckBlock(blockid []byte, validator func(*pb.InternalBlock) bool) error

CheckBlock check if a block is valid and support passing in a check function

func (*Ledger) Close

func (l *Ledger) Close()

Close close an instance of ledger

func (*Ledger) ConfirmBlock

func (l *Ledger) ConfirmBlock(block *pb.InternalBlock, isRoot bool) ConfirmStatus

ConfirmBlock submit a block to ledger

func (*Ledger) Dump

func (l *Ledger) Dump() ([][]string, error)

Dump dump ledger structure, block height to blockid

func (*Ledger) ExistBlock

func (l *Ledger) ExistBlock(blockid []byte) bool

ExistBlock check if a block exists in the ledger

func (*Ledger) FindUndoAndTodoBlocks

func (l *Ledger) FindUndoAndTodoBlocks(curBlockid []byte, destBlockid []byte) ([]*pb.InternalBlock, []*pb.InternalBlock, error)

FindUndoAndTodoBlocks get blocks required to undo and todo range from curBlockid to destBlockid

func (*Ledger) FormatBlock

func (l *Ledger) FormatBlock(txList []*pb.Transaction,
	proposer []byte, ecdsaPk *ecdsa.PrivateKey,
	timestamp int64, curTerm int64, curBlockNum int64,
	preHash []byte, utxoTotal *big.Int) (*pb.InternalBlock, error)

FormatBlock format normal block

func (*Ledger) FormatFakeBlock

func (l *Ledger) FormatFakeBlock(txList []*pb.Transaction,
	proposer []byte, ecdsaPk *ecdsa.PrivateKey,
	timestamp int64, curTerm int64, curBlockNum int64,
	preHash []byte, utxoTotal *big.Int) (*pb.InternalBlock, error)

FormatFakeBlock format fake block for contract pre-execution without signing

func (*Ledger) FormatPOWBlock

func (l *Ledger) FormatPOWBlock(txList []*pb.Transaction,
	proposer []byte, ecdsaPk *ecdsa.PrivateKey,
	timestamp int64, curTerm int64, curBlockNum int64,
	preHash []byte, targetBits int32, utxoTotal *big.Int, failedTxs map[string]string) (*pb.InternalBlock, error)

FormatPOWBlock format block for consensus of pow

func (*Ledger) FormatRootBlock

func (l *Ledger) FormatRootBlock(txList []*pb.Transaction) (*pb.InternalBlock, error)

FormatRootBlock format genesis block

func (*Ledger) GetEstimatedTotal

func (l *Ledger) GetEstimatedTotal() *big.Int

GetEstimatedTotal estimate total assets of chain

func (*Ledger) GetGenesisBlock

func (l *Ledger) GetGenesisBlock() *GenesisBlock

GetGenesisBlock returns genesis block if it exists

func (*Ledger) GetLDB

func (l *Ledger) GetLDB() kvdb.Database

GetLDB returns the instance of underlying of kv db

func (*Ledger) GetMaxBlockSize

func (l *Ledger) GetMaxBlockSize() int64

GetMaxBlockSize return max block size

func (*Ledger) GetMeta

func (l *Ledger) GetMeta() *pb.LedgerMeta

GetMeta returns meta info of Ledger, such as genesis block ID, current block height, tip block ID

func (*Ledger) GetPendingBlock

func (l *Ledger) GetPendingBlock(blockID []byte) (*pb.Block, error)

GetPendingBlock get block from pending table

func (*Ledger) HasTransaction

func (l *Ledger) HasTransaction(txid []byte) (bool, error)

HasTransaction check if a transaction exists in the ledger

func (*Ledger) IsTxInTrunk

func (l *Ledger) IsTxInTrunk(txid []byte) bool

IsTxInTrunk check if a transaction is in trunk by transaction ID

func (*Ledger) IsValidTx

func (l *Ledger) IsValidTx(idx int, tx *pb.Transaction, block *pb.InternalBlock) bool

IsValidTx valid transactions of coinbase in block

func (*Ledger) MaxTxSizePerBlock

func (l *Ledger) MaxTxSizePerBlock() int

MaxTxSizePerBlock max total size of all txs in one block

func (*Ledger) QueryBlock

func (l *Ledger) QueryBlock(blockid []byte) (*pb.InternalBlock, error)

QueryBlock query a block by blockID in the ledger

func (*Ledger) QueryBlockByHeight

func (l *Ledger) QueryBlockByHeight(height int64) (*pb.InternalBlock, error)

QueryBlockByHeight query block by height

func (*Ledger) QueryBlockHeader

func (l *Ledger) QueryBlockHeader(blockid []byte) (*pb.InternalBlock, error)

QueryBlockHeader query a block by blockID in the ledger and return only block header

func (*Ledger) QueryTransaction

func (l *Ledger) QueryTransaction(txid []byte) (*pb.Transaction, error)

QueryTransaction query a transaction in the ledger and return it if exist

func (*Ledger) SavePendingBlock

func (l *Ledger) SavePendingBlock(block *pb.Block) error

SavePendingBlock put block into pending table

func (*Ledger) UpdateMaxBlockSize

func (l *Ledger) UpdateMaxBlockSize(maxBlockSize int64) error

UpdateMaxBlockSize update block max size

type RootConfig

type RootConfig struct {
	Version   string `json:"version"`
	Consensus struct {
		Type  string `json:"type"`
		Miner string `json:"miner"`
	} `json:"consensus"`
	Predistribution []struct {
		Address string `json:"address"`
		Quota   string `json:"quota"`
	}
	// max block size in MB
	MaxBlockSize string `json:"maxblocksize"`
	Period       string `json:"period"`
	Award        string `json:"award"`
	AwardDecay   struct {
		HeightGap int64   `json:"height_gap"`
		Ratio     float64 `json:"ratio"`
	} `json:"award_decay"`
	Decimals          string                 `json:"decimals"`
	GenesisConsensus  map[string]interface{} `json:"genesis_consensus"`
	ReservedContracts []InvokeRequest        `json:"reserved_contracts"`
	ReservedWhitelist struct {
		Account string `json:"account"`
	} `json:"reserved_whitelist"`
}

RootConfig genesis block configure

func (*RootConfig) GetGenesisConsensus

func (rc *RootConfig) GetGenesisConsensus() (map[string]interface{}, error)

GetGenesisConsensus get consensus config of genesis block

func (*RootConfig) GetMaxBlockSizeInByte

func (rc *RootConfig) GetMaxBlockSizeInByte() (n int64)

GetMaxBlockSizeInByte get max block size in Byte

func (*RootConfig) GetReservedContract

func (rc *RootConfig) GetReservedContract() ([]*pb.InvokeRequest, error)

GetReservedContract get default contract config of genesis block

func (*RootConfig) GetReservedWhitelistAccount

func (rc *RootConfig) GetReservedWhitelistAccount() string

GetReservedWhitelistAccount return reserved whitelist account

Jump to

Keyboard shortcuts

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